Skip to content

Instantly share code, notes, and snippets.

@pivotaljohn
Last active June 30, 2022 14:40
Show Gist options
  • Save pivotaljohn/c3081b9fa97077b4c70fed593e5e16b7 to your computer and use it in GitHub Desktop.
Save pivotaljohn/c3081b9fa97077b4c70fed593e5e16b7 to your computer and use it in GitHub Desktop.
Transform flat keyset into a list
#@ load("@ytt:data", "data")
#@ load("parse-input.star", "fruit_from_struct")
---
fruits: #@ fruit_from_struct(data.values)
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
#@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