schema

package
v0.0.0-...-3a06bfb Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrDecodeMsgBadSig = errors.New("bad type signature")
View Source
var ErrRequired = errors.New("value is required")
View Source
var ErrUnexpectedField = errors.New("unexpected field")
View Source
var ErrValidation = errors.New("validation error")
View Source
var ZSTSchema = Struct(&ZST{}).DebugName("ZST").buildStruct()

Functions

func Bool

func Bool() atomBuilder[bool]

func F

func F[T any](name string, ptr *T, subBuilder builder[T]) *fieldBuilder

func Int

func Int() atomBuilder[int64]

func Map

func Map[V any](valueBuilder builder[V]) mapBuilder[V]

func Ptr

func Ptr[T any](elemBuilder builder[T]) ptrBuilder[T]

func Rat

func Rat() atomBuilder[*big.Rat]

func String

func String() atomBuilder[string]

func StructFunc

func StructFunc[S any](f func(*S) *StructBuilder[S]) *_Struct[S]

func ZSTSchemaAs

func ZSTSchemaAs[T ~struct{} | ~struct{ ZST }]() *_Struct[T]

Types

type AtomVisitor

type AtomVisitor interface {
	VisitInt64(x int64, isDefault bool) error
	VisitRat(x *big.Rat, isDefault bool) error
	VisitBool(x bool, isDefault bool) error
	VisitString(x string, isDefault bool) error
}

type ListVisitor

type ListVisitor interface {
	VisitListBegin(size int) error
	VisitListEnd(size int) error
	VisitListItemBegin(i int) error
	VisitListItemEnd(i int) error
}

type MapVisitor

type MapVisitor interface {
	VisitMapBegin(size int) error
	VisitMapEnd(size int) error
	VisitMapItemBegin(key string) error
	VisitMapItemEnd(key string) error
}

type Scanner

type Scanner interface {
	Snapshot() any
	Reset(snapshot any)
	// contains filtered or unexported methods
}

type Schema

type Schema[T any] interface {
	ScanFrom(r Scanner) (T, pos.P, error)
	DecodeMsg(r *fastbuf.R) (T, error)
	EncodeMsg(w *fastbuf.W, source T)
	Visit(v Visitor, source T) error
}

func New

func New[S any](builder *StructBuilder[S]) Schema[*S]
Example
package main

import (
	"fmt"
	"math/big"

	"github.com/hsfzxjy/imbed/schema"
	schemascanner "github.com/hsfzxjy/imbed/schema/scanner"
	"github.com/hsfzxjy/imbed/util/fastbuf"
)

type X struct {
	int64
	bool
	*big.Rat
	string
	m map[string]int64
}

func (x X) GoString() string {
	return fmt.Sprintf("%d %v %s %s %#v", x.int64, x.bool, x.RatString(), x.string, x.m)
}

func main() {
	var x = new(X)
	s := schema.Struct(x,
		schema.F("int", &x.int64, schema.Int()),
		schema.F("bool", &x.bool, schema.Bool()),
		schema.F("float", &x.Rat, schema.Rat()),
		schema.F("m", &x.m, schema.Map(schema.Int())),
		schema.F("str", &x.string, schema.String())).
		DebugName("X")
	sch := schema.New(s)
	x, _, err := sch.ScanFrom(schemascanner.Any(map[string]any{
		"int":   int64(1),
		"bool":  true,
		"m":     map[string]any{"a": int64(1), "b": int64(2)},
		"float": "3.14",
		"str":   "test",
	}))
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", x)

	var w fastbuf.W
	sch.EncodeMsg(&w, x)
	r := fastbuf.R{Buf: w.Result()}
	encoded := w.Result()
	for i := 0; i < 10; i++ {
		var w fastbuf.W
		sch.EncodeMsg(&w, x)
		if string(w.Result()) != string(encoded) {
			panic("encode not idempotent")
		}
	}
	x, err = sch.DecodeMsg(&r)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", x)

}
Output:

1 true 157/50 test map[string]int64{"a":1, "b":2}
1 true 157/50 test map[string]int64{"a":1, "b":2}

type StructBuilder

type StructBuilder[T any] struct {
	// contains filtered or unexported fields
}

func Struct

func Struct[S any](ptr *S, fieldBuilders ...*fieldBuilder) *StructBuilder[S]

func (*StructBuilder[T]) Build

func (s *StructBuilder[T]) Build() Schema[*T]

func (*StructBuilder[T]) DebugName

func (s *StructBuilder[T]) DebugName(name string) *StructBuilder[T]

type StructVisitor

type StructVisitor interface {
	VisitStructBegin(size int) error
	VisitStructEnd(size int) error
	VisitStructFieldBegin(name string) error
	VisitStructFieldEnd(name string) error
}

type Validator

type Validator interface {
	Validate() error
}

type Visitor

type Visitor interface {
	AtomVisitor
	VisitList(size int) (lv ListVisitor, elem Visitor, err error)
	VisitMap(size int) (mv MapVisitor, elem Visitor, err error)
	VisitStruct(size int) (sv StructVisitor, elem Visitor, err error)
	VisitPtr(isNil, isDefault bool) (elem Visitor, err error)
}

type ZST

type ZST struct{}

func (ZST) EncodeMsg

func (x ZST) EncodeMsg(w *fastbuf.W)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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