gen

package
v0.0.0-...-605fd06 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2020 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrContinue = errors.New("continue")

ErrContinue can be returned from a Planner if there is no match. Other planners may then be tried.

Functions

func IsDebug

func IsDebug(ctx context.Context) bool

IsDebug returns whether or not the debug flag has been set to true in this context.

func SetDebug

func SetDebug(ctx context.Context) context.Context

SetDebug sets a flag in the context indicating that debug mode has been enabled. The value may be accessed with IsDebug

Types

type BoolOrSchema

type BoolOrSchema struct {
	Bool   *bool
	Schema *RefOrSchema
}

BoolOrSchema may have either a boolean or a RefOrSchema.

func (*BoolOrSchema) Present

func (a *BoolOrSchema) Present() bool

func (*BoolOrSchema) UnmarshalJSON

func (a *BoolOrSchema) UnmarshalJSON(data []byte) error

UnmarshalJSON performs some custom deserialization of JSON into BoolOrSchema

type Config

type Config struct {
	GoPath        string        `json:"gopath"`
	Exclude       bool          `json:"exclude"`
	Discriminator Discriminator `json:"Discriminator"`
	NoValidate    bool          `json:"noValidate"`
	PromoteFields bool          `json:"promoteFields"`
	NoOmitEmpty   bool          `json:"noOmitEmpty"`
}

Config is a series of jsonschema2go user extensions

type Discriminator

type Discriminator struct {
	PropertyName string            `json:"propertyName"`
	Mapping      map[string]string `json:"mapping"`
}

Discriminator is jsonschema2go specific info for discriminating between multiple oneOf objects

func (*Discriminator) IsSet

func (d *Discriminator) IsSet() bool

IsSet returns whether there is a discriminator present.

type GoBaseType

type GoBaseType int
const (
	GoUnknown GoBaseType = iota
	GoBool
	GoInt64
	GoFloat64
	GoString
	GoEmpty
	GoSlice
	GoArray
	GoMap
	GoStruct
)

type Helper

type Helper interface {
	Loader
	Dep(ctx context.Context, schemas ...*Schema) error
	TypeInfo(s *Schema) (TypeInfo, error)
	ErrSimpleTypeUnknown(err error) bool
	DetectSimpleType(ctx context.Context, s *Schema) (JSONType, error)
	DetectGoBaseType(ctx context.Context, s *Schema) (GoBaseType, error)
	TypeInfoHinted(s *Schema, t JSONType) TypeInfo
	JSONPropertyExported(name string) string
	Primitive(s JSONType) string
}

Helper is an interface provided to each planner

type Import

type Import struct {
	GoPath, Alias string
}

Import contains GoPath and Alias information, if any.

type Imports

type Imports struct {
	// contains filtered or unexported fields
}

Imports encapsulates knowledge about the current imports and namespace. It provides utilities for generating appropriately qualified names.

func NewImports

func NewImports(currentGoPath string, importGoPaths []string) *Imports

NewImports creates a new Imports. Paths and aliases for the `importGoPaths` will be derived according to the `currentGoPath`

func (*Imports) CurPackage

func (i *Imports) CurPackage() string

CurPackage the current package for this Imports

func (*Imports) List

func (i *Imports) List() (imports []Import)

List returns all of the imports in this value, ready to be rendered in a template. Aliases will be derived and provided as necessary.

func (*Imports) QualName

func (i *Imports) QualName(info TypeInfo) string

QualName returns a qualified name for the provided TypeInfo; for example, if the value is from an imported package, QualName will return it with the appropriate dot prefix.

type ItemsField

type ItemsField struct {
	Items       *RefOrSchema
	TupleFields []*RefOrSchema
}

ItemsField contains information indicating whether the modified array is a dynamically sized list of multiple types or a "tuple" -- a specifically sized array with potentially different types for each position.

func (*ItemsField) Present

func (i *ItemsField) Present() bool

func (*ItemsField) UnmarshalJSON

func (i *ItemsField) UnmarshalJSON(data []byte) error

UnmarshalJSON conditionally deserializes into ItemsField according to the shape of the provided JSON

type JSONType

type JSONType uint8

JSONType is the enumeration of JSONSchema's supported types.

const (
	JSONUnknown JSONType = iota
	JSONArray
	JSONBoolean
	JSONInteger
	JSONNull
	JSONNumber
	JSONObject
	JSONString
)

Each of these is a core type of JSONSchema, except for JSONUnknown, which is a useful zero value.

type Loader

type Loader interface {
	io.Closer
	// Read returns a schema for a URL
	Load(ctx context.Context, u *url.URL) (*Schema, error)
}

Loader is the contract required to be able to resolve a schema.

func NewLoader

func NewLoader() Loader

NewLoader returns a basic loader which can handle either file system or HTTP(s) requests

type Plan

type Plan interface {
	// Type returns the TypeInfo for the current type
	Type() TypeInfo
	// Deps returns any dependent types for the current type (i.e. any types requiring import)
	Deps() []TypeInfo
	// Execute is provided a resolved imports and should provide the type rendered as a string.
	Execute(imports *Imports) (string, error)
}

Plan is the contract that must be filled for a type to be rendered.

type Planner

type Planner interface {
	// Plan generates a Plan from a Schema. If the error matches `errors.Is(err, ErrContinue)`, processing may continue.
	Plan(ctx context.Context, helper Helper, schema *Schema) (Plan, error)
}

