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

Updated: 6/17/2016 by Krish Verma https://github.com/kverma

In case the JSON server is HTTPS with unknown certificate signing authority, pass the -insecure flag to disable TLS certificate check

Install

CLI Application

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

Library

import "github.com/galeone/rts"

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

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

Example

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"`
	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"`
	NotificationsURL                 string `json:"notifications_url"`
	OrganizationRepositoriesURL      string `json:"organization_repositories_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"`
	TeamURL                          string `json:"team_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 {
	ArchiveURL       string      `json:"archive_url"`
	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"`
	DownloadsURL     string      `json:"downloads_url"`
	EventsURL        string      `json:"events_url"`
	Fork             bool        `json:"fork"`
	Forks            int         `json:"forks"`
	ForksCount       int         `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"`
	HasWiki          bool        `json:"has_wiki"`
	Homepage         string      `json:"homepage"`
	HooksURL         string      `json:"hooks_url"`
	HTMLURL          string      `json:"html_url"`
	ID               int         `json:"id"`
	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"`
	MergesURL        string      `json:"merges_url"`
	MilestonesURL    string      `json:"milestones_url"`
	MirrorURL        interface{} `json:"mirror_url"`
	Name             string      `json:"name"`
	NetworkCount     int         `json:"network_count"`
	NotificationsURL string      `json:"notifications_url"`
	OpenIssues       int         `json:"open_issues"`
	OpenIssuesCount  int         `json:"open_issues_count"`
	Owner            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                int    `json:"id"`
		Login             string `json:"login"`
		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"`
	} `json:"owner"`
	Private          bool   `json:"private"`
	PullsURL         string `json:"pulls_url"`
	PushedAt         string `json:"pushed_at"`
	ReleasesURL      string `json:"releases_url"`
	Size             int    `json:"size"`
	SSHURL           string `json:"ssh_url"`
	StargazersCount  int    `json:"stargazers_count"`
	StargazersURL    string `json:"stargazers_url"`
	StatusesURL      string `json:"statuses_url"`
	SubscribersCount int    `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"`
	TreesURL         string `json:"trees_url"`
	UpdatedAt        string `json:"updated_at"`
	URL              string `json:"url"`
	Watchers         int    `json:"watchers"`
	WatchersCount    int    `json:"watchers_count"`
}

License

RTS: Request to Struct. Generates Go structs from a server response. Copyright (C) 2016 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.

Owner
Paolo Galeone
Researcher in love with: Machine/Deep Learning, PostgreSQL, Tensorflow and Go. Owner of @nerdzeu
Paolo Galeone
Similar Resources

Export the private key from a Swarm json key file

exportSwarmKey Currently it is a pain in the A** to export bee key in to metamask as they are not compatible. This programe will export the private ke

Oct 2, 2022

Simple API that returns JSON of statically defined

GOLANG API This is a simple example of a Go API using: Docker Docker-compose Built in go packages How to run using Docker-compose (Easiest) Clone repo

Nov 2, 2021

Program to solve sudokus given in JSON Format, written in Go

Program to solve sudokus given in JSON Format, written in Go

May 2, 2022

To get json about repo&pkg relationship

pkg_repo_tool 1. Usage 把从 frameworks 收集到的 bp 文件放到一个文件夹中。在这里为了方便叙述,就把这个文件夹命名为 frameworks 并且放在 bprepo2json/main 之下。 然后: cd bprepo2json/main go run .

Jan 11, 2022

traefik-certs-cleaner is a simple helper to clean the Traefik acme.json file by removing certificates

traefik-certs-cleaner If you appreciate this project: Description traefik-certs-cleaner is a simple helper to clean the Traefik acme.json file by remo

Oct 31, 2022

Go-keymap - Generate keymap.c, layers.h and keymap.svg from visual keymaps defined through keymaps.json and .layer files

Go-keymap - Generate keymap.c, layers.h and keymap.svg from visual keymaps defined through keymaps.json and .layer files

generating keymap.c This utility can generate a keymap.c, layers.h and keymap.sv

Feb 15, 2022

A minimalistic message queue server, in Go

lineup, a tiny queue server lineup is a very simple queue server. all it does is serve a binary-safe priority queue with a simple protocol insp

Aug 17, 2022

ephemeral metric aggregation server

hemera hemera is a zero-dependency statsd implementation written in Go. The project takes advantage of modular packages and interfaces to make connect

Jun 13, 2020

OTS: Open Terraforming Server

 OTS: Open Terraforming Server

OTS: Open Terraforming Server A prototype open source alternative to terraform enterprise

