Skip to content

Commit

Permalink
Remove errors from compile function signatures
Browse files Browse the repository at this point in the history
  • Loading branch information
zolstein committed Sep 28, 2024
1 parent 421d8ec commit 7213531
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 88 deletions.
12 changes: 6 additions & 6 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
return nil
}
})
register.RegisterCompileSliceFn(func(r reflect.Type) (tw.SliceWalkFn[*bytes.Buffer], error) {
register.RegisterCompileSliceFn(func(r reflect.Type) tw.WalkSliceFn[*bytes.Buffer] {
return func(ctx *bytes.Buffer, s tw.Slice[*bytes.Buffer]) error {
bb.WriteRune('[')
for i := range s.Len() {
Expand All @@ -132,9 +132,9 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
}
bb.WriteRune(']')
return nil
}, nil
}
})
register.RegisterCompileStructFn(func(r reflect.Type, sfr tw.StructFieldRegister) (tw.StructWalkFn[*bytes.Buffer], error) {
register.RegisterCompileStructFn(func(r reflect.Type, sfr tw.StructFieldRegister) tw.WalkStructFn[*bytes.Buffer] {
fields := make([]reflect.StructField, r.NumField())
for i := range fields {
idx := sfr.RegisterField(i)
Expand All @@ -157,15 +157,15 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
}
bb.WriteRune('}')
return nil
}, nil
}
})
register.RegisterCompilePtrFn(func(r reflect.Type) (tw.PtrWalkFn[*bytes.Buffer], error) {
register.RegisterCompilePtrFn(func(r reflect.Type) tw.WalkPtrFn[*bytes.Buffer] {
return func(ctx *bytes.Buffer, p tw.Ptr[*bytes.Buffer]) error {
if p.IsNil() {
bb.WriteString("null")
}
return p.Walk(ctx)
}, nil
}
})

walker := tw.NewWalker(register)
Expand Down
56 changes: 46 additions & 10 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,36 +166,36 @@ func (r *Register[Ctx]) RegisterCompileUnsafePointerFn(fn CompileFn[Ctx, unsafe.
r.compileFns[reflect.UnsafePointer] = eraseTypedCompileFn(fn)
}

type CompileStructFn[Ctx any] func(reflect.Type, StructFieldRegister) (StructWalkFn[Ctx], error)
type StructWalkFn[Ctx any] func(Ctx, Struct[Ctx]) error
type CompileStructFn[Ctx any] func(reflect.Type, StructFieldRegister) WalkStructFn[Ctx]
type WalkStructFn[Ctx any] func(Ctx, Struct[Ctx]) error

func (r *Register[Ctx]) RegisterCompileStructFn(fn CompileStructFn[Ctx]) {
r.compileFns[reflect.Struct] = eraseCompileStructFn(fn)
}

type CompileArrayFn[Ctx any] func(reflect.Type) (ArrayWalkFn[Ctx], error)
type ArrayWalkFn[Ctx any] func(Ctx, Array[Ctx]) error
type CompileArrayFn[Ctx any] func(reflect.Type) WalkArrayFn[Ctx]
type WalkArrayFn[Ctx any] func(Ctx, Array[Ctx]) error

func (r *Register[Ctx]) RegisterCompileArrayFn(fn CompileArrayFn[Ctx]) {
r.compileFns[reflect.Array] = eraseCompileArrayFn(fn)
}

type CompilePtrFn[Ctx any] func(reflect.Type) (PtrWalkFn[Ctx], error)
type PtrWalkFn[Ctx any] func(Ctx, Ptr[Ctx]) error
type CompilePtrFn[Ctx any] func(reflect.Type) WalkPtrFn[Ctx]
type WalkPtrFn[Ctx any] func(Ctx, Ptr[Ctx]) error

func (r *Register[Ctx]) RegisterCompilePtrFn(fn CompilePtrFn[Ctx]) {
r.compileFns[reflect.Ptr] = eraseCompilePtrFn(fn)
}

type CompileSliceFn[Ctx any] func(reflect.Type) (SliceWalkFn[Ctx], error)
type SliceWalkFn[Ctx any] func(Ctx, Slice[Ctx]) error
type CompileSliceFn[Ctx any] func(reflect.Type) WalkSliceFn[Ctx]
type WalkSliceFn[Ctx any] func(Ctx, Slice[Ctx]) error

func (r *Register[Ctx]) RegisterCompileSliceFn(fn CompileSliceFn[Ctx]) {
r.compileFns[reflect.Slice] = eraseCompileSliceFn(fn)
}

type CompileMapFn[Ctx any] func(reflect.Type) (MapWalkFn[Ctx], error)
type MapWalkFn[Ctx any] func(Ctx, Map[Ctx]) error
type CompileMapFn[Ctx any] func(reflect.Type) WalkMapFn[Ctx]
type WalkMapFn[Ctx any] func(Ctx, Map[Ctx]) error

func (r *Register[Ctx]) RegisterCompileMapFn(fn CompileMapFn[Ctx]) {
r.compileFns[reflect.Map] = eraseCompileMapFn(fn)
Expand Down Expand Up @@ -238,3 +238,39 @@ func castTo[Out any](p unsafe.Pointer) Out {
func reflectType[T any]() g_reflect.Type {
return g_reflect.TypeOf((*T)(nil)).Elem()
}

func ReturnErrFn[Ctx any, In any](err error) WalkFn[Ctx, In] {
return func(Ctx, Arg[In]) error {
return err
}
}

func ReturnErrArrayFn[Ctx any](err error) WalkArrayFn[Ctx] {
return func(Ctx, Array[Ctx]) error {
return err
}
}

func ReturnErrSliceFn[Ctx any](err error) WalkSliceFn[Ctx] {
return func(Ctx, Slice[Ctx]) error {
return err
}
}

func ReturnErrStructFn[Ctx any](err error) WalkStructFn[Ctx] {
return func(Ctx, Struct[Ctx]) error {
return err
}
}

func ReturnErrPtrFn[Ctx any](err error) WalkPtrFn[Ctx] {
return func(Ctx, Ptr[Ctx]) error {
return err
}
}

func ReturnErrMapFn[Ctx any](err error) WalkMapFn[Ctx] {
return func(Ctx, Map[Ctx]) error {
return err
}
}
25 changes: 5 additions & 20 deletions walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (w *Walker[Ctx]) compileFn(t g_reflect.Type) (walkFn[Ctx], error) {
}

func (w *Walker[Ctx]) compileArray(t g_reflect.Type, fn CompileArrayFn[Ctx]) (walkFn[Ctx], error) {
arrayWalkFn := fn(g_reflect.ToReflectType(t))
elemFn, err := w.getFn(t.Elem())
if err != nil {
return nil, err
Expand All @@ -137,17 +138,14 @@ func (w *Walker[Ctx]) compileArray(t g_reflect.Type, fn CompileArrayFn[Ctx]) (wa
length: t.Len(),
elemFn: elemFn,
}
arrayWalkFn, err := fn(g_reflect.ToReflectType(t))
if err != nil {
return nil, err
}
return func(ctx Ctx, arg arg) error {
structWalker := Array[Ctx]{meta: &arrayMeta, arg: arg}
return arrayWalkFn(ctx, structWalker)
}, nil
}

func (w *Walker[Ctx]) compilePtr(t g_reflect.Type, fn CompilePtrFn[Ctx]) (walkFn[Ctx], error) {
ptrWalkFn := fn(g_reflect.ToReflectType(t))
elemFn, err := w.getFn(t.Elem())
if err != nil {
return nil, err
Expand All @@ -156,17 +154,14 @@ func (w *Walker[Ctx]) compilePtr(t g_reflect.Type, fn CompilePtrFn[Ctx]) (walkFn
typ: t,
elemFn: elemFn,
}
ptrWalkFn, err := fn(g_reflect.ToReflectType(t))
if err != nil {
return nil, err
}
return func(ctx Ctx, arg arg) error {
structWalker := Ptr[Ctx]{meta: &ptrMeta, arg: arg}
return ptrWalkFn(ctx, structWalker)
}, nil
}

func (w *Walker[Ctx]) compileSlice(t g_reflect.Type, fn CompileSliceFn[Ctx]) (walkFn[Ctx], error) {
sliceWalkFn := fn(g_reflect.ToReflectType(t))
elemFn, err := w.getFn(t.Elem())
if err != nil {
return nil, err
Expand All @@ -176,10 +171,6 @@ func (w *Walker[Ctx]) compileSlice(t g_reflect.Type, fn CompileSliceFn[Ctx]) (wa
elemSize: t.Elem().Size(),
elemFn: elemFn,
}
sliceWalkFn, err := fn(g_reflect.ToReflectType(t))
if err != nil {
return nil, err
}
return func(ctx Ctx, arg arg) error {
structWalker := Slice[Ctx]{meta: &sliceMeta, arg: arg}
return sliceWalkFn(ctx, structWalker)
Expand All @@ -190,10 +181,7 @@ func (w *Walker[Ctx]) compileStruct(t g_reflect.Type, fn CompileStructFn[Ctx]) (
reg := structFieldRegister{
typ: t,
}
structWalkFn, err := fn(g_reflect.ToReflectType(t), StructFieldRegister{&reg})
if err != nil {
return nil, err
}
structWalkFn := fn(g_reflect.ToReflectType(t), StructFieldRegister{&reg})
meta := &structMetadata[Ctx]{
typ: t,
fieldInfo: make([]structFieldMetadata[Ctx], len(reg.indexes)),
Expand Down Expand Up @@ -248,6 +236,7 @@ func lookupFieldFn(offsets []uintptr) lookupFn {
}

func (w *Walker[Ctx]) compileMap(t g_reflect.Type, fn CompileMapFn[Ctx]) (walkFn[Ctx], error) {
mapWalkFn := fn(g_reflect.ToReflectType(t))
keyFn, err := w.getFn(t.Key())
if err != nil {
return nil, err
Expand All @@ -261,10 +250,6 @@ func (w *Walker[Ctx]) compileMap(t g_reflect.Type, fn CompileMapFn[Ctx]) (walkFn
keyFn: keyFn,
valFn: valFn,
}
mapWalkFn, err := fn(g_reflect.ToReflectType(t))
if err != nil {
return nil, err
}
return func(ctx Ctx, arg arg) error {
mapWalker := Map[Ctx]{meta: &mapMeta, arg: arg}
return mapWalkFn(ctx, mapWalker)
Expand Down
Loading

0 comments on commit 7213531

Please sign in to comment.