Skip to content

Commit

Permalink
fix: fix object function args is nil (#2586)
Browse files Browse the repository at this point in the history
Signed-off-by: yisaer <disxiaofei@163.com>
  • Loading branch information
Yisaer authored Jan 26, 2024
1 parent a687968 commit e320e75
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/binder/function/funcs_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ func registerObjectFunc() {
}
m := make(map[string]interface{}, len(lists))
for _, item := range lists {
if item == nil {
continue
}
a, ok := item.([]interface{})
if !ok {
return fmt.Errorf("each argument should be [][2]interface{}"), false
Expand Down Expand Up @@ -164,7 +167,7 @@ func registerObjectFunc() {
exec: func(ctx api.FunctionContext, args []interface{}) (interface{}, bool) {
result := make(map[string]interface{})
for i := 0; i < len(args); i += 2 {
if args[i+1] != nil {
if args[i] != nil && args[i+1] != nil {
s, err := cast.ToString(args[i], cast.CONVERT_SAMEKIND)
if err != nil {
return fmt.Errorf("key %v is not a string", args[i]), false
Expand Down
87 changes: 87 additions & 0 deletions internal/binder/function/funcs_obj_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,90 @@ func TestObjectFunctionsNil(t *testing.T) {
}
}
}

func TestObjectFuncArgNil(t *testing.T) {
registerObjectFunc()
tests := []struct {
funcName string
args []interface{}
result interface{}
}{
{
funcName: "object_pick",
args: []interface{}{
map[string]interface{}{"k1": nil, "k2": "2"},
"k1",
},
result: map[string]interface{}{
"k1": nil,
},
},
{
funcName: "erase",
args: []interface{}{
map[string]interface{}{"k1": nil, "k2": "2"},
"k1",
},
result: map[string]interface{}{
"k2": "2",
},
},
{
funcName: "object_construct",
args: []interface{}{
nil, "v1", "k2", "v2",
},
result: map[string]interface{}{
"k2": "v2",
},
},
{
funcName: "object_concat",
args: []interface{}{
map[string]interface{}{"k1": "v1"},
nil,
map[string]interface{}{"k2": "v2"},
},
result: map[string]interface{}{
"k1": "v1",
"k2": "v2",
},
},
{
funcName: "items",
args: []interface{}{
map[string]interface{}{"k2": nil},
},
result: []interface{}{[]interface{}{"k2", nil}},
},
{
funcName: "zip",
args: []interface{}{
[]interface{}{[]interface{}{"k1", "v1"}, nil, []interface{}{"k2", "v2"}},
},
result: map[string]interface{}{"k1": "v1", "k2": "v2"},
},
{
funcName: "object",
args: []interface{}{
[]interface{}{"k1"},
[]interface{}{nil},
},
result: map[string]interface{}{"k1": nil},
},
{
funcName: "values",
args: []interface{}{
map[string]interface{}{"k": nil},
},
result: []interface{}{nil},
},
}
for _, tt := range tests {
f, ok := builtins[tt.funcName]
require.True(t, ok)
r, ok := f.exec(nil, tt.args)
require.True(t, ok)
require.Equal(t, tt.result, r, tt.funcName)
}
}

0 comments on commit e320e75

Please sign in to comment.