carvel-dev / carvel-dev/ytt

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

Open
#317 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Go
Stars
1.9k
Forks
167
PR merge metrics
No merged PRs in 30d

Description

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

For example, given the following input:

```yaml
---
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`:

```yaml
---
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:

```yaml
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

Contributor guide

Open the contributing guide

Research direction

Start with the linked vendor/github.com/k14s/starlark-go/starlark/eval.go implementation of the `in` operator, then reproduce the overlay using a yamlfragment wrapping an array. Trace how membership is evaluated for that wrapper and add regression coverage in the existing relevant tests; done means matching the element and producing the expected overlay output.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.