Documentation ¶
Index ¶
- Variables
- func Bool() atomBuilder[bool]
- func F[T any](name string, ptr *T, subBuilder builder[T]) *fieldBuilder
- func Int() atomBuilder[int64]
- func Map[V any](valueBuilder builder[V]) mapBuilder[V]
- func Ptr[T any](elemBuilder builder[T]) ptrBuilder[T]
- func Rat() atomBuilder[*big.Rat]
- func String() atomBuilder[string]
- func StructFunc[S any](f func(*S) *StructBuilder[S]) *_Struct[S]
- func ZSTSchemaAs[T ~struct{} | ~struct{ ZST }]() *_Struct[T]
- type AtomVisitor
- type ListVisitor
- type MapVisitor
- type Scanner
- type Schema
- type StructBuilder
- type StructVisitor
- type Validator
- type Visitor
- type ZST
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 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 ListVisitor ¶
type MapVisitor ¶
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 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) }
Source Files ¶
- builder.go
- builder_atom.go
- builder_list.go
- builder_map.go
- builder_ptr.go
- builder_struct.go
- builder_toplevel.go
- errors.go
- interface.go
- rat.go
- scanner.go
- schema_atom.go
- schema_list.go
- schema_map.go
- schema_ptr.go
- schema_struct.go
- schema_struct_field.go
- schema_toplevel.go
- sig.go
- value.go
- visitor.go
- zst.go
Click to show internal directories.
Click to hide internal directories.