Generates Go (golang) Structs and Validation code from JSON schema.
- Go 1.8+
Install
$ go get -u github.com/daverlo/generate/...or
Build
$ makeRun
$ schema-generate exampleschema.jsonThis schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Example",
"id": "http://example.com/exampleschema.json",
"type": "object",
"description": "An example JSON Schema",
"properties": {
"name": {
"type": "string"
},
"address": {
"$ref": "#/definitions/address"
},
"status": {
"$ref": "#/definitions/status"
}
},
"definitions": {
"address": {
"id": "address",
"type": "object",
"description": "Address",
"properties": {
"street": {
"type": "string",
"description": "Address 1",
"maxLength": 40
},
"houseNumber": {
"type": "integer",
"description": "House Number"
}
}
},
"status": {
"type": "object",
"properties": {
"favouritecat": {
"enum": [
"A",
"B",
"C"
],
"type": "string",
"description": "The favourite cat.",
"maxLength": 1
}
}
}
}
}generates
package main
typeAddressstruct {
HouseNumberint`json:"houseNumber,omitempty"`Streetstring`json:"street,omitempty"`
}
typeExamplestruct {
Address*Address`json:"address,omitempty"`Namestring`json:"name,omitempty"`Status*Status`json:"status,omitempty"`
}
typeStatusstruct {
Favouritecatstring`json:"favouritecat,omitempty"`
}See the test/ directory for more examples.