Skip to content

Instantly share code, notes, and snippets.

@chakrit
Created May 14, 2023 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chakrit/a68bb5ea628839cfb61a19a6aae4f3c5 to your computer and use it in GitHub Desktop.
Save chakrit/a68bb5ea628839cfb61a19a6aae4f3c5 to your computer and use it in GitHub Desktop.
CUE script for breaking down a value, appending an item to the leaf element, and reconstructing back the original value.
input: {
outer: {
inner: [
{item: "one", values: [3, 2, 1]},
{item: "two", values: [1, 2, 3]},
{item: "three", values: [7, 8, 9]},
]
innernope: "innernope"
}
unrelated: "unrelated"
}
path: ["outer", "inner", 1, "values"]
append: 4
breakdown: [
for idx, p in path {
if idx == 0 {
input[p]
}
if idx > 0 {
breakdown[idx-1][p]
}
},
]
reconstruct: [
for idx, _ in path {
if (int & path[len(path)-idx-1]) == _|_ {
{
if idx < len(path)-1 {
for key, value in breakdown[len(path)-idx-2] if key != path[len(path)-idx-1] {
"\(key)": value
}
}
if idx == len(path)-1 {
for key, value in input if "\(key)" != "\(path[len(path)-idx-1])" {
"\(key)": value
}
}
if idx == 0 {
"\(path[len(path)-1])": breakdown[len(path)-1] + [append]
}
if idx > 0 {
"\(path[len(path)-idx-1])": reconstruct[idx-1]
}
}
}
if (int & path[len(path)-idx-1]) != _|_ {
[
for key, value in breakdown[len(path)-idx-2] {
if key != path[len(path)-idx-1] {
value
}
if key == path[len(path)-idx-1] {
if idx == 0 {
breakdown[len(path)-1] + [append]
}
if idx > 0 {
reconstruct[idx-1]
}
}
},
]
}
},
]
output: reconstruct[len(path)-1] & {
outer: {
inner: [
{item: "one", values: [3, 2, 1]},
{item: "two", values: [1, 2, 3, 4]},
{item: "three", values: [7, 8, 9]},
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment