Skip to content

Commit 78a3912

Browse files
committed
fix: fix panic on nil attribute
1 parent 54078d1 commit 78a3912

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

go.work.sum

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4
33
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
44
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
55
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
6+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
67
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
78
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
89
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=

kv.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ func dereferencePointers(data map[string]any) map[string]any {
1414
for key, value := range data {
1515
val := reflect.ValueOf(value)
1616
if val.Kind() == reflect.Ptr {
17-
// @TODO: might be a pointer to a pointer
18-
data[key] = val.Elem().Interface()
17+
data[key] = dereferencePointer(val)
1918
}
2019
}
2120

2221
return data
22+
}
23+
24+
func dereferencePointer(val reflect.Value) any {
25+
if val.IsNil() {
26+
return nil
27+
} else if val.Elem().Kind() == reflect.Ptr {
28+
return dereferencePointer(val.Elem())
29+
}
2330

31+
return val.Elem().Interface()
2432
}
2533

2634
func lazyMapEvaluation(data map[string]any) map[string]any {

kv_test.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,29 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9+
const anErrorStr = "assert.AnError general error for testing"
10+
911
func TestDereferencePointers(t *testing.T) {
1012
is := assert.New(t)
1113

1214
ptr := func(v string) *string { return &v }
1315

14-
err := With("hello", "world").Errorf(assert.AnError.Error()).(OopsError) //nolint:govet
16+
err := With("hello", "world").Errorf(anErrorStr).(OopsError) //nolint:govet
1517
is.EqualValues(map[string]any{"hello": "world"}, err.Context())
1618

17-
err = With("hello", ptr("world")).Errorf(assert.AnError.Error()).(OopsError) //nolint:govet
19+
err = With("hello", ptr("world")).Errorf(anErrorStr).(OopsError) //nolint:govet
1820
is.EqualValues(map[string]any{"hello": "world"}, err.Context())
1921

20-
err = With("hello", nil).Errorf(assert.AnError.Error()).(OopsError) //nolint:govet
22+
err = With("hello", nil).Errorf(anErrorStr).(OopsError) //nolint:govet
23+
is.EqualValues(map[string]any{"hello": nil}, err.Context())
24+
25+
err = With("hello", (*int)(nil)).Errorf(anErrorStr).(OopsError) //nolint:govet
26+
is.EqualValues(map[string]any{"hello": nil}, err.Context())
27+
28+
err = With("hello", (***int)(nil)).Errorf(anErrorStr).(OopsError) //nolint:govet
29+
is.EqualValues(map[string]any{"hello": nil}, err.Context())
30+
31+
var i **int
32+
err = With("hello", (***int)(&i)).Errorf(anErrorStr).(OopsError) //nolint:govet
2133
is.EqualValues(map[string]any{"hello": nil}, err.Context())
2234
}

0 commit comments

Comments
 (0)