12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package configparser
15
+ package config
16
16
17
17
import (
18
18
"fmt"
@@ -35,58 +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 loads configuration.
77
- 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 {
78
79
k * koanf.Koanf
79
80
}
80
81
81
82
// AllKeys returns all keys holding a value, regardless of where they are set.
82
83
// Nested keys are returned with a KeyDelimiter separator.
83
- func (l * ConfigMap ) AllKeys () []string {
84
+ func (l * Map ) AllKeys () []string {
84
85
return l .k .Keys ()
85
86
}
86
87
87
88
// Unmarshal unmarshalls the config into a struct.
88
89
// Tags on the fields of the structure must be properly set.
89
- func (l * ConfigMap ) Unmarshal (rawVal interface {}) error {
90
+ func (l * Map ) Unmarshal (rawVal interface {}) error {
90
91
decoder , err := mapstructure .NewDecoder (decoderConfig (rawVal ))
91
92
if err != nil {
92
93
return err
@@ -95,7 +96,7 @@ func (l *ConfigMap) Unmarshal(rawVal interface{}) error {
95
96
}
96
97
97
98
// UnmarshalExact unmarshalls the config into a struct, erroring if a field is nonexistent.
98
- func (l * ConfigMap ) UnmarshalExact (intoCfg interface {}) error {
99
+ func (l * Map ) UnmarshalExact (intoCfg interface {}) error {
99
100
dc := decoderConfig (intoCfg )
100
101
dc .ErrorUnused = true
101
102
decoder , err := mapstructure .NewDecoder (dc )
@@ -106,12 +107,12 @@ func (l *ConfigMap) UnmarshalExact(intoCfg interface{}) error {
106
107
}
107
108
108
109
// Get can retrieve any value given the key to use.
109
- func (l * ConfigMap ) Get (key string ) interface {} {
110
+ func (l * Map ) Get (key string ) interface {} {
110
111
return l .k .Get (key )
111
112
}
112
113
113
114
// Set sets the value for the key.
114
- func (l * ConfigMap ) Set (key string , value interface {}) {
115
+ func (l * Map ) Set (key string , value interface {}) {
115
116
// koanf doesn't offer a direct setting mechanism so merging is required.
116
117
merged := koanf .New (KeyDelimiter )
117
118
_ = merged .Load (confmap .Provider (map [string ]interface {}{key : value }, KeyDelimiter ), nil )
@@ -120,34 +121,34 @@ func (l *ConfigMap) Set(key string, value interface{}) {
120
121
121
122
// IsSet checks to see if the key has been set in any of the data locations.
122
123
// IsSet is case-insensitive for a key.
123
- func (l * ConfigMap ) IsSet (key string ) bool {
124
+ func (l * Map ) IsSet (key string ) bool {
124
125
return l .k .Exists (key )
125
126
}
126
127
127
128
// Merge merges the input given configuration into the existing config.
128
129
// Note that the given map may be modified.
129
- func (l * ConfigMap ) Merge (in * ConfigMap ) error {
130
+ func (l * Map ) Merge (in * Map ) error {
130
131
return l .k .Merge (in .k )
131
132
}
132
133
133
134
// Sub returns new Parser instance representing a sub-config of this instance.
134
135
// It returns an error is the sub-config is not a map (use Get()) and an empty Parser if
135
136
// none exists.
136
- func (l * ConfigMap ) Sub (key string ) (* ConfigMap , error ) {
137
+ func (l * Map ) Sub (key string ) (* Map , error ) {
137
138
data := l .Get (key )
138
139
if data == nil {
139
- return NewConfigMap (), nil
140
+ return NewMap (), nil
140
141
}
141
142
142
143
if reflect .TypeOf (data ).Kind () == reflect .Map {
143
- return NewConfigMapFromStringMap (cast .ToStringMap (data )), nil
144
+ return NewMapFromStringMap (cast .ToStringMap (data )), nil
144
145
}
145
146
146
147
return nil , fmt .Errorf ("unexpected sub-config value kind for key:%s value:%v kind:%v)" , key , data , reflect .TypeOf (data ).Kind ())
147
148
}
148
149
149
150
// ToStringMap creates a map[string]interface{} from a Parser.
150
- func (l * ConfigMap ) ToStringMap () map [string ]interface {} {
151
+ func (l * Map ) ToStringMap () map [string ]interface {} {
151
152
return maps .Unflatten (l .k .All (), KeyDelimiter )
152
153
}
153
154
0 commit comments