Fix singleton unwrapping for $each, $spread, and $match#3
Merged
NirBarak-RecoLabs merged 1 commit intomainfrom Mar 24, 2026
Merged
Conversation
7fe47d1 to
dc20de3
Compare
MickeyShnaiderman-RecoLabs
previously approved these changes
Mar 24, 2026
dc20de3 to
c8f8286
Compare
These three functions returned []any instead of *Sequence,
preventing CollapseSequence from unwrapping single-element
results. This caused parity divergence from jsonata-js:
- $each({"a":1}, fn) returned ["result"] instead of "result"
- $spread({"a":1}) returned [{"a":1}] instead of {"a":1}
- $match("hello", /^h/) returned [{...}] instead of {...}
Return *Sequence from all three so evalFunction properly
singleton-unwraps single-element results. Also introduces
CollapseAndKeep helper in value.go (with shallow struct copy
for alias safety) to centralize KeepArray/singleton logic,
and collapses intermediate results in function composition
to prevent raw *Sequence leaking between composed functions.
Signed-off-by: NirBarak-RecoLabs <nirb@recolabs.ai>
c8f8286 to
d3ed71a
Compare
MickeyShnaiderman-RecoLabs
approved these changes
Mar 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
[]anyinstead of*Sequence, which preventedCollapseSequencefrom performing singleton unwrapping. Per the JSONata spec, when a function returns a single-element sequence the result must be unwrapped to its scalar value.KeepArrayoperator ([]suffix) now correctly setsKeepSingletonon theSequencebeforeCollapseSequenceruns, ensuring single-element arrays are preserved when the operator is applied.What changed
functions/object_funcs.go$eachand$spreadreturn*Sequenceinstead of[]anyfunctions/string_match_replace.go$matchreturns*Sequenceinstead of[]anyinternal/evaluator/eval_function.goKeepSingletonwhenKeepArrayis true, before callingCollapseSequenceevaluator_test.goTestEachSingletonUnwrapcovering scalar, array, nested, and error casestestdata/groups/function-each/testdata/groups/function-spread/testdata/groups/function-match/Examples
Test plan