GitXplorerGitXplorer
m

go-fromjsonschema

public
20 stars
2 forks
1 issues

Commits

List of commits on branch master.
Unverified
29bc59e7ff418c499c2a53b1390685481e14e592

[autopush] «Scenery is here, wish you were beautiful.»

mmetaleap committed 3 years ago
Unverified
59f989290caa42dd4c11312e2aadd3399e719c72

[autopush] «(null cookie; hope that's ok)»

mmetaleap committed 6 years ago
Unverified
1705de477d52c32018f7f6a4a9e0d9ffbab1e1e6

[autopush] «This Fortune Examined By INSPECTOR NO. 2-14»

mmetaleap committed 7 years ago
Unverified
b19a021c48f0fb410693f9b20a9cd6409c367829

[autopush] «If there are epigrams, there must be meta-epigrams.»

mmetaleap committed 7 years ago
Unverified
6914b15db1c7871a5f999214bb660b10084f025c

[autopush] «Being ugly isn't illegal. Yet.»

mmetaleap committed 7 years ago
Unverified
dbaee4df873c290080745463b9d163ed2472e3ab

[autopush] «You're ugly and your mother dresses you funny.»

mmetaleap committed 7 years ago

README

The README file for this repository.

fromjsd

-- import "github.com/metaleap/go-fromjsonschema"

Generates Go struct (et al) type definitions (ready to json.Unmarshal into) from a JSON Schema definition.

Caution: contains a few strategically placed panics for edge cases not-yet-considered/implemented/handled/needed. If it panics for your JSON Schema, report!

Usage

var (
	//	Will be appended to the resulting generated package doc-comment summary
	GoPkgDesc = "Package codegen'd via github.com/metaleap/go-fromjsonschema"

	//	Default JSON-to-Go type mappings, `number` could be tweaked to `float64` depending on the use-case at hand
	TypeMapping = map[string]string{
		"boolean": "bool",
		"number":  "int64",
		"integer": "int",
		"string":  "string",
		"null":    "interface{/*nil*/}",
		"array":   "[]interface{}",
		"object":  "map[string]interface{}",
	}
)

type JsonDef

type JsonDef struct {
	Desc  string              `json:"description,omitempty"`          // top-level defs
	AllOf []*JsonDef          `json:"allOf,omitempty"`                // tld
	Props map[string]*JsonDef `json:"properties,omitempty"`           // tld
	Type  []string            `json:"type,omitempty"`                 // tld
	Req   []string            `json:"required,omitempty"`             // tld
	Enum  []string            `json:"enum,omitempty"`                 // tld
	Enum_ []string            `json:"_enum,omitempty"`                // prop defs
	TMap  *JsonDef            `json:"additionalProperties,omitempty"` // pd
	TArr  *JsonDef            `json:"items,omitempty"`                // pd
	Ref   string              `json:"$ref,omitempty"`                 // pd or base from allof[0]
}
Represents a top-level type definition, or a property definition,

a type-reference, an embedded anonymous struct/object type definition, or an array/map element type definition..

func (*JsonDef) EnsureProps

func (me *JsonDef) EnsureProps(propNamesAndTypes map[string]string)

type JsonSchema

type JsonSchema struct {
	//	Something like `http://json-schema.org/draft-04/schema#`
	Schema string `json:"$schema,omitempty"`

	Title string `json:"title,omitempty"`

	Desc string `json:"description,omitempty"`

	//	Ignored, assuming `["object"]` (prior to unmarshaling a JSON Schhema, we transform all `\"type\": \"foo\"` into \"type\": [\"foo\"] for now)
	Type []string `json:"type,omitempty"`

	//	The JSON Schema's type definitions
	Defs map[string]*JsonDef `json:"definitions,omitempty"`
}

Top-level declarations of a JSON Schema

func NewJsonSchema

func NewJsonSchema(jsonSchemaDefSrc string) (*JsonSchema, error)

Obtains from the given JSON Schema source a *JsonSchema that can be passed to Generate. err is nil unless unmarshaling the specified jsonSchemaDefSrc failed.

func (*JsonSchema) ForceCopyProps

func (me *JsonSchema) ForceCopyProps(fromBaseTypeName string, toBaseTypeName string, pnames ...string)

func (*JsonSchema) Generate

func (me *JsonSchema) Generate(goPkgName string, generateDecodeHelpersForBaseTypeNames map[string]string, generateHandlinScaffoldsForBaseTypes map[string]string, generateCtorsForBaseTypes ...string) string

Generate a Go package source with type-defs representing the Defs in jsd (typically obtained via NewJsonSchema).

Arguments beyond goPkgName generate further code beyond the type-defs: these may all be nil/zeroed, or if one "sounds like what you need", check the source for how they're handled otherwise =)