schema

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 19 Imported by: 0

README

Schema

Schema is a system for defining a schema for structured data long with support for type generation, marshalling, and unmarshalling.

Types

Simple

A simple type is a type such as int or string. Schema supports the following simple types:

  • byte - a single byte
  • int - a 64-bit signed integer
  • uint - a 64-bit unsigned integer
  • float - a 64-bit floating point number
  • bool - a boolean value (true or false)
  • string - a string
  • bytes - an array of bytes
  • hash - a fixed-length, 32-byte value

A simple type may be named, such as type MyInt int:

MyInt:
  class: simple
  type: int
Composite

A composite type is a product type, aka record, named tuple, struct, or class. A composite type has one or more named fields. For example:

MyStruct:
  class: composite
  fields:
    - name: Foo
      type: { class: simple, type: string }
Map

A map type is an associative array, aka map or dictionary. For example:

MyMap:
  class: map
  key: { class: simple, type: string }
  value: { class: simple, type: int }
Array

An array type is an array, aka list, slice, or ordered set. For example:

MyArray:
  class: array
  elem: { class: simple, type: string }

Unless otherwise specified, an array type is variable length. The length property may be used to indicate a fixed-length array type. Language support for fixed-length arrays is limited.

Pointer

A pointer type is a pointer to another type. Language support for explicit pointer types is limited. Pointer types are most often used to point to composite types. For example:

class: pointer
elem:
  name: MyStruct
  class: composite
  fields:
    - name: Foo
      type: { class: simple, type: string }
Enumeration

An enumeration is an enumerated type consisting of a set of named values. Schema only supports enumerations where all values are of the same underlying type, which must be either int or uint. For example:

MyEnum:
  class: enum
  underlying: { class: simple, type: int }
  values:
    Foo: { value: 1 }
    Bar: { value: 2 }
Union

A union type is a tagged or discriminated union, a kind of sum type that uses a tag field to discriminate between member types of the union. A tagged union allows for polymorphic data types while still being able to reliably marshal and unmarshal values. Schema only supports using enumerations as the value of the discriminator/tag field. Members of the union must be named types or pointers to named types.

Note, Schema's marshalling and unmarshalling implementation only supports unions where all members are composite types or pointers to composite types. A union with other types of members cannot be marshalled or unmarshalled without custom functions.

Example:

Value:
  class: union
  discriminator:
    method: Tag
    type:
      name: ValueTag
      class: enum
      underlying: { class: simple, type: int }
      values:
        Foo: { value: 1 }
        Bar: { value: 2 }
        Baz: { value: 3 }

  members:
    - class: pointer
      elem:
        name: FooValue
        class: composite
        # ...
Reference

A reference type is a reference to another type in the schema, or to an external type. References that reference another type in the schema are replaced with that type when the schema is validated.

Defining a schema

This Go implementation of Schema has a few features that make YAML/JSON schema definitions less verbose. These features are not part of the specification and may not be present in other implementations.

Type expressions

Simple expressions can be used as a short-hand for some types. For example:

Foo:
  class: composite
  fields:
    - name: Bar
      type: EXPR

There are four types of valid expressions:

  • Simple types and references. These are a name, e.g. Foo, or a selector expression, foo.Bar. If the expression matches a type of simple type, such as int, it is interpreted as such. Otherwise it is interpreted as a type reference.
  • Pointers. *EXPR is interpreted as a pointer to EXPR.
  • Arrays. []EXPR is interpreted as an array of EXPR. [N]EXPR, where N is a positive integer, is interpreted as a fixed-length array of EXPR.
  • Maps. map[EXPR1]EXPR2 is interpreted as a map from EXPR1 to EXPR2.
Pointers and arrays

An extra pointer: true or repeatable: true property within a type definition wraps that definition in a pointer and/or array type. For example, { pointer: true, name: Foo, class: composite, ... } is interpreted as *Foo and { pointer: true, repeatable: true, name: Foo, class: composite, ... } is interpreted as []*Foo. This allows more compact and less indented schemas when definitions are nested.

Example

The following schema defines the request and response types for a hello world web server:

Request:
  class: union
  discriminator:
    method: type
    type:
      name: RequestType
      class: enum
      underlying: int
      values:
        Formal:   { value: 0 }
        Informal: { value: 1 }

  members:
    - name: FormalRequest
      pointer: true
      class: composite
      fields:
        - name: Name
          type: string

    - name: InformalRequest
      pointer: true
      class: composite
      fields:
        - name: Name
          type: string

Response:
  class: composite
  fields:
    - name: Greeting
      type: string
type Request interface {
	Type() RequestType
}

func main() {
	fmt.Println(http.ListenAndServe("localhost:8080", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		err := serve(w, r)
		if err == nil {
			return
		}
		w.WriteHeader(http.StatusBadRequest)
		json.NewEncoder(w).Encode(map[string]any{
			"error": err.Error(),
		})
	})))
}

func serve(w http.ResponseWriter, r *http.Request) error {
	b, err := io.ReadAll(r.Body)
	if err != nil {
		return err
	}
	req, err := UnmarshalRequestJSON(b)
	if err != nil {
		return err
	}

	res := new(Response)
	switch req := req.(type) {
	case *FormalRequest:
		res.Greeting = fmt.Sprintf("Hello, %s", req.Name)
	case *InformalRequest:
		res.Greeting = fmt.Sprintf("Hi %s!", req.Name)
	default:
		return fmt.Errorf("invalid request %T", req)
	}
	return json.NewEncoder(w).Encode(res)
}

Documentation

Overview

Package schema allows the user to define a schema for structured data along with tools for marshalling and unmarshalling.

Package schema defines the data types and schema used for defining schemas.

Index

Constants

This section is empty.

Variables

View Source
var Source string

Source is the source of the schema specification for Schema, Type, and Value.

Functions

func EqualType

func EqualType(a, b Type) bool

EqualType returns true if A and B are equal.

func EqualValue

func EqualValue(a, b Value) bool

EqualValue returns true if A and B are equal.

func TypeOf

func TypeOf[T any]() reflect.Type

TypeOf is a wrapper for reflect to allow generated code to avoid an import.

Types

type ArrayType

