Skip to content

Commit

Permalink
error when not slice
Browse files Browse the repository at this point in the history
  • Loading branch information
binaek committed Nov 20, 2023
1 parent cbe797f commit b65784d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions plugin/transform/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,10 @@ func NullIfEmptySliceValue(_ context.Context, d *TransformData) (interface{}, er
return nil, nil
}
v := helpers.DereferencePointer(d.Value)
b, l := reflect.TypeOf(v).Kind() == reflect.Slice, reflect.ValueOf(v).Len()
if b && l == 0 {
if reflect.TypeOf(v).Kind() != reflect.Slice {
return nil, fmt.Errorf("NullIfEmptySliceValue transform requires the input to be a slice, got %T", v)
}
if reflect.ValueOf(v).Len() == 0 {
return nil, nil
}
return d.Value, nil
Expand Down
7 changes: 7 additions & 0 deletions plugin/transform/primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,13 @@ Statement:
function: NullIfEmptySliceValue,
expected: nil,
},
"NullIfEmptySlice struct": {
d: &TransformData{
Value: testStruct{},
},
function: NullIfEmptySliceValue,
expected: "ERROR",
},
}

func TestTransformFunctions(t *testing.T) {
Expand Down

0 comments on commit b65784d

Please sign in to comment.