-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples and update docs and README
- Loading branch information
Showing
5 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,53 @@ | ||
# JSON typedef infer | ||
|
||
This is a port of [`json-typedef-infer`][jtd-infer] from Rust to Go. The reason | ||
for porting this is that I was in need of JTD inference from code and not as a | ||
CLI tool so I ported this straight from Rust. | ||
This is a port of [`json-typedef-infer`][jtd-infer] Go. The reason for porting | ||
this is that I was in need of JTD inference from code and not as a CLI tool. | ||
|
||
For more information about JSON Typedef and its RFC and how to use different | ||
kind of hints see [`json-typedef-infer`][jtd-infer] | ||
|
||
## Usage | ||
|
||
See [examples] directory for runnable examples and how to infer JTD. | ||
|
||
```go | ||
schema := NewInferrer(WithoutHints()). | ||
Infer("my-string"). | ||
IntoSchema(WithoutHints()) | ||
// { | ||
// "type": "string" | ||
// } | ||
``` | ||
|
||
If you have multiple rows of objects or lists as strings you can pass them to | ||
the shorthand function `InferStrings`. | ||
|
||
```go | ||
rows := []string{ | ||
`{"name":"Joe", "age": 52, "something_optional": true, "something_nullable": 1.1}`, | ||
`{"name":"Jane", "age": 48, "something_nullable": null}`, | ||
} | ||
schema := InferStrings(rows, WithoutHints()).IntoSchema(WithoutHints()) | ||
// { | ||
// "properties": { | ||
// "age": { | ||
// "type": "uint8" | ||
// }, | ||
// "name": { | ||
// "type": "string" | ||
// }, | ||
// "something_nullable": { | ||
// "nullable": true, | ||
// "type": "float64" | ||
// } | ||
// }, | ||
// "optionalProperties": { | ||
// "something_optional": { | ||
// "type": "boolean" | ||
// } | ||
// } | ||
// } | ||
``` | ||
|
||
[jtd-infer]: https://github.com/jsontypedef/json-typedef-infer/ | ||
[examples]: examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/bombsimon/jtdinfer" | ||
) | ||
|
||
func main() { | ||
rows := []string{ | ||
`{"name":"Joe", "age": 52, "something_optional": true, "something_nullable": 1.1}`, | ||
`{"name":"Jane", "age": 48, "something_nullable": null}`, | ||
} | ||
schema := jtdinfer. | ||
InferStrings(rows, jtdinfer.WithoutHints()). | ||
IntoSchema(jtdinfer.WithoutHints()) | ||
|
||
j, _ := json.MarshalIndent(schema, "", " ") | ||
print(string(j)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/bombsimon/jtdinfer" | ||
) | ||
|
||
func main() { | ||
schema := jtdinfer. | ||
NewInferrer(jtdinfer.WithoutHints()). | ||
Infer("my-string"). | ||
IntoSchema(jtdinfer.WithoutHints()) | ||
|
||
j, _ := json.MarshalIndent(schema, "", " ") | ||
print(string(j)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/bombsimon/jtdinfer" | ||
) | ||
|
||
func main() { | ||
rows := []string{ | ||
`{ | ||
"name":"Joe", | ||
"age":52, | ||
"work":{"department": "sales"}, | ||
"values":{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]}, | ||
"discriminator":[{"type":"s", "value":"foo"},{"type":"n", "value":3.14}] | ||
}`, | ||
`{ | ||
"name":"Jane", | ||
"age":48, | ||
"work":{"department": "engineering"}, | ||
"values":{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, -2000]}, | ||
"discriminator":[{"type":"s", "value":"foo"},{"type":"n", "value":3.14}] | ||
}`, | ||
} | ||
hints := jtdinfer.Hints{ | ||
DefaultNumType: jtdinfer.NumTypeUint32, | ||
Enums: jtdinfer.NewHintSet().Add([]string{"work", "department"}), | ||
Values: jtdinfer.NewHintSet().Add([]string{"values"}), | ||
Discriminator: jtdinfer.NewHintSet().Add([]string{"discriminator", "-", "type"}), | ||
} | ||
|
||
schema := jtdinfer.InferStrings(rows, hints).IntoSchema(hints) | ||
j, _ := json.MarshalIndent(schema, "", " ") | ||
print(string(j)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters