schema

package
v1.20.1 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2023 License: MIT Imports: 15 Imported by: 11

README

x/schema - Golang recursive schema

Library allows to write code that work with any type of schemas. Regardless if those are JSON, XML, YAML, or golang structs.

Most benefits

  • Union types can be deserialized into interface field

How to convert between json <-> go

data := `{"name": "John", "cars": [{"name":"Ford"}]}`
schema := schema.FromJSON(data)
nativego, err := schema.ToGo(schema)

expected := map[string]any{
    "name": "John",
    "cars": []any{
        map[string]any{
            "name": "Ford",
        },
    },
}
assert.Equal(t, expected, nativego)

How to convert schema into named golang struct?

This example shows how to convert only part of schema to golang struct. List of cars will have type Car when parent Person object will be map[string]any.

type Car struct {
    Name string
}
nativego := schema.MustToGo(schema, WithOnlyTheseRules(
	WhenPath([]string{"cars", "[*]"}, UseStruct(Car{}))))

expected := map[string]any{
    "name": "John",
    "cars": []any{
        Car{
            Name: "Ford",
        },
    },
}
assert.Equal(t, expected, nativego)

How to define custom serialization and deserilization?

Currently, ser-deser operations are available on maps. This is current design decision, it might change in the future.

type Car struct {
    Name string
}

// make sure to Car implements schema.Marshaler and schema.Unmarshaler
var (
	_ schema.Marshaler = (*Car)(nil)
    _ schema.Unmarshaler = (*Car)(nil)
)

func (c *Car) MarshalSchema() (schema.*Map, error) {
    return schema.MkMap(map[string]schema.Schema{
        "name": schema.MkString(c.Name),
    }), nil
}

func (c *Car) UnmarshalSchema(x schema.*Map) error {
    for _, field := range x.Field {
        switch key {
        case "name":
            c.Name = s.MustToString()
        default:
            return fmt.Errorf("unknown key %s", key)
        }
    }
	
    return nil
}
How to convert well defined types from external packages?
type Car struct {
    Name string
    LastDriven time.Time
}

// Register conversion from time.Time to schema.String
schema.RegisterWellDefinedTypesConversion[time.Time](
  func(x time.Time) Schema {
      return MkString(x.Format(time.RFC3339Nano))
  },
  func(x Schema) time.Time {
      if v, ok := x.(*String); ok {
          t, _ := time.Parse(time.RFC3339Nano, string(*v))
          return t
      }

      panic("invalid type")
  },
)

// Then you can translate schema between back and forth without worrying about time.Time
schema := FromGo(data)
result, err := ToGoG[ExternalPackageStruct](schema)
assert.NoError(t, err)
assert.Equal(t, data, result)

Roadmap

V0.1.0
  • Json <-> Schema <-> Go (with structs mapping)
  • Write test with wrong type conversions
  • Value are split into Number(Int, Float), String, Bool, and Null
  • Default schema registry + mkunion make union serialization/deserialization work transperently
  • Support pointers *string, etc.
  • Support DynamoDB (FromDynamoDB, ToDynamoDB)
  • Support for pointer to types like *string, *int, etc.
  • Support for relative paths like WhenPath([]string{"*", "ListOfCars", "Car"}, UseStruct(Car{})). Absolute paths are without * at the beginning.
V0.2.x
  • Support options for ToGo & FromGo like WithOnlyTheseRules, WithExtraRules, WithDefaultMapDef, etc. Gives better control on how schema is converted to golang. It's especially important from security reasons, whey you want to allow rules only whitelisted rules, for user generated json input.
  • Schema support interface for custom type-setters, that don't require reflection, and mkunion can leverage them. Use UseTypeDef eg. WhenPath([]string{}, UseTypeDef(&someTypeDef{})),
  • Support for how union names should be expressed in schema WithUnionFormatter(func(t reflect.Type) string)
V0.3.x
  • schema.Compare method to compare two schemas
V0.4.x
  • Support for Binary type
  • Add missing function for MkBinary, MkFloat, MkNone
V0.5.x
  • schema.UnwrapDynamoDB takes DynamoDB specific nesting and removes it.
  • Eliminate data races in *UnionVariants[A] -> MapDefFor & UseUnionFormatter data race
  • Introduce ToGoG[T any] function, that makes ToGo with type assertion and tries to convert to T
  • Rename schema.As to schema.AsDefault and make schema.As as variant that returns false, if type is not supported
V0.6.x
  • Support serialization of schema.Marchaler to schema.Unmarshaller, that can dramatically improve performance in some cases. Limitation of current implementation is that it works only on *Map, and don't allow custom ser-deser on other types. It's not hard decision. It's just that I don't have use case for other types yet.
V0.7.x
  • schema.Schema is now serializable and deserilizable
V0.8.x
  • schema.ToGo can deduce nested types, for fields in struct that have type information
  • Generator of custom ser-deser that improve performance and developer experiences for free
V0.9.x
  • Support json tags in golang to map field names to schema
  • Add cata, ana, and hylo morphisms
  • Open goConfigFunc to allow customizing how golang types are converted to schema, passed from external code.
V0.10.x
  • schema use x/shape and native types to represent variants like Map and List and Bytes
  • schema becomes data
  • data.FromGo and data.ToGo works only on primitive values
  • data.FromStruct and data.ToStruct works only on structs and reflection
  • data.FromJSON and data.ToJSON removed and replaced by mkunion defaults
  • data.FromDynamoDB and data.ToDynamoDB refactored

Documentation

Overview

Code generated by mkunion. DO NOT EDIT.

Code generated by mkunion. DO NOT EDIT.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func As added in v1.9.0

func As[A int | int8 | int16 | int32 | int64 |
	uint | uint8 | uint16 | uint32 | uint64 |
	float32 | float64 |
	bool | string | []byte](x Schema) (A, bool)

func AsDefault added in v1.12.0

func AsDefault[A int | int8 | int16 | int32 | int64 |
	uint | uint8 | uint16 | uint32 | uint64 |
	float32 | float64 |
	bool | string | []byte](x Schema, def A) A

func BinaryToJSON added in v1.20.0

func BinaryToJSON(x *Binary) ([]byte, error)

func BoolToJSON added in v1.20.0

func BoolToJSON(x *Bool) ([]byte, error)

func Compare added in v1.10.0

func Compare(a, b Schema) int

func FormatUnionNameUsingFullName added in v1.9.0

func FormatUnionNameUsingFullName(t reflect.Type) string

func FormatUnionNameUsingTypeName added in v1.9.0

func FormatUnionNameUsingTypeName(t reflect.Type) string

func FormatUnionNameUsingTypeNameWithPackage added in v1.9.0

func FormatUnionNameUsingTypeNameWithPackage(t reflect.Type) string

func ListToJSON added in v1.20.0

func ListToJSON(x *List) ([]byte, error)

func LocationAnythingToJSON added in v1.20.0

func LocationAnythingToJSON(x *LocationAnything) ([]byte, error)

func LocationFieldToJSON added in v1.20.0

func LocationFieldToJSON(x *LocationField) ([]byte, error)

func LocationIndexToJSON added in v1.20.0

func LocationIndexToJSON(x *LocationIndex) ([]byte, error)

func LocationToJSON added in v1.20.0

func LocationToJSON(x Location) ([]byte, error)

func LocationToStr added in v1.19.0

func LocationToStr(location []Location) string

func MapToJSON added in v1.20.0

func MapToJSON(x *Map) ([]byte, error)

func MatchLocation added in v1.19.0

func MatchLocation[TOut any](
	x Location,
	f1 func(x *LocationField) TOut,
	f2 func(x *LocationIndex) TOut,
	f3 func(x *LocationAnything) TOut,
	df func(x Location) TOut,
) TOut

func MatchLocationR2 added in v1.19.0

func MatchLocationR2[TOut1, TOut2 any](
	x Location,
	f1 func(x *LocationField) (TOut1, TOut2),
	f2 func(x *LocationIndex) (TOut1, TOut2),
	f3 func(x *LocationAnything) (TOut1, TOut2),
	df func(x Location) (TOut1, TOut2),
) (TOut1, TOut2)

func MatchSchema added in v1.10.0

func MatchSchema[TOut any](
	x Schema,
	f1 func(x *None) TOut,
	f2 func(x *Bool) TOut,
	f3 func(x *Number) TOut,
	f4 func(x *String) TOut,
	f5 func(x *Binary) TOut,
	f6 func(x *List) TOut,
	f7 func(x *Map) TOut,
	df func(x Schema) TOut,
) TOut

func MatchSchemaR2 added in v1.10.0

func MatchSchemaR2[TOut1, TOut2 any](
	x Schema,
	f1 func(x *None) (TOut1, TOut2),
	f2 func(x *Bool) (TOut1, TOut2),
	f3 func(x *Number) (TOut1, TOut2),
	f4 func(x *String) (TOut1, TOut2),
	f5 func(x *Binary) (TOut1, TOut2),
	f6 func(x *List) (TOut1, TOut2),
	f7 func(x *Map) (TOut1, TOut2),
	df func(x Schema) (TOut1, TOut2),
) (TOut1, TOut2)

func MustMatchLocation added in v1.19.0

func MustMatchLocation[TOut any](
	x Location,
	f1 func(x *LocationField) TOut,
	f2 func(x *LocationIndex) TOut,
	f3 func(x *LocationAnything) TOut,
) TOut

func MustMatchLocationR0 added in v1.19.0

func MustMatchLocationR0(
	x Location,
	f1 func(x *LocationField),
	f2 func(x *LocationIndex),
	f3 func(x *LocationAnything),
)

func MustMatchLocationR2 added in v1.19.0

func MustMatchLocationR2[TOut1, TOut2 any](
	x Location,
	f1 func(x *LocationField) (TOut1, TOut2),
	f2 func(x *LocationIndex) (TOut1, TOut2),
	f3 func(x *LocationAnything) (TOut1, TOut2),
) (TOut1, TOut2)

func MustMatchSchema added in v1.7.3

func MustMatchSchema[TOut any](
	x Schema,
	f1 func(x *None) TOut,
	f2 func(x *Bool) TOut,
	f3 func(x *Number) TOut,
	f4 func(x *String) TOut,
	f5 func(x *Binary) TOut,
	f6 func(x *List) TOut,
	f7 func(x *Map) TOut,
) TOut

func MustMatchSchemaR0 added in v1.14.1

func MustMatchSchemaR0(
	x Schema,
	f1 func(x *None),
	f2 func(x *Bool),
	f3 func(x *Number),
	f4 func(x *String),
	f5 func(x *Binary),
	f6 func(x *List),
	f7 func(x *Map),
)

func MustMatchSchemaR2 added in v1.7.3

func MustMatchSchemaR2[TOut1, TOut2 any](
	x Schema,
	f1 func(x *None) (TOut1, TOut2),
	f2 func(x *Bool) (TOut1, TOut2),
	f3 func(x *Number) (TOut1, TOut2),
	f4 func(x *String) (TOut1, TOut2),
	f5 func(x *Binary) (TOut1, TOut2),
	f6 func(x *List) (TOut1, TOut2),
	f7 func(x *Map) (TOut1, TOut2),
) (TOut1, TOut2)

func MustToGo added in v1.9.0

func MustToGo(x Schema, options ...goConfigFunc) any

func NoneToJSON added in v1.20.0

func NoneToJSON(x *None) ([]byte, error)

func NumberToJSON added in v1.20.0

func NumberToJSON(x *Number) ([]byte, error)

func Reduce added in v1.7.7

func Reduce[A any](data Schema, init A, fn func(Schema, A) A) A

func ReduceLocationBreadthFirst added in v1.19.0

func ReduceLocationBreadthFirst[A any](r LocationReducer[A], v Location, init A) A

func ReduceLocationDepthFirst added in v1.19.0

func ReduceLocationDepthFirst[A any](r LocationReducer[A], v Location, init A) A

func ReduceSchemaBreadthFirst added in v1.7.3

func ReduceSchemaBreadthFirst[A any](r SchemaReducer[A], v Schema, init A) A

func ReduceSchemaDepthFirst added in v1.7.3

func ReduceSchemaDepthFirst[A any](r SchemaReducer[A], v Schema, init A) A

func RegisterRules added in v1.7.2

func RegisterRules(xs []RuleMatcher)

func RegisterUnionTypes added in v1.9.0

func RegisterUnionTypes[A any](x *UnionVariants[A])

func RegisterWellDefinedTypesConversion added in v1.20.0

func RegisterWellDefinedTypesConversion[T any](from func(T) Schema, to func(Schema) T)

func SchemaToJSON added in v1.20.0

func SchemaToJSON(x Schema) ([]byte, error)

func SetDefaultUnionTypeFormatter added in v1.9.0

func SetDefaultUnionTypeFormatter(f UnionFormatFunc)

func StringToJSON added in v1.20.0

func StringToJSON(x *String) ([]byte, error)

func ToDynamoDB added in v1.7.7

func ToDynamoDB(x Schema) types.AttributeValue

func ToGo added in v1.7.2

func ToGo(x Schema, options ...goConfigFunc) (any, error)

func ToGoG added in v1.12.0

func ToGoG[A any](x Schema, options ...goConfigFunc) (A, error)

ToGoG converts a Schema to a Go value of the given type.

func ToJSON added in v1.7.2

func ToJSON(schema Schema) ([]byte, error)

deprecated: ToJSON use SchemaToJSON

func UnionOf added in v1.19.0

func UnionOf(t reflect.Type) (reflect.Type, []reflect.Type, bool)

func WithDefaultListDef added in v1.9.0

func WithDefaultListDef(def TypeListDefinition) goConfigFunc

func WithDefaultMaoDef added in v1.9.0

func WithDefaultMaoDef(def TypeMapDefinition) goConfigFunc

func WithExtraRules added in v1.9.0

func WithExtraRules(rules ...RuleMatcher) goConfigFunc

func WithOnlyTheseRules added in v1.9.0

func WithOnlyTheseRules(rules ...RuleMatcher) goConfigFunc

func WithRulesFromRegistry added in v1.9.0

func WithRulesFromRegistry(registry *Registry) goConfigFunc

func WithUnionFormatter added in v1.9.0

func WithUnionFormatter(f UnionFormatFunc) goConfigFunc

func WithWellDefinedTypeConversion added in v1.20.0

func WithWellDefinedTypeConversion[T any](from func(T) Schema, to func(Schema) T) goConfigFunc

func WithoutDefaultRegistry added in v1.9.0

func WithoutDefaultRegistry() goConfigFunc

Types

type Acc added in v1.19.0

type Acc struct {
	Name  *string `@(String|Char|RawString)`
	Index *int    `| @Int`
	Any   bool    `| @("*") `
}

func (Acc) ToAccessor added in v1.19.0

func (a Acc) ToAccessor() Location

type Binary added in v1.11.0

type Binary []byte

func BinaryFromJSON added in v1.20.0

func BinaryFromJSON(x []byte) (*Binary, error)

func MkBinary added in v1.11.0

func MkBinary(b []byte) *Binary

func (*Binary) AcceptSchema added in v1.15.0

func (r *Binary) AcceptSchema(v SchemaVisitor) any

func (*Binary) UnmarshalSchema added in v1.19.0

func (self *Binary) UnmarshalSchema(x *Map) error

type Bool

type Bool bool

func BoolFromJSON added in v1.20.0

func BoolFromJSON(x []byte) (*Bool, error)

func MkBool added in v1.10.0

func MkBool(b bool) *Bool

func (*Bool) AcceptSchema added in v1.15.0

func (r *Bool) AcceptSchema(v SchemaVisitor) any

func (*Bool) UnmarshalSchema added in v1.19.0

func (self *Bool) UnmarshalSchema(x *Map) error

type Field

type Field struct {
	Name  string
	Value Schema
}

func MkField added in v1.10.0

func MkField(name string, value Schema) Field

func (*Field) MarshalJSON added in v1.20.1

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

func (*Field) UnmarshalJSON added in v1.20.1

func (f *Field) UnmarshalJSON(bytes []byte) error

type List

type List []Schema

func ListFromJSON added in v1.20.0

func ListFromJSON(x []byte) (*List, error)

func MkList added in v1.10.0

func MkList(items ...Schema) *List

func (*List) AcceptSchema added in v1.15.0

func (r *List) AcceptSchema(v SchemaVisitor) any

type ListBuilder added in v1.9.0

type ListBuilder interface {
	Append(value any) error
	Build() any
}

type Location added in v1.19.0

type Location interface {
	AcceptLocation(g LocationVisitor) any
}

func LocationFromJSON added in v1.20.0

func LocationFromJSON(x []byte) (Location, error)

func MustParseLocation added in v1.19.0

func MustParseLocation(input string) []Location

func ParseLocation added in v1.19.0

func ParseLocation(input string) ([]Location, error)

type LocationAnything added in v1.19.0

type LocationAnything struct{}

func LocationAnythingFromJSON added in v1.20.0

func LocationAnythingFromJSON(x []byte) (*LocationAnything, error)

func (*LocationAnything) AcceptLocation added in v1.19.0

func (r *LocationAnything) AcceptLocation(v LocationVisitor) any

func (*LocationAnything) MarshalJSON added in v1.20.0

func (self *LocationAnything) MarshalJSON() ([]byte, error)

func (*LocationAnything) UnmarshalJSON added in v1.20.0

func (self *LocationAnything) UnmarshalJSON(x []byte) error

type LocationBreadthFirstVisitor added in v1.19.0

type LocationBreadthFirstVisitor[A any] struct {
	// contains filtered or unexported fields
}

func (*LocationBreadthFirstVisitor[A]) VisitLocationAnything added in v1.19.0

func (d *LocationBreadthFirstVisitor[A]) VisitLocationAnything(v *LocationAnything) any

func (*LocationBreadthFirstVisitor[A]) VisitLocationField added in v1.19.0

func (d *LocationBreadthFirstVisitor[A]) VisitLocationField(v *LocationField) any

func (*LocationBreadthFirstVisitor[A]) VisitLocationIndex added in v1.19.0

func (d *LocationBreadthFirstVisitor[A]) VisitLocationIndex(v *LocationIndex) any

type LocationDefaultReduction added in v1.19.0

type LocationDefaultReduction[A any] struct {
	PanicOnFallback      bool
	DefaultStopReduction bool
	OnLocationField      func(x *LocationField, agg A) (result A, stop bool)
	OnLocationIndex      func(x *LocationIndex, agg A) (result A, stop bool)
	OnLocationAnything   func(x *LocationAnything, agg A) (result A, stop bool)
}

func (*LocationDefaultReduction[A]) ReduceLocationAnything added in v1.19.0

func (t *LocationDefaultReduction[A]) ReduceLocationAnything(x *LocationAnything, agg A) (result A, stop bool)

func (*LocationDefaultReduction[A]) ReduceLocationField added in v1.19.0

func (t *LocationDefaultReduction[A]) ReduceLocationField(x *LocationField, agg A) (result A, stop bool)

func (*LocationDefaultReduction[A]) ReduceLocationIndex added in v1.19.0

func (t *LocationDefaultReduction[A]) ReduceLocationIndex(x *LocationIndex, agg A) (result A, stop bool)

type LocationDefaultVisitor added in v1.19.0

type LocationDefaultVisitor[A any] struct {
	Default            A
	OnLocationField    func(x *LocationField) A
	OnLocationIndex    func(x *LocationIndex) A
	OnLocationAnything func(x *LocationAnything) A
}

mkunion-extension:default_visitor

func (*LocationDefaultVisitor[A]) VisitLocationAnything added in v1.19.0

func (t *LocationDefaultVisitor[A]) VisitLocationAnything(v *LocationAnything) any

func (*LocationDefaultVisitor[A]) VisitLocationField added in v1.19.0

func (t *LocationDefaultVisitor[A]) VisitLocationField(v *LocationField) any

func (*LocationDefaultVisitor[A]) VisitLocationIndex added in v1.19.0

func (t *LocationDefaultVisitor[A]) VisitLocationIndex(v *LocationIndex) any

type LocationDepthFirstVisitor added in v1.19.0

type LocationDepthFirstVisitor[A any] struct {
	// contains filtered or unexported fields
}

func (*LocationDepthFirstVisitor[A]) VisitLocationAnything added in v1.19.0

func (d *LocationDepthFirstVisitor[A]) VisitLocationAnything(v *LocationAnything) any

func (*LocationDepthFirstVisitor[A]) VisitLocationField added in v1.19.0

func (d *LocationDepthFirstVisitor[A]) VisitLocationField(v *LocationField) any

func (*LocationDepthFirstVisitor[A]) VisitLocationIndex added in v1.19.0

func (d *LocationDepthFirstVisitor[A]) VisitLocationIndex(v *LocationIndex) any

type LocationField added in v1.19.0

type LocationField struct {
	Name string
}

func LocationFieldFromJSON added in v1.20.0

func LocationFieldFromJSON(x []byte) (*LocationField, error)

func (*LocationField) AcceptLocation added in v1.19.0

func (r *LocationField) AcceptLocation(v LocationVisitor) any

func (*LocationField) MarshalJSON added in v1.20.0

func (self *LocationField) MarshalJSON() ([]byte, error)

func (*LocationField) UnmarshalJSON added in v1.20.0

func (self *LocationField) UnmarshalJSON(x []byte) error

type LocationIndex added in v1.19.0

type LocationIndex struct {
	Index int
}

func LocationIndexFromJSON added in v1.20.0

func LocationIndexFromJSON(x []byte) (*LocationIndex, error)

func (*LocationIndex) AcceptLocation added in v1.19.0

func (r *LocationIndex) AcceptLocation(v LocationVisitor) any

func (*LocationIndex) MarshalJSON added in v1.20.0

func (self *LocationIndex) MarshalJSON() ([]byte, error)

func (*LocationIndex) UnmarshalJSON added in v1.20.0

func (self *LocationIndex) UnmarshalJSON(x []byte) error

type LocationReducer added in v1.19.0

type LocationReducer[A any] interface {
	ReduceLocationField(x *LocationField, agg A) (result A, stop bool)
	ReduceLocationIndex(x *LocationIndex, agg A) (result A, stop bool)
	ReduceLocationAnything(x *LocationAnything, agg A) (result A, stop bool)
}

mkunion-extension:reducer_dfs

type LocationUnionJSON added in v1.20.0

type LocationUnionJSON struct {
	Type             string          `json:"$type,omitempty"`
	LocationField    json.RawMessage `json:"schema.LocationField,omitempty"`
	LocationIndex    json.RawMessage `json:"schema.LocationIndex,omitempty"`
	LocationAnything json.RawMessage `json:"schema.LocationAnything,omitempty"`
}

mkunion-extension:json

type LocationVisitor added in v1.19.0

type LocationVisitor interface {
	VisitLocationField(v *LocationField) any
	VisitLocationIndex(v *LocationIndex) any
	VisitLocationAnything(v *LocationAnything) any
}

type Map

type Map map[string]Schema

func MapFromJSON added in v1.20.0

func MapFromJSON(x []byte) (*Map, error)

func MkMap added in v1.10.0

func MkMap(fields ...Field) *Map

func (*Map) AcceptSchema added in v1.15.0

func (r *Map) AcceptSchema(v SchemaVisitor) any

func (*Map) UnmarshalJSON added in v1.20.0

func (x *Map) UnmarshalJSON(bytes []byte) error

func (*Map) UnmarshalSchema added in v1.19.0

func (x *Map) UnmarshalSchema(y *Map) error

type MapBuilder added in v1.9.0

type MapBuilder interface {
	Set(key string, value any) error
	Build() any
}

type Marshaler added in v1.13.0

type Marshaler interface {
	MarshalSchema() (*Map, error)
}

type NativeList

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

func (*NativeList) Append added in v1.9.0

func (s *NativeList) Append(value any) error

func (*NativeList) Build added in v1.9.0

func (s *NativeList) Build() any

func (*NativeList) NewListBuilder added in v1.9.0

func (s *NativeList) NewListBuilder() ListBuilder

type NativeMap

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

func (*NativeMap) Build added in v1.9.0

func (s *NativeMap) Build() any

func (*NativeMap) NewMapBuilder added in v1.9.0

func (s *NativeMap) NewMapBuilder() MapBuilder

func (*NativeMap) Set

func (s *NativeMap) Set(k string, value any) error

type None

type None struct{}

func MkNone added in v1.11.0

func MkNone() *None

func NoneFromJSON added in v1.20.0

func NoneFromJSON(x []byte) (*None, error)

func (*None) AcceptSchema added in v1.15.0

func (r *None) AcceptSchema(v SchemaVisitor) any

func (*None) MarshalJSON added in v1.20.0

func (self *None) MarshalJSON() ([]byte, error)

func (*None) UnmarshalJSON added in v1.20.0

func (self *None) UnmarshalJSON(x []byte) error

type Number

type Number float64

func MkFloat added in v1.11.0

func MkFloat(x float64) *Number

func MkInt

func MkInt(x int) *Number

func NumberFromJSON added in v1.20.0

func NumberFromJSON(x []byte) (*Number, error)

func (*Number) AcceptSchema added in v1.15.0

func (r *Number) AcceptSchema(v SchemaVisitor) any

func (*Number) UnmarshalSchema added in v1.19.0

func (self *Number) UnmarshalSchema(x *Map) error

type Part added in v1.19.0

type Part struct {
	Location string `@Ident`
	Acc      []Acc  `("[" @@ "]")*`
}

func (Part) ToLocation added in v1.19.0

func (p Part) ToLocation() []Location

type PathAst added in v1.19.0

type PathAst struct {
	Parts []Part `@@ ( "." @@ )*`
}

func (PathAst) ToLocation added in v1.19.0

func (ast PathAst) ToLocation() ([]Location, error)

type Registry added in v1.7.2

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

func NewRegistry added in v1.7.2

func NewRegistry() *Registry

func (*Registry) RegisterRules added in v1.7.2

func (r *Registry) RegisterRules(xs []RuleMatcher)

func (*Registry) SetUnionTypeFormatter added in v1.9.0

func (r *Registry) SetUnionTypeFormatter(f UnionFormatFunc)

func (*Registry) TypeFormatter added in v1.20.0

func (r *Registry) TypeFormatter(t reflect.Type) string

func (*Registry) UnionOf added in v1.19.0

func (r *Registry) UnionOf(t reflect.Type) (reflect.Type, []reflect.Type, bool)

type RuleMatcher

type RuleMatcher interface {
	MapDefFor(x *Map, path []string, config *goConfig) (TypeMapDefinition, bool)
	SchemaToUnionType(x any, schema Schema, config *goConfig) (Schema, bool)
}

type Schema added in v1.7.3

type Schema interface {
	AcceptSchema(g SchemaVisitor) any
}

func FromDynamoDB added in v1.7.7

func FromDynamoDB(x types.AttributeValue) (Schema, error)

func FromGo added in v1.7.3

func FromGo(x any, options ...goConfigFunc) Schema

func FromJSON added in v1.7.3

func FromJSON(data []byte) (Schema, error)

deprecated FromJSON

func Get added in v1.9.0

func Get(data Schema, location string) Schema

func GetLocation added in v1.19.0

func GetLocation(data Schema, locations []Location) Schema

func SchemaFromJSON added in v1.20.0

func SchemaFromJSON(x []byte) (Schema, error)

func UnwrapDynamoDB added in v1.12.0

func UnwrapDynamoDB(data Schema) (Schema, error)

type SchemaBreadthFirstVisitor added in v1.7.3

type SchemaBreadthFirstVisitor[A any] struct {
	// contains filtered or unexported fields
}

func (*SchemaBreadthFirstVisitor[A]) VisitBinary added in v1.11.0

func (d *SchemaBreadthFirstVisitor[A]) VisitBinary(v *Binary) any

func (*SchemaBreadthFirstVisitor[A]) VisitBool added in v1.7.3

func (d *SchemaBreadthFirstVisitor[A]) VisitBool(v *Bool) any

func (*SchemaBreadthFirstVisitor[A]) VisitList added in v1.7.3

func (d *SchemaBreadthFirstVisitor[A]) VisitList(v *List) any

func (*SchemaBreadthFirstVisitor[A]) VisitMap added in v1.7.3

func (d *SchemaBreadthFirstVisitor[A]) VisitMap(v *Map) any

func (*SchemaBreadthFirstVisitor[A]) VisitNone added in v1.7.3

func (d *SchemaBreadthFirstVisitor[A]) VisitNone(v *None) any

func (*SchemaBreadthFirstVisitor[A]) VisitNumber added in v1.7.3

func (d *SchemaBreadthFirstVisitor[A]) VisitNumber(v *Number) any

func (*SchemaBreadthFirstVisitor[A]) VisitString added in v1.7.3

func (d *SchemaBreadthFirstVisitor[A]) VisitString(v *String) any

type SchemaDefaultReduction added in v1.7.3

type SchemaDefaultReduction[A any] struct {
	PanicOnFallback      bool
	DefaultStopReduction bool
	OnNone               func(x *None, agg A) (result A, stop bool)
	OnBool               func(x *Bool, agg A) (result A, stop bool)
	OnNumber             func(x *Number, agg A) (result A, stop bool)
	OnString             func(x *String, agg A) (result A, stop bool)
	OnBinary             func(x *Binary, agg A) (result A, stop bool)
	OnList               func(x *List, agg A) (result A, stop bool)
	OnMap                func(x *Map, agg A) (result A, stop bool)
}

func (*SchemaDefaultReduction[A]) ReduceBinary added in v1.11.0

func (t *SchemaDefaultReduction[A]) ReduceBinary(x *Binary, agg A) (result A, stop bool)

func (*SchemaDefaultReduction[A]) ReduceBool added in v1.7.3

func (t *SchemaDefaultReduction[A]) ReduceBool(x *Bool, agg A) (result A, stop bool)

func (*SchemaDefaultReduction[A]) ReduceList added in v1.7.3

func (t *SchemaDefaultReduction[A]) ReduceList(x *List, agg A) (result A, stop bool)

func (*SchemaDefaultReduction[A]) ReduceMap added in v1.7.3

func (t *SchemaDefaultReduction[A]) ReduceMap(x *Map, agg A) (result A, stop bool)

func (*SchemaDefaultReduction[A]) ReduceNone added in v1.7.3

func (t *SchemaDefaultReduction[A]) ReduceNone(x *None, agg A) (result A, stop bool)

func (*SchemaDefaultReduction[A]) ReduceNumber added in v1.7.3

func (t *SchemaDefaultReduction[A]) ReduceNumber(x *Number, agg A) (result A, stop bool)

func (*SchemaDefaultReduction[A]) ReduceString added in v1.7.3

func (t *SchemaDefaultReduction[A]) ReduceString(x *String, agg A) (result A, stop bool)

type SchemaDefaultVisitor added in v1.7.3

type SchemaDefaultVisitor[A any] struct {
	Default  A
	OnNone   func(x *None) A
	OnBool   func(x *Bool) A
	OnNumber func(x *Number) A
	OnString func(x *String) A
	OnBinary func(x *Binary) A
	OnList   func(x *List) A
	OnMap    func(x *Map) A
}

mkunion-extension:default_visitor

func (*SchemaDefaultVisitor[A]) VisitBinary added in v1.11.0

func (t *SchemaDefaultVisitor[A]) VisitBinary(v *Binary) any

func (*SchemaDefaultVisitor[A]) VisitBool added in v1.7.3

func (t *SchemaDefaultVisitor[A]) VisitBool(v *Bool) any

func (*SchemaDefaultVisitor[A]) VisitList added in v1.7.3

func (t *SchemaDefaultVisitor[A]) VisitList(v *List) any

func (*SchemaDefaultVisitor[A]) VisitMap added in v1.7.3

func (t *SchemaDefaultVisitor[A]) VisitMap(v *Map) any

func (*SchemaDefaultVisitor[A]) VisitNone added in v1.7.3

func (t *SchemaDefaultVisitor[A]) VisitNone(v *None) any

func (*SchemaDefaultVisitor[A]) VisitNumber added in v1.7.3

func (t *SchemaDefaultVisitor[A]) VisitNumber(v *Number) any

func (*SchemaDefaultVisitor[A]) VisitString added in v1.7.3

func (t *SchemaDefaultVisitor[A]) VisitString(v *String) any

type SchemaDepthFirstVisitor added in v1.7.3

type SchemaDepthFirstVisitor[A any] struct {
	// contains filtered or unexported fields
}

func (*SchemaDepthFirstVisitor[A]) VisitBinary added in v1.11.0

func (d *SchemaDepthFirstVisitor[A]) VisitBinary(v *Binary) any

func (*SchemaDepthFirstVisitor[A]) VisitBool added in v1.7.3

func (d *SchemaDepthFirstVisitor[A]) VisitBool(v *Bool) any

func (*SchemaDepthFirstVisitor[A]) VisitList added in v1.7.3

func (d *SchemaDepthFirstVisitor[A]) VisitList(v *List) any

func (*SchemaDepthFirstVisitor[A]) VisitMap added in v1.7.3

func (d *SchemaDepthFirstVisitor[A]) VisitMap(v *Map) any

func (*SchemaDepthFirstVisitor[A]) VisitNone added in v1.7.3

func (d *SchemaDepthFirstVisitor[A]) VisitNone(v *None) any

func (*SchemaDepthFirstVisitor[A]) VisitNumber added in v1.7.3

func (d *SchemaDepthFirstVisitor[A]) VisitNumber(v *Number) any

func (*SchemaDepthFirstVisitor[A]) VisitString added in v1.7.3

func (d *SchemaDepthFirstVisitor[A]) VisitString(v *String) any

type SchemaReducer added in v1.7.3

type SchemaReducer[A any] interface {
	ReduceNone(x *None, agg A) (result A, stop bool)
	ReduceBool(x *Bool, agg A) (result A, stop bool)
	ReduceNumber(x *Number, agg A) (result A, stop bool)
	ReduceString(x *String, agg A) (result A, stop bool)
	ReduceBinary(x *Binary, agg A) (result A, stop bool)
	ReduceList(x *List, agg A) (result A, stop bool)
	ReduceMap(x *Map, agg A) (result A, stop bool)
}

mkunion-extension:reducer_dfs

type SchemaUnionJSON added in v1.20.0

type SchemaUnionJSON struct {
	Type   string          `json:"$type,omitempty"`
	None   json.RawMessage `json:"schema.None,omitempty"`
	Bool   json.RawMessage `json:"schema.Bool,omitempty"`
	Number json.RawMessage `json:"schema.Number,omitempty"`
	String json.RawMessage `json:"schema.String,omitempty"`
	Binary json.RawMessage `json:"schema.Binary,omitempty"`
	List   json.RawMessage `json:"schema.List,omitempty"`
	Map    json.RawMessage `json:"schema.Map,omitempty"`
}

mkunion-extension:json

type SchemaVisitor added in v1.7.3

type SchemaVisitor interface {
	VisitNone(v *None) any
	VisitBool(v *Bool) any
	VisitNumber(v *Number) any
	VisitString(v *String) any
	VisitBinary(v *Binary) any
	VisitList(v *List) any
	VisitMap(v *Map) any
}

type SelfUnmarshallingStructBuilder added in v1.13.0

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

func UseSelfUnmarshallingStruct added in v1.13.0

func UseSelfUnmarshallingStruct(new func() Unmarshaler) *SelfUnmarshallingStructBuilder

func (*SelfUnmarshallingStructBuilder) NewMapBuilder added in v1.13.0

func (c *SelfUnmarshallingStructBuilder) NewMapBuilder() MapBuilder

type String

type String string

func MkString

func MkString(s string) *String

func StringFromJSON added in v1.20.0

func StringFromJSON(x []byte) (*String, error)

func (*String) AcceptSchema added in v1.15.0

func (r *String) AcceptSchema(v SchemaVisitor) any

func (*String) UnmarshalSchema added in v1.19.0

func (self *String) UnmarshalSchema(x *Map) error

type StructBuilder added in v1.9.0

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

func (*StructBuilder) Build added in v1.9.0

func (s *StructBuilder) Build() any

func (*StructBuilder) Set added in v1.9.0

func (s *StructBuilder) Set(key string, value any) error

func (*StructBuilder) WithWellDefinedTypesConversion added in v1.20.0

func (s *StructBuilder) WithWellDefinedTypesConversion(from func(x Schema, r reflect.Type) any)

type StructDefinition added in v1.9.0

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

func (*StructDefinition) NewMapBuilder added in v1.9.0

func (s *StructDefinition) NewMapBuilder() MapBuilder

type TypeListDefinition added in v1.9.0

type TypeListDefinition interface {
	NewListBuilder() ListBuilder
}

type TypeMapDefinition added in v1.9.0

type TypeMapDefinition interface {
	NewMapBuilder() MapBuilder
}

func UseReflectionUnmarshallingStruct added in v1.13.0

func UseReflectionUnmarshallingStruct(t any) TypeMapDefinition

func UseStruct

func UseStruct(t any) TypeMapDefinition

func UseTypeDef added in v1.9.0

func UseTypeDef(definition TypeMapDefinition) TypeMapDefinition

type UnionFormatFunc added in v1.9.0

type UnionFormatFunc func(t reflect.Type) string

type UnionInformationRule added in v1.19.0

type UnionInformationRule interface {
	UnionType() reflect.Type
	VariantsTypes() []reflect.Type
	IsUnionOrUnionType(t reflect.Type) bool
}

type UnionMap added in v1.9.0

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

func (*UnionMap) Build added in v1.9.0

func (u *UnionMap) Build() any

func (*UnionMap) NewMapBuilder added in v1.9.0

func (u *UnionMap) NewMapBuilder() MapBuilder

func (*UnionMap) Set added in v1.9.0

func (u *UnionMap) Set(key string, value any) error

type UnionVariants added in v1.9.0

type UnionVariants[A any] struct {
	// contains filtered or unexported fields
}

func MustDefineUnion added in v1.9.0

func MustDefineUnion[A any](xs ...A) *UnionVariants[A]

func SchemaSchemaDef added in v1.19.0

func SchemaSchemaDef() *UnionVariants[Schema]

func (*UnionVariants[A]) IsUnionOrUnionType added in v1.19.0

func (u *UnionVariants[A]) IsUnionOrUnionType(t reflect.Type) bool

func (*UnionVariants[A]) MapDefFor added in v1.9.0

func (u *UnionVariants[A]) MapDefFor(x *Map, path []string, config *goConfig) (TypeMapDefinition, bool)

func (*UnionVariants[A]) SchemaToUnionType added in v1.9.0

func (u *UnionVariants[A]) SchemaToUnionType(x any, schema Schema, config *goConfig) (Schema, bool)

func (*UnionVariants[A]) UnionType added in v1.19.0

func (u *UnionVariants[A]) UnionType() reflect.Type

func (*UnionVariants[A]) VariantsTypes added in v1.19.0

func (u *UnionVariants[A]) VariantsTypes() []reflect.Type

type Unmarshaler added in v1.13.0

type Unmarshaler interface {
	UnmarshalSchema(x *Map) error
}

type WellDefinedFromToStrategy added in v1.20.0

type WellDefinedFromToStrategy[T any] struct {
	ToSchema func(x T) Schema
	ToGo     func(x Schema) T
}

func NewWellDefinedFromToStrategy added in v1.20.0

func NewWellDefinedFromToStrategy[T any](from func(T) Schema, to func(Schema) T) WellDefinedFromToStrategy[any]

NewWellDefinedFromToStrategy assumption is that from and to functions are symmetrical and that function works on values, not pointers (to reduce implementation complexity) and if there is need to do conversion on pointers, then it should be done in "runtime" wrapper (here)

type WhenField

type WhenField[A any] struct {
	// contains filtered or unexported fields
}

func WhenPath

func WhenPath(path []string, setter TypeMapDefinition) *WhenField[struct{}]

func (*WhenField[A]) MapDefFor added in v1.9.0

func (r *WhenField[A]) MapDefFor(x *Map, path []string, config *goConfig) (TypeMapDefinition, bool)

func (*WhenField[A]) SchemaToUnionType added in v1.9.0

func (r *WhenField[A]) SchemaToUnionType(x any, schema Schema, config *goConfig) (Schema, bool)

type WrapInMap added in v1.9.0

type WrapInMap[A any] struct {
	ForType A
	InField string
}

func (*WrapInMap[A]) MapDefFor added in v1.9.0

func (w *WrapInMap[A]) MapDefFor(x *Map, path []string, config *goConfig) (TypeMapDefinition, bool)

func (*WrapInMap[A]) SchemaToUnionType added in v1.9.0

func (w *WrapInMap[A]) SchemaToUnionType(x any, schema Schema, config *goConfig) (Schema, bool)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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