Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on nil attribute #49

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
12 changes: 10 additions & 2 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 15 additions & 3 deletions kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Loading