12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package configmap
15
+ package config
16
16
17
17
import (
18
18
"fmt"
@@ -35,59 +35,59 @@ const (
35
35
KeyDelimiter = "::"
36
36
)
37
37
38
- // NewConfigMap creates a new empty ConfigMap instance.
39
- func NewConfigMap () * ConfigMap {
40
- return & ConfigMap {k : koanf .New (KeyDelimiter )}
38
+ // NewMap creates a new empty config.Map instance.
39
+ func NewMap () * Map {
40
+ return & Map {k : koanf .New (KeyDelimiter )}
41
41
}
42
42
43
- // NewConfigMapFromFile creates a new ConfigMap by reading the given file.
44
- func NewConfigMapFromFile (fileName string ) (* ConfigMap , error ) {
43
+ // NewMapFromFile creates a new config.Map by reading the given file.
44
+ func NewMapFromFile (fileName string ) (* Map , error ) {
45
45
// Read yaml config from file.
46
- p := NewConfigMap ()
46
+ p := NewMap ()
47
47
if err := p .k .Load (file .Provider (fileName ), yaml .Parser ()); err != nil {
48
48
return nil , fmt .Errorf ("unable to read the file %v: %w" , fileName , err )
49
49
}
50
50
return p , nil
51
51
}
52
52
53
- // NewConfigMapFromBuffer creates a new ConfigMap by reading the given yaml buffer.
54
- func NewConfigMapFromBuffer (buf io.Reader ) (* ConfigMap , error ) {
53
+ // NewMapFromBuffer creates a new config.Map by reading the given yaml buffer.
54
+ func NewMapFromBuffer (buf io.Reader ) (* Map , error ) {
55
55
content , err := ioutil .ReadAll (buf )
56
56
if err != nil {
57
57
return nil , err
58
58
}
59
59
60
- p := NewConfigMap ()
60
+ p := NewMap ()
61
61
if err := p .k .Load (rawbytes .Provider (content ), yaml .Parser ()); err != nil {
62
62
return nil , err
63
63
}
64
64
65
65
return p , nil
66
66
}
67
67
68
- // NewConfigMapFromStringMap creates a ConfigMap from a map[string]interface{}.
69
- func NewConfigMapFromStringMap (data map [string ]interface {}) * ConfigMap {
70
- p := NewConfigMap ()
68
+ // NewMapFromStringMap creates a config.Map from a map[string]interface{}.
69
+ func NewMapFromStringMap (data map [string ]interface {}) * Map {
70
+ p := NewMap ()
71
71
// Cannot return error because the koanf instance is empty.
72
72
_ = p .k .Load (confmap .Provider (data , KeyDelimiter ), nil )
73
73
return p
74
74
}
75
75
76
- // ConfigMap represents the raw configuration map for the OpenTelemetry Collector.
77
- // The ConfigMap can be unmarshalled into the Collector's config using the "configunmarshaler" package.
78
- type ConfigMap struct {
76
+ // Map represents the raw configuration map for the OpenTelemetry Collector.
77
+ // The config.Map can be unmarshalled into the Collector's config using the "configunmarshaler" package.
78
+ type Map struct {
79
79
k * koanf.Koanf
80
80
}
81
81
82
82
// AllKeys returns all keys holding a value, regardless of where they are set.
83
83
// Nested keys are returned with a KeyDelimiter separator.
84
- func (l * ConfigMap ) AllKeys () []string {
84
+ func (l * Map ) AllKeys () []string {
85
85
return l .k .Keys ()
86
86
}
87
87
88
88
// Unmarshal unmarshalls the config into a struct.
89
89
// Tags on the fields of the structure must be properly set.
90
- func (l * ConfigMap ) Unmarshal (rawVal interface {}) error {
90
+ func (l * Map ) Unmarshal (rawVal interface {}) error {
91
91
decoder , err := mapstructure .NewDecoder (decoderConfig (rawVal ))
92
92
if err != nil {
93
93
return err
@@ -96,7 +96,7 @@ func (l *ConfigMap) Unmarshal(rawVal interface{}) error {
96
96
}
97
97
98
98
// UnmarshalExact unmarshalls the config into a struct, erroring if a field is nonexistent.
99
- func (l * ConfigMap ) UnmarshalExact (intoCfg interface {}) error {
99
+ func (l * Map ) UnmarshalExact (intoCfg interface {}) error {
100
100
dc := decoderConfig (intoCfg )
101
101
dc .ErrorUnused = true
102
102
decoder , err := mapstructure .NewDecoder (dc )
@@ -107,12 +107,12 @@ func (l *ConfigMap) UnmarshalExact(intoCfg interface{}) error {
107
107
}
108
108
109
109
// Get can retrieve any value given the key to use.
110
- func (l * ConfigMap ) Get (key string ) interface {} {
110
+ func (l * Map ) Get (key string ) interface {} {
111
111
return l .k .Get (key )
112
112
}
113
113
114
114
// Set sets the value for the key.
115
- func (l * ConfigMap ) Set (key string , value interface {}) {
115
+ func (l * Map ) Set (key string , value interface {}) {
116
116
// koanf doesn't offer a direct setting mechanism so merging is required.
117
117
merged := koanf .New (KeyDelimiter )
118
118
_ = merged .Load (confmap .Provider (map [string ]interface {}{key : value }, KeyDelimiter ), nil )
@@ -121,34 +121,34 @@ func (l *ConfigMap) Set(key string, value interface{}) {
121
121
122
122
// IsSet checks to see if the key has been set in any of the data locations.
123
123
// IsSet is case-insensitive for a key.
124
- func (l * ConfigMap ) IsSet (key string ) bool {
124
+ func (l * Map ) IsSet (key string ) bool {
125
125
return l .k .Exists (key )
126
126
}
127
127
128
128
// Merge merges the input given configuration into the existing config.
129
129
// Note that the given map may be modified.
130
- func (l * ConfigMap ) Merge (in * ConfigMap ) error {
130
+ func (l * Map ) Merge (in * Map ) error {
131
131
return l .k .Merge (in .k )
132
132
}
133
133
134
134
// Sub returns new Parser instance representing a sub-config of this instance.
135
135
// It returns an error is the sub-config is not a map (use Get()) and an empty Parser if
136
136
// none exists.
137
- func (l * ConfigMap ) Sub (key string ) (* ConfigMap , error ) {
137
+ func (l * Map ) Sub (key string ) (* Map , error ) {
138
138
data := l .Get (key )
139
139
if data == nil {
140
- return NewConfigMap (), nil
140
+ return NewMap (), nil
141
141
}
142
142
143
143
if reflect .TypeOf (data ).Kind () == reflect .Map {
144
- return NewConfigMapFromStringMap (cast .ToStringMap (data )), nil
144
+ return NewMapFromStringMap (cast .ToStringMap (data )), nil
145
145
}
146
146
147
147
return nil , fmt .Errorf ("unexpected sub-config value kind for key:%s value:%v kind:%v)" , key , data , reflect .TypeOf (data ).Kind ())
148
148
}
149
149
150
150
// ToStringMap creates a map[string]interface{} from a Parser.
151
- func (l * ConfigMap ) ToStringMap () map [string ]interface {} {
151
+ func (l * Map ) ToStringMap () map [string ]interface {} {
152
152
return maps .Unflatten (l .k .All (), KeyDelimiter )
153
153
}
154
154
0 commit comments