Documentation
¶
Overview ¶
Example (PrettyPrint) ¶
// Ctx tracks the buffer that we're writing into, as well as the current indentation level.
// Writing errors can mostly be ignored because writing to strings.Builder cannot fail.
type Ctx struct {
Buffer *strings.Builder
Indent int
}
// Write N spaces to the buffer to indent correctly.
indent := func(ctx Ctx) {
for i := 0; i < ctx.Indent; i++ {
ctx.Buffer.WriteRune(' ')
}
}
register := tw.NewRegister[Ctx]()
// For simple types, just serialize into the buffer.
tw.RegisterCompileBoolFn(register, func(typ reflect.Type) tw.WalkFn[Ctx, bool] {
return func(ctx Ctx, v tw.Bool) error {
_, err := fmt.Fprintf(ctx.Buffer, "%t", v.Get())
return err
}
})
tw.RegisterCompileIntFn(register, func(typ reflect.Type) tw.WalkFn[Ctx, int] {
return func(ctx Ctx, v tw.Int) error {
_, err := fmt.Fprintf(ctx.Buffer, "%d", v.Get())
return err
}
})
tw.RegisterCompileStringFn(register, func(typ reflect.Type) tw.WalkFn[Ctx, string] {
return func(ctx Ctx, v tw.String) error {
_, err := fmt.Fprintf(ctx.Buffer, `"%s"`, v.Get())
return err
}
})
// For pointers, handle nil, otherwise handle recursively.
tw.RegisterCompilePtrFn(register, func(typ reflect.Type) tw.WalkPtrFn[Ctx] {
return func(ctx Ctx, p tw.Ptr[Ctx]) error {
if p.IsNil() {
_, _ = ctx.Buffer.WriteString("nil")
return nil
}
return p.Walk(ctx)
}
})
// For slices, handle each element, setting up the correct indentation.
tw.RegisterCompileSliceFn(register, func(typ reflect.Type) tw.WalkSliceFn[Ctx] {
return func(ctx Ctx, p tw.Slice[Ctx]) error {
if p.IsNil() {
_, err := fmt.Fprintf(ctx.Buffer, "nil")
return err
}
ctx.Buffer.WriteString("[\n")
ctx.Indent += 2
for i := 0; i < p.Len(); i++ {
indent(ctx)
err := p.Elem(i).Walk(ctx)
if err != nil {
return err
}
ctx.Buffer.WriteString("\n")
}
ctx.Indent -= 2
indent(ctx)
ctx.Buffer.WriteString("]")
return nil
}
})
// For structs, when compiling, register the fields and track the field names.
// When walking, handle each element, printing the correct name and setting up the correct indentation.
tw.RegisterCompileStructFn(register, func(typ reflect.Type, r tw.StructFieldRegister) tw.WalkStructFn[Ctx] {
fieldNames := make([]string, typ.NumField())
for i := 0; i < typ.NumField(); i++ {
idx := r.RegisterField(i)
fieldNames[idx] = typ.Field(i).Name
}
return func(ctx Ctx, s tw.Struct[Ctx]) error {
ctx.Buffer.WriteString("{\n")
ctx.Indent += 2
for i := 0; i < s.NumFields(); i++ {
indent(ctx)
ctx.Buffer.WriteString(fieldNames[i])
ctx.Buffer.WriteString(": ")
err := s.Field(i).Walk(ctx)
if err != nil {
return err
}
ctx.Buffer.WriteString("\n")
}
ctx.Indent -= 2
indent(ctx)
ctx.Buffer.WriteString("}")
return nil
}
})
walker := tw.NewWalker[Ctx](register)
type Inner struct {
A *int
B bool
C []string
}
type Outer struct {
S []*Inner
}
ctx := Ctx{Buffer: &strings.Builder{}}
err := walker.Walk(ctx, Outer{
S: []*Inner{
{A: ptr(1), B: true, C: []string{"foo", "bar", "baz"}},
nil,
{A: nil, B: false, C: nil},
},
})
if err != nil {
panic(err)
}
fmt.Println(ctx.Buffer.String())
Output: { S: [ { A: 1 B: true C: [ "foo" "bar" "baz" ] } nil { A: nil B: false C: nil } ] }
Index ¶
- func RegisterCompileArrayFn[Ctx any](register *Register[Ctx], fn CompileArrayFn[Ctx])
- func RegisterCompileBoolFn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, bool])
- func RegisterCompileComplex64Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, complex64])
- func RegisterCompileComplex128Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, complex128])
- func RegisterCompileFloat32Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, float32])
- func RegisterCompileFloat64Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, float64])
- func RegisterCompileInt8Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, int8])
- func RegisterCompileInt16Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, int16])
- func RegisterCompileInt32Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, int32])
- func RegisterCompileInt64Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, int64])
- func RegisterCompileIntFn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, int])
- func RegisterCompileInterfaceFn[Ctx any](register *Register[Ctx], fn CompileInterfaceFn[Ctx])
- func RegisterCompileMapFn[Ctx any](register *Register[Ctx], fn CompileMapFn[Ctx])
- func RegisterCompilePtrFn[Ctx any](register *Register[Ctx], fn CompilePtrFn[Ctx])
- func RegisterCompileSliceFn[Ctx any](register *Register[Ctx], fn CompileSliceFn[Ctx])
- func RegisterCompileStringFn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, string])
- func RegisterCompileStructFn[Ctx any](register *Register[Ctx], fn CompileStructFn[Ctx])
- func RegisterCompileUint8Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, uint8])
- func RegisterCompileUint16Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, uint16])
- func RegisterCompileUint32Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, uint32])
- func RegisterCompileUint64Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, uint64])
- func RegisterCompileUintFn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, uint])
- func RegisterCompileUintptrFn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, uintptr])
- func RegisterCompileUnsafePointerFn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, unsafe.Pointer])
- func RegisterTypeFn[Ctx any, In any](register *Register[Ctx], fn WalkFn[Ctx, In])
- type Arg
- type Array
- type ArrayElem
- type Bool
- type CompileArrayFn
- type CompileFn
- type CompileInterfaceFn
- type CompileMapFn
- type CompilePtrFn
- type CompileSliceFn
- type CompileStructFn
- type Complex64
- type Complex128
- type Float32
- type Float64
- type Int
- type Int8
- type Int16
- type Int32
- type Int64
- type Interface
- type Map
- type MapEntry
- type MapIter
- type MapKey
- type MapValue
- type Ptr
- type Register
- type Slice
- type SliceElem
- type String
- type Struct
- type StructField
- type StructFieldRegister
- type TypeFn
- type Uint
- type Uint8
- type Uint16
- type Uint32
- type Uint64
- type Uintptr
- type UnsafePointer
- type WalkArrayFn
- type WalkFn
- type WalkInterfaceFn
- type WalkMapFn
- type WalkPtrFn
- type WalkSliceFn
- type WalkStructFn
- type Walker
- type WalkerOpt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterCompileArrayFn ¶ added in v0.2.0
func RegisterCompileArrayFn[Ctx any](register *Register[Ctx], fn CompileArrayFn[Ctx])
RegisterCompileArrayFn registers a compile function for types of kind Array.
func RegisterCompileBoolFn ¶ added in v0.2.0
RegisterCompileBoolFn registers a compile function for types of kind Bool.
func RegisterCompileComplex64Fn ¶ added in v0.2.0
RegisterCompileComplex64Fn registers a compile function for types of kind Complex64.
func RegisterCompileComplex128Fn ¶ added in v0.2.0
func RegisterCompileComplex128Fn[Ctx any](register *Register[Ctx], fn CompileFn[Ctx, complex128])
RegisterCompileComplex128Fn registers a compile function for types of kind Complex128.
func RegisterCompileFloat32Fn ¶ added in v0.2.0
RegisterCompileFloat32Fn registers a compile function for types of kind Float32.
func RegisterCompileFloat64Fn ¶ added in v0.2.0
RegisterCompileFloat64Fn registers a compile function for types of kind Float64.
func RegisterCompileInt8Fn ¶ added in v0.2.0
RegisterCompileInt8Fn registers a compile function for types of kind Int8.
func RegisterCompileInt16Fn ¶ added in v0.2.0
RegisterCompileInt16Fn registers a compile function for types of kind Int16.
func RegisterCompileInt32Fn ¶ added in v0.2.0
RegisterCompileInt32Fn registers a compile function for types of kind Int32.
func RegisterCompileInt64Fn ¶ added in v0.2.0
RegisterCompileInt64Fn registers a compile function for types of kind Int64.
func RegisterCompileIntFn ¶ added in v0.2.0
RegisterCompileIntFn registers a compile function for types of kind Int.
func RegisterCompileInterfaceFn ¶ added in v0.2.0
func RegisterCompileInterfaceFn[Ctx any](register *Register[Ctx], fn CompileInterfaceFn[Ctx])
RegisterCompileInterfaceFn registers a compile function for types of kind Interface.
func RegisterCompileMapFn ¶ added in v0.2.0
func RegisterCompileMapFn[Ctx any](register *Register[Ctx], fn CompileMapFn[Ctx])
RegisterCompileMapFn registers a compile function for types of kind Map.
func RegisterCompilePtrFn ¶ added in v0.2.0
func RegisterCompilePtrFn[Ctx any](register *Register[Ctx], fn CompilePtrFn[Ctx])
RegisterCompilePtrFn registers a compile function for types of kind Ptr.
func RegisterCompileSliceFn ¶ added in v0.2.0
func RegisterCompileSliceFn[Ctx any](register *Register[Ctx], fn CompileSliceFn[Ctx])
RegisterCompileSliceFn registers a compile function for types of kind Slice.
func RegisterCompileStringFn ¶ added in v0.2.0
RegisterCompileStringFn registers a compile function for types of kind String.
func RegisterCompileStructFn ¶ added in v0.2.0
func RegisterCompileStructFn[Ctx any](register *Register[Ctx], fn CompileStructFn[Ctx])
RegisterCompileStructFn registers a compile function for types of kind Struct.
func RegisterCompileUint8Fn ¶ added in v0.2.0
RegisterCompileUint8Fn registers a compile function for types of kind Uint8.
func RegisterCompileUint16Fn ¶ added in v0.2.0
RegisterCompileUint16Fn registers a compile function for types of kind Uint16.
func RegisterCompileUint32Fn ¶ added in v0.2.0
RegisterCompileUint32Fn registers a compile function for types of kind Uint32.
func RegisterCompileUint64Fn ¶ added in v0.2.0
RegisterCompileUint64Fn registers a compile function for types of kind Uint64.
func RegisterCompileUintFn ¶ added in v0.2.0
RegisterCompileUintFn registers a compile function for types of kind Uint.
func RegisterCompileUintptrFn ¶ added in v0.2.0
RegisterCompileUintptrFn registers a compile function for types of kind Uintptr.
Types ¶
type Arg ¶
type Arg[T any] struct { // contains filtered or unexported fields }
Arg represents a value of a known type.
type Array ¶
type Array[Ctx any] struct { // contains filtered or unexported fields }
Array represents an array value.
func (Array[Ctx]) Elem ¶
Elem returns an ArrayElem representing an element of the array by idx. idx must be in the range [0..Len()).
type ArrayElem ¶
type ArrayElem[Ctx any] struct { // contains filtered or unexported fields }
ArrayElem represents an element of an array.
type CompileArrayFn ¶
type CompileArrayFn[Ctx any] func(reflect.Type) WalkArrayFn[Ctx]
CompileArrayFn defines the function type that will be called to generate a WalkArrayFn when an array value is encountered while walking, if a WalkFn has not already been registered.
type CompileFn ¶
CompileFn defines the function type that will be called to generate a WalkFn when a value with In's kind is encountered while walking, if a WalkFn has not already been registered.
type CompileInterfaceFn ¶
type CompileInterfaceFn[Ctx any] func(reflect.Type) WalkInterfaceFn[Ctx]
CompileInterfaceFn defines the function type that will be called to generate a WalkInterfaceFn when an interface value is encountered while walking, if a WalkFn has not already been registered.
type CompileMapFn ¶
CompileMapFn defines the function type that will be called to generate a WalkMapFn when a map value is encountered while walking, if a WalkFn has not already been registered.
type CompilePtrFn ¶
CompilePtrFn defines the function type that will be called to generate a WalkPtrFn when a pointer value is encountered while walking, if a WalkFn has not already been registered.
type CompileSliceFn ¶
type CompileSliceFn[Ctx any] func(reflect.Type) WalkSliceFn[Ctx]
CompileSliceFn defines the function type that will be called to generate a WalkSliceFn when a slice value is encountered while walking, if a WalkFn has not already been registered.
type CompileStructFn ¶
type CompileStructFn[Ctx any] func(reflect.Type, StructFieldRegister) WalkStructFn[Ctx]
CompileStructFn defines the function type that will be called to generate a WalkStructFn when a struct value is encountered while walking, if a WalkFn has not already been registered.
type Complex128 ¶
type Complex128 = Arg[complex128]
type Interface ¶
type Interface[Ctx any] struct { // contains filtered or unexported fields }
Interface represents an interface value.
type Map ¶
type Map[Ctx any] struct { // contains filtered or unexported fields }
Map represents a Map value.
type MapEntry ¶
type MapEntry[Ctx any] struct { // contains filtered or unexported fields }
MapEntry represents a key and value in the map.
type MapIter ¶
type MapIter[Ctx any] struct { // contains filtered or unexported fields }
MapIter represents an iterator over the entries of the map.
type MapKey ¶
type MapKey[Ctx any] struct { // contains filtered or unexported fields }
MapKey represents a key in the map.
type MapValue ¶
type MapValue[Ctx any] struct { // contains filtered or unexported fields }
MapValue represents a value in the map.
type Ptr ¶
type Ptr[Ctx any] struct { // contains filtered or unexported fields }
Ptr represents a pointer value.
type Register ¶
type Register[Ctx any] struct { // contains filtered or unexported fields }
Register stores a set of WalkFns used to walk specific types, and functions to compile WalkFns for kinds of types.
type Slice ¶
type Slice[Ctx any] struct { // contains filtered or unexported fields }
Slice represents a slice value.
func (Slice[Ctx]) Elem ¶
Elem returns a SliceElem representing an element of the slice by idx. idx must be in the range [0..Len()).
type SliceElem ¶
type SliceElem[Ctx any] struct { // contains filtered or unexported fields }
SliceElem represents an element of an array.
type Struct ¶
type Struct[Ctx any] struct { // contains filtered or unexported fields }
Struct represents a struct value.
func (Struct[Ctx]) Field ¶
func (s Struct[Ctx]) Field(idx int) StructField[Ctx]
Field returns the StructField value for a registered field, by index in the order the fields were registered. idx must be in the range [0..NumFields())
type StructField ¶
type StructField[Ctx any] struct { // contains filtered or unexported fields }
StructField represents the field of a struct.
func (StructField[Ctx]) Interface ¶
func (f StructField[Ctx]) Interface() any
Interface returns the underlying value as an interface.
func (StructField[Ctx]) IsValid ¶
func (f StructField[Ctx]) IsValid() bool
IsValid returns true if the StructField is valid, otherwise false. Calling Walk on an invalid struct field panics.
IsValid returns false only if the field is defined on the parent struct by a multipart Index, one or more of the intermediate fields is a pointer, and one or more of the pointers used to look up the field on this value is nil. For example, in the following type, Example.F1.F2 will be invalid if Example.F1 is nil, but not if F2 is nil. Example.F3.F4 can never be invalid, because it is not referenced through a pointer (even though it is itself a pointer.)
type Example struct {
F1 *struct {
F2 *int
}
F3 struct {
F4 *int
}
}
func (StructField[Ctx]) Walk ¶
func (f StructField[Ctx]) Walk(ctx Ctx) error
Walk walks the StructField. The StructField must be valid.
type StructFieldRegister ¶
type StructFieldRegister struct {
// contains filtered or unexported fields
}
StructFieldRegister stores information about which fields to walk within a struct.
func (StructFieldRegister) RegisterField ¶
RegisterField registers a field to be available while walking the struct, by its field number. When walking the struct, Struct.Field(n) will return nth field registered.
func (StructFieldRegister) RegisterFieldByIndex ¶
RegisterFieldByIndex registers a field to be available while walking the struct, according to the Index field of the reflect.StructField representing the field.
type TypeFn ¶
TypeFn is a function to walk a value of a particular type.
Despite taking an argument of type (*In) it is walked as a value of type In. If a function to walk a (*In) is registered, it will not be called.
type UnsafePointer ¶
type WalkArrayFn ¶
WalkArrayFn defines the function that will be called when an array value is encountered while walking.
func ReturnErrArrayFn ¶
func ReturnErrArrayFn[Ctx any](err error) WalkArrayFn[Ctx]
ReturnErrArrayFn returns a WalkArrayFn that returns the given error.
This is intended to be used when a CompileArrayFn encounters an error.
type WalkFn ¶
WalkFn defines the function that will be called when a value of type In is encountered while walking.
type WalkInterfaceFn ¶
WalkInterfaceFn defines the function that will be called when an interface value is encountered while walking.
func ReturnErrInterfaceFn ¶
func ReturnErrInterfaceFn[Ctx any](err error) WalkInterfaceFn[Ctx]
ReturnErrInterfaceFn returns a WalkInterfaceFn that returns the given error.
This is intended to be used when a CompileInterfaceFn encounters an error.
type WalkMapFn ¶
WalkMapFn defines the function that will be called when a map value is encountered while walking.
func ReturnErrMapFn ¶
ReturnErrMapFn returns a WalkMapFn that returns the given error.
This is intended to be used when a CompileMapFn encounters an error.
type WalkPtrFn ¶
WalkPtrFn defines the function that will be called when a pointer value is encountered while walking.
func ReturnErrPtrFn ¶
ReturnErrPtrFn returns a WalkPtrFn that returns the given error.
This is intended to be used when a CompilePtrFn encounters an error.
type WalkSliceFn ¶
WalkSliceFn defines the function that will be called when a slice value is encountered while walking.
func ReturnErrSliceFn ¶
func ReturnErrSliceFn[Ctx any](err error) WalkSliceFn[Ctx]
ReturnErrSliceFn returns a WalkSliceFn that returns the given error.
This is intended to be used when a CompileSliceFn encounters an error.
type WalkStructFn ¶
WalkStructFn defines the function that will be called when a struct value is encountered while walking.
func ReturnErrStructFn ¶
func ReturnErrStructFn[Ctx any](err error) WalkStructFn[Ctx]
ReturnErrStructFn returns a WalkStructFn that returns the given error.
This is intended to be used when a CompileStructFn encounters an error.
type Walker ¶
type Walker[Ctx any] struct { // contains filtered or unexported fields }
Walker represents a collection of functions that can be used to walk a value using the Walk method.