• Stars
    star
    247
  • Rank 164,086 (Top 4 %)
  • Language
    Go
  • License
    Other
  • Created over 8 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RTS: request to struct. Generates Go structs from JSON server responses.

RTS: Request to Struct

GoDoc Build Status

Generate Go structs definitions from JSON server responses.

RTS defines type names using the specified lines in the route file and skipping numbers. e.g: a request to a route like /users/1/posts generates type UsersPosts

It supports parameters: a line like /users/:user/posts/:pid 1 200 generates type UsersUserPostsPid from the response to the request GET /users/1/posts/200.

RTS supports headers personalization as well, thus it can be used to generate types from responses protected by some authorization method

Install

CLI Application

go get -u github.com/galeone/rts/cmd/rts

CLI Usage

rts [options]
  -headers string
    	Headers to add in every request
  -help
    	prints this help
  -insecure
    	Disables TLS Certificate check for HTTPS, use in case HTTPS Server Certificate is signed by an unknown authority
  -out string
    	Output file. Stdout is used if not specified
  -pkg string
    	Package name (default "main")
  -routes string
    	Routes to request. One per line (default "routes.txt")
  -server string
    	sets the server address (default "http://localhost:9090")
  -substruct
    	Creates types for sub-structs

Examples

You can invoke rts piping from stdin a single JSON (anonymous) and get it converted to a go structure

echo '  {
    "Book Id": 30558257,
    "Title": "Unsouled (Cradle, #1)",
    "Author": "Will Wight",
    "Author l-f": "Wight, Will",
    "Additional Authors": "",
    "BCID": ""
  }' | ./rts

obtaining

package main

type Foo1 struct {
        Additional_Authors string `json:"Additional Authors"`
        Author             string `json:"Author"`
        Author_l_f         string `json:"Author l-f"`
        Bcid               string `json:"BCID"`
        Book_Id            int64  `json:"Book Id"`
        Title              string `json:"Title"`
}

Or you can define a more complex scenario, definining the routes.txt file with a line for each (parametric) request and use it as shown below.

routes.txt:

/
/repos/:user/:repo galeone igor

Run:

rts -server https://api.github.com -pkg example

Returns:

package example

type Foo1 struct {
	AuthorizationsURL                string `json:"authorizations_url"`
	CodeSearchURL                    string `json:"code_search_url"`
	CommitSearchURL                  string `json:"commit_search_url"`
	CurrentUserAuthorizationsHTMLURL string `json:"current_user_authorizations_html_url"`
	CurrentUserRepositoriesURL       string `json:"current_user_repositories_url"`
	CurrentUserURL                   string `json:"current_user_url"`
	EmailsURL                        string `json:"emails_url"`
	EmojisURL                        string `json:"emojis_url"`
	EventsURL                        string `json:"events_url"`
	FeedsURL                         string `json:"feeds_url"`
	FollowersURL                     string `json:"followers_url"`
	FollowingURL                     string `json:"following_url"`
	GistsURL                         string `json:"gists_url"`
	HubURL                           string `json:"hub_url"`
	IssueSearchURL                   string `json:"issue_search_url"`
	IssuesURL                        string `json:"issues_url"`
	KeysURL                          string `json:"keys_url"`
	LabelSearchURL                   string `json:"label_search_url"`
	NotificationsURL                 string `json:"notifications_url"`
	OrganizationRepositoriesURL      string `json:"organization_repositories_url"`
	OrganizationTeamsURL             string `json:"organization_teams_url"`
	OrganizationURL                  string `json:"organization_url"`
	PublicGistsURL                   string `json:"public_gists_url"`
	RateLimitURL                     string `json:"rate_limit_url"`
	RepositorySearchURL              string `json:"repository_search_url"`
	RepositoryURL                    string `json:"repository_url"`
	StarredGistsURL                  string `json:"starred_gists_url"`
	StarredURL                       string `json:"starred_url"`
	TopicSearchURL                   string `json:"topic_search_url"`
	UserOrganizationsURL             string `json:"user_organizations_url"`
	UserRepositoriesURL              string `json:"user_repositories_url"`
	UserSearchURL                    string `json:"user_search_url"`
	UserURL                          string `json:"user_url"`
}

