Generates Go (golang) Structs from JSON schema.
Generates Go (golang) Structs and Validation code from JSON schema.
Install
$ go get -u github.com/a-h/generate/...
or
Build
$ make
Run
$ schema-generate exampleschema.json
This 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 maintype Address struct { HouseNumber int
json:"houseNumber,omitempty"
Street stringjson:"street,omitempty"
}type Example struct { Address *Address
json:"address,omitempty"
Name stringjson:"name,omitempty"
Status *Statusjson:"status,omitempty"
}type Status struct { Favouritecat string
json:"favouritecat,omitempty"
}
See the test/ directory for more examples.