Scope Proposal #11
Closed
TotalTechGeek
started this conversation in
Ideas
Replies: 1 comment
-
|
I believe I'll be closing this proposal and introducing a new one, based on misc discussions we've had in different threads. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
One undefined behavior in JSON Logic is how you should access context from outer scopes when working with scope introduced by an iterator.
This prevents you from using
filterusing an item stored in your context, or adding the index / a value from your context within a map statement.See:
Suggestion
The
json-logic-engineimplementation resolves it in the following way:What if we use directory style traversal to allow folks to reference outer scopes?
../would go up a layer.So to reference the index of an iterator, you'd reach into the iterator's scope with
../index.And to reference the context above the iterator, to fetch a property like
nameyou'd climb up two levels,../../name.Proposed Test Suite
[ "A proposal for handling scopes", { "description": "Map can add each number to index", "rule": { "map": [ { "var": "numbers" }, { "+": [{ "var": "../index" }, { "var": "" }]} ] }, "data": { "numbers": [1,2,3] }, "result": [1,3,5] }, { "description": "Map can add each number to value from context", "rule": { "map": [ { "var": "numbers" }, { "+": [{ "var": "../../value" }, { "var": "" }]} ] }, "data": { "numbers": [1,2,3], "value": 10 }, "result": [11,12,13] }, { "description": "Filter can use parent context to filter", "rule": { "filter": [ { "var": "people" }, { "===": [{ "var": "department" }, { "var": "../../department" }] } ] }, "data": { "department": "Engineering", "people": [ { "name": "Jay Ortiz", "department": "Engineering" }, { "name": "Louisa Hall", "department": "Sales" }, { "name": "Kyle Carlson", "department": "Sales" }, { "name": "Grace Ortiz", "department": "Engineering" }, { "name": "Isabelle Harrington", "department": "Marketing" }, { "name": "Harold Moore", "department": "Sales" }, { "name": "Clarence Schultz", "department": "Sales" }, { "name": "Jesse Keller", "department": "Engineering" }, { "name": "Phillip Holland", "department": "Marketing" }, { "name": "Mason Sullivan", "department": "Engineering" } ] }, "result": [ { "name": "Jay Ortiz", "department": "Engineering" }, { "name": "Grace Ortiz", "department": "Engineering" }, { "name": "Jesse Keller", "department": "Engineering" }, { "name": "Mason Sullivan", "department": "Engineering" } ] }, "The specification below depends upon escaping from another proposal", { "description": "Access an escaped key from above", "rule": { "map": [ { "var": "arr" }, { "+": [{ "var": "../../\\.\\./" }, { "var": "../../..\\/" }]} ] }, "data": { "arr": [1,2,3], "../": 10, "": { "": { "/": 7 }} }, "result": [17,17,17] } ]Beta Was this translation helpful? Give feedback.
All reactions