Documentation
¶
Overview ¶
Package generate implements a tooling to generate Golang structs from a JSON schema file. It is intended to be used with the go generate, https://blog.golang.org/generate
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildStructs ¶
BuildStructs is a backward-compatibility wrapper for BuildStructsWithArgs.
func BuildStructsRename ¶ added in v0.5.13
func BuildStructsRename(schemaPath string, outputDir string, useMessagePack bool, nameMap map[string]string) error
BuildStructsRename is a backward-compatibility wrapper for BuildStructsWithArgs.
func BuildStructsWithArgs ¶ added in v0.6.2
BuildStructsWithArgs takes a JSON Schema and generates Golang structs that match the schema. The generated structs include struct tags for marshaling/unmarshaling to/from JSON. One file will be created for each included allOf/oneOf file in the root schema with any allOf files resulting in structs which are embedded in the oneOf files.
The JSON schema can specify more information than the structs enforce (like field size) and so validation of any JSON generated from the structs is still necessary.
The args parameter is a BuildArgs struct that defines the settings for this function ¶
If undefined args.OutputDir defaults to the current working directory.
The package name is set to the args.OutputDir directory name.
NOTE: If oneOf/allOf entries exist than any JSON schema instances in the root schema file will be skipped.
Types ¶
type BuildArgs ¶ added in v0.6.2
type BuildArgs struct {
SchemaPath string
OutputDir string
UseMessagePack bool
StructNameMap map[string]string
FieldNameMap map[string]string
}
BuildArgs contains information used to build the structs for a JSONschema SchemaPath is the path tot he jsonSchema file to use generate the Go struct representations OutputDir is the destination for the generated files UseMessagePack is a flag that defines if message pack should be fined for this model. StructNameMap allows specifying the type name of the struct for each JSON file. FieldNameMap is used to provide alternate names for fields in the resulting structs.
The property names in the JSON tags for these structs remains the same as supplied. This can be used to accommodate names that are valid JSON but not valid Go identifiers