Jan 2, 2023
Comments
  • Create struct from JSON fed into the `stdin`

    Create struct from JSON fed into the `stdin`

    E.g.,

    echo '  {
        "Book Id": 30558257,
        "Title": "Unsouled (Cradle, #1)",
        "Author": "Will Wight",
        "Author l-f": "Wight, Will",
        "Additional Authors": "",
        "ISBN": "=\"\"",
        "ISBN13": "=\"\"",
        "My Rating": 0,
        "Average Rating": 4.18,
        "Publisher": "Hidden Gnome Publishing",
        "Binding": "Kindle Edition",
        "Number of Pages": 294,
        "Year Published": 2016,
        "Original Publication Year": 2016,
        "Date Read": "",
        "Date Added": "2020/10/11",
        "Bookshelves": "xianxia, fiction, 87p",
        "Bookshelves with positions": "xianxia (#1), fiction (#13), 87p (#27)",
        "Exclusive Shelf": "87p",
        "My Review": "",
        "Spoiler": "",
        "Private Notes": "",
        "Read Count": 0,
        "Recommended For": "",
        "Recommended By": "",
        "Owned Copies": 0,
        "Original Purchase Date": "",
        "Original Purchase Location": "",
        "Condition": "",
        "Condition Description": "",
        "BCID": ""
      }' | rts
    

    It would be awesome if it generated the parser/reader code, too.

  • Input pipe

    Input pipe

    This merge request closes #2.

    Test case

    type Foo1 struct {
            Additional_Authors         string  `json:"Additional Authors"`
            Author                     string  `json:"Author"`
            Author_l_f                 string  `json:"Author l-f"`
            Average_Rating             float64 `json:"Average Rating"`
            Bcid                       string  `json:"BCID"`
            Binding                    string  `json:"Binding"`
            Book_Id                    int64   `json:"Book Id"`
            Bookshelves                string  `json:"Bookshelves"`
            Bookshelves_with_positions string  `json:"Bookshelves with positions"`
            Condition                  string  `json:"Condition"`
            Condition_Description      string  `json:"Condition Description"`
            Date_Added                 string  `json:"Date Added"`
            Date_Read                  string  `json:"Date Read"`
            Exclusive_Shelf            string  `json:"Exclusive Shelf"`
            Isbn                       string  `json:"ISBN"`
            ISBN13                     string  `json:"ISBN13"`
            My_Rating                  int64   `json:"My Rating"`
            My_Review                  string  `json:"My Review"`
            Number_of_Pages            int64   `json:"Number of Pages"`
            Original_Publication_Year  int64   `json:"Original Publication Year"`
            Original_Purchase_Date     string  `json:"Original Purchase Date"`
            Original_Purchase_Location string  `json:"Original Purchase Location"`
            Owned_Copies               int64   `json:"Owned Copies"`
            Private_Notes              string  `json:"Private Notes"`
            Publisher                  string  `json:"Publisher"`
            Read_Count                 int64   `json:"Read Count"`
            Recommended_By             string  `json:"Recommended By"`
            Recommended_For            string  `json:"Recommended For"`
            Spoiler                    string  `json:"Spoiler"`
            Title                      string  `json:"Title"`
            Year_Published             int64   `json:"Year Published"`
    }
    

    Output:

    package main
    
    type Foo1 struct {
            Additional_Authors         string  `json:"Additional Authors"`
            Author                     string  `json:"Author"`
            Author_l_f                 string  `json:"Author l-f"`
            Average_Rating             float64 `json:"Average Rating"`
            Bcid                       string  `json:"BCID"`
            Binding                    string  `json:"Binding"`
            Book_Id                    int64   `json:"Book Id"`
            Bookshelves                string  `json:"Bookshelves"`
            Bookshelves_with_positions string  `json:"Bookshelves with positions"`
            Condition                  string  `json:"Condition"`
            Condition_Description      string  `json:"Condition Description"`
            Date_Added                 string  `json:"Date Added"`
            Date_Read                  string  `json:"Date Read"`
            Exclusive_Shelf            string  `json:"Exclusive Shelf"`
            Isbn                       string  `json:"ISBN"`
            ISBN13                     string  `json:"ISBN13"`
            My_Rating                  int64   `json:"My Rating"`
            My_Review                  string  `json:"My Review"`
            Number_of_Pages            int64   `json:"Number of Pages"`
            Original_Publication_Year  int64   `json:"Original Publication Year"`
            Original_Purchase_Date     string  `json:"Original Purchase Date"`
            Original_Purchase_Location string  `json:"Original Purchase Location"`
            Owned_Copies               int64   `json:"Owned Copies"`
            Private_Notes              string  `json:"Private Notes"`
            Publisher                  string  `json:"Publisher"`
            Read_Count                 int64   `json:"Read Count"`
            Recommended_By             string  `json:"Recommended By"`
            Recommended_For            string  `json:"Recommended For"`
            Spoiler                    string  `json:"Spoiler"`
            Title                      string  `json:"Title"`
            Year_Published             int64   `json:"Year Published"`
    }
    
    
Initialize structs with default values

defaults Initialize structs with default values Supports almost all kind of types Scalar types int/8/16/32/64, uint/8/16/32/64, float32/64 uintptr, bo

Jan 7, 2023
🦉 Docuowl generates a static single-page documentation from Markdown files
🦉 Docuowl generates a static single-page documentation from Markdown files

?? Docuowl generates a static single-page documentation from Markdown files

Jan 2, 2023
Generates Golang code with enums from TOML specification

enum-generator This enum-generator is intended to generate golang source code for string-based enum types and enum types with associated data from des

Dec 29, 2021
Random fake data and struct generator for Go.

Faker Random fake data and struct generator for Go. More than 100 generator functions Struct generator Unique data generator Builtin types support Eas

Oct 3, 2022
Provide Go Statistics Handler, Struct, Measure Method

Go Statistics Handler About The gosh is an abbreviation for Go Statistics Handler. This Repository is provided following functions. Go runtime statist

Jan 8, 2023
Gountries provides: Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO-6709) as well as translations, country borders and other stuff exposed as struct data.

gountries Inspired by the countries gem for ruby. Countries (ISO-3166-1), Country Subdivisions(ISO-3166-2), Currencies (ISO 4217), Geo Coordinates(ISO

Dec 22, 2022
Align Golang struct tags

Formattag The tool is used to align golang struct's tags. eg.: Before // TestStruct this is a test struct type TestStruct struct { ID stri

Aug 1, 2022
The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

The kprobe package allows construction of dynamic struct based on kprobe event format descriptions.

Oct 27, 2021
An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported.

go-fsm An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported. usage import github.co

Dec 26, 2021
HTTP service to generate PDF from Json requests

pdfgen HTTP service to generate PDF from Json requests Install and run The recommended method is to use the docker container by mounting your template

Dec 2, 2022