diff --git a/capability/policy/selector/selector.go b/capability/policy/selector/selector.go index fe5dbd5..2fa257f 100644 --- a/capability/policy/selector/selector.go +++ b/capability/policy/selector/selector.go @@ -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) } diff --git a/capability/policy/selector/supported_test.go b/capability/policy/selector/supported_test.go index 6524950..de8f83c 100644 --- a/capability/policy/selector/supported_test.go +++ b/capability/policy/selector/supported_test.go @@ -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`},