Last active
June 30, 2022 14:40
-
-
Save pivotaljohn/c3081b9fa97077b4c70fed593e5e16b7 to your computer and use it in GitHub Desktop.
Transform flat keyset into a list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@ load("@ytt:data", "data") | |
#@ load("parse-input.star", "fruit_from_struct") | |
--- | |
fruits: #@ fruit_from_struct(data.values) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load("@ytt:regexp", "regexp") | |
attrs = { | |
"fruit": "name", | |
"count": "number" | |
} | |
def fruit_from_struct(values): | |
fruits = [] | |
for a in dir(values): | |
idx = int(find("[^0-9]+([0-9]+)", a)) | |
attr = find("([^0-9]+)[0-9]+", a) | |
attr = attrs[attr] if attr in attrs else attr | |
for i in range(idx-len(fruits)+1): | |
fruits.append({}) | |
end | |
fruits[idx].update({attr: values[a]}) | |
end | |
return fruits | |
end | |
def find(regex, str): | |
return regexp.replace(regex, str, "$1") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@data/values | |
--- | |
fruit0: bannana | |
count0: 2 | |
fruit1: strawberry | |
count1: 400 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment