Skip to content

Commit

Permalink
Lower minimum version to 1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
zolstein authored Oct 7, 2024
1 parent c13bcaa commit b272cde
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ '1.22.x', '1.23.x' ]
go-version: [ '1.21.x', '1.22.x', '1.23.x' ]

steps:
- uses: actions/checkout@v4
Expand Down
20 changes: 10 additions & 10 deletions bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"github.com/stretchr/testify/require"
tw "github.com/zolstein/type-walk"
"math/rand/v2"
"math/rand"
"reflect"
"runtime"
"strconv"
Expand All @@ -26,7 +26,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {

toSerialize := make([]*Outer, 100)
for i := range toSerialize {
inners := make([]*Inner, rand.N(10))
inners := make([]*Inner, rand.Intn(10))
for j := range inners {
inners[j] = &Inner{
D: rand.Int(),
Expand Down Expand Up @@ -62,7 +62,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
}
case reflect.Slice:
bb.WriteRune('[')
for i := range v.Len() {
for i := 0; i < v.Len(); i++ {
if i > 0 {
bb.WriteRune(',')
}
Expand All @@ -72,7 +72,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
case reflect.Struct:
bb.WriteRune('{')
t := v.Type()
for i := range t.NumField() {
for i := 0; i < t.NumField(); i++ {
if i > 0 {
bb.WriteRune(',')
}
Expand All @@ -93,7 +93,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
runtime.GC()
b.ResetTimer()
b.ReportAllocs()
for range b.N {
for i := 0; i < b.N; i++ {
bb.Reset()
serializeReflect(reflect.ValueOf(toSerialize))
bs := bb.Bytes()
Expand Down Expand Up @@ -121,7 +121,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
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() {
for i := 0; i < s.Len(); i++ {
if i > 0 {
bb.WriteRune(',')
}
Expand All @@ -142,7 +142,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
}
return func(ctx *bytes.Buffer, s tw.Struct[*bytes.Buffer]) error {
bb.WriteRune('{')
for i := range s.NumFields() {
for i := 0; i < s.NumFields(); i++ {
if i > 0 {
bb.WriteRune(',')
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {
runtime.GC()
b.ResetTimer()
b.ReportAllocs()
for range b.N {
for i := 0; i < b.N; i++ {
serializeTypeWalk(toSerializePtr)
bs := bb.Bytes()
_ = bs
Expand All @@ -193,9 +193,9 @@ func BenchmarkSimpleJsonSerialize(b *testing.B) {

func randString(length int) string {
var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bs := make([]byte, rand.N(length))
bs := make([]byte, rand.Intn(length))
for i := range bs {
bs[i] = letters[rand.N(len(letters))]
bs[i] = letters[rand.Intn(len(letters))]
}
return string(bs)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/zolstein/type-walk

go 1.22
go 1.21

require (
github.com/goccy/go-reflect v1.2.0
Expand Down
3 changes: 2 additions & 1 deletion walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ type Walker[Ctx any] struct {
// Any new functions that are added to the register after calling NewWalker will not be used by the returned Walker.
func NewWalker[Ctx any](register *Register[Ctx]) *Walker[Ctx] {
typeFns := make(map[g_reflect.Type]*walkFn[Ctx], len(register.typeFns))
for _, e := range register.typeFns {
for i := range register.typeFns {
e := &register.typeFns[i]
typeFns[e.t] = &e.fn
}
return &Walker[Ctx]{
Expand Down
12 changes: 6 additions & 6 deletions walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ func TestRegisterCompileArrayFn(t *testing.T) {
register.RegisterCompileArrayFn(func(typ reflect.Type) tw.WalkArrayFn[*strings.Builder] {
return func(ctx *strings.Builder, aw tw.Array[*strings.Builder]) error {
ctx.WriteRune('[')
for i := range aw.Len() {
for i := 0; i < aw.Len(); i++ {
if i > 0 {
ctx.WriteRune(',')
}
Expand Down Expand Up @@ -363,7 +363,7 @@ func TestRegisterCompileSliceFn(t *testing.T) {
return nil
}
ctx.WriteRune('[')
for i := range sw.Len() {
for i := 0; i < sw.Len(); i++ {
if i > 0 {
ctx.WriteRune(',')
}
Expand Down Expand Up @@ -740,7 +740,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 := range s.Len() {
for i := 0; i < s.Len(); i++ {
err := s.Walk(ctx, i)
if err != nil {
return err
Expand Down Expand Up @@ -792,7 +792,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 := range a.Len() {
for i := 0; i < a.Len(); i++ {
err := a.Walk(ctx, i)
if err != nil {
return err
Expand Down Expand Up @@ -844,11 +844,11 @@ func settableStructHelper(t *testing.T) {
return nil
})
register.RegisterCompileStructFn(func(typ reflect.Type, register tw.StructFieldRegister) tw.WalkStructFn[struct{}] {
for i := range typ.NumField() {
for i := 0; i < typ.NumField(); i++ {
register.RegisterField(i)
}
return func(ctx struct{}, s tw.Struct[struct{}]) error {
for i := range s.NumFields() {
for i := 0; i < s.NumFields(); i++ {
err := s.Walk(ctx, i)
if err != nil {
return err
Expand Down

0 comments on commit b272cde

Please sign in to comment.