Skip to content

fix(policy-devel): inputs don't accept multiple values#2355

Merged
Piskoo merged 2 commits into
chainloop-dev:mainfrom
Piskoo:fix-multivalued-input-for-eval
Aug 13, 2025
Merged

fix(policy-devel): inputs don't accept multiple values#2355
Piskoo merged 2 commits into
chainloop-dev:mainfrom
Piskoo:fix-multivalued-input-for-eval

Conversation

@Piskoo

@Piskoo Piskoo commented Aug 13, 2025

Copy link
Copy Markdown
Contributor

This PR fixes issue with passing arrays in --input flag.

Example

By passing --input licenses=AGPL,GPL in the eval command values can now be accessed with input.args.licenses[0] and input.args.licenses[1] in the rego script.

Closes #2351

Piskoo added 2 commits August 13, 2025 08:57
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
@Piskoo Piskoo marked this pull request as ready for review August 13, 2025 07:12
@Piskoo Piskoo requested review from jiparis and migmartri August 13, 2025 07:12
cmd.Flags().StringVar(&kind, "kind", "", fmt.Sprintf("Kind of the material: %q", schemaapi.ListAvailableMaterialKind()))
cmd.Flags().StringSliceVar(&annotations, "annotation", []string{}, "Key-value pairs of material annotations (key=value)")
cmd.Flags().StringVarP(&policyPath, "policy", "p", "policy.yaml", "Path to custom policy file")
cmd.Flags().StringSliceVar(&inputs, "input", []string{}, "Key-value pairs of policy inputs (key=value)")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mind explaining why the previous one didn't work? Thanks!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue was caused by how Cobra treats commas for StringSliceVar. Here Cobra does preprocessing where values are split with , into separate strings, where in StringArrayVar strings are kept as is.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so in that case we should use String no? StringSliceVar allows us to do

--input foo=bar --input bar=baz

is what I wrote correct?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StringArrayVar also allows

--input foo=bar --input bar=baz

but also allows

--input foo=bar,baz

to be treated as multi value, but has a trade-off of not being able to declare

--input foo=bar,bar=baz

because currently that becomes

[]string{"foo=bar,bar", "baz"}

Both StringSliceVar and StringArrayVar have drawbacks that can be fixed with custom parsing logic

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, but my question is, do we need multi input or regular strings are enough? I thought that we were asking to provide comma separated values and the engine was doing smth, I guess that not, that we want to have arrays.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want multi input, the previous change related to commas was for escaping comma separated values, before there was no way to pass e.g.

with:
  foo: bar, baz

to get

"foo": "bar, baz"

now \, can be used to get that output. But in this case using StringArrayVar makes it so the input is preprocessed by Cobra before it is passed to engine

@Piskoo Piskoo merged commit 8d8fb44 into chainloop-dev:main Aug 13, 2025
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(eval): policy inputs don't accept multiple values

3 participants