Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support looping over map variables #1436

Merged
merged 2 commits into from
Dec 21, 2023
Merged

Conversation

pd93
Copy link
Member

@pd93 pd93 commented Dec 21, 2023

Adds support for looping over a map-type variable using the for syntax. Since Go maps are unordered, the order of the loop will be random.

This also fixes a bug where specifying a map variable when looping over a variable would give an invalid error about using sh with the Any Variables experiment.

@pd93 pd93 mentioned this pull request Dec 21, 2023
3 tasks
@vmaerten
Copy link
Member

Is it possible to also access the key in the loop ? For exemple with {{.KEY}} or {{.ITEM.KEY}} / {{.ITEM.VALUE}} ?

Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

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

👏 👏 👏

Since Go maps are unordered, the order of the loop will be random.

Perhaps having a custom UnmarshalYAML function to make the keys ordered would be interesting for the users. Many would expect keys order to persist. We already do that for the variables map itself.

Can be done in another PR, though.

@pd93 pd93 force-pushed the support-looping-over-map-variables branch from 9b9cc6d to f6b9fb0 Compare December 21, 2023 15:37
@pd93
Copy link
Member Author

pd93 commented Dec 21, 2023

Is it possible to also access the key in the loop ? For exemple with {{.KEY}} or {{.ITEM.KEY}} / {{.ITEM.VALUE}} ?

@vmaerten I think this is a good idea. I've added the value to a {{.KEY}} variable for now and we'll see what people think.

Since Go maps are unordered, the order of the loop will be random.

Perhaps having a custom UnmarshalYAML function to make the keys ordered would be interesting for the users. Many would expect keys order to persist. We already do that for the variables map itself.

Can be done in another PR, though.

@andreynering This might be a bit of a challenge (though I'm sure it's doable). The problem with custom unmarshaling is that we'd have to store the map in an OrderedMap struct. This is going to create issues when trying to access the map via templating as regular templating function and dot syntax is not going to work. We'd need to find a way to store the raw map so that its available for templating, but also store the order so that it can be iterated over correctly when looping. Might require a bit of thought into the best implementation of this.

@pd93 pd93 merged commit b592648 into main Dec 21, 2023
11 checks passed
@pd93 pd93 deleted the support-looping-over-map-variables branch December 21, 2023 15:43
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.

3 participants