flattenAndSum recursively walks a JSON object and sums all numeric leaf values into sums, using dot-separated keys for nested objects.
(sums map[string]int64, prefix string, m map[string]json.RawMessage)
| 1476 | // flattenAndSum recursively walks a JSON object and sums all numeric |
| 1477 | // leaf values into sums, using dot-separated keys for nested objects. |
| 1478 | func flattenAndSum(sums map[string]int64, prefix string, m map[string]json.RawMessage) { |
| 1479 | for k, raw := range m { |
| 1480 | key := k |
| 1481 | if prefix != "" { |
| 1482 | key = prefix + "." + k |
| 1483 | } |
| 1484 | |
| 1485 | // Try as a number first. |
| 1486 | var n json.Number |
| 1487 | if err := json.Unmarshal(raw, &n); err == nil { |
| 1488 | if v, err := n.Int64(); err == nil { |
| 1489 | sums[key] += v |
| 1490 | } |
| 1491 | continue |
| 1492 | } |
| 1493 | |
| 1494 | // Try as a nested object. |
| 1495 | var nested map[string]json.RawMessage |
| 1496 | if err := json.Unmarshal(raw, &nested); err == nil { |
| 1497 | flattenAndSum(sums, key, nested) |
| 1498 | } |
| 1499 | // Arrays, strings, booleans, nulls are skipped. |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | func GroupAIBudget(b database.GroupAiBudget) codersdk.GroupAIBudget { |
| 1504 | return codersdk.GroupAIBudget{ |
no test coverage detected