Planner is a strategy for generating a Plan from a Schema

type RefOrSchema

type RefOrSchema struct {
	// contains filtered or unexported fields
}

RefOrSchema is either a schema or a reference to a schema.

func NewRefOrSchema

func NewRefOrSchema(s *Schema, ref *string) *RefOrSchema

NewRefOrSchema is a convenience constructor for RefOrSchema

func (*RefOrSchema) Resolve

func (r *RefOrSchema) Resolve(ctx context.Context, referer *Schema, loader Loader) (*Schema, error)

Resolve either returns the schema if set or else resolves the reference using the referer schema and loader.

func (*RefOrSchema) UnmarshalJSON

func (r *RefOrSchema) UnmarshalJSON(b []byte) error

UnmarshalJSON conditionally deserializes the JSON, either into a reference or a schema.

type Schema

type Schema struct {
	// this could be a ref
	Ref *string `json:"$ref,omitempty"`

	// meta
	ID     *url.URL `json:"-"` // set either from "$id", "id", or calculated based on parent (see IDCalc); never nil
	IDCalc bool     `json:"-"` // whether this ID was calculated
	Src    *url.URL `json:"-"` // the resource from which this schema was loaded; never nil
	Schema string   `json:"$schema,omitempty"`

	// number qualifiers
	MultipleOf       *float64 `json:"multipleOf,omitempty"`
	Maximum          *float64 `json:"maximum,omitempty"`
	ExclusiveMaximum *bool    `json:"exclusiveMaximum,omitempty"`
	Minimum          *float64 `json:"minimum,omitempty"`
	ExclusiveMinimum *bool    `json:"exclusiveMinimum,omitempty"`

	// string qualifiers
	MaxLength *uint64 `json:"maxLength,omitempty"`
	MinLength uint64  `json:"minLength,omitempty"`
	Pattern   *string `json:"pattern,omitempty"`

	// array qualifiers
	AdditionalItems *BoolOrSchema `json:"additionalItems,omitempty"`
	Items           *ItemsField   `json:"items,omitempty"`
	MaxItems        *uint64       `json:"maxItems,omitempty"`
	MinItems        uint64        `json:"minItems,omitempty"`
	UniqueItems     bool          `json:"uniqueItems,omitempty"`

	// object qualifiers
	MaxProperties        *uint64                 `json:"maxProperties,omitempty"`
	MinProperties        uint64                  `json:"minProperties,omitempty"`
	Required             []string                `json:"required,omitempty"`
	AdditionalProperties *BoolOrSchema           `json:"additionalProperties,omitempty"`
	Definitions          map[string]*RefOrSchema `json:"definitions,omitempty"`
	Properties           map[string]*RefOrSchema `json:"properties,omitempty"`
	PatternProperties    map[string]*RefOrSchema `json:"patternProperties,omitempty"`
	Dependencies         map[string]*RefOrSchema `json:"dependencies,omitempty"`

	// extra special
	Enum   []interface{} `json:"enum,omitempty"`
	Type   *TypeField    `json:"type,omitempty"`
	Format string        `json:"format,omitempty"`

	// polymorphic support
	AllOf []*RefOrSchema `json:"allOf,omitempty"`
	AnyOf []*RefOrSchema `json:"anyOf,omitempty"`
	OneOf []*RefOrSchema `json:"oneOf,omitempty"`
	Not   *RefOrSchema   `json:"not,omitempty"`

	// jsonschema2go Config
	Config Config `json:"x-jsonschema2go"`

	// user extensible
	Annotations TagMap `json:"-"`
}

Schema is the core representation of the JSONSchema meta schema.

func (*Schema) ChooseType

func (s *Schema) ChooseType() JSONType

ChooseType returns the best known type for this field.

func (*Schema) String

func (s *Schema) String() string

String returns a simple string identifier for the schema

func (*Schema) UnmarshalJSON

func (s *Schema) UnmarshalJSON(data []byte) error

UnmarshalJSON is custom JSON deserialization for the Schema type

type TagMap

type TagMap map[string]json.RawMessage

TagMap contains all of the different user extended tags as json.RawMessage for later deserialization

func (TagMap) GetString

func (t TagMap) GetString(k string) (s string)

GetString attempts to deserialize the value for the provided key into a string. If the key is absent or there is an error deserializing the value, the returned string will be empty.

func (TagMap) Unmarshal

func (t TagMap) Unmarshal(k string, val interface{}) (bool, error)

Read unmarshals the json at the provided key into the provided interface (which should be a pointer amenable to json.Read. If the key is not present, the pointer will be untouched, and false and nil will be returned. If the deserialization fails, an error will be returned.

type TypeField

type TypeField []JSONType

TypeField wraps the type field in JSONSchema, supporting either an array of types or a single type as the metaschema allows

func (*TypeField) UnmarshalJSON

func (t *TypeField) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals JSON into the TypeField

type TypeInfo

type TypeInfo struct {
	GoPath  string
	Name    string
	Pointer bool
	ValPath string
}

TypeInfo encapsulates common information about a go value.

func (TypeInfo) BuiltIn

func (t TypeInfo) BuiltIn() bool

BuiltIn returns whether or not this is a TypeInfo for a built-in type, where built-in means a primitive value type always available in the runtime

func (TypeInfo) Unknown

func (t TypeInfo) Unknown() bool

Unknown returns whether this TypeInfo is unset.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL