| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"math" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
"reflect" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/stretchr/testify/assert" |
| 16 |
"github.com/stretchr/testify/require" |
| 17 |
yaml "go.yaml.in/yaml/v3" |
| 18 |
) |
| 19 |
|
| 20 |
func TestToStringMapFlatten(t *testing.T) { |
| 21 |
conf := NewFromStringMap(map[string]any{"key::embedded": int64(123)}) |
| 22 |
assert.Equal(t, map[string]any{"key": map[string]any{"embedded": int64(123)}}, conf.ToStringMap()) |
| 23 |
} |
| 24 |
|
| 25 |
func TestToStringMap(t *testing.T) { |
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
fileName string |
| 29 |
stringMap map[string]any |
| 30 |
}{ |
| 31 |
{ |
| 32 |
name: "Sample Collector configuration", |
| 33 |
fileName: filepath.Join("testdata", "config.yaml"), |
| 34 |
stringMap: map[string]any{ |
| 35 |
"receivers": map[string]any{ |
| 36 |
"nop": nil, |
| 37 |
"nop/myreceiver": nil, |
| 38 |
}, |
| 39 |
|
| 40 |
"processors": map[string]any{ |
| 41 |
"nop": nil, |
| 42 |
"nop/myprocessor": nil, |
| 43 |
}, |
| 44 |
|
| 45 |
"exporters": map[string]any{ |
| 46 |
"nop": nil, |
| 47 |
"nop/myexporter": nil, |
| 48 |
}, |
| 49 |
|
| 50 |
"extensions": map[string]any{ |
| 51 |
"nop": nil, |
| 52 |
"nop/myextension": nil, |
| 53 |
}, |
| 54 |
|
| 55 |
"service": map[string]any{ |
| 56 |
"extensions": []any{"nop"}, |
| 57 |
"pipelines": map[string]any{ |
| 58 |
"traces": map[string]any{ |
| 59 |
"receivers": []any{"nop"}, |
| 60 |
"processors": []any{"nop"}, |
| 61 |
"exporters": []any{"nop"}, |
| 62 |
}, |
| 63 |
}, |
| 64 |
}, |
| 65 |
}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "Sample types", |
| 69 |
fileName: filepath.Join("testdata", "basic_types.yaml"), |
| 70 |
stringMap: map[string]any{ |
| 71 |
"typed.options": map[string]any{ |
| 72 |
"floating.point.example": 3.14, |
| 73 |
"integer.example": 1234, |
| 74 |
"bool.example": false, |
| 75 |
"string.example": "this is a string", |
| 76 |
"nil.example": nil, |
| 77 |
}, |
| 78 |
}, |
| 79 |
}, |
| 80 |
{ |
| 81 |
name: "Embedded keys", |
| 82 |
fileName: filepath.Join("testdata", "embedded_keys.yaml"), |
| 83 |
stringMap: map[string]any{ |
| 84 |
"typed": map[string]any{"options": map[string]any{ |
| 85 |
"floating": map[string]any{"point": map[string]any{"example": 3.14}}, |
| 86 |
"integer": map[string]any{"example": 1234}, |
| 87 |
"bool": map[string]any{"example": false}, |
| 88 |
"string": map[string]any{"example": "this is a string"}, |
| 89 |
"nil": map[string]any{"example": nil}, |
| 90 |
}}, |
| 91 |
}, |
| 92 |
}, |
| 93 |
} |
| 94 |
for _, test := range tests { |
| 95 |
t.Run(test.name, func(t *testing.T) { |
| 96 |
assert.Equal(t, test.stringMap, newConfFromFile(t, test.fileName)) |
| 97 |
}) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
type testConfigAny struct { |
| 102 |
AnyField any `mapstructure:"any_field"` |
| 103 |
} |
| 104 |
|
| 105 |
func TestNilToAnyField(t *testing.T) { |
| 106 |
stringMap := map[string]any{ |
| 107 |
"any_field": nil, |
| 108 |
} |
| 109 |
conf := NewFromStringMap(stringMap) |
| 110 |
cfg := &testConfigAny{} |
| 111 |
require.NoError(t, conf.Unmarshal(cfg)) |
| 112 |
assert.Nil(t, cfg.AnyField) |
| 113 |
} |
| 114 |
|
| 115 |
func TestExpandNilStructPointersHookFunc(t *testing.T) { |
| 116 |
stringMap := map[string]any{ |
| 117 |
"boolean": nil, |
| 118 |
"struct": nil, |
| 119 |
"map_struct": map[string]any{ |
| 120 |
"struct": nil, |
| 121 |
}, |
| 122 |
} |
| 123 |
conf := NewFromStringMap(stringMap) |
| 124 |
cfg := &testConfig{} |
| 125 |
assert.Nil(t, cfg.Struct) |
| 126 |
require.NoError(t, conf.Unmarshal(cfg)) |
| 127 |
assert.Nil(t, cfg.Boolean) |
| 128 |
// assert.False(t, *cfg.Boolean) |
| 129 |
assert.Nil(t, cfg.Struct) |
| 130 |
assert.NotNil(t, cfg.MapStruct) |
| 131 |
assert.Equal(t, &myStruct{}, cfg.MapStruct["struct"]) |
| 132 |
} |
| 133 |
|
| 134 |
func TestExpandNilStructPointersHookFuncDefaultNotNilConfigNil(t *testing.T) { |
| 135 |
stringMap := map[string]any{ |
| 136 |
"boolean": nil, |
| 137 |
"struct": nil, |
| 138 |
"map_struct": map[string]any{ |
| 139 |
"struct": nil, |
| 140 |
}, |
| 141 |
} |
| 142 |
conf := NewFromStringMap(stringMap) |
| 143 |
varBool := true |
| 144 |
s1 := &myStruct{Name: "s1"} |
| 145 |
s2 := &myStruct{Name: "s2"} |
| 146 |
cfg := &testConfig{ |
| 147 |
Boolean: &varBool, |
| 148 |
Struct: s1, |
| 149 |
MapStruct: map[string]*myStruct{"struct": s2}, |
| 150 |
} |
| 151 |
require.NoError(t, conf.Unmarshal(cfg)) |
| 152 |
assert.NotNil(t, cfg.Boolean) |
| 153 |
assert.True(t, *cfg.Boolean) |
| 154 |
assert.NotNil(t, cfg.Struct) |
| 155 |
assert.Equal(t, s1, cfg.Struct) |
| 156 |
assert.NotNil(t, cfg.MapStruct) |
| 157 |
assert.Equal(t, &myStruct{}, cfg.MapStruct["struct"]) |
| 158 |
} |
| 159 |
|
| 160 |
func TestUnmarshalWithIgnoreUnused(t *testing.T) { |
| 161 |
stringMap := map[string]any{ |
| 162 |
"boolean": true, |
| 163 |
"string": "this is a string", |
| 164 |
} |
| 165 |
conf := NewFromStringMap(stringMap) |
| 166 |
require.Error(t, conf.Unmarshal(&testIDConfig{})) |
| 167 |
assert.NoError(t, conf.Unmarshal(&testIDConfig{}, WithIgnoreUnused())) |
| 168 |
} |
| 169 |
|
| 170 |
type testConfig struct { |
| 171 |
Boolean *bool `mapstructure:"boolean"` |
| 172 |
Struct *myStruct `mapstructure:"struct"` |
| 173 |
MapStruct map[string]*myStruct `mapstructure:"map_struct"` |
| 174 |
} |
| 175 |
|
| 176 |
func (t testConfig) Marshal(conf *Conf) error { |
| 177 |
if t.Boolean != nil && !*t.Boolean { |
| 178 |
return errors.New("unable to marshal") |
| 179 |
} |
| 180 |
if err := conf.Marshal(t); err != nil { |
| 181 |
return err |
| 182 |
} |
| 183 |
return conf.Merge(NewFromStringMap(map[string]any{ |
| 184 |
"additional": "field", |
| 185 |
})) |
| 186 |
} |
| 187 |
|
| 188 |
type myStruct struct { |
| 189 |
Name string |
| 190 |
} |
| 191 |
|
| 192 |
type TestID string |
| 193 |
|
| 194 |
func (tID *TestID) UnmarshalText(text []byte) error { |
| 195 |
*tID = TestID(strings.TrimSuffix(string(text), "_")) |
| 196 |
if *tID == "error" { |
| 197 |
return errors.New("parsing error") |
| 198 |
} |
| 199 |
return nil |
| 200 |
} |
| 201 |
|
| 202 |
func (tID TestID) MarshalText() (text []byte, err error) { |
| 203 |
out := string(tID) |
| 204 |
if !strings.HasSuffix(out, "_") { |
| 205 |
out += "_" |
| 206 |
} |
| 207 |
return []byte(out), nil |
| 208 |
} |
| 209 |
|
| 210 |
type testIDConfig struct { |
| 211 |
Boolean bool `mapstructure:"bool"` |
| 212 |
Map map[TestID]string `mapstructure:"map"` |
| 213 |
} |
| 214 |
|
| 215 |
func TestMapKeyStringToMapKeyTextUnmarshalerHookFunc(t *testing.T) { |
| 216 |
stringMap := map[string]any{ |
| 217 |
"bool": true, |
| 218 |
"map": map[string]any{ |
| 219 |
"string": "this is a string", |
| 220 |
}, |
| 221 |
} |
| 222 |
conf := NewFromStringMap(stringMap) |
| 223 |
|
| 224 |
cfg := &testIDConfig{} |
| 225 |
require.NoError(t, conf.Unmarshal(cfg)) |
| 226 |
assert.True(t, cfg.Boolean) |
| 227 |
assert.Equal(t, map[TestID]string{"string": "this is a string"}, cfg.Map) |
| 228 |
} |
| 229 |
|
| 230 |
type uint32Config struct { |
| 231 |
Value uint32 `mapstructure:"value"` |
| 232 |
} |
| 233 |
|
| 234 |
func TestUint32UnmarshalerSuccess(t *testing.T) { |
| 235 |
tests := []struct { |
| 236 |
name string |
| 237 |
testValue uint32 |
| 238 |
}{ |
| 239 |
{ |
| 240 |
name: "Test convert 0 to uint", |
| 241 |
testValue: 0, |
| 242 |
}, |
| 243 |
{ |
| 244 |
name: "Test positive uint conversion", |
| 245 |
testValue: 1000, |
| 246 |
}, |
| 247 |
{ |
| 248 |
name: "Test largest uint64 conversion", |
| 249 |
testValue: math.MaxUint32, |
| 250 |
}, |
| 251 |
} |
| 252 |
|
| 253 |
for _, tt := range tests { |
| 254 |
t.Run(tt.name, func(t *testing.T) { |
| 255 |
stringMap := map[string]any{ |
| 256 |
"value": int(tt.testValue), |
| 257 |
} |
| 258 |
conf := NewFromStringMap(stringMap) |
| 259 |
cfg := &uint32Config{} |
| 260 |
err := conf.Unmarshal(cfg) |
| 261 |
|
| 262 |
require.NoError(t, err) |
| 263 |
assert.Equal(t, cfg.Value, tt.testValue) |
| 264 |
}) |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
func TestUint32UnmarshalerFailure(t *testing.T) { |
| 269 |
stringMap := map[string]any{ |
| 270 |
"value": -1000, |
| 271 |
} |
| 272 |
conf := NewFromStringMap(stringMap) |
| 273 |
cfg := &uint32Config{} |
| 274 |
err := conf.Unmarshal(cfg) |
| 275 |
|
| 276 |
assert.ErrorContains(t, err, "decoding failed due to the following error(s):\n\n'value' cannot parse value as 'uint32': -1000 overflows uint") |
| 277 |
} |
| 278 |
|
| 279 |
type uint64Config struct { |
| 280 |
Value uint64 `mapstructure:"value"` |
| 281 |
} |
| 282 |
|
| 283 |
func TestUint64Unmarshaler(t *testing.T) { |
| 284 |
// Equivalent to -1000, but converted to uint64 |
| 285 |
value := uint64(1000) |
| 286 |
testValue := ^(value - 1) |
| 287 |
|
| 288 |
stringMap := map[string]any{ |
| 289 |
"value": testValue, |
| 290 |
} |
| 291 |
|
| 292 |
conf := NewFromStringMap(stringMap) |
| 293 |
cfg := &uint64Config{} |
| 294 |
err := conf.Unmarshal(cfg) |
| 295 |
|
| 296 |
require.NoError(t, err) |
| 297 |
assert.Equal(t, cfg.Value, testValue) |
| 298 |
} |
| 299 |
|
| 300 |
func TestUint64UnmarshalerFailure(t *testing.T) { |
| 301 |
stringMap := map[string]any{ |
| 302 |
"value": -1000, |
| 303 |
} |
| 304 |
conf := NewFromStringMap(stringMap) |
| 305 |
cfg := &uint64Config{} |
| 306 |
err := conf.Unmarshal(cfg) |
| 307 |
|
| 308 |
assert.ErrorContains(t, err, "decoding failed due to the following error(s):\n\n'value' cannot parse value as 'uint64': -1000 overflows uint") |
| 309 |
} |
| 310 |
|
| 311 |
func TestMapKeyStringToMapKeyTextUnmarshalerHookFuncDuplicateID(t *testing.T) { |
| 312 |
stringMap := map[string]any{ |
| 313 |
"bool": true, |
| 314 |
"map": map[string]any{ |
| 315 |
"string": "this is a string", |
| 316 |
"string_": "this is another string", |
| 317 |
}, |
| 318 |
} |
| 319 |
conf := NewFromStringMap(stringMap) |
| 320 |
|
| 321 |
cfg := &testIDConfig{} |
| 322 |
assert.Error(t, conf.Unmarshal(cfg)) |
| 323 |
} |
| 324 |
|
| 325 |
func TestMapKeyStringToMapKeyTextUnmarshalerHookFuncErrorUnmarshal(t *testing.T) { |
| 326 |
stringMap := map[string]any{ |
| 327 |
"bool": true, |
| 328 |
"map": map[string]any{ |
| 329 |
"error": "this is a string", |
| 330 |
}, |
| 331 |
} |
| 332 |
conf := NewFromStringMap(stringMap) |
| 333 |
|
| 334 |
cfg := &testIDConfig{} |
| 335 |
assert.Error(t, conf.Unmarshal(cfg)) |
| 336 |
} |
| 337 |
|
| 338 |
func TestMarshal(t *testing.T) { |
| 339 |
conf := New() |
| 340 |
cfg := &testIDConfig{ |
| 341 |
Boolean: true, |
| 342 |
Map: map[TestID]string{ |
| 343 |
"string": "this is a string", |
| 344 |
}, |
| 345 |
} |
| 346 |
require.NoError(t, conf.Marshal(cfg)) |
| 347 |
assert.Equal(t, true, conf.Get("bool")) |
| 348 |
assert.Equal(t, map[string]any{"string_": "this is a string"}, conf.Get("map")) |
| 349 |
} |
| 350 |
|
| 351 |
func TestMarshalDuplicateID(t *testing.T) { |
| 352 |
conf := New() |
| 353 |
cfg := &testIDConfig{ |
| 354 |
Boolean: true, |
| 355 |
Map: map[TestID]string{ |
| 356 |
"string": "this is a string", |
| 357 |
"string_": "this is another string", |
| 358 |
}, |
| 359 |
} |
| 360 |
assert.Error(t, conf.Marshal(cfg)) |
| 361 |
} |
| 362 |
|
| 363 |
func TestMarshalError(t *testing.T) { |
| 364 |
conf := New() |
| 365 |
assert.Error(t, conf.Marshal(nil)) |
| 366 |
} |
| 367 |
|
| 368 |
func TestMarshaler(t *testing.T) { |
| 369 |
conf := New() |
| 370 |
cfg := &testConfig{ |
| 371 |
Struct: &myStruct{ |
| 372 |
Name: "StructName", |
| 373 |
}, |
| 374 |
} |
| 375 |
require.NoError(t, conf.Marshal(cfg)) |
| 376 |
assert.Equal(t, "field", conf.Get("additional")) |
| 377 |
|
| 378 |
conf = New() |
| 379 |
type NestedMarshaler struct { |
| 380 |
TestConfig *testConfig |
| 381 |
} |
| 382 |
nmCfg := &NestedMarshaler{ |
| 383 |
TestConfig: cfg, |
| 384 |
} |
| 385 |
require.NoError(t, conf.Marshal(nmCfg)) |
| 386 |
sub, err := conf.Sub("testconfig") |
| 387 |
require.NoError(t, err) |
| 388 |
assert.True(t, sub.IsSet("additional")) |
| 389 |
assert.Equal(t, "field", sub.Get("additional")) |
| 390 |
varBool := false |
| 391 |
nmCfg.TestConfig.Boolean = &varBool |
| 392 |
assert.Error(t, conf.Marshal(nmCfg)) |
| 393 |
} |
| 394 |
|
| 395 |
// newConfFromFile creates a new Conf by reading the given file. |
| 396 |
func newConfFromFile(tb testing.TB, fileName string) map[string]any { |
| 397 |
content, err := os.ReadFile(filepath.Clean(fileName)) |
| 398 |
require.NoErrorf(tb, err, "unable to read the file %v", fileName) |
| 399 |
|
| 400 |
var data map[string]any |
| 401 |
require.NoError(tb, yaml.Unmarshal(content, &data), "unable to parse yaml") |
| 402 |
|
| 403 |
return NewFromStringMap(data).ToStringMap() |
| 404 |
} |
| 405 |
|
| 406 |
type testConfig2 struct { |
| 407 |
Next *nextConfig `mapstructure:"next"` |
| 408 |
Another string `mapstructure:"another"` |
| 409 |
EmbeddedConfig `mapstructure:",squash"` |
| 410 |
EmbeddedConfig2 `mapstructure:",squash"` |
| 411 |
} |
| 412 |
|
| 413 |
type testConfigWithoutUnmarshaler struct { |
| 414 |
Next *nextConfig `mapstructure:"next"` |
| 415 |
Another string `mapstructure:"another"` |
| 416 |
EmbeddedConfig `mapstructure:",squash"` |
| 417 |
EmbeddedConfig2 `mapstructure:",squash"` |
| 418 |
} |
| 419 |
|
| 420 |
type testConfigWithEmbeddedError struct { |
| 421 |
Next *nextConfig `mapstructure:"next"` |
| 422 |
Another string `mapstructure:"another"` |
| 423 |
EmbeddedConfigWithError `mapstructure:",squash"` |
| 424 |
} |
| 425 |
|
| 426 |
type testConfigWithMarshalError struct { |
| 427 |
Next *nextConfig `mapstructure:"next"` |
| 428 |
Another string `mapstructure:"another"` |
| 429 |
EmbeddedConfigWithMarshalError `mapstructure:",squash"` |
| 430 |
} |
| 431 |
|
| 432 |
func (tc *testConfigWithEmbeddedError) Unmarshal(component *Conf) error { |
| 433 |
if err := component.Unmarshal(tc, WithIgnoreUnused()); err != nil { |
| 434 |
return err |
| 435 |
} |
| 436 |
return nil |
| 437 |
} |
| 438 |
|
| 439 |
type EmbeddedConfig struct { |
| 440 |
Some string `mapstructure:"some"` |
| 441 |
} |
| 442 |
|
| 443 |
func (ec *EmbeddedConfig) Unmarshal(component *Conf) error { |
| 444 |
if err := component.Unmarshal(ec, WithIgnoreUnused()); err != nil { |
| 445 |
return err |
| 446 |
} |
| 447 |
ec.Some += " is also called" |
| 448 |
return nil |
| 449 |
} |
| 450 |
|
| 451 |
type EmbeddedConfig2 struct { |
| 452 |
Some2 string `mapstructure:"some_2"` |
| 453 |
} |
| 454 |
|
| 455 |
func (ec *EmbeddedConfig2) Unmarshal(component *Conf) error { |
| 456 |
if err := component.Unmarshal(ec, WithIgnoreUnused()); err != nil { |
| 457 |
return err |
| 458 |
} |
| 459 |
ec.Some2 += " also called2" |
| 460 |
return nil |
| 461 |
} |
| 462 |
|
| 463 |
type EmbeddedConfigWithError struct{} |
| 464 |
|
| 465 |
func (ecwe *EmbeddedConfigWithError) Unmarshal(_ *Conf) error { |
| 466 |
return errors.New("embedded error") |
| 467 |
} |
| 468 |
|
| 469 |
type EmbeddedConfigWithMarshalError struct{} |
| 470 |
|
| 471 |
func (ecwe EmbeddedConfigWithMarshalError) Marshal(_ *Conf) error { |
| 472 |
return errors.New("marshaling error") |
| 473 |
} |
| 474 |
|
| 475 |
func (ecwe EmbeddedConfigWithMarshalError) Unmarshal(_ *Conf) error { |
| 476 |
return nil |
| 477 |
} |
| 478 |
|
| 479 |
func (tc *testConfig2) Unmarshal(component *Conf) error { |
| 480 |
if err := component.Unmarshal(tc); err != nil { |
| 481 |
return err |
| 482 |
} |
| 483 |
tc.Another += " is only called directly" |
| 484 |
return nil |
| 485 |
} |
| 486 |
|
| 487 |
type nextConfig struct { |
| 488 |
String string `mapstructure:"string"` |
| 489 |
private string |
| 490 |
} |
| 491 |
|
| 492 |
func (nc *nextConfig) Unmarshal(component *Conf) error { |
| 493 |
if err := component.Unmarshal(nc); err != nil { |
| 494 |
return err |
| 495 |
} |
| 496 |
nc.String += " is called" |
| 497 |
return nil |
| 498 |
} |
| 499 |
|
| 500 |
func TestUnmarshaler(t *testing.T) { |
| 501 |
cfgMap := NewFromStringMap(map[string]any{ |
| 502 |
"next": map[string]any{ |
| 503 |
"string": "make sure this", |
| 504 |
}, |
| 505 |
"another": "make sure this", |
| 506 |
"some": "make sure this", |
| 507 |
"some_2": "this better be", |
| 508 |
}) |
| 509 |
|
| 510 |
tc := &testConfig2{} |
| 511 |
require.NoError(t, cfgMap.Unmarshal(tc)) |
| 512 |
assert.Equal(t, "make sure this is only called directly", tc.Another) |
| 513 |
assert.Equal(t, "make sure this is called", tc.Next.String) |
| 514 |
assert.Equal(t, "make sure this is also called", tc.Some) |
| 515 |
assert.Equal(t, "this better be also called2", tc.Some2) |
| 516 |
} |
| 517 |
|
| 518 |
func TestEmbeddedUnmarshaler(t *testing.T) { |
| 519 |
cfgMap := NewFromStringMap(map[string]any{ |
| 520 |
"next": map[string]any{ |
| 521 |
"string": "make sure this", |
| 522 |
}, |
| 523 |
"another": "make sure this", |
| 524 |
"some": "make sure this", |
| 525 |
"some_2": "this better be", |
| 526 |
}) |
| 527 |
|
| 528 |
tc := &testConfigWithoutUnmarshaler{} |
| 529 |
require.NoError(t, cfgMap.Unmarshal(tc)) |
| 530 |
assert.Equal(t, "make sure this", tc.Another) |
| 531 |
assert.Equal(t, "make sure this is called", tc.Next.String) |
| 532 |
assert.Equal(t, "make sure this is also called", tc.Some) |
| 533 |
assert.Equal(t, "this better be also called2", tc.Some2) |
| 534 |
} |
| 535 |
|
| 536 |
func TestEmbeddedUnmarshalerError(t *testing.T) { |
| 537 |
cfgMap := NewFromStringMap(map[string]any{ |
| 538 |
"next": map[string]any{ |
| 539 |
"string": "make sure this", |
| 540 |
}, |
| 541 |
"another": "make sure this", |
| 542 |
"some": "make sure this", |
| 543 |
}) |
| 544 |
|
| 545 |
tc := &testConfigWithEmbeddedError{} |
| 546 |
assert.ErrorContains(t, cfgMap.Unmarshal(tc), "embedded error") |
| 547 |
} |
| 548 |
|
| 549 |
func TestEmbeddedMarshalerError(t *testing.T) { |
| 550 |
t.Skip("This test fails because the main struct calls the embedded struct Unmarshal method, and doesn't execute the embedded struct hook.") |
| 551 |
cfgMap := NewFromStringMap(map[string]any{ |
| 552 |
"next": map[string]any{ |
| 553 |
"string": "make sure this", |
| 554 |
}, |
| 555 |
"another": "make sure this", |
| 556 |
}) |
| 557 |
|
| 558 |
tc := &testConfigWithMarshalError{} |
| 559 |
assert.EqualError(t, cfgMap.Unmarshal(tc), "error running encode hook: marshaling error") |
| 560 |
} |
| 561 |
|
| 562 |
type B struct { |
| 563 |
String string `mapstructure:"string"` |
| 564 |
} |
| 565 |
|
| 566 |
func (b *B) Unmarshal(conf *Conf) error { |
| 567 |
return conf.Unmarshal(b) |
| 568 |
} |
| 569 |
|
| 570 |
type A struct { |
| 571 |
B `mapstructure:",squash"` |
| 572 |
} |
| 573 |
|
| 574 |
func (a *A) Unmarshal(conf *Conf) error { |
| 575 |
return conf.Unmarshal(a) |
| 576 |
} |
| 577 |
|
| 578 |
func TestUnmarshalerEmbeddedNilMap(t *testing.T) { |
| 579 |
cfg := A{} |
| 580 |
nilConf := NewFromStringMap(nil) |
| 581 |
require.NoError(t, nilConf.Unmarshal(&cfg)) |
| 582 |
} |
| 583 |
|
| 584 |
func TestUnmarshalerKeepAlreadyInitialized(t *testing.T) { |
| 585 |
cfgMap := NewFromStringMap(map[string]any{ |
| 586 |
"next": map[string]any{ |
| 587 |
"string": "make sure this", |
| 588 |
}, |
| 589 |
"another": "make sure this", |
| 590 |
}) |
| 591 |
|
| 592 |
tc := &testConfig2{Next: &nextConfig{ |
| 593 |
private: "keep already configured members", |
| 594 |
}} |
| 595 |
require.NoError(t, cfgMap.Unmarshal(tc)) |
| 596 |
assert.Equal(t, "make sure this is only called directly", tc.Another) |
| 597 |
assert.Equal(t, "make sure this is called", tc.Next.String) |
| 598 |
assert.Equal(t, "keep already configured members", tc.Next.private) |
| 599 |
} |
| 600 |
|
| 601 |
func TestDirectUnmarshaler(t *testing.T) { |
| 602 |
cfgMap := NewFromStringMap(map[string]any{ |
| 603 |
"next": map[string]any{ |
| 604 |
"string": "make sure this", |
| 605 |
}, |
| 606 |
"another": "make sure this", |
| 607 |
}) |
| 608 |
|
| 609 |
tc := &testConfig2{Next: &nextConfig{ |
| 610 |
private: "keep already configured members", |
| 611 |
}} |
| 612 |
require.NoError(t, tc.Unmarshal(cfgMap)) |
| 613 |
assert.Equal(t, "make sure this is only called directly is only called directly", tc.Another) |
| 614 |
assert.Equal(t, "make sure this is called", tc.Next.String) |
| 615 |
assert.Equal(t, "keep already configured members", tc.Next.private) |
| 616 |
} |
| 617 |
|
| 618 |
type testErrConfig struct { |
| 619 |
Err errConfig `mapstructure:"err"` |
| 620 |
} |
| 621 |
|
| 622 |
type errConfig struct { |
| 623 |
Foo string `mapstructure:"foo"` |
| 624 |
} |
| 625 |
|
| 626 |
func (tc *errConfig) Unmarshal(*Conf) error { |
| 627 |
return errors.New("never works") |
| 628 |
} |
| 629 |
|
| 630 |
func TestUnmarshalerErr(t *testing.T) { |
| 631 |
cfgMap := NewFromStringMap(map[string]any{ |
| 632 |
"err": map[string]any{ |
| 633 |
"foo": "will not unmarshal due to error", |
| 634 |
}, |
| 635 |
}) |
| 636 |
|
| 637 |
tc := &testErrConfig{} |
| 638 |
require.EqualError(t, cfgMap.Unmarshal(tc), "decoding failed due to the following error(s):\n\n'err' never works") |
| 639 |
assert.Empty(t, tc.Err.Foo) |
| 640 |
} |
| 641 |
|
| 642 |
func TestZeroSliceHookFunc(t *testing.T) { |
| 643 |
type structWithSlices struct { |
| 644 |
Strings []string `mapstructure:"strings"` |
| 645 |
} |
| 646 |
|
| 647 |
tests := []struct { |
| 648 |
name string |
| 649 |
cfg map[string]any |
| 650 |
provided any |
| 651 |
expected any |
| 652 |
}{ |
| 653 |
{ |
| 654 |
name: "overridden by slice", |
| 655 |
cfg: map[string]any{ |
| 656 |
"strings": []string{"111"}, |
| 657 |
}, |
| 658 |
provided: &structWithSlices{ |
| 659 |
Strings: []string{"xxx", "yyyy", "zzzz"}, |
| 660 |
}, |
| 661 |
expected: &structWithSlices{ |
| 662 |
Strings: []string{"111"}, |
| 663 |
}, |
| 664 |
}, |
| 665 |
{ |
| 666 |
name: "overridden by a bigger slice", |
| 667 |
cfg: map[string]any{ |
| 668 |
"strings": []string{"111", "222", "333"}, |
| 669 |
}, |
| 670 |
provided: &structWithSlices{ |
| 671 |
Strings: []string{"xxx", "yyyy"}, |
| 672 |
}, |
| 673 |
expected: &structWithSlices{ |
| 674 |
Strings: []string{"111", "222", "333"}, |
| 675 |
}, |
| 676 |
}, |
| 677 |
{ |
| 678 |
name: "overridden by an empty slice", |
| 679 |
cfg: map[string]any{ |
| 680 |
"strings": []string{}, |
| 681 |
}, |
| 682 |
provided: &structWithSlices{ |
| 683 |
Strings: []string{"xxx", "yyyy"}, |
| 684 |
}, |
| 685 |
expected: &structWithSlices{ |
| 686 |
Strings: []string{}, |
| 687 |
}, |
| 688 |
}, |
| 689 |
{ |
| 690 |
name: "not overridden by nil", |
| 691 |
cfg: map[string]any{ |
| 692 |
"strings": nil, |
| 693 |
}, |
| 694 |
provided: &structWithSlices{ |
| 695 |
Strings: []string{"xxx", "yyyy"}, |
| 696 |
}, |
| 697 |
expected: &structWithSlices{ |
| 698 |
Strings: []string{"xxx", "yyyy"}, |
| 699 |
}, |
| 700 |
}, |
| 701 |
{ |
| 702 |
name: "not overridden by missing value", |
| 703 |
cfg: map[string]any{}, |
| 704 |
provided: &structWithSlices{ |
| 705 |
Strings: []string{"xxx", "yyyy"}, |
| 706 |
}, |
| 707 |
expected: &structWithSlices{ |
| 708 |
Strings: []string{"xxx", "yyyy"}, |
| 709 |
}, |
| 710 |
}, |
| 711 |
} |
| 712 |
|
| 713 |
for _, tt := range tests { |
| 714 |
t.Run(tt.name, func(t *testing.T) { |
| 715 |
cfg := NewFromStringMap(tt.cfg) |
| 716 |
|
| 717 |
err := cfg.Unmarshal(tt.provided) |
| 718 |
if assert.NoError(t, err) { |
| 719 |
assert.Equal(t, tt.expected, tt.provided) |
| 720 |
} |
| 721 |
}) |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
// Tests for issue that happened in https://github.com/open-telemetry/opentelemetry-collector/issues/12661. |
| 726 |
func TestStructValuesReplaced(t *testing.T) { |
| 727 |
type S struct { |
| 728 |
A string `mapstructure:"A,omitempty"` |
| 729 |
B string `mapstructure:"B,omitempty"` |
| 730 |
} |
| 731 |
|
| 732 |
type structWithSlices struct { |
| 733 |
Structs []S `mapstructure:"structs"` |
| 734 |
} |
| 735 |
|
| 736 |
slicesStruct := structWithSlices{ |
| 737 |
Structs: []S{ |
| 738 |
{A: "A"}, |
| 739 |
}, |
| 740 |
} |
| 741 |
|
| 742 |
bCfg := map[string]any{ |
| 743 |
"structs": []any{ |
| 744 |
map[string]any{ |
| 745 |
"B": "B", |
| 746 |
}, |
| 747 |
}, |
| 748 |
} |
| 749 |
bConf := NewFromStringMap(bCfg) |
| 750 |
err := bConf.Unmarshal(&slicesStruct) |
| 751 |
require.NoError(t, err) |
| 752 |
|
| 753 |
assert.Equal(t, []S{{B: "B"}}, slicesStruct.Structs) |
| 754 |
} |
| 755 |
|
| 756 |
func TestNilValuesUnchanged(t *testing.T) { |
| 757 |
type structWithSlices struct { |
| 758 |
Strings []string `mapstructure:"strings"` |
| 759 |
} |
| 760 |
|
| 761 |
slicesStruct := &structWithSlices{} |
| 762 |
|
| 763 |
nilCfg := map[string]any{ |
| 764 |
"strings": []any(nil), |
| 765 |
} |
| 766 |
nilConf := NewFromStringMap(nilCfg) |
| 767 |
err := nilConf.Unmarshal(slicesStruct) |
| 768 |
require.NoError(t, err) |
| 769 |
|
| 770 |
confFromStruct := New() |
| 771 |
err = confFromStruct.Marshal(slicesStruct) |
| 772 |
require.NoError(t, err) |
| 773 |
|
| 774 |
require.Equal(t, nilCfg, nilConf.ToStringMap()) |
| 775 |
require.Equal(t, confFromStruct.ToStringMap(), nilConf.ToStringMap()) |
| 776 |
} |
| 777 |
|
| 778 |
func TestEmptySliceUnchanged(t *testing.T) { |
| 779 |
type structWithSlices struct { |
| 780 |
Strings []string `mapstructure:"strings"` |
| 781 |
} |
| 782 |
|
| 783 |
slicesStruct := &structWithSlices{} |
| 784 |
|
| 785 |
nilCfg := map[string]any{ |
| 786 |
"strings": []any{}, |
| 787 |
} |
| 788 |
nilConf := NewFromStringMap(nilCfg) |
| 789 |
err := nilConf.Unmarshal(slicesStruct) |
| 790 |
require.NoError(t, err) |
| 791 |
|
| 792 |
confFromStruct := New() |
| 793 |
err = confFromStruct.Marshal(slicesStruct) |
| 794 |
require.NoError(t, err) |
| 795 |
|
| 796 |
require.Equal(t, nilCfg, nilConf.ToStringMap()) |
| 797 |
require.Equal(t, nilConf.ToStringMap(), confFromStruct.ToStringMap()) |
| 798 |
} |
| 799 |
|
| 800 |
type c struct { |
| 801 |
Modifiers []string `mapstructure:"modifiers"` |
| 802 |
} |
| 803 |
|
| 804 |
func (c *c) Unmarshal(conf *Conf) error { |
| 805 |
if err := conf.Unmarshal(c); err != nil { |
| 806 |
return err |
| 807 |
} |
| 808 |
c.Modifiers = append(c.Modifiers, "c.Unmarshal") |
| 809 |
return nil |
| 810 |
} |
| 811 |
|
| 812 |
type b struct { |
| 813 |
Modifiers []string `mapstructure:"modifiers"` |
| 814 |
C c `mapstructure:"c"` |
| 815 |
} |
| 816 |
|
| 817 |
func (b *b) Unmarshal(conf *Conf) error { |
| 818 |
if err := conf.Unmarshal(b); err != nil { |
| 819 |
return err |
| 820 |
} |
| 821 |
b.Modifiers = append(b.Modifiers, "B.Unmarshal") |
| 822 |
b.C.Modifiers = append(b.C.Modifiers, "B.Unmarshal") |
| 823 |
return nil |
| 824 |
} |
| 825 |
|
| 826 |
type a struct { |
| 827 |
Modifiers []string `mapstructure:"modifiers"` |
| 828 |
B b `mapstructure:"b"` |
| 829 |
} |
| 830 |
|
| 831 |
func (a *a) Unmarshal(conf *Conf) error { |
| 832 |
if err := conf.Unmarshal(a); err != nil { |
| 833 |
return err |
| 834 |
} |
| 835 |
a.Modifiers = append(a.Modifiers, "A.Unmarshal") |
| 836 |
a.B.Modifiers = append(a.B.Modifiers, "A.Unmarshal") |
| 837 |
a.B.C.Modifiers = append(a.B.C.Modifiers, "A.Unmarshal") |
| 838 |
return nil |
| 839 |
} |
| 840 |
|
| 841 |
type wrapper struct { |
| 842 |
A a `mapstructure:"a"` |
| 843 |
} |
| 844 |
|
| 845 |
// Test that calling the Unmarshal method on configuration structs is done from the inside out. |
| 846 |
func TestNestedUnmarshalerImplementations(t *testing.T) { |
| 847 |
conf := NewFromStringMap(map[string]any{"a": map[string]any{ |
| 848 |
"modifiers": []string{"conf.Unmarshal"}, |
| 849 |
"b": map[string]any{ |
| 850 |
"modifiers": []string{"conf.Unmarshal"}, |
| 851 |
"c": map[string]any{ |
| 852 |
"modifiers": []string{"conf.Unmarshal"}, |
| 853 |
}, |
| 854 |
}, |
| 855 |
}}) |
| 856 |
|
| 857 |
// Use a wrapper struct until we deprecate component.UnmarshalConfig |
| 858 |
w := &wrapper{} |
| 859 |
require.NoError(t, conf.Unmarshal(w)) |
| 860 |
|
| 861 |
a := w.A |
| 862 |
assert.Equal(t, []string{"conf.Unmarshal", "A.Unmarshal"}, a.Modifiers) |
| 863 |
assert.Equal(t, []string{"conf.Unmarshal", "B.Unmarshal", "A.Unmarshal"}, a.B.Modifiers) |
| 864 |
assert.Equal(t, []string{"conf.Unmarshal", "c.Unmarshal", "B.Unmarshal", "A.Unmarshal"}, a.B.C.Modifiers) |
| 865 |
} |
| 866 |
|
| 867 |
// Test that unmarshaling the same conf twice works. |
| 868 |
func TestUnmarshalDouble(t *testing.T) { |
| 869 |
conf := NewFromStringMap(map[string]any{ |
| 870 |
"str": "test", |
| 871 |
}) |
| 872 |
|
| 873 |
type Struct struct { |
| 874 |
Str string `mapstructure:"str"` |
| 875 |
} |
| 876 |
s := &Struct{} |
| 877 |
require.NoError(t, conf.Unmarshal(s)) |
| 878 |
assert.Equal(t, "test", s.Str) |
| 879 |
|
| 880 |
type Struct2 struct { |
| 881 |
Str string `mapstructure:"str"` |
| 882 |
} |
| 883 |
s2 := &Struct2{} |
| 884 |
require.NoError(t, conf.Unmarshal(s2)) |
| 885 |
assert.Equal(t, "test", s2.Str) |
| 886 |
} |
| 887 |
|
| 888 |
type embeddedStructWithUnmarshal struct { |
| 889 |
Foo string `mapstructure:"foo"` |
| 890 |
success string |
| 891 |
} |
| 892 |
|
| 893 |
func (e *embeddedStructWithUnmarshal) Unmarshal(c *Conf) error { |
| 894 |
if err := c.Unmarshal(e, WithIgnoreUnused()); err != nil { |
| 895 |
return err |
| 896 |
} |
| 897 |
e.success = "success" |
| 898 |
return nil |
| 899 |
} |
| 900 |
|
| 901 |
type configWithUnmarshalFromEmbeddedStruct struct { |
| 902 |
embeddedStructWithUnmarshal |
| 903 |
} |
| 904 |
|
| 905 |
type topLevel struct { |
| 906 |
Cfg *configWithUnmarshalFromEmbeddedStruct `mapstructure:"toplevel"` |
| 907 |
} |
| 908 |
|
| 909 |
// Test that Unmarshal is called on the embedded struct on the struct. |
| 910 |
func TestUnmarshalThroughEmbeddedStruct(t *testing.T) { |
| 911 |
c := NewFromStringMap(map[string]any{ |
| 912 |
"toplevel": map[string]any{ |
| 913 |
"foo": "bar", |
| 914 |
}, |
| 915 |
}) |
| 916 |
cfg := &topLevel{} |
| 917 |
err := c.Unmarshal(cfg) |
| 918 |
require.NoError(t, err) |
| 919 |
require.Equal(t, "success", cfg.Cfg.success) |
| 920 |
require.Equal(t, "bar", cfg.Cfg.Foo) |
| 921 |
} |
| 922 |
|
| 923 |
type configWithOwnUnmarshalAndEmbeddedSquashedStruct struct { |
| 924 |
embeddedStructWithUnmarshal `mapstructure:",squash"` |
| 925 |
} |
| 926 |
|
| 927 |
type topLevelSquashedEmbedded struct { |
| 928 |
Cfg *configWithOwnUnmarshalAndEmbeddedSquashedStruct `mapstructure:"toplevel"` |
| 929 |
} |
| 930 |
|
| 931 |
// Test that the Unmarshal method is called on the squashed, embedded struct. |
| 932 |
func TestUnmarshalOwnThroughEmbeddedSquashedStruct(t *testing.T) { |
| 933 |
c := NewFromStringMap(map[string]any{ |
| 934 |
"toplevel": map[string]any{ |
| 935 |
"foo": "bar", |
| 936 |
}, |
| 937 |
}) |
| 938 |
cfg := &topLevelSquashedEmbedded{} |
| 939 |
err := c.Unmarshal(cfg) |
| 940 |
require.NoError(t, err) |
| 941 |
require.Equal(t, "success", cfg.Cfg.success) |
| 942 |
require.Equal(t, "bar", cfg.Cfg.Foo) |
| 943 |
} |
| 944 |
|
| 945 |
type recursive struct { |
| 946 |
Foo string `mapstructure:"foo"` |
| 947 |
} |
| 948 |
|
| 949 |
func (r *recursive) Unmarshal(conf *Conf) error { |
| 950 |
newR := &recursive{} |
| 951 |
if err := conf.Unmarshal(newR); err != nil { |
| 952 |
return err |
| 953 |
} |
| 954 |
*r = *newR |
| 955 |
return nil |
| 956 |
} |
| 957 |
|
| 958 |
// Tests that a struct can unmarshal itself by creating a new copy of itself, unmarshaling itself, and setting its value. |
| 959 |
func TestRecursiveUnmarshaling(t *testing.T) { |
| 960 |
conf := NewFromStringMap(map[string]any{ |
| 961 |
"foo": "something", |
| 962 |
}) |
| 963 |
r := &recursive{} |
| 964 |
require.NoError(t, conf.Unmarshal(r)) |
| 965 |
require.Equal(t, "something", r.Foo) |
| 966 |
} |
| 967 |
|
| 968 |
func TestExpandedValue(t *testing.T) { |
| 969 |
cm := NewFromStringMap(map[string]any{ |
| 970 |
"key": ExpandedValue{ |
| 971 |
Value: 0xdeadbeef, |
| 972 |
Original: "original", |
| 973 |
}, |
| 974 |
}) |
| 975 |
assert.Equal(t, 0xdeadbeef, cm.Get("key")) |
| 976 |
assert.Equal(t, map[string]any{"key": 0xdeadbeef}, cm.ToStringMap()) |
| 977 |
|
| 978 |
type ConfigStr struct { |
| 979 |
Key string `mapstructure:"key"` |
| 980 |
} |
| 981 |
|
| 982 |
cfgStr := ConfigStr{} |
| 983 |
require.NoError(t, cm.Unmarshal(&cfgStr)) |
| 984 |
assert.Equal(t, "original", cfgStr.Key) |
| 985 |
|
| 986 |
type ConfigInt struct { |
| 987 |
Key int `mapstructure:"key"` |
| 988 |
} |
| 989 |
cfgInt := ConfigInt{} |
| 990 |
require.NoError(t, cm.Unmarshal(&cfgInt)) |
| 991 |
assert.Equal(t, 0xdeadbeef, cfgInt.Key) |
| 992 |
|
| 993 |
type ConfigBool struct { |
| 994 |
Key bool `mapstructure:"key"` |
| 995 |
} |
| 996 |
cfgBool := ConfigBool{} |
| 997 |
assert.Error(t, cm.Unmarshal(&cfgBool)) |
| 998 |
} |
| 999 |
|
| 1000 |
func TestSubExpandedValue(t *testing.T) { |
| 1001 |
cm := NewFromStringMap(map[string]any{ |
| 1002 |
"key": map[string]any{ |
| 1003 |
"subkey": ExpandedValue{ |
| 1004 |
Value: map[string]any{"subsubkey": "value"}, |
| 1005 |
Original: "subsubkey: value", |
| 1006 |
}, |
| 1007 |
}, |
| 1008 |
}) |
| 1009 |
|
| 1010 |
assert.Equal(t, map[string]any{"subkey": map[string]any{"subsubkey": "value"}}, cm.Get("key")) |
| 1011 |
assert.Equal(t, map[string]any{"key": map[string]any{"subkey": map[string]any{"subsubkey": "value"}}}, cm.ToStringMap()) |
| 1012 |
assert.Equal(t, map[string]any{"subsubkey": "value"}, cm.Get("key::subkey")) |
| 1013 |
|
| 1014 |
sub, err := cm.Sub("key::subkey") |
| 1015 |
require.NoError(t, err) |
| 1016 |
assert.Equal(t, map[string]any{"subsubkey": "value"}, sub.ToStringMap()) |
| 1017 |
|
| 1018 |
// This should return value, but currently `Get` does not support keys within expanded values. |
| 1019 |
assert.Nil(t, cm.Get("key::subkey::subsubkey")) |
| 1020 |
} |
| 1021 |
|
| 1022 |
func TestStringyTypes(t *testing.T) { |
| 1023 |
tests := []struct { |
| 1024 |
valueOfType any |
| 1025 |
isStringy bool |
| 1026 |
}{ |
| 1027 |
{ |
| 1028 |
valueOfType: "string", |
| 1029 |
isStringy: true, |
| 1030 |
}, |
| 1031 |
{ |
| 1032 |
valueOfType: 1, |
| 1033 |
isStringy: false, |
| 1034 |
}, |
| 1035 |
{ |
| 1036 |
valueOfType: map[string]any{}, |
| 1037 |
isStringy: false, |
| 1038 |
}, |
| 1039 |
{ |
| 1040 |
valueOfType: []any{}, |
| 1041 |
isStringy: false, |
| 1042 |
}, |
| 1043 |
{ |
| 1044 |
valueOfType: map[string]string{}, |
| 1045 |
isStringy: true, |
| 1046 |
}, |
| 1047 |
{ |
| 1048 |
valueOfType: []string{}, |
| 1049 |
isStringy: true, |
| 1050 |
}, |
| 1051 |
{ |
| 1052 |
valueOfType: map[string][]string{}, |
| 1053 |
isStringy: true, |
| 1054 |
}, |
| 1055 |
{ |
| 1056 |
valueOfType: map[string]map[string]string{}, |
| 1057 |
isStringy: true, |
| 1058 |
}, |
| 1059 |
{ |
| 1060 |
valueOfType: []map[string]any{}, |
| 1061 |
isStringy: false, |
| 1062 |
}, |
| 1063 |
{ |
| 1064 |
valueOfType: []map[string]string{}, |
| 1065 |
isStringy: true, |
| 1066 |
}, |
| 1067 |
} |
| 1068 |
|
| 1069 |
for _, tt := range tests { |
| 1070 |
// Create a reflect.Type from the value |
| 1071 |
to := reflect.TypeOf(tt.valueOfType) |
| 1072 |
assert.Equal(t, tt.isStringy, isStringyStructure(to)) |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
func TestConfDelete(t *testing.T) { |
| 1077 |
tests := []struct { |
| 1078 |
path string |
| 1079 |
stringMap map[string]any |
| 1080 |
}{ |
| 1081 |
{ |
| 1082 |
path: "key", |
| 1083 |
stringMap: map[string]any{"key": "value"}, |
| 1084 |
}, |
| 1085 |
{ |
| 1086 |
path: "map::expanded", |
| 1087 |
stringMap: map[string]any{"map": map[string]any{ |
| 1088 |
"expanded": ExpandedValue{ |
| 1089 |
Value: 0o1234, |
| 1090 |
Original: "01234", |
| 1091 |
}, |
| 1092 |
}}, |
| 1093 |
}, |
| 1094 |
} |
| 1095 |
|
| 1096 |
for _, test := range tests { |
| 1097 |
t.Run(test.path, func(t *testing.T) { |
| 1098 |
cm := NewFromStringMap(test.stringMap) |
| 1099 |
|
| 1100 |
assert.True(t, cm.IsSet(test.path)) |
| 1101 |
assert.True(t, cm.Delete(test.path)) |
| 1102 |
assert.Nil(t, cm.Get(test.path)) |
| 1103 |
assert.False(t, cm.IsSet(test.path)) |
| 1104 |
|
| 1105 |
assert.False(t, cm.Delete(test.path)) |
| 1106 |
assert.Nil(t, cm.Get(test.path)) |
| 1107 |
assert.False(t, cm.IsSet(test.path)) |
| 1108 |
}) |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
type structWithConfigOpaqueMap struct { |
| 1113 |
Headers map[string]string `mapstructure:"headers"` |
| 1114 |
} |
| 1115 |
|
| 1116 |
func TestMapMerge(t *testing.T) { |
| 1117 |
tests := []struct { |
| 1118 |
name string |
| 1119 |
initial map[string]string |
| 1120 |
added map[string]string |
| 1121 |
expected map[string]string |
| 1122 |
}{ |
| 1123 |
{ |
| 1124 |
name: "both nil", |
| 1125 |
initial: nil, |
| 1126 |
added: nil, |
| 1127 |
expected: nil, |
| 1128 |
}, |
| 1129 |
{ |
| 1130 |
name: "nil map", |
| 1131 |
initial: map[string]string{}, |
| 1132 |
added: nil, |
| 1133 |
expected: map[string]string{}, |
| 1134 |
}, |
| 1135 |
{ |
| 1136 |
name: "initialized", |
| 1137 |
initial: map[string]string{ |
| 1138 |
"foo": "bar", |
| 1139 |
}, |
| 1140 |
added: nil, |
| 1141 |
expected: map[string]string{ |
| 1142 |
"foo": "bar", |
| 1143 |
}, |
| 1144 |
}, |
| 1145 |
{ |
| 1146 |
name: "both", |
| 1147 |
initial: map[string]string{ |
| 1148 |
"foo": "bar", |
| 1149 |
}, |
| 1150 |
added: map[string]string{ |
| 1151 |
"foobar": "bar", |
| 1152 |
}, |
| 1153 |
expected: map[string]string{ |
| 1154 |
"foo": "bar", |
| 1155 |
"foobar": "bar", |
| 1156 |
}, |
| 1157 |
}, |
| 1158 |
} |
| 1159 |
|
| 1160 |
for _, test := range tests { |
| 1161 |
t.Run(test.name, func(t *testing.T) { |
| 1162 |
s := structWithConfigOpaqueMap{ |
| 1163 |
Headers: test.initial, |
| 1164 |
} |
| 1165 |
c := NewFromStringMap(map[string]any{ |
| 1166 |
"headers": test.added, |
| 1167 |
}) |
| 1168 |
require.NoError(t, c.Unmarshal(&s)) |
| 1169 |
assert.Equal(t, test.expected, s.Headers) |
| 1170 |
}) |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
func TestConfIsNil(t *testing.T) { |
| 1175 |
const subKey = "foo" |
| 1176 |
testCases := []struct { |
| 1177 |
name string |
| 1178 |
input map[string]any |
| 1179 |
expectIsNil bool |
| 1180 |
subExpectNil bool |
| 1181 |
subExpectErr string |
| 1182 |
}{ |
| 1183 |
{ |
| 1184 |
name: "nil input", |
| 1185 |
input: nil, |
| 1186 |
expectIsNil: true, |
| 1187 |
subExpectNil: true, |
| 1188 |
}, |
| 1189 |
{ |
| 1190 |
name: "empty map", |
| 1191 |
input: map[string]any{}, |
| 1192 |
expectIsNil: false, |
| 1193 |
subExpectNil: true, |
| 1194 |
}, |
| 1195 |
{ |
| 1196 |
name: "nil subkey", |
| 1197 |
input: map[string]any{subKey: nil}, |
| 1198 |
expectIsNil: false, |
| 1199 |
subExpectNil: true, |
| 1200 |
}, |
| 1201 |
{ |
| 1202 |
name: "empty subkey", |
| 1203 |
input: map[string]any{subKey: map[string]any{}}, |
| 1204 |
expectIsNil: false, |
| 1205 |
subExpectNil: false, |
| 1206 |
}, |
| 1207 |
{ |
| 1208 |
name: "non-empty map", |
| 1209 |
input: map[string]any{subKey: map[string]any{"bar": 42}}, |
| 1210 |
expectIsNil: false, |
| 1211 |
subExpectNil: false, |
| 1212 |
}, |
| 1213 |
{ |
| 1214 |
name: "non-map subkey", |
| 1215 |
input: map[string]any{subKey: 123}, |
| 1216 |
expectIsNil: false, |
| 1217 |
subExpectErr: "unexpected sub-config value kind", |
| 1218 |
}, |
| 1219 |
} |
| 1220 |
|
| 1221 |
for _, tc := range testCases { |
| 1222 |
t.Run(tc.name, func(t *testing.T) { |
| 1223 |
conf := NewFromStringMap(tc.input) |
| 1224 |
if tc.expectIsNil { |
| 1225 |
assert.Empty(t, conf.AllKeys()) |
| 1226 |
assert.Equal(t, map[string]any(nil), conf.ToStringMap()) |
| 1227 |
} else { |
| 1228 |
assert.NotEqual(t, map[string]any(nil), conf.ToStringMap()) |
| 1229 |
} |
| 1230 |
|
| 1231 |
sub, err := conf.Sub(subKey) |
| 1232 |
if tc.subExpectErr != "" { |
| 1233 |
assert.ErrorContains(t, err, tc.subExpectErr) |
| 1234 |
} else { |
| 1235 |
assert.NoError(t, err) |
| 1236 |
if tc.subExpectNil { |
| 1237 |
assert.Empty(t, sub.AllKeys()) |
| 1238 |
assert.Equal(t, map[string]any(nil), sub.ToStringMap()) |
| 1239 |
} else { |
| 1240 |
assert.NotEqual(t, map[string]any(nil), sub.ToStringMap()) |
| 1241 |
} |
| 1242 |
} |
| 1243 |
}) |
| 1244 |
} |
| 1245 |
} |
| 1246 |
|
| 1247 |
func TestConfmapNilMerge(t *testing.T) { |
| 1248 |
tests := []struct { |
| 1249 |
name string |
| 1250 |
left map[string]any |
| 1251 |
right map[string]any |
| 1252 |
expected map[string]any |
| 1253 |
}{ |
| 1254 |
{ |
| 1255 |
name: "both nil", |
| 1256 |
left: nil, |
| 1257 |
right: nil, |
| 1258 |
expected: nil, |
| 1259 |
}, |
| 1260 |
{ |
| 1261 |
name: "left nil", |
| 1262 |
left: nil, |
| 1263 |
right: map[string]any{"key": "value"}, |
| 1264 |
expected: map[string]any{"key": "value"}, |
| 1265 |
}, |
| 1266 |
{ |
| 1267 |
name: "right nil", |
| 1268 |
left: map[string]any{"key": "value"}, |
| 1269 |
right: nil, |
| 1270 |
expected: map[string]any{"key": "value"}, |
| 1271 |
}, |
| 1272 |
{ |
| 1273 |
name: "both non-nil", |
| 1274 |
left: map[string]any{"key1": "value1"}, |
| 1275 |
right: map[string]any{"key2": "value2"}, |
| 1276 |
expected: map[string]any{"key1": "value1", "key2": "value2"}, |
| 1277 |
}, |
| 1278 |
{ |
| 1279 |
name: "left empty, right non-empty", |
| 1280 |
left: map[string]any{}, |
| 1281 |
right: map[string]any{"key": "value"}, |
| 1282 |
expected: map[string]any{"key": "value"}, |
| 1283 |
}, |
| 1284 |
{ |
| 1285 |
name: "left non-empty, right empty", |
| 1286 |
left: map[string]any{"key": "value"}, |
| 1287 |
right: map[string]any{}, |
| 1288 |
expected: map[string]any{"key": "value"}, |
| 1289 |
}, |
| 1290 |
{ |
| 1291 |
name: "left nil, right empty", |
| 1292 |
left: nil, |
| 1293 |
right: map[string]any{}, |
| 1294 |
expected: map[string]any{}, |
| 1295 |
}, |
| 1296 |
{ |
| 1297 |
name: "left empty, right nil", |
| 1298 |
left: map[string]any{}, |
| 1299 |
right: nil, |
| 1300 |
expected: map[string]any{}, |
| 1301 |
}, |
| 1302 |
} |
| 1303 |
for _, test := range tests { |
| 1304 |
t.Run(test.name, func(t *testing.T) { |
| 1305 |
leftConf := NewFromStringMap(test.left) |
| 1306 |
assert.Equal(t, test.left, leftConf.ToStringMap()) |
| 1307 |
rightConf := NewFromStringMap(test.right) |
| 1308 |
assert.Equal(t, test.right, rightConf.ToStringMap()) |
| 1309 |
|
| 1310 |
err := leftConf.Merge(rightConf) |
| 1311 |
require.NoError(t, err) |
| 1312 |
|
| 1313 |
assert.Equal(t, test.expected, leftConf.ToStringMap()) |
| 1314 |
}) |
| 1315 |
|
| 1316 |
t.Run(test.name+"merge append", func(t *testing.T) { |
| 1317 |
leftConf := NewFromStringMap(test.left) |
| 1318 |
assert.Equal(t, test.left, leftConf.ToStringMap()) |
| 1319 |
rightConf := NewFromStringMap(test.right) |
| 1320 |
assert.Equal(t, test.right, rightConf.ToStringMap()) |
| 1321 |
|
| 1322 |
err := leftConf.mergeAppend(rightConf) |
| 1323 |
require.NoError(t, err) |
| 1324 |
|
| 1325 |
assert.Equal(t, test.expected, leftConf.ToStringMap()) |
| 1326 |
}) |
| 1327 |
} |
| 1328 |
} |