type ArrayType struct {
	TypeBase
	// Elem is the type of the array's elements.
	Elem Type `json:"elem"`
	// Length is the length of the array type, if it is fixed length.
	Length uint64 `json:"length"`
}

ArrayType is an array type, aka list, slice, or ordered set.

func (ArrayType) Class

func (ArrayType) Class() TypeClass

func (*ArrayType) Copy

func (v *ArrayType) Copy() *ArrayType

Copy returns a copy of the ArrayType.

func (*ArrayType) Equal

func (v *ArrayType) Equal(u *ArrayType) bool

EqualArrayType returns true if V is equal to U.

func (*ArrayType) GoString

func (a *ArrayType) GoString() string

GoString returns the Go representation of this type.

func (*ArrayType) GoType

func (a *ArrayType) GoType() reflect.Type

GoType returns the reflect.Type of this type.

func (*ArrayType) MarshalBinary

func (v *ArrayType) MarshalBinary() ([]byte, error)

MarshalBinary marshals the ArrayType to bytes using binary.

func (*ArrayType) MarshalBinaryV2

func (v *ArrayType) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the ArrayType to a binary.Encoder.

func (*ArrayType) MarshalJSON

func (v *ArrayType) MarshalJSON() ([]byte, error)

MarshalBinary marshals the ArrayType to JSON.

func (*ArrayType) ResolveElemTo

func (a *ArrayType) ResolveElemTo(s *ResolverSet, name string) *ArrayType

ResolveElemTo resolves [ArrayType.Elem] to the given named type.

func (*ArrayType) SetGoType

func (t *ArrayType) SetGoType(g reflect.Type) Type

func (*ArrayType) String

func (t *ArrayType) String() string

func (*ArrayType) Underlying

func (a *ArrayType) Underlying() *ArrayType

Underlying returns an unnamed version of this type.

func (*ArrayType) UnmarshalBinary

func (v *ArrayType) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the ArrayType from bytes using binary.

func (*ArrayType) UnmarshalBinaryV2

func (v *ArrayType) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the ArrayType from a binary.Decoder.

func (*ArrayType) UnmarshalJSON

