From 7060d4bb3370816fd34e2c2201ea767a17b073e6 Mon Sep 17 00:00:00 2001 From: Fabio Bozzo Date: Fri, 13 Sep 2024 15:33:11 +0200 Subject: [PATCH] handle Optional Null Iterator selector --- capability/policy/selector/selector.go | 15 +++++++++++++-- capability/policy/selector/supported_test.go | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) 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`},