sfj

package module
v0.0.0-...-54b2723 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

SFJ GoDoc

Struct from JSON

Generator of Go structs from JSON server responses.

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

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

SFJ also supports headers personalization, so it can be used to generate types from responses protected by some authorization method.

Install

go get -u github.com/pchchv/sfj

Usage
sfj [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 sfj by passing a single JSON (anonymous) from stdin 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": ""
  }' | ./sfj
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 set up a more complex scenario by defining a routes.txt file with a line for each (parametric) request, and use it as shown below.

routes.txt:

/
/repos/:user/:repo pchchv sfj

Run:

sfj -server https://api.github.com -pkg example
Returns

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(pkg, server string, lines []string, headerMap map[string]string, insecure, subStruct bool) ([]byte, error)

Do performs a GET request to each defined route concatenated with the server name. lines is a slice containing routes in the format: `/path/to/request` or `/path/:parameter1/:parameter2/request parameter1Value parameter2Value`. It passes headers in each request and returns a file whose package is a pkg containing structure definitions.

func DoRaw

func DoRaw(pkg, json string) ([]byte, error)

DoRaw performs the conversion of JSON passed as a string into go structure definitions. pkg is a new package for the generated structures.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL