Debian Codegraph @

config/configopaque/maplist.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package configopaque // import "go.opentelemetry.io/collector/config/configopaque"
5
6 import (
7 "cmp"
8 "fmt"
9 "iter"
10 "slices"
11
12 "go.opentelemetry.io/collector/confmap"
13 "go.opentelemetry.io/collector/confmap/xconfmap"
14 )
15
16 // Pair is an element of a MapList, and consists of a name and an opaque value.
17 type Pair struct {
18 Name string `mapstructure:"name"`
19 Value String `mapstructure:"value"`
20
21 // prevent unkeyed literal initialization
22 _ struct{}
23 }
24
25 // MapList is a replacement for map[string]configopaque.String with a similar API,
26 // which can also be unmarshalled from (and is stored as) a list of name/value pairs.
27 //
28 // Pairs are assumed to have distinct names. This is checked during config validation.
29 type MapList []Pair
30
31 var _ confmap.Unmarshaler = (*MapList)(nil)
32
33 // Unmarshal is called by the Collector when unmarshalling from a map.
34 // When the input config is a slice, this will be skipped,
35 // and mapstructure's default unmarshalling logic will be used.
36 func (ml *MapList) Unmarshal(conf *confmap.Conf) error {
37 var m2 map[string]String
38 if err := conf.Unmarshal(&m2); err != nil {
39 return err
40 }
41 *ml = make(MapList, 0, len(m2))
42 for name, value := range m2 {
43 *ml = append(*ml, Pair{
44 Name: name,
45 Value: value,
46 })
47 }
48 slices.SortFunc(*ml, func(p1, p2 Pair) int {
49 return cmp.Compare(p1.Name, p2.Name)
50 })
51 return nil
52 }
53
54 var _ xconfmap.Validator = MapList(nil)
55
56 func (ml MapList) Validate() error {
57 // Check for duplicate keys
58 counts := make(map[string]int, len(ml))
59 for _, OpaquePair := range ml {
60 counts[OpaquePair.Name]++
61 }
62 if len(counts) == len(ml) {
63 return nil
64 }
65 var duplicates []string
66 for name, cnt := range counts {
67 if cnt > 1 {
68 duplicates = append(duplicates, name)
69 }
70 }
71 slices.Sort(duplicates)
72 return fmt.Errorf("duplicate keys in map-style list: %v", duplicates)
73 }
74
75 var _ iter.Seq2[string, String] = MapList(nil).Iter
76
77 // Iter is an iterator over key/value pairs for use in for-range loops.
78 // It is the MapList equivalent of directly ranging over a map.
79 func (ml MapList) Iter(yield func(name string, value String) bool) {
80 for _, OpaquePair := range ml {
81 if !yield(OpaquePair.Name, OpaquePair.Value) {
82 break
83 }
84 }
85 }
86
87 // Get looks up a pair's value based on its name.
88 // It is the MapList equivalent of `val, ok := m[key]`.
89 // However, it has linear time complexity.
90 func (ml MapList) Get(name string) (val String, ok bool) {
91 for _, OpaquePair := range ml {
92 if OpaquePair.Name == name {
93 return OpaquePair.Value, true
94 }
95 }
96 return val, false
97 }
98
99 // Set sets the value corresponding to a given name.
100 // It is the MapList equivalent of `m[key] = val`.
101 // However, it has linear time complexity,
102 // and does not affect shallow copies.
103 func (ml *MapList) Set(name string, val String) {
104 if ml == nil {
105 panic("assignment to entry in nil *MapList")
106 }
107 for i, OpaquePair := range *ml {
108 if OpaquePair.Name == name {
109 *ml = slices.Clone(*ml)
110 (*ml)[i].Value = val
111 return
112 }
113 }
114 *ml = append(make(MapList, 0, len(*ml)+1), *ml...)
115 *ml = append(*ml, Pair{Name: name, Value: val})
116 }