Description
I'm seeing some unexpected behavior when usingpath from walk as an object key inside a comprehension.
It's a bit strange to use an array as a key, so I'm not totally sure if this is a bug. However, it seems to work reliably when not using walk in the object comprehension.
I verified you can construct an object with arrays as keys:
a := ["a", 0, "b", 0, "c", 0, "aa"]
b := ["a", 0, "b", 0, "c", 1, "bb"]
c := ["a", 0, "b", 0, "c", 2, "cc"]
obj := {
a: "AA",
b: "BB",
c: "CC",
}
print(obj)
results in {["a", 0, "b", 0, "c", 0, "aa"]: "AA", ["a", 0, "b", 0, "c", 1, "bb"]: "BB", ["a", 0, "b", 0, "c", 2, "cc"]: "CC"}
But I ran into issues using the path array from the walk() builtin to do something similar.
x := {"a": [{"b": [{"c": [
{"aa": "AA"},
{"bb": "BB"},
{"cc": "CC"},
{"dd": "DDD"},
]}]}]}
result := {path: value |
[path, value] := walk(x)
count(value) == 2
}
print(result)
results in {["a", 0, "b", 0, "c", 3, "dd"]: "AA", ["a", 0, "b", 0, "c", 3, "dd"]: "BB", ["a", 0, "b", 0, "c", 3, "dd"]: "CC"}, which is almost correct, but the key is repeated thrice instead of corresponding to the value's path.
I ran a similar test manually creating the object with a comprehension:
a := [["a", 0, "b", 0, "c", 0, "aa"], "AA"]
b := [["a", 0, "b", 0, "c", 1, "bb"], "BB"]
c := [["a", 0, "b", 0, "c", 2, "cc"], "CC"]
d := [a, b, c]
obj := {item[0]: item[1] |
some item in d
}
print(obj)
which seems to work and resulted in {["a", 0, "b", 0, "c", 0, "aa"]: "AA", ["a", 0, "b", 0, "c", 1, "bb"]: "BB", ["a", 0, "b", 0, "c", 2, "cc"]: "CC"}
Expected behavior
I expected the object comprehension using the walk builtin to work the same way it did when created manually.
Additional context
If this is not a bug, then some additional documentation and additional examples might help others avoid this pitfall.
Description
I'm seeing some unexpected behavior when using
pathfrom walk as an object key inside a comprehension.It's a bit strange to use an array as a key, so I'm not totally sure if this is a bug. However, it seems to work reliably when not using walk in the object comprehension.
I verified you can construct an object with arrays as keys:
results in
{["a", 0, "b", 0, "c", 0, "aa"]: "AA", ["a", 0, "b", 0, "c", 1, "bb"]: "BB", ["a", 0, "b", 0, "c", 2, "cc"]: "CC"}But I ran into issues using the
patharray from thewalk()builtin to do something similar.results in
{["a", 0, "b", 0, "c", 3, "dd"]: "AA", ["a", 0, "b", 0, "c", 3, "dd"]: "BB", ["a", 0, "b", 0, "c", 3, "dd"]: "CC"}, which is almost correct, but the key is repeated thrice instead of corresponding to the value's path.I ran a similar test manually creating the object with a comprehension:
which seems to work and resulted in
{["a", 0, "b", 0, "c", 0, "aa"]: "AA", ["a", 0, "b", 0, "c", 1, "bb"]: "BB", ["a", 0, "b", 0, "c", 2, "cc"]: "CC"}Expected behavior
I expected the object comprehension using the walk builtin to work the same way it did when created manually.
Additional context
If this is not a bug, then some additional documentation and additional examples might help others avoid this pitfall.