From aad0112b749ee94828c22d41737cb4bae92e68a7 Mon Sep 17 00:00:00 2001 From: Zach Olstein Date: Wed, 27 Nov 2024 16:03:38 -0800 Subject: [PATCH] Remove old-style Walk functions --- bench_test.go | 4 ++-- walk.go | 28 ---------------------------- walk_test.go | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/bench_test.go b/bench_test.go index c28d20b..639dd23 100644 --- a/bench_test.go +++ b/bench_test.go @@ -125,7 +125,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) { if i > 0 { bb.WriteRune(',') } - err := s.Walk(ctx, i) + err := s.Elem(i).Walk(ctx) if err != nil { return err } @@ -150,7 +150,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) { bb.WriteString(fields[i].Name) bb.WriteRune('"') bb.WriteRune(':') - err := s.Walk(ctx, i) + err := s.Field(i).Walk(ctx) if err != nil { return err } diff --git a/walk.go b/walk.go index 7a84c8a..0e19df2 100644 --- a/walk.go +++ b/walk.go @@ -183,12 +183,6 @@ func (s Struct[Ctx]) Field(idx int) StructField[Ctx] { } } -// Walk walks a registered field of the struct value. -// idx must be in the range [0..NumFields()) -func (s Struct[Ctx]) Walk(ctx Ctx, idx int) error { - return s.Field(idx).Walk(ctx) -} - func (s Struct[Ctx]) Interface() any { return g_reflect.NewAt(s.meta.typ, s.arg.p).Elem().Interface() } @@ -264,12 +258,6 @@ func (a Array[Ctx]) Elem(idx int) ArrayElem[Ctx] { } } -// Walk walks an element of the array value. -// idx must be in the range [0..Len()) -func (a Array[Ctx]) Walk(ctx Ctx, idx int) error { - return a.Elem(idx).Walk(ctx) -} - func (a Array[Ctx]) Interface() any { return g_reflect.NewAt(a.meta.typ, a.arg.p).Elem().Interface() } @@ -335,12 +323,6 @@ func (s Slice[Ctx]) Elem(idx int) SliceElem[Ctx] { } } -// Walk walks an element of the slice value. -// idx must be in the range [0..Len()) -func (s Slice[Ctx]) Walk(ctx Ctx, idx int) error { - return s.Elem(idx).Walk(ctx) -} - func (a Slice[Ctx]) Interface() any { return g_reflect.NewAt(a.meta.typ, a.arg.p).Elem().Interface() } @@ -482,16 +464,6 @@ func (m MapEntry[Ctx]) Value() MapValue[Ctx] { } } -// WalkKey walks the key associated with this MapEntry. -func (m MapEntry[Ctx]) WalkKey(ctx Ctx) error { - return m.Key().Walk(ctx) -} - -// WalkValue walks the value associated with this MapEntry. -func (m MapEntry[Ctx]) WalkValue(ctx Ctx) error { - return m.Value().Walk(ctx) -} - // MapKey represents a key in the map. type MapKey[Ctx any] struct { meta *mapMetadata[Ctx] diff --git a/walk_test.go b/walk_test.go index a00553b..729761b 100644 --- a/walk_test.go +++ b/walk_test.go @@ -280,7 +280,7 @@ func TestRegisterCompileArrayFn(t *testing.T) { if i > 0 { ctx.WriteRune(',') } - err := aw.Walk(ctx, i) + err := aw.Elem(i).Walk(ctx) if err != nil { return err } @@ -327,7 +327,7 @@ func TestRegisterCompileSliceFn(t *testing.T) { if i > 0 { ctx.WriteRune(',') } - err := sw.Walk(ctx, i) + err := sw.Elem(i).Walk(ctx) if err != nil { return err } @@ -490,12 +490,12 @@ func TestRegisterCompileMapFn(t *testing.T) { ctx.WriteRune(',') } e := iter.Entry() - err := e.WalkKey(ctx) + err := e.Key().Walk(ctx) if err != nil { return err } ctx.WriteRune(':') - err = e.WalkValue(ctx) + err = e.Value().Walk(ctx) if err != nil { return err } @@ -817,7 +817,7 @@ func settableSliceHelper(t *testing.T) { register.RegisterCompileSliceFn(func(typ reflect.Type) tw.WalkSliceFn[struct{}] { return func(ctx struct{}, s tw.Slice[struct{}]) error { for i := 0; i < s.Len(); i++ { - err := s.Walk(ctx, i) + err := s.Elem(i).Walk(ctx) if err != nil { return err } @@ -869,7 +869,7 @@ func settableArrayHelper(t *testing.T) { register.RegisterCompileArrayFn(func(typ reflect.Type) tw.WalkArrayFn[struct{}] { return func(ctx struct{}, a tw.Array[struct{}]) error { for i := 0; i < a.Len(); i++ { - err := a.Walk(ctx, i) + err := a.Elem(i).Walk(ctx) if err != nil { return err } @@ -925,7 +925,7 @@ func settableStructHelper(t *testing.T) { } return func(ctx struct{}, s tw.Struct[struct{}]) error { for i := 0; i < s.NumFields(); i++ { - err := s.Walk(ctx, i) + err := s.Field(i).Walk(ctx) if err != nil { return err } @@ -1034,11 +1034,11 @@ func settableMapHelper(t *testing.T) { iter := m.Iter() for iter.Next() { entry := iter.Entry() - err := entry.WalkKey(ctx) + err := entry.Key().Walk(ctx) if err != nil { return err } - err = entry.WalkValue(ctx) + err = entry.Value().Walk(ctx) if err != nil { return err } @@ -1448,7 +1448,7 @@ func printStruct(fields []reflect.StructField) tw.WalkStructFn[*strings.Builder] ctx.WriteString(field.Name) ctx.WriteRune(':') - err := sw.Walk(ctx, i) + err := sw.Field(i).Walk(ctx) if err != nil { return err }