Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
cneill committed Sep 13, 2023
1 parent 96707f2 commit 51def12
Showing 1 changed file with 134 additions and 6 deletions.
140 changes: 134 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,34 @@ go install github.com/cneill/jsonstruct/cmd/jsonstruct@latest
## Usage

```
Usage of ./jsonstruct:
./jsonstruct [flags] [file name...]
NAME:
jsonstruct - generate Go structs for JSON values
Flags:
-value-comments
add a comment to struct fields with the example value(s)
USAGE:
jsonstruct [global options] command [command options] [file]...
DESCRIPTION:
You can either pass in files as args or JSON in STDIN. Results are printed to STDOUT.
COMMANDS:
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--name value, -n value override the default name derived from filename
--value-comments, -c add a comment to struct fields with the example value(s) (default: false)
--sort-fields, -s sort the fields in alphabetical order; default behavior is to mirror input (default: false)
--inline-structs, -i use inline structs instead of creating different types for each object (default: false)
--print-filenames, -f print the filename above the structs defined within (default: false)
--debug enable debug logs (default: false)
--help, -h show help
```

## Examples

### JSON object

**Input:**

```json
{
"currency": "value",
Expand Down Expand Up @@ -65,7 +81,7 @@ Flags:
}
```

If passed in through stdin, the above JSON object will produce this output:
**Output:**

```golang
type Stdin1 struct {
Expand Down Expand Up @@ -119,6 +135,118 @@ type Stdin1 struct {
}
```

### Value comments (`-c`)

**Input:**

```json
{
"test_bool": true,
"test_int64": 1234,
"test_float64": 1234.0,
"test_string": "test",
"test_array_of_bool": [true, false, true],
"test_array_of_int64": [1, 2, 3, 4],
"test_array_of_float64": [1.0, 2.0, 3.0, 4.0],
"test_array_of_string": ["test1", "test2", "test3"],
"test_struct": {
"test_string": "test",
"test_array_of_string": ["test1", "test2", "test3"]
},
"test_array_of_struct": [
{
"test_string": "test1",
"test_optional_string": "test1"
},
{
"test_string": "test2",
"test_optional_string": "test2"
},
{
"test_string": "test3"
},
{
"test_string": "test4"
}
],
"test_garbage_array": [1, "1", 1.0],
"terrible-name.for_a.key": "test"
}
```

**Output:**

```golang
type Stdin1 struct {
TestBool bool `json:"test_bool"` // Example: true
TestInt64 int64 `json:"test_int64"` // Example: 1234
TestFloat64 float64 `json:"test_float64"` // Example: 1234.000
TestString string `json:"test_string"` // Example: "test"
TestArrayOfBool []bool `json:"test_array_of_bool"` // Example: [true, false, true]
TestArrayOfInt64 []int64 `json:"test_array_of_int64"` // Example: [1, 2, 3, 4]
TestArrayOfFloat64 []float64 `json:"test_array_of_float64"` // Example: [1.000, 2.000, 3.000, 4.000]
TestArrayOfString []string `json:"test_array_of_string"` // Example: ["test1", "test2", "test3"]
TestStruct *TestStruct `json:"test_struct"`
TestArrayOfStruct []*TestArrayOfStruct `json:"test_array_of_struct"`
TestGarbageArray []*json.RawMessage `json:"test_garbage_array"`
TerribleNameForAKey string `json:"terrible-name.for_a.key"` // Example: "test"
}

type TestStruct struct {
TestString string `json:"test_string"` // Example: "test"
TestArrayOfString []string `json:"test_array_of_string"` // Example: ["test1", "test2", "test3"]
}

type TestArrayOfStruct struct {
TestString string `json:"test_string"` // Example: "test1"
TestOptionalString string `json:"test_optional_string,omitempty"` // Example: "test1"
}
```

### Inline struct definitions (`-i`)

**Input:**

```json
{
"test_struct": {
"test_string": "test",
"test_array_of_string": ["test1", "test2", "test3"]
},
"test_array_of_struct": [
{
"test_string": "test1",
"test_optional_string": "test1"
},
{
"test_string": "test2",
"test_optional_string": "test2"
},
{
"test_string": "test3"
},
{
"test_string": "test4"
}
]
}
```

**Output:**

```golang
type Stdin1 struct {
TestStruct struct {
TestString string `json:"test_string"`
TestArrayOfString []string `json:"test_array_of_string"`
} `json:"test_struct"`
TestArrayOfStruct []struct {
TestString string `json:"test_string"`
TestOptionalString string `json:"test_optional_string,omitempty"`
} `json:"test_array_of_struct"`
}
```

## Notes

* When an array of JSON objects is detected, any keys that are provided in some objects but not others
Expand Down

0 comments on commit 51def12

Please sign in to comment.