Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/pkg/river/encoding/map.go
4095 views
1
package encoding
2
3
import (
4
"sort"
5
6
"github.com/grafana/agent/pkg/river/internal/value"
7
)
8
9
// mapField represents a value in river.
10
type mapField struct {
11
Type string `json:"type,omitempty"`
12
Value []*keyField `json:"value,omitempty"`
13
}
14
15
func newRiverMap(val value.Value) (*mapField, error) {
16
mf := &mapField{}
17
return mf, mf.convertMap(val)
18
}
19
20
func (mf *mapField) hasValue() bool {
21
if mf == nil {
22
return false
23
}
24
return len(mf.Value) > 0
25
}
26
27
func (mf *mapField) convertMap(val value.Value) error {
28
mf.Type = objectType
29
fields := make([]*keyField, 0)
30
31
keys := val.Keys()
32
33
// If v isn't an ordered object (i.e., a Go map), sort the keys so they have
34
// a deterministic print order.
35
if !val.OrderedKeys() {
36
sort.Strings(keys)
37
}
38
39
for _, key := range keys {
40
kf := &keyField{}
41
42
kf.Key = key
43
mapVal, found := val.Key(key)
44
if !found {
45
continue
46
}
47
rv, err := convertRiverValue(mapVal)
48
if err != nil {
49
return err
50
}
51
if rv.hasValue() {
52
kf.Value = rv
53
} else {
54
kf.Value = &valueField{Type: "null"}
55
}
56
fields = append(fields, kf)
57
}
58
mf.Value = fields
59
return nil
60
}
61
62