func (v *ArrayType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the ArrayType from JSON.

func (*ArrayType) Validate

func (a *ArrayType) Validate() error

Validate validates the array type.

type BooleanValue

type BooleanValue bool

func (BooleanValue) Bool

func (v BooleanValue) Bool(name ...string) (bool, bool)

Bool resolves a value (if name is specified) and attempts to convert it to a [bool].

func (BooleanValue) Copy

func (v BooleanValue) Copy() BooleanValue

Copy returns a copy of the BooleanValue.

func (BooleanValue) Equal

func (v BooleanValue) Equal(u BooleanValue) bool

EqualBooleanValue returns true if V is equal to U.

func (BooleanValue) Kind

func (BooleanValue) Kind() ValueKind

func (BooleanValue) MarshalBinaryV2

func (v BooleanValue) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinaryV2 marshals the value to a binary.Encoder.

func (BooleanValue) MarshalJSON

func (v BooleanValue) MarshalJSON() ([]byte, error)

MarshalJSON marshals the value to JSON.

func (BooleanValue) Number

func (v BooleanValue) Number(name ...string) (float64, bool)

Number resolves a value (if name is specified) and attempts to convert it to a [float64].

func (BooleanValue) String

func (v BooleanValue) String(name ...string) (string, bool)

String resolves a value (if name is specified) and attempts to convert it to a [string].

func (*BooleanValue) UnmarshalBinaryV2

func (v *BooleanValue) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinaryV2 unmarshals the value from a binary.Decoder.

type CompositeType

type CompositeType struct {
	TypeBase
	// Fields are the fields of the composite type.
	Fields []*Field `json:"fields"`
	// Transients are fields that are not included when encoding values of this type.
	Transients []*Field `json:"transients"`
}

CompositeType is a composite type, aka struct or class.

func (CompositeType) Class

func (CompositeType) Class() TypeClass

func (*CompositeType) Copy

func (v *CompositeType) Copy() *CompositeType

Copy returns a copy of the CompositeType.

func (*CompositeType) Equal

func (v *CompositeType) Equal(u *CompositeType) bool

EqualCompositeType returns true if V is equal to U.

func (*CompositeType) GoString

func (c *CompositeType) GoString() string

GoString returns the Go representation of this type.

func (*CompositeType) GoType

func (c *CompositeType) GoType() reflect.Type

GoType returns the reflect.Type of this type.

func (*CompositeType) MarshalBinary

func (v *CompositeType) MarshalBinary() ([]byte, error)

MarshalBinary marshals the CompositeType to bytes using binary.

func (*CompositeType) MarshalBinaryV2

func (v *CompositeType) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the CompositeType to a binary.Encoder.

func (*CompositeType) MarshalJSON

func (v *CompositeType) MarshalJSON() ([]byte, error)

MarshalBinary marshals the CompositeType to JSON.

func (*CompositeType) SetGoType

func (t *CompositeType) SetGoType(g reflect.Type) Type

func (*CompositeType) String

func (t *CompositeType) String() string

func (*CompositeType) UnmarshalBinary

func (v *CompositeType) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the CompositeType from bytes using binary.

func (*CompositeType) UnmarshalBinaryV2

func (v *CompositeType) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the CompositeType from a binary.Decoder.

func (*CompositeType) UnmarshalJSON

func (v *CompositeType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the CompositeType from JSON.

func (*CompositeType) Validate

func (c *CompositeType) Validate() error

Validate validates the composite type.

type EncodeContext

type EncodeContext = traverse.Context[reflect.Value]

EncodeContext is a traverse.Context used for encoding.

type EncodingHooks

type EncodingHooks struct {
	MarshalReferenceTo func(*EncodeContext, Type, reflect.Value) (Type, reflect.Value, bool)

	MarshalJSON             func(*json.Encoder, reflect.Value) (bool, error)
	PreUnmarshalJSON        func(*json.Decoder, reflect.Value) (bool, error)
	UnmarshalJSON           func(*json.Decoder, reflect.Value) error
	UnmarshalExtraJSONField func(*json.Decoder, reflect.Value, string) error

	MarshalBinary   func(*binary.Encoder, reflect.Value) (bool, error)
	UnmarshalBinary func(*binary.Decoder, reflect.Value) error
}

EncodingHooks are hooks to control the encoding of a Type.

type EnumMethods

type EnumMethods[R constraints.Integer] struct {
	Methods[R, *R, *EnumType]
}

EnumMethods allows generated enumeration types to use an EnumType for ByName, String, etc in a type-safe manner.

func WithEnumMethods

func WithEnumMethods[R constraints.Integer](typ *EnumType) EnumMethods[R]

WithEnumMethods returns an EnumMethods for the given type.

func (EnumMethods[R]) ByName

func (e EnumMethods[R]) ByName(s string) (R, bool)

ByName looks up a member of the enumeration by name.

func (EnumMethods[R]) Copy

func (e EnumMethods[R]) Copy(v R) R

Copy does nothing (since enum values are immutable).

func (EnumMethods[R]) Equal

func (e EnumMethods[R]) Equal(a, b R) bool

Equal returns true if A and B are the same.

func (EnumMethods[R]) SetGoType

func (e EnumMethods[R]) SetGoType() EnumMethods[R]

SetGoType sets the Go type of the Type.

func (EnumMethods[R]) String

func (e EnumMethods[R]) String(v R) string

String returns the label or name of the enumeration member.

type EnumType

type EnumType struct {
	TypeBase
	// Underlying is the underlying type of enumeration members.
	Underlying *SimpleType `json:"underlying"`
	// Values describes the members of the enumeration.
	Values map[string]*EnumValue `json:"values"`
}

EnumType is an enumeration type.

func (EnumType) Class

func (EnumType) Class() TypeClass

func (*EnumType) Copy

func (v *EnumType) Copy() *EnumType

Copy returns a copy of the EnumType.

func (*EnumType) Equal

func (v *EnumType) Equal(u *EnumType) bool

EqualEnumType returns true if V is equal to U.

func (*EnumType) GoString

func (e *EnumType) GoString() string

GoString returns the Go representation of this type.

func (*EnumType) MarshalBinary

func (v *EnumType) MarshalBinary() ([]byte, error)

MarshalBinary marshals the EnumType to bytes using binary.

func (*EnumType) MarshalBinaryV2

func (v *EnumType) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the EnumType to a binary.Encoder.

func (*EnumType) MarshalJSON

func (v *EnumType) MarshalJSON() ([]byte, error)

MarshalBinary marshals the EnumType to JSON.

func (*EnumType) SetGoType

func (t *EnumType) SetGoType(g reflect.Type) Type

func (*EnumType) String

func (t *EnumType) String() string

func (*EnumType) UnmarshalBinary

func (v *EnumType) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the EnumType from bytes using binary.

func (*EnumType) UnmarshalBinaryV2

func (v *EnumType) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the EnumType from a binary.Decoder.

func (*EnumType) UnmarshalJSON

func (v *EnumType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the EnumType from JSON.

func (*EnumType) Validate

func (e *EnumType) Validate() error

Validate validates the enum type.

type EnumValue

type EnumValue struct {
	// Name is the name of the enumeration member.
	Name string `json:"name"`
	// Description is the description of the enumeration member.
	Description string `json:"description"`
	// Label is the label of the enumeration member, if different from its name.
	Label string `json:"label"`
	// Value is the underlying value of the enumeration member.
	Value int64 `json:"value"`
	// Aliases are aliases of the enumeration member's name or label.
	Aliases []string `json:"aliases"`

	Enum *EnumType
	// contains filtered or unexported fields
}

EnumValue describes a member of an enumeration.

func (*EnumValue) Copy

func (v *EnumValue) Copy() *EnumValue

Copy returns a copy of the EnumValue.

func (*EnumValue) Equal

func (v *EnumValue) Equal(u *EnumValue) bool

EqualEnumValue returns true if V is equal to U.

func (*EnumValue) GoValue

func (e *EnumValue) GoValue() any

GoValue returns the Go value of this enum value.

func (*EnumValue) MarshalBinary

func (v *EnumValue) MarshalBinary() ([]byte, error)

MarshalBinary marshals the EnumValue to bytes using binary.

func (*EnumValue) MarshalBinaryV2

func (v *EnumValue) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the EnumValue to a binary.Encoder.

func (*EnumValue) MarshalJSON

func (v *EnumValue) MarshalJSON() ([]byte, error)

MarshalBinary marshals the EnumValue to JSON.

func (*EnumValue) String

func (e *EnumValue) String() string

func (*EnumValue) UnmarshalBinary

func (v *EnumValue) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the EnumValue from bytes using binary.

func (*EnumValue) UnmarshalBinaryV2

func (v *EnumValue) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the EnumValue from a binary.Decoder.

func (*EnumValue) UnmarshalJSON

func (v *EnumValue) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the EnumValue from JSON.

type Field

type Field struct {
	// Name is the name of the field.
	Name string `json:"name"`
	// Description is the description of the field.
	Description string `json:"description"`
	// Type is the type of the field.
	Type Type `json:"type"`
	// Optional indicates whether the field is optional.
	Optional bool `json:"optional"`
	// Encode holds settings for encoding.
	Encode MapValue `json:"encode"`
	// Generate holds settings for code generation.
	Generate MapValue `json:"generate"`
	Index    uint64
	ID       uint
	Of       *CompositeType
}

Field is a field of a composite type.

func (*Field) Copy

func (v *Field) Copy() *Field

Copy returns a copy of the Field.

func (*Field) Equal

func (v *Field) Equal(u *Field) bool

EqualField returns true if V is equal to U.

func (*Field) MarshalBinary

func (v *Field) MarshalBinary() ([]byte, error)

MarshalBinary marshals the Field to bytes using binary.

func (*Field) MarshalBinaryV2

func (v *Field) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the Field to a binary.Encoder.

func (*Field) MarshalJSON

func (v *Field) MarshalJSON() ([]byte, error)

MarshalBinary marshals the Field to JSON.

func (*Field) ResolveTo

func (f *Field) ResolveTo(s *ResolverSet, name string) *Field

ResolveTo resolves [Field.Type] to the given named type.

func (*Field) UnmarshalBinary

func (v *Field) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the Field from bytes using binary.

func (*Field) UnmarshalBinaryV2

func (v *Field) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the Field from a binary.Decoder.

func (*Field) UnmarshalJSON

func (v *Field) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the Field from JSON.

func (*Field) Validate

func (f *Field) Validate() error

Validate validates the field.

type ListValue

type ListValue []Value

func (ListValue) Bool

func (v ListValue) Bool(name ...string) (bool, bool)

Bool resolves a value (if name is specified) and attempts to convert it to a [bool].

func (ListValue) Copy

func (v ListValue) Copy() ListValue

Copy returns a copy of the ListValue.

func (ListValue) Equal

func (v ListValue) Equal(u ListValue) bool

EqualListValue returns true if V is equal to U.

func (ListValue) Kind

func (ListValue) Kind() ValueKind

func (ListValue) MarshalBinaryV2

func (v ListValue) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinaryV2 marshals the value to a binary.Encoder.

func (ListValue) MarshalJSON

func (v ListValue) MarshalJSON() ([]byte, error)

MarshalJSON marshals the value to JSON.

func (ListValue) Number

func (v ListValue) Number(name ...string) (float64, bool)

Number resolves a value (if name is specified) and attempts to convert it to a [float64].

func (ListValue) String

func (v ListValue) String(name ...string) (string, bool)

String resolves a value (if name is specified) and attempts to convert it to a [string].

func (*ListValue) UnmarshalBinaryV2

func (v *ListValue) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinaryV2 unmarshals the value from a binary.Decoder.

func (*ListValue) UnmarshalJSON

func (v *ListValue) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the value from JSON.

type MapType

type MapType struct {
	TypeBase
	// Key is the type of the map's keys.
	Key Type `json:"key"`
	// Value is the type of the map's values.
	Value Type `json:"value"`
}

MapType is a map type, aka dictionary or associative array.

func (MapType) Class

func (MapType) Class() TypeClass

func (*MapType) Copy

func (v *MapType) Copy() *MapType

Copy returns a copy of the MapType.

func (*MapType) Equal

func (v *MapType) Equal(u *MapType) bool

EqualMapType returns true if V is equal to U.

func (*MapType) GoString

func (m *MapType) GoString() string

GoString returns the Go representation of this type.

func (*MapType) GoType

func (m *MapType) GoType() reflect.Type

GoType returns the reflect.Type of this type.

func (*MapType) MarshalBinary

func (v *MapType) MarshalBinary() ([]byte, error)

MarshalBinary marshals the MapType to bytes using binary.

func (*MapType) MarshalBinaryV2

func (v *MapType) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the MapType to a binary.Encoder.

func (*MapType) MarshalJSON

func (v *MapType) MarshalJSON() ([]byte, error)

MarshalBinary marshals the MapType to JSON.

func (*MapType) ResolveKeyTo

func (m *MapType) ResolveKeyTo(s *ResolverSet, name string) *MapType

ResolveKeyTo resolves [MapType.Key] to the given named type.

func (*MapType) ResolveValueTo

func (m *MapType) ResolveValueTo(s *ResolverSet, name string) *MapType

ResolveValueTo resolves [MapType.Value] to the given named type.

func (*MapType) SetGoType

func (t *MapType) SetGoType(g reflect.Type) Type

func (*MapType) String

func (t *MapType) String() string

func (*MapType) UnmarshalBinary

func (v *MapType) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the MapType from bytes using binary.

func (*MapType) UnmarshalBinaryV2

func (v *MapType) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the MapType from a binary.Decoder.

func (*MapType) UnmarshalJSON

func (v *MapType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the MapType from JSON.

func (*MapType) Validate

func (m *MapType) Validate() error

Validate validates the map type.

type MapValue

type MapValue map[string]Value

func (MapValue) Bool

func (v MapValue) Bool(name ...string) (bool, bool)

Bool resolves a value (if name is specified) and attempts to convert it to a [bool].

func (MapValue) Copy

func (v MapValue) Copy() MapValue

Copy returns a copy of the MapValue.

func (MapValue) Equal

func (v MapValue) Equal(u MapValue) bool

EqualMapValue returns true if V is equal to U.

func (MapValue) Kind

func (MapValue) Kind() ValueKind

func (MapValue) MarshalBinaryV2

func (v MapValue) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinaryV2 marshals the value to a binary.Encoder.

func (MapValue) MarshalJSON

func (v MapValue) MarshalJSON() ([]byte, error)

MarshalJSON marshals the value to JSON.

func (MapValue) Number

func (v MapValue) Number(name ...string) (float64, bool)

Number resolves a value (if name is specified) and attempts to convert it to a [float64].

func (MapValue) String

func (v MapValue) String(name ...string) (string, bool)

String resolves a value (if name is specified) and attempts to convert it to a [string].

func (*MapValue) UnmarshalBinaryV2

func (v *MapValue) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinaryV2 unmarshals the value from a binary.Decoder.

func (*MapValue) UnmarshalJSON

func (v *MapValue) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the value from JSON.

type Methods

type Methods[R, W any, T Type] struct {
	Type T
}

Methods allows generated types to use a Type for equality, marshalling, etc in a type-safe manner.

func WithMethods

func WithMethods[R, W any, T Type](typ T) Methods[R, W, T]

WithMethods returns a Methods for the given type.

func (Methods[R, W, T]) Copy

func (t Methods[R, W, T]) Copy(v R) R

Copy copies the value.

func (Methods[R, W, T]) Equal

func (t Methods[R, W, T]) Equal(a, b R) bool

Equal returns true if A and B are equal.

func (Methods[R, W, T]) MarshalBinary

func (t Methods[R, W, T]) MarshalBinary(v R) ([]byte, error)

MarshalBinary marshals the value to bytes using binary.

func (Methods[R, W, T]) MarshalBinaryV2

func (t Methods[R, W, T]) MarshalBinaryV2(enc *binary.Encoder, v R) error

MarshalBinaryV2 marshals the value to a binary.Encoder

func (Methods[R, W, T]) MarshalJSON

func (t Methods[R, W, T]) MarshalJSON(v R) ([]byte, error)

MarshalJSON marshals the value to JSON.

func (Methods[R, W, T]) SetGoType

func (t Methods[R, W, T]) SetGoType() Methods[R, W, T]

SetGoType sets the Go type of the Type.

func (Methods[R, W, T]) UnmarshalBinary

func (t Methods[R, W, T]) UnmarshalBinary(b []byte, v W) error

UnmarshalBinary unmarshals the value from bytes using binary.

func (Methods[R, W, T]) UnmarshalBinaryV2

func (t Methods[R, W, T]) UnmarshalBinaryV2(dec *binary.Decoder, v W) error

UnmarshalBinaryV2 unmarshals the value from a binary.Decoder.

func (Methods[R, W, T]) UnmarshalJSON

func (t Methods[R, W, T]) UnmarshalJSON(b []byte, v W) error

UnmarshalJSON unmarshals the value from JSON.

type NumberValue

type NumberValue float64

func (NumberValue) Bool

func (v NumberValue) Bool(name ...string) (bool, bool)

Bool resolves a value (if name is specified) and attempts to convert it to a [bool].

func (NumberValue) Copy

func (v NumberValue) Copy() NumberValue

Copy returns a copy of the NumberValue.

func (NumberValue) Equal

func (v NumberValue) Equal(u NumberValue) bool

EqualNumberValue returns true if V is equal to U.

func (NumberValue) Kind

func (NumberValue) Kind() ValueKind

func (NumberValue) MarshalBinaryV2

func (v NumberValue) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinaryV2 marshals the value to a binary.Encoder.

func (NumberValue) MarshalJSON

func (v NumberValue) MarshalJSON() ([]byte, error)

MarshalJSON marshals the value to JSON.

func (NumberValue) Number

func (v NumberValue) Number(name ...string) (float64, bool)

Number resolves a value (if name is specified) and attempts to convert it to a [float64].

func (NumberValue) String

func (v NumberValue) String(name ...string) (string, bool)

String resolves a value (if name is specified) and attempts to convert it to a [string].

func (*NumberValue) UnmarshalBinaryV2

func (v *NumberValue) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinaryV2 unmarshals the value from a binary.Decoder.

type PointerType

type PointerType struct {
	TypeBase
	// Elem is the type that is pointed to.
	Elem Type `json:"elem"`
}

PointerType is a pointer type.

func (PointerType) Class

func (PointerType) Class() TypeClass

func (*PointerType) Copy

func (v *PointerType) Copy() *PointerType

Copy returns a copy of the PointerType.

func (*PointerType) Equal

func (v *PointerType) Equal(u *PointerType) bool

EqualPointerType returns true if V is equal to U.

func (*PointerType) GoString

func (p *PointerType) GoString() string

GoString returns the Go representation of this type.

func (*PointerType) GoType

func (p *PointerType) GoType() reflect.Type

GoType returns the reflect.Type of this type.

func (*PointerType) MarshalBinary

func (v *PointerType) MarshalBinary() ([]byte, error)

MarshalBinary marshals the PointerType to bytes using binary.

func (*PointerType) MarshalBinaryV2

func (v *PointerType) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the PointerType to a binary.Encoder.

func (*PointerType) MarshalJSON

func (v *PointerType) MarshalJSON() ([]byte, error)

MarshalBinary marshals the PointerType to JSON.

func (*PointerType) ResolveElemTo

func (p *PointerType) ResolveElemTo(s *ResolverSet, name string) *PointerType

ResolveElemTo resolves [PointerType.Elem] to the given named type.

func (*PointerType) SetGoType

func (t *PointerType) SetGoType(g reflect.Type) Type

func (*PointerType) String

func (t *PointerType) String() string

func (*PointerType) UnmarshalBinary

func (v *PointerType) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the PointerType from bytes using binary.

func (*PointerType) UnmarshalBinaryV2

func (v *PointerType) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the PointerType from a binary.Decoder.

func (*PointerType) UnmarshalJSON

func (v *PointerType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the PointerType from JSON.

func (*PointerType) Validate

func (p *PointerType) Validate() error

Validate validates the pointer type.

type ResolverSet

type ResolverSet []func(*Schema)

A ResolverSet is used by the schema generator to break initialization cycles.

func (ResolverSet) Resolve

func (s ResolverSet) Resolve(sch *Schema, types ...Type)

Resolve executes callbacks queued by ResolveXTo calls.

type Schema

type Schema struct {
	Types map[string]Type `json:"types"`
}

Schema describes a set of related [Type]s.

func New

func New(types ...Type) (*Schema, error)

New creates a Schema for the given set of types. New returns an error if any of the types are unnamed, or have the same name. **Validate must be called after New**.

func (*Schema) Copy

func (v *Schema) Copy() *Schema

Copy returns a copy of the Schema.

func (*Schema) Equal

func (v *Schema) Equal(u *Schema) bool

EqualSchema returns true if V is equal to U.

func (*Schema) MarshalBinary

func (v *Schema) MarshalBinary() ([]byte, error)

MarshalBinary marshals the Schema to bytes using binary.

func (*Schema) MarshalBinaryV2

func (v *Schema) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the Schema to a binary.Encoder.

func (*Schema) MarshalJSON

func (v *Schema) MarshalJSON() ([]byte, error)

MarshalBinary marshals the Schema to JSON.

func (*Schema) UnmarshalBinary

func (v *Schema) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the Schema from bytes using binary.

func (*Schema) UnmarshalBinaryV2

func (v *Schema) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the Schema from a binary.Decoder.

func (*Schema) UnmarshalJSON

func (v *Schema) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the Schema from JSON.

func (*Schema) Validate

func (s *Schema) Validate() error

Validate validates the schema.

type SimpleType

type SimpleType struct {
	TypeBase
	// Type is a type such as [int] or [string].
	Type SimpleTypeKind `json:"type"`
}

SimpleType specifies a simple type, such as [int] or [string].

func (SimpleType) Class

func (SimpleType) Class() TypeClass

func (*SimpleType) Copy

func (v *SimpleType) Copy() *SimpleType

Copy returns a copy of the SimpleType.

func (*SimpleType) Equal

func (v *SimpleType) Equal(u *SimpleType) bool

EqualSimpleType returns true if V is equal to U.

func (*SimpleType) GoString

func (s *SimpleType) GoString() string

GoString returns the Go representation of this type.

func (*SimpleType) GoType

func (s *SimpleType) GoType() reflect.Type

GoType returns the reflect.Type of this type.

func (*SimpleType) MarshalBinary

func (v *SimpleType) MarshalBinary() ([]byte, error)

MarshalBinary marshals the SimpleType to bytes using binary.

func (*SimpleType) MarshalBinaryV2

func (v *SimpleType) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the SimpleType to a binary.Encoder.

func (*SimpleType) MarshalJSON

func (v *SimpleType) MarshalJSON() ([]byte, error)

MarshalBinary marshals the SimpleType to JSON.

func (*SimpleType) SetGoType

func (t *SimpleType) SetGoType(g reflect.Type) Type

func (*SimpleType) String

func (t *SimpleType) String() string

func (*SimpleType) UnmarshalBinary

func (v *SimpleType) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the SimpleType from bytes using binary.

func (*SimpleType) UnmarshalBinaryV2

func (v *SimpleType) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the SimpleType from a binary.Decoder.

func (*SimpleType) UnmarshalJSON

func (v *SimpleType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the SimpleType from JSON.

func (*SimpleType) Validate

func (s *SimpleType) Validate() error

Validate validates the simple type.

type SimpleTypeKind

type SimpleTypeKind int64

SimpleTypeKind is the type of SimpleType, e.g. [int] or [string].

const (
	SimpleTypeBool   SimpleTypeKind = 4
	SimpleTypeByte   SimpleTypeKind = 0
	SimpleTypeBytes  SimpleTypeKind = 6
	SimpleTypeFloat  SimpleTypeKind = 3
	SimpleTypeHash   SimpleTypeKind = 7
	SimpleTypeInt    SimpleTypeKind = 1
	SimpleTypeString SimpleTypeKind = 5
	SimpleTypeUint   SimpleTypeKind = 2
)

func SimpleTypeKindByName

func SimpleTypeKindByName(s string) (SimpleTypeKind, bool)

SimpleTypeKindByName looks up a SimpleTypeKind by name.

func (SimpleTypeKind) GoString

func (k SimpleTypeKind) GoString() string

GoString returns the Go representation of this type.

func (SimpleTypeKind) MarshalBinary

func (v SimpleTypeKind) MarshalBinary() ([]byte, error)

MarshalBinary marshals the SimpleTypeKind to bytes using binary.

func (SimpleTypeKind) MarshalBinaryV2

func (v SimpleTypeKind) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the SimpleTypeKind to a binary.Encoder.

func (SimpleTypeKind) MarshalJSON

func (v SimpleTypeKind) MarshalJSON() ([]byte, error)

MarshalBinary marshals the SimpleTypeKind to JSON.

func (SimpleTypeKind) String

func (v SimpleTypeKind) String() string

String returns the label or name of the SimpleTypeKind.

func (*SimpleTypeKind) UnmarshalBinary

func (v *SimpleTypeKind) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the SimpleTypeKind from bytes using binary.

func (*SimpleTypeKind) UnmarshalBinaryV2

func (v *SimpleTypeKind) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the SimpleTypeKind from a binary.Decoder.

func (*SimpleTypeKind) UnmarshalJSON

func (v *SimpleTypeKind) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the SimpleTypeKind from JSON.

type StringValue

type StringValue string

func (StringValue) Bool

func (v StringValue) Bool(name ...string) (bool, bool)

Bool resolves a value (if name is specified) and attempts to convert it to a [bool].

func (StringValue) Copy

func (v StringValue) Copy() StringValue

Copy returns a copy of the StringValue.

func (StringValue) Equal

func (v StringValue) Equal(u StringValue) bool

EqualStringValue returns true if V is equal to U.

func (StringValue) Kind

func (StringValue) Kind() ValueKind

func (StringValue) MarshalBinaryV2

func (v StringValue) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinaryV2 marshals the value to a binary.Encoder.

func (StringValue) MarshalJSON

func (v StringValue) MarshalJSON() ([]byte, error)

MarshalJSON marshals the value to JSON.

func (StringValue) Number

func (v StringValue) Number(name ...string) (float64, bool)

Number resolves a value (if name is specified) and attempts to convert it to a [float64].

func (StringValue) String

func (v StringValue) String(name ...string) (string, bool)

String resolves a value (if name is specified) and attempts to convert it to a [string].

func (*StringValue) UnmarshalBinaryV2

func (v *StringValue) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinaryV2 unmarshals the value from a binary.Decoder.

type Type

type Type interface {
	Class() TypeClass
	Validate() error

	String() string
	GoString() string
	GoType() reflect.Type
	SetGoType(reflect.Type) Type

	GetName() string
	GetUnion() *UnionMember
	EncodeSettings() MapValue
	GenerateSettings() MapValue
	// contains filtered or unexported methods
}

Type describes a data type.

func CopyType

func CopyType(v Type) Type

CopyType returns a copy of the Type.

func UnmarshalTypeBinary

func UnmarshalTypeBinary(b []byte) (Type, error)

UnmarshalTypeBinary unmarshals a Type from bytes using binary.

func UnmarshalTypeBinaryV2

func UnmarshalTypeBinaryV2(dec *binary.Decoder) (Type, error)

UnmarshalTypeBinary unmarshals a Type from a binary.Decoder.

func UnmarshalTypeJSON

func UnmarshalTypeJSON(b []byte) (Type, error)

UnmarshalTypeJSON unmarshals a Type from JSON.

type TypeBase

type TypeBase struct {
	// Name is the name of the type, if the type is named.
	Name string `json:"name"`
	// Description is the description of the type.
	Description string `json:"description"`
	// Encode holds settings for encoding.
	Encode MapValue `json:"encode"`
	// Generate holds settings for code generation.
	Generate MapValue `json:"generate"`

	Union  *UnionMember
	Schema *Schema
	Hook   EncodingHooks
	// contains filtered or unexported fields
}

TypeBase is the base implementation of Type.

func (*TypeBase) Copy

func (v *TypeBase) Copy() *TypeBase

Copy returns a copy of the TypeBase.

func (*TypeBase) EncodeSettings

func (t *TypeBase) EncodeSettings() MapValue

func (*TypeBase) Equal

func (v *TypeBase) Equal(u *TypeBase) bool

EqualTypeBase returns true if V is equal to U.

func (*TypeBase) GenerateSettings

func (t *TypeBase) GenerateSettings() MapValue

func (*TypeBase) GetName

func (t *TypeBase) GetName() string

func (*TypeBase) GetUnion

func (t *TypeBase) GetUnion() *UnionMember

func (*TypeBase) GoType

func (t *TypeBase) GoType() reflect.Type

GoType returns the reflect.Type of this type.

func (*TypeBase) MarshalBinary

func (v *TypeBase) MarshalBinary() ([]byte, error)

MarshalBinary marshals the TypeBase to bytes using binary.

func (*TypeBase) MarshalBinaryV2

func (v *TypeBase) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the TypeBase to a binary.Encoder.

func (*TypeBase) MarshalJSON

func (v *TypeBase) MarshalJSON() ([]byte, error)

MarshalBinary marshals the TypeBase to JSON.

func (*TypeBase) UnmarshalBinary

func (v *TypeBase) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the TypeBase from bytes using binary.

func (*TypeBase) UnmarshalBinaryV2

func (v *TypeBase) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the TypeBase from a binary.Decoder.

func (*TypeBase) UnmarshalJSON

func (v *TypeBase) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the TypeBase from JSON.

type TypeClass

type TypeClass int64

TypeClass is the class of a specific Type.

const (
	TypeClassArray     TypeClass = 6
	TypeClassComposite TypeClass = 5
	TypeClassEnum      TypeClass = 3
	TypeClassMap       TypeClass = 4
	TypeClassPointer   TypeClass = 7
	TypeClassReference TypeClass = 1
	TypeClassSimple    TypeClass = 0
	TypeClassUnion     TypeClass = 2
)

func TypeClassByName

func TypeClassByName(s string) (TypeClass, bool)

TypeClassByName looks up a TypeClass by name.

func (TypeClass) MarshalBinary

func (v TypeClass) MarshalBinary() ([]byte, error)

MarshalBinary marshals the TypeClass to bytes using binary.

func (TypeClass) MarshalBinaryV2

func (v TypeClass) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the TypeClass to a binary.Encoder.

func (TypeClass) MarshalJSON

func (v TypeClass) MarshalJSON() ([]byte, error)

MarshalBinary marshals the TypeClass to JSON.

func (TypeClass) String

func (v TypeClass) String() string

String returns the label or name of the TypeClass.

func (*TypeClass) UnmarshalBinary

func (v *TypeClass) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the TypeClass from bytes using binary.

func (*TypeClass) UnmarshalBinaryV2

func (v *TypeClass) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the TypeClass from a binary.Decoder.

func (*TypeClass) UnmarshalJSON

func (v *TypeClass) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the TypeClass from JSON.

type TypeReference

type TypeReference struct {
	TypeBase
}

TypeReference is a reference to a named type.

func (TypeReference) Class

func (TypeReference) Class() TypeClass

func (*TypeReference) Copy

func (v *TypeReference) Copy() *TypeReference

Copy returns a copy of the TypeReference.

func (*TypeReference) Equal

func (v *TypeReference) Equal(u *TypeReference) bool

EqualTypeReference returns true if V is equal to U.

func (*TypeReference) GoString

func (r *TypeReference) GoString() string

GoString returns the Go representation of this type.

func (*TypeReference) MarshalBinary

func (v *TypeReference) MarshalBinary() ([]byte, error)

MarshalBinary marshals the TypeReference to bytes using binary.

func (*TypeReference) MarshalBinaryV2

func (v *TypeReference) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the TypeReference to a binary.Encoder.

func (*TypeReference) MarshalJSON

func (v *TypeReference) MarshalJSON() ([]byte, error)

MarshalBinary marshals the TypeReference to JSON.

func (*TypeReference) SetGoType

func (t *TypeReference) SetGoType(g reflect.Type) Type

func (*TypeReference) String

func (t *TypeReference) String() string

func (*TypeReference) UnmarshalBinary

func (v *TypeReference) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the TypeReference from bytes using binary.

func (*TypeReference) UnmarshalBinaryV2

func (v *TypeReference) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the TypeReference from a binary.Decoder.

func (*TypeReference) UnmarshalJSON

func (v *TypeReference) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the TypeReference from JSON.

func (*TypeReference) Validate

func (r *TypeReference) Validate() error

Validate validates the type reference.

type UnionDiscriminator

type UnionDiscriminator struct {
	// Type is the enumeration type that discriminates members of the union. Must be an [EnumType].
	Type Type `json:"type"`
	// Method is the name of the virtual field that discriminates members of the union.
	Method string `json:"method"`
	Enum   *EnumType
}

UnionDiscriminator specifies how members of the union are discriminated.

func (*UnionDiscriminator) Copy

Copy returns a copy of the UnionDiscriminator.

func (*UnionDiscriminator) Equal

EqualUnionDiscriminator returns true if V is equal to U.

func (*UnionDiscriminator) MarshalBinary

func (v *UnionDiscriminator) MarshalBinary() ([]byte, error)

MarshalBinary marshals the UnionDiscriminator to bytes using binary.

func (*UnionDiscriminator) MarshalBinaryV2

func (v *UnionDiscriminator) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the UnionDiscriminator to a binary.Encoder.

func (*UnionDiscriminator) MarshalJSON

func (v *UnionDiscriminator) MarshalJSON() ([]byte, error)

MarshalBinary marshals the UnionDiscriminator to JSON.

func (*UnionDiscriminator) ResolveEnumTo

func (u *UnionDiscriminator) ResolveEnumTo(s *ResolverSet, name string) *UnionDiscriminator

ResolveEnumTo resolves [UnionDiscriminator.Enum] to the given named type.

func (*UnionDiscriminator) ResolveTypeTo

func (u *UnionDiscriminator) ResolveTypeTo(s *ResolverSet, name string) *UnionDiscriminator

ResolveTypeTo resolves [UnionDiscriminator.Type] to the given named type.

func (*UnionDiscriminator) UnmarshalBinary

func (v *UnionDiscriminator) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the UnionDiscriminator from bytes using binary.

func (*UnionDiscriminator) UnmarshalBinaryV2

func (v *UnionDiscriminator) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the UnionDiscriminator from a binary.Decoder.

func (*UnionDiscriminator) UnmarshalJSON

func (v *UnionDiscriminator) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the UnionDiscriminator from JSON.

type UnionMember

type UnionMember struct {
	// Discriminator specifies which value of the discriminator enum corresponds to this union member. May be omitted if it can be inferred from the union and member type names.
	Discriminator string `json:"discriminator"`
	Type
	Union              *UnionType
	Named              Type
	DiscriminatorValue *EnumValue
}

UnionMember specifies a member of a union.

func (*UnionMember) Copy

func (v *UnionMember) Copy() *UnionMember

Copy returns a copy of the UnionMember.

func (*UnionMember) Equal

func (v *UnionMember) Equal(u *UnionMember) bool

EqualUnionMember returns true if V is equal to U.

func (*UnionMember) GoType

func (u *UnionMember) GoType() reflect.Type

GoType returns the reflect.Type of this type.

func (*UnionMember) MarshalBinary

func (v *UnionMember) MarshalBinary() ([]byte, error)

MarshalBinary marshals the UnionMember to bytes using binary.

func (*UnionMember) MarshalBinaryV2

func (v *UnionMember) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the UnionMember to a binary.Encoder.

func (*UnionMember) MarshalJSON

func (v *UnionMember) MarshalJSON() ([]byte, error)

MarshalBinary marshals the UnionMember to JSON.

func (*UnionMember) ResolveTo

func (u *UnionMember) ResolveTo(s *ResolverSet, name string) *UnionMember

ResolveTo resolves [UnionMember.Type] to the given named type.

func (*UnionMember) UnmarshalBinary

func (v *UnionMember) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the UnionMember from bytes using binary.

func (*UnionMember) UnmarshalBinaryV2

func (v *UnionMember) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the UnionMember from a binary.Decoder.

func (*UnionMember) UnmarshalJSON

func (v *UnionMember) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the UnionMember from JSON.

type UnionType

type UnionType struct {
	TypeBase
	// Discriminator specifies how members of the union are discriminated.
	Discriminator *UnionDiscriminator `json:"discriminator"`
	// Members lists the members of the union.
	Members []*UnionMember `json:"members"`
}

UnionType is a discriminated union of Type.

func (UnionType) Class

func (UnionType) Class() TypeClass

func (*UnionType) Copy

func (v *UnionType) Copy() *UnionType

Copy returns a copy of the UnionType.

func (*UnionType) Equal

func (v *UnionType) Equal(u *UnionType) bool

EqualUnionType returns true if V is equal to U.

func (*UnionType) GoString

func (u *UnionType) GoString() string

GoString returns the Go representation of this type.

func (*UnionType) MarshalBinary

func (v *UnionType) MarshalBinary() ([]byte, error)

MarshalBinary marshals the UnionType to bytes using binary.

func (*UnionType) MarshalBinaryV2

func (v *UnionType) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the UnionType to a binary.Encoder.

func (*UnionType) MarshalJSON

func (v *UnionType) MarshalJSON() ([]byte, error)

MarshalBinary marshals the UnionType to JSON.

func (*UnionType) SetGoType

func (t *UnionType) SetGoType(g reflect.Type) Type

func (*UnionType) String

func (t *UnionType) String() string

func (*UnionType) UnmarshalBinary

func (v *UnionType) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the UnionType from bytes using binary.

func (*UnionType) UnmarshalBinaryV2

func (v *UnionType) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the UnionType from a binary.Decoder.

func (*UnionType) UnmarshalJSON

func (v *UnionType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the UnionType from JSON.

func (*UnionType) Validate

func (u *UnionType) Validate() error

Validate validates the union type.

type Value

type Value interface {
	Kind() ValueKind
	Bool(...string) (bool, bool)
	Number(...string) (float64, bool)
	String(...string) (string, bool)
}

Value holds unstructured data.

func CopyValue

func CopyValue(v Value) Value

CopyValue returns a copy of the Value.

func UnmarshalValueBinary

func UnmarshalValueBinary(b []byte) (Value, error)

UnmarshalValueBinary unmarshals a Value from bytes using binary.

func UnmarshalValueBinaryV2

func UnmarshalValueBinaryV2(dec *binary.Decoder) (Value, error)

UnmarshalValueBinary unmarshals a Value from a binary.Decoder.

func UnmarshalValueJSON

func UnmarshalValueJSON(b []byte) (Value, error)

UnmarshalValueJSON unmarshals a Value from JSON.

type ValueKind

type ValueKind uint64
const (
	ValueKindBoolean ValueKind = 2
	ValueKindList    ValueKind = 1
	ValueKindMap     ValueKind = 0
	ValueKindNumber  ValueKind = 3
	ValueKindString  ValueKind = 4
)

func ValueKindByName

func ValueKindByName(s string) (ValueKind, bool)

ValueKindByName looks up a ValueKind by name.

func (ValueKind) MarshalBinary

func (v ValueKind) MarshalBinary() ([]byte, error)

MarshalBinary marshals the ValueKind to bytes using binary.

func (ValueKind) MarshalBinaryV2

func (v ValueKind) MarshalBinaryV2(enc *binary.Encoder) error

MarshalBinary marshals the ValueKind to a binary.Encoder.

func (ValueKind) MarshalJSON

func (v ValueKind) MarshalJSON() ([]byte, error)

MarshalBinary marshals the ValueKind to JSON.

func (ValueKind) String

func (v ValueKind) String() string

String returns the label or name of the ValueKind.

func (*ValueKind) UnmarshalBinary

func (v *ValueKind) UnmarshalBinary(b []byte) error

UnmarshalBinary unmarshals the ValueKind from bytes using binary.

func (*ValueKind) UnmarshalBinaryV2

func (v *ValueKind) UnmarshalBinaryV2(dec *binary.Decoder) error

UnmarshalBinary unmarshals the ValueKind from a binary.Decoder.

func (*ValueKind) UnmarshalJSON

func (v *ValueKind) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals the ValueKind from JSON.

Directories

Path Synopsis
cmd
examples
gen
pkg

Jump to

Keyboard shortcuts

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