MCPcopy Index your code
hub / github.com/coder/coder / flattenAndSum

Function flattenAndSum

coderd/database/db2sdk/db2sdk.go:1478–1501  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1476// flattenAndSum recursively walks a JSON object and sums all numeric
1477// leaf values into sums, using dot-separated keys for nested objects.
1478func 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
1503func GroupAIBudget(b database.GroupAiBudget) codersdk.GroupAIBudget {
1504 return codersdk.GroupAIBudget{

Callers 1

aggregateTokenMetadataFunction · 0.85

Calls 2

Int64Method · 0.80
UnmarshalMethod · 0.45

Tested by

no test coverage detected