From 44fd0108e31862e720fafe27356dec946d89b8f3 Mon Sep 17 00:00:00 2001 From: Jarrah-libremfg <122067969+Jarrah-libremfg@users.noreply.github.com> Date: Tue, 30 May 2023 08:46:55 +0000 Subject: [PATCH] Allow empty arrays to be returned (#18) In Jsonata it is a valid response return `[]any{}` from a transform, but this was previously filtered out. For example, in jsonata-js, the following input and transform result in a response of `[]`. Input: ``` { "Account": { "Account Name": "Firefly", "Order": [ ] } } ``` Transform: "Account.Order" The same data and transform resulted in a "no results found" error in jsonata-go. --- eval.go | 3 --- jsonata_test.go | 12 ++++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/eval.go b/eval.go index d3eb7dc..6a88072 100644 --- a/eval.go +++ b/eval.go @@ -205,9 +205,6 @@ func evalPath(node *jparse.PathNode, data reflect.Value, env *environment) (refl return undefined, err } - if jtypes.IsArray(output) && jtypes.Resolve(output).Len() == 0 { - return undefined, nil - } } if node.KeepArrays { diff --git a/jsonata_test.go b/jsonata_test.go index a6ffbc4..5be6eb5 100644 --- a/jsonata_test.go +++ b/jsonata_test.go @@ -621,6 +621,18 @@ func TestArraySelectors4(t *testing.T) { } +func TestEmptyArray(t *testing.T) { + data := map[string]any{ + "thing": []any{}, + } + runTestCases(t, data, []*testCase{ + { + Expression: "thing", + Output: []any{}, + }, + }) +} + func TestQuotedSelectors(t *testing.T) { runTestCases(t, testdata.foobar, []*testCase{