diff --git a/go.work.sum b/go.work.sum index 8abf1de..919e519 100644 --- a/go.work.sum +++ b/go.work.sum @@ -3,6 +3,7 @@ github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4 github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= diff --git a/kv.go b/kv.go index 38a769b..d14378a 100644 --- a/kv.go +++ b/kv.go @@ -14,13 +14,21 @@ func dereferencePointers(data map[string]any) map[string]any { for key, value := range data { val := reflect.ValueOf(value) if val.Kind() == reflect.Ptr { - // @TODO: might be a pointer to a pointer - data[key] = val.Elem().Interface() + data[key] = dereferencePointer(val) } } return data +} + +func dereferencePointer(val reflect.Value) any { + if val.IsNil() { + return nil + } else if val.Elem().Kind() == reflect.Ptr { + return dereferencePointer(val.Elem()) + } + return val.Elem().Interface() } func lazyMapEvaluation(data map[string]any) map[string]any { diff --git a/kv_test.go b/kv_test.go index 513665e..c884f35 100644 --- a/kv_test.go +++ b/kv_test.go @@ -6,17 +6,29 @@ import ( "github.com/stretchr/testify/assert" ) +const anErrorStr = "assert.AnError general error for testing" + func TestDereferencePointers(t *testing.T) { is := assert.New(t) ptr := func(v string) *string { return &v } - err := With("hello", "world").Errorf(assert.AnError.Error()).(OopsError) //nolint:govet + err := With("hello", "world").Errorf(anErrorStr).(OopsError) //nolint:govet is.EqualValues(map[string]any{"hello": "world"}, err.Context()) - err = With("hello", ptr("world")).Errorf(assert.AnError.Error()).(OopsError) //nolint:govet + err = With("hello", ptr("world")).Errorf(anErrorStr).(OopsError) //nolint:govet is.EqualValues(map[string]any{"hello": "world"}, err.Context()) - err = With("hello", nil).Errorf(assert.AnError.Error()).(OopsError) //nolint:govet + err = With("hello", nil).Errorf(anErrorStr).(OopsError) //nolint:govet + is.EqualValues(map[string]any{"hello": nil}, err.Context()) + + err = With("hello", (*int)(nil)).Errorf(anErrorStr).(OopsError) //nolint:govet + is.EqualValues(map[string]any{"hello": nil}, err.Context()) + + err = With("hello", (***int)(nil)).Errorf(anErrorStr).(OopsError) //nolint:govet + is.EqualValues(map[string]any{"hello": nil}, err.Context()) + + var i **int + err = With("hello", (***int)(&i)).Errorf(anErrorStr).(OopsError) //nolint:govet is.EqualValues(map[string]any{"hello": nil}, err.Context()) }