https://github.com/pchchv/sfj
Generator of Go structs from JSON server responses.
https://github.com/pchchv/sfj
go go-lib go-library go-package golang golang-library golang-package json response structs structures
Last synced: 9 months ago
JSON representation
Generator of Go structs from JSON server responses.
- Host: GitHub
- URL: https://github.com/pchchv/sfj
- Owner: pchchv
- License: apache-2.0
- Created: 2025-02-09T17:16:24.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-09T17:20:22.000Z (11 months ago)
- Last Synced: 2025-02-09T18:27:30.849Z (11 months ago)
- Topics: go, go-lib, go-library, go-package, golang, golang-library, golang-package, json, response, structs, structures
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SFJ [](https://godoc.org/github.com/pchchv/sfj)
### 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
```go
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*:
```txt
/
/repos/:user/:repo pchchv sfj
```
Run:
```sh
sfj -server https://api.github.com -pkg example
```
#### [Returns](./expected_out.txt)