Skip to content

Commit

Permalink
Adds logic to handle empty star array expansion
Browse files Browse the repository at this point in the history
When a star array expression is stringified we need to handle the case where the array is zero-length by returning the empty string.

This commit adds check and tests for this case.
  • Loading branch information
skaegi authored and tekton-robot committed Mar 9, 2020
1 parent 6d9c2eb commit e1d636a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pkg/jsonpath/expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ func expandStringAsList(input string, context interface{}) ([]interface{}, error
}
return match
}

// if the array or object star expansion is empty, the replacement is the empty string
if len(expanded) == 0 {
return ""
}

// unless expanded in the context of an array we only want the first list item
// at some point we might allow {range} to process JSONPath lists
result := expanded[0]
Expand Down Expand Up @@ -137,6 +143,10 @@ func expandString(input string, context interface{}) (interface{}, error) {
if err != nil {
return nil, err
}
// if the array or object star expansion is empty, return an empty string
if len(expanded) == 0 {
return "", nil
}
return expanded[0], nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/jsonpath/expand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ func TestExpand(t *testing.T) {
"a": float64(1),
"b": "abcd",
},
"null": nil,
"null": nil,
"empty_array": []interface{}{},
}

tests := []struct {
Expand Down Expand Up @@ -157,6 +158,7 @@ func TestExpand(t *testing.T) {
{"$('')$(null)", "null"},
{"$('')$(obj)", `{"a":1,"b":"abcd"}`},
{"$('')$(array)", `[1,2,3,"a","b","c"]`},
{"$(empty_array[*])", ""},
}

for _, test := range tests {
Expand Down

0 comments on commit e1d636a

Please sign in to comment.