type ReposUserRepo struct {
	AllowForking             bool               `json:"allow_forking"`
	ArchiveURL               string             `json:"archive_url"`
	Archived                 bool               `json:"archived"`
	AssigneesURL             string             `json:"assignees_url"`
	BlobsURL                 string             `json:"blobs_url"`
	BranchesURL              string             `json:"branches_url"`
	CloneURL                 string             `json:"clone_url"`
	CollaboratorsURL         string             `json:"collaborators_url"`
	CommentsURL              string             `json:"comments_url"`
	CommitsURL               string             `json:"commits_url"`
	CompareURL               string             `json:"compare_url"`
	ContentsURL              string             `json:"contents_url"`
	ContributorsURL          string             `json:"contributors_url"`
	CreatedAt                string             `json:"created_at"`
	DefaultBranch            string             `json:"default_branch"`
	DeploymentsURL           string             `json:"deployments_url"`
	Description              string             `json:"description"`
	Disabled                 bool               `json:"disabled"`
	DownloadsURL             string             `json:"downloads_url"`
	EventsURL                string             `json:"events_url"`
	Fork                     bool               `json:"fork"`
	Forks                    int64              `json:"forks"`
	ForksCount               int64              `json:"forks_count"`
	ForksURL                 string             `json:"forks_url"`
	FullName                 string             `json:"full_name"`
	GitCommitsURL            string             `json:"git_commits_url"`
	GitRefsURL               string             `json:"git_refs_url"`
	GitTagsURL               string             `json:"git_tags_url"`
	GitURL                   string             `json:"git_url"`
	HasDownloads             bool               `json:"has_downloads"`
	HasIssues                bool               `json:"has_issues"`
	HasPages                 bool               `json:"has_pages"`
	HasProjects              bool               `json:"has_projects"`
	HasWiki                  bool               `json:"has_wiki"`
	Homepage                 string             `json:"homepage"`
	HooksURL                 string             `json:"hooks_url"`
	HTMLURL                  string             `json:"html_url"`
	ID                       int64              `json:"id"`
	IsTemplate               bool               `json:"is_template"`
	IssueCommentURL          string             `json:"issue_comment_url"`
	IssueEventsURL           string             `json:"issue_events_url"`
	IssuesURL                string             `json:"issues_url"`
	KeysURL                  string             `json:"keys_url"`
	LabelsURL                string             `json:"labels_url"`
	Language                 string             `json:"language"`
	LanguagesURL             string             `json:"languages_url"`
	License                  ReposUserRepo_sub1 `json:"license"`
	MergesURL                string             `json:"merges_url"`
	MilestonesURL            string             `json:"milestones_url"`
	MirrorURL                interface{}        `json:"mirror_url"`
	Name                     string             `json:"name"`
	NetworkCount             int64              `json:"network_count"`
	NodeID                   string             `json:"node_id"`
	NotificationsURL         string             `json:"notifications_url"`
	OpenIssues               int64              `json:"open_issues"`
	OpenIssuesCount          int64              `json:"open_issues_count"`
	Owner                    ReposUserRepo_sub2 `json:"owner"`
	Private                  bool               `json:"private"`
	PullsURL                 string             `json:"pulls_url"`
	PushedAt                 string             `json:"pushed_at"`
	ReleasesURL              string             `json:"releases_url"`
	Size                     int64              `json:"size"`
	SSHURL                   string             `json:"ssh_url"`
	StargazersCount          int64              `json:"stargazers_count"`
	StargazersURL            string             `json:"stargazers_url"`
	StatusesURL              string             `json:"statuses_url"`
	SubscribersCount         int64              `json:"subscribers_count"`
	SubscribersURL           string             `json:"subscribers_url"`
	SubscriptionURL          string             `json:"subscription_url"`
	SvnURL                   string             `json:"svn_url"`
	TagsURL                  string             `json:"tags_url"`
	TeamsURL                 string             `json:"teams_url"`
	TempCloneToken           interface{}        `json:"temp_clone_token"`
	Topics                   []string           `json:"topics"`
	TreesURL                 string             `json:"trees_url"`
	UpdatedAt                string             `json:"updated_at"`
	URL                      string             `json:"url"`
	Visibility               string             `json:"visibility"`
	Watchers                 int64              `json:"watchers"`
	WatchersCount            int64              `json:"watchers_count"`
	WebCommitSignoffRequired bool               `json:"web_commit_signoff_required"`
}

type ReposUserRepo_sub2 struct {
	AvatarURL         string `json:"avatar_url"`
	EventsURL         string `json:"events_url"`
	FollowersURL      string `json:"followers_url"`
	FollowingURL      string `json:"following_url"`
	GistsURL          string `json:"gists_url"`
	GravatarID        string `json:"gravatar_id"`
	HTMLURL           string `json:"html_url"`
	ID                int64  `json:"id"`
	Login             string `json:"login"`
	NodeID            string `json:"node_id"`
	OrganizationsURL  string `json:"organizations_url"`
	ReceivedEventsURL string `json:"received_events_url"`
	ReposURL          string `json:"repos_url"`
	SiteAdmin         bool   `json:"site_admin"`
	StarredURL        string `json:"starred_url"`
	SubscriptionsURL  string `json:"subscriptions_url"`
	Type              string `json:"type"`
	URL               string `json:"url"`
}

type ReposUserRepo_sub1 struct {
	Key    string `json:"key"`
	Name   string `json:"name"`
	NodeID string `json:"node_id"`
	SpdxID string `json:"spdx_id"`
	URL    string `json:"url"`
}

Library

import "github.com/galeone/rts"

byteFile, err := rts.Do(pkg, server, lines, headerMap)

License

RTS: Request to Struct. Generates Go structs from a server response. Copyright (C) 2016-2022 Paolo Galeone [email protected]

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Exhibit B is not attached; this software is compatible with the licenses expressed under Section 1.12 of the MPL v2.

More Repositories

1

tfgo

Tensorflow + Go, the gopher way
Go
2,305
star
2

dynamic-training-bench

Simplify the training and tuning of Tensorflow models
Python
215
star
3

igor

igor is an abstraction layer for PostgreSQL with a gorm like syntax.
Go
113
star
4

italian-machine-learning-course

[Italian] Material for a theoretical practical machine learning crash course with TensorFlow 2.0
Jupyter Notebook
75
star
5

openat

OpenAT: Open Source Algorithmic Trading Library
C++
74
star
6

letsencrypt-lighttpd

Renew your let's encrypt certificates monthly, using lighttpd as webserver.
Shell
66
star
7

face-miner

Data mining applied to face detection
C++
28
star
8

tf-aoc

Python
27
star
9

bacup

An easy-to-use backup tool designed for servers.
Rust
24
star
10

crypto15

Dataset of snapshots captured every 15 minutes of the status of 9 cryptocurrencies.
Python
23
star
11

facectrl

Control your media player with your face. Dataset creation, training, model selection and inference.
Python
19
star
12

openatd

Cryptocurrency monitor and algorithmic trading bot
C++
18
star
13

raf

RaF is a bot for creating referral-based contests for your Telegram channels, groups, and supergroups.
Rust
18
star
14

sa

Simple Ajax: a lightweight library to make AJAX requests
JavaScript
12
star
15

ue-plugin-tutorial

C++
9
star
16

rtf

Bring the power of the Tensorflow Python API to any language, using gRPC.
Python
7
star
17

pgnet

Python
7
star
18

galeone.github.io

SCSS
5
star
19

db-gen

Generate JDBC/DAO/Hibernate classes from a db description.
Java
4
star
20

bitly-api-python

Bitly API client: for python 3
Python
3
star
21

fitbit

Fitbit Web API - Go client. Create an OAuth2 server application that interacts with the Fitbit API
Go
3
star
22

download-monitor

Monitors your download folder and organizes its contents
Shell
3
star
23

srs

Social Recommender System
C++
1
star
24

mlcnn

Python
1
star
25

fitbit-pgdb

Implementation of the fitbit.Storage interface for the Fitbit Web API - Go client package
Go
1
star
26

PASCIFAR

Subset of CIFAR 10/100 datasets PASCAL VOC 2012 compatible.
Python
1
star
27

tf-function-talk

GitHub hosted version of the talk "Dissection tf.function to discover AutoGraph strengths and subtleties".
CSS
1
star