Files
releases/test/cases/testdata/objectget/test-objectget-path.yaml
T
Charlie Egan 301efc6997 Alter object.get to support nested key array
This commit extends the go and wasm implementations of object.get to
allow a key to also be an array.

When passed an array, each element in the array will be used as a key in
turn. This allows values at deeply nested paths to be extracted from
objects.

It also supports getting indexes of nested arrays.

The functionality was originally inspired by Ruby's Hash.dig function:
https://ruby-doc.org/core-2.3.0_preview1/Hash.html#method-i-dig however
we opted to include the behavior in object.get instead after being
uncertain 'dig' was a commonly understood name.

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
2022-01-27 22:01:01 +01:00

125 lines
2.5 KiB
YAML

cases:
- note: objectget/empty_path_returns_object
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({"a": 1}, [], 2)
}
want_result: [{x: {a: 1}}]
- note: objectget/path_with_single_element
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": 1 }, ["a"], 2)
}
want_result: [{x: 1}]
- note: objectget/path_with_two_elements
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": { "b": 1 } }, ["a", "b"], 2)
}
want_result: [{x: 1}]
- note: objectget/path_with_three_elements
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": { "b": { "c": 1 } } }, ["a", "b", "c"], 2)
}
want_result: [{x: 1}]
- note: objectget/path_with_single_element_no_result
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": 1 }, ["b"], 2)
}
want_result: [{x: 2}]
- note: objectget/path_with_two_elements_no_result
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": { "b": 1 } }, ["b", "a"], 2)
}
want_result: [{x: 2}]
- note: objectget/path_with_three_elements_no_result
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": { "b": { "c": 1 } } }, ["a", "b", "a"], 2)
}
want_result: [{x: 2}]
- note: objectget/path_with_non_string_keys
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ 1: { "b": { [1,2,3]: 1 } } }, [1, "b", [1,2,3]], 2)
}
want_result: [{x: 1}]
- note: objectget/get_intermediate_non_object
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": { "b": [1,2,3] } }, ["a", "b", "a"], 2)
}
want_result: [{x: 2}]
- note: objectget/get_intermediate_array
query: data.test.p = x
modules:
- |
package test
p = x {
x := object.get({ "a": { "b": [{"c": 1}] } }, ["a", "b", 0, "c"], 2)
}
want_result: [{x: 1}]
- note: objectget/get_for_non_object
query: data.test.p = x
input_term: '{"obj":"object"}'
modules:
- |
package test
p = x {
x := object.get(input.obj, ["a"], 2)
}
want_error: 'object.get: operand 1 must be object but got string'
want_error_code: eval_type_error
strict_error: true