ytt icon indicating copy to clipboard operation
ytt copied to clipboard

`in` operator on a `yamlfragment` wrapping an array fails to match elements

Open pivotaljohn opened this issue 4 years ago • 1 comments

What steps did you take: Attempted to use the in operator on a yamlfragment.

For example, given the following input:

---
rules:
- resources:
  - podsecuritypolicies
- resources:
  - configmaps

#@overlay/match by=lambda _,__,___: True
---
rules:
#@overlay/match by=lambda _,l,r: "podsecuritypolicies" in l["resources"]
#@overlay/replace
- resources:
  - pods
  - pods/attach

What happened: The overlay failed its expectations:

Overlaying (in following order: clusterrole.yml):
  Document on line clusterrole.yml:9:
    Map item (key 'rules') on line clusterrole.yml:10:
      Array item on line clusterrole.yml:13:
        Expected number of matched nodes to be 1, but was 0

What did you expect: I expected in to iterate over the array wrapped in the yamlfragment in l.

Anything else you would like to add: A work around is to unwrap the yamlfragment:

---
rules:
- resources:
  - podsecuritypolicies
- resources:
  - configmaps

#@ load("@ytt:yaml", "yaml")
#@overlay/match by=lambda _,__,___: True
---
rules:
#@overlay/match by=lambda _,l,r: "podsecuritypolicies" in yaml.decode(yaml.encode(l["resources"]))
#@overlay/replace
- resources:
  - pods
  - pods/attach

which results in the desired output:

rules:
- resources:
  - pods
  - pods/attach
- resources:
  - configmaps

Also, the implementation of in being used is here: https://github.com/vmware-tanzu/carvel-ytt/blob/05af143134326077e3a06ce85ae976511e8b23ae/vendor/github.com/k14s/starlark-go/starlark/eval.go#L812-L854

pivotaljohn avatar Mar 10 '21 17:03 pivotaljohn

#@ def foo():
- foo
#@ end
---
#@ def blah():
foo: blah
#@ end
---
in1: #@ "foo" in foo()
in1_idx: #@ 0 in foo()
in2: #@ "foo" in ["foo"]
in2_idx: #@ 0 in ["foo"]
in3: #@ "foo" in blah()
in3_idx: #@ 0 in blah()

results in

in1: false
in1_idx: true
in2: true
in2_idx: false
in3: true
in3_idx: false

cppforlife avatar Mar 10 '21 21:03 cppforlife