Skip to content

Commit

Permalink
Merge pull request #172 from mimir-d/bugfix/handle_nil_in_paramexpander
Browse files Browse the repository at this point in the history
handle nils correctly in ParamExpander
  • Loading branch information
mimir-d authored May 9, 2024
2 parents 6ff4d0f + d494b08 commit 1b1abe5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pkg/test/param_expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ func (pe *ParamExpander) ExpandObject(obj interface{}, out interface{}) error {
vout.Set(val)

case reflect.Map:
if vin.IsNil() {
// handle nil maps, eg. map[T]U(nil)
vout.Set(vin)
break
}

val := reflect.MakeMap(vin.Type())
iter := vin.MapRange()
for iter.Next() {
Expand Down Expand Up @@ -112,6 +118,12 @@ func (pe *ParamExpander) ExpandObject(obj interface{}, out interface{}) error {
vout.Set(val.Elem())

case reflect.Slice:
if vin.IsNil() {
// handle nil slices, eg. []T(nil)
vout.Set(vin)
break
}

elemType := vin.Type().Elem()
len := vin.Len()

Expand Down
12 changes: 10 additions & 2 deletions pkg/test/param_expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,18 @@ func TestParamExpander(t *testing.T) {

t.Run("nulls", func(t *testing.T) {
type testObj struct {
Str *string
Intf any
Str *string
Intf any
Ints []int
IntMap map[int]int
}

// both fields null
input := &testObj{}
require.Nil(t, input.Str)
require.Nil(t, input.Intf)
require.Nil(t, input.Ints)
require.Nil(t, input.IntMap)

e := NewParamExpander(target, &mockVars{})

Expand All @@ -180,5 +186,7 @@ func TestParamExpander(t *testing.T) {

require.Nil(t, out.Str)
require.Nil(t, out.Intf)
require.Nil(t, out.Ints)
require.Nil(t, out.IntMap)
})
}

0 comments on commit 1b1abe5

Please sign in to comment.