Skip to content

Unexpected behavior with walk path as key in object comprehension #7656

@robmyersrobmyers

Description

@robmyersrobmyers

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"}

  • OPA version : 1.5.0

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions