how to cumulate elements in array of objects #241
Answered
by
quanguachong
quanguachong
asked this question in
OPA and Rego
-
Input is an array of object, I want to generate an object by cumulating the array's elements. Example Input: {
"containers": [
{
"cpu": "100m",
"memory": "200Mi"
},
{
"cpu": "25m",
"memory": "400Mi"
}
]
}
expect output: {
"result": {
"cpu": 125,
"memory": 629145600000
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
quanguachong
Jul 22, 2022
Replies: 1 comment 2 replies
-
I resolve this issue with following code: result[key] := value {
keys := { key | container := input.containers[i]; _ = container[key]}
key := keys[_]
# canonify_value is the function to convert string-value to number.
values := [val | val := canonify_value(input.containers[_][key],key)]
value := sum(values)
} The core idea is convert {
"cpu": [100, 25],
"memory": [209715200000, 419430400000]
} and then use build-in function Is there any better resolution to resolve such problem with rego? |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
quanguachong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I resolve this issue with following code:
The core idea is convert
input.containers
to the following object:and then use build-in function
sum
to sum elements of array.Is there any better resolution to resolve such problem with rego?