Skip to content

Commit

Permalink
handle Optional Null Iterator selector
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobozzo committed Sep 13, 2024
1 parent a183b62 commit 7060d4b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 13 additions & 2 deletions capability/policy/selector/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,20 @@ func resolve(sel Selector, subject ipld.Node, at []string) (ipld.Node, []ipld.No
// 2nd level: handle different node kinds (list, map, string, bytes)
switch {
case seg.Iterator():
if cur == nil {
if cur == nil || cur.Kind() == datamodel.Kind_Null {
if seg.Optional() {
cur = nil
// build empty list
nb := basicnode.Prototype.List.NewBuilder()
assembler, err := nb.BeginList(0)
if err != nil {
return nil, nil, err
}

if err = assembler.Finish(); err != nil {
return nil, nil, err
}

return nb.Build(), nil, nil
} else {
return nil, nil, newResolutionError(fmt.Sprintf("can not iterate over kind: %s", kindString(cur)), at)
}
Expand Down
2 changes: 1 addition & 1 deletion capability/policy/selector/supported_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestSupportedForms(t *testing.T) {
for _, testcase := range []Testcase{
{Name: "Identity", Selector: `.`, Input: `{"x":1}`, Output: `{"x":1}`},
{Name: "Iterator", Selector: `.[]`, Input: `[1, 2]`, Output: `[1, 2]`},
{Name: "Optional Null Iterator", Selector: `.[]?`, Input: `null`, Output: `()`},
{Name: "Optional Null Iterator", Selector: `.[]?`, Input: `null`, Output: `[]`},
{Name: "Optional Iterator", Selector: `.[][]?`, Input: `[[1], 2, [3]]`, Output: `[1, 3]`},
{Name: "Object Key", Selector: `.x`, Input: `{"x": 1 }`, Output: `1`},
{Name: "Quoted Key", Selector: `.["x"]`, Input: `{"x": 1}`, Output: `1`},
Expand Down

0 comments on commit 7060d4b

Please sign in to comment.