| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package configoptional |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
|
| 14 |
"go.opentelemetry.io/collector/confmap" |
| 15 |
"go.opentelemetry.io/collector/confmap/confmaptest" |
| 16 |
"go.opentelemetry.io/collector/confmap/xconfmap" |
| 17 |
"go.opentelemetry.io/collector/featuregate" |
| 18 |
) |
| 19 |
|
| 20 |
type Config[T any] struct { |
| 21 |
Sub1 Optional[T] `mapstructure:"sub"` |
| 22 |
} |
| 23 |
|
| 24 |
type Sub struct { |
| 25 |
Foo string `mapstructure:"foo"` |
| 26 |
} |
| 27 |
|
| 28 |
type WithEnabled struct { |
| 29 |
Enabled bool `mapstructure:"enabled"` |
| 30 |
} |
| 31 |
|
| 32 |
type WithEnabledOmitEmpty struct { |
| 33 |
Enabled bool `mapstructure:"enabled,omitempty"` |
| 34 |
} |
| 35 |
|
| 36 |
type OmitEmpty struct { |
| 37 |
Foo int `mapstructure:",omitempty"` |
| 38 |
} |
| 39 |
|
| 40 |
type NoMapstructure struct { |
| 41 |
Foo string |
| 42 |
} |
| 43 |
|
| 44 |
var subDefault = Sub{ |
| 45 |
Foo: "foobar", |
| 46 |
} |
| 47 |
|
| 48 |
func ptr[T any](v T) *T { |
| 49 |
return &v |
| 50 |
} |
| 51 |
|
| 52 |
func TestDefaultPanics(t *testing.T) { |
| 53 |
assert.Panics(t, func() { |
| 54 |
_ = Default(1) |
| 55 |
}) |
| 56 |
|
| 57 |
assert.Panics(t, func() { |
| 58 |
_ = Default(ptr(1)) |
| 59 |
}) |
| 60 |
|
| 61 |
assert.Panics(t, func() { |
| 62 |
_ = Default(WithEnabled{}) |
| 63 |
}) |
| 64 |
|
| 65 |
assert.Panics(t, func() { |
| 66 |
_ = Default(WithEnabledOmitEmpty{}) |
| 67 |
}) |
| 68 |
|
| 69 |
assert.Panics(t, func() { |
| 70 |
_ = Some(WithEnabled{}) |
| 71 |
}) |
| 72 |
|
| 73 |
assert.Panics(t, func() { |
| 74 |
_ = None[WithEnabled]() |
| 75 |
}) |
| 76 |
|
| 77 |
assert.Panics(t, func() { |
| 78 |
opt := None[int]() |
| 79 |
_ = opt.GetOrInsertDefault() |
| 80 |
}) |
| 81 |
|
| 82 |
assert.Panics(t, func() { |
| 83 |
var opt Optional[WithEnabled] |
| 84 |
_ = opt.GetOrInsertDefault() |
| 85 |
}) |
| 86 |
|
| 87 |
assert.NotPanics(t, func() { |
| 88 |
_ = Default(NoMapstructure{}) |
| 89 |
}) |
| 90 |
|
| 91 |
assert.NotPanics(t, func() { |
| 92 |
_ = Default(OmitEmpty{}) |
| 93 |
}) |
| 94 |
|
| 95 |
assert.NotPanics(t, func() { |
| 96 |
_ = Default(subDefault) |
| 97 |
}) |
| 98 |
|
| 99 |
assert.NotPanics(t, func() { |
| 100 |
_ = Default(ptr(subDefault)) |
| 101 |
}) |
| 102 |
} |
| 103 |
|
| 104 |
func TestEqualityDefault(t *testing.T) { |
| 105 |
defaultOne := Default(subDefault) |
| 106 |
defaultTwo := Default(subDefault) |
| 107 |
assert.Equal(t, defaultOne, defaultTwo) |
| 108 |
} |
| 109 |
|
| 110 |
func TestNoneZeroVal(t *testing.T) { |
| 111 |
var none Optional[Sub] |
| 112 |
require.False(t, none.HasValue()) |
| 113 |
require.Nil(t, none.Get()) |
| 114 |
|
| 115 |
var zeroVal Sub |
| 116 |
ret := none.GetOrInsertDefault() |
| 117 |
require.True(t, none.HasValue()) |
| 118 |
assert.Equal(t, &zeroVal, ret) |
| 119 |
} |
| 120 |
|
| 121 |
func TestNone(t *testing.T) { |
| 122 |
none := None[Sub]() |
| 123 |
require.False(t, none.HasValue()) |
| 124 |
require.Nil(t, none.Get()) |
| 125 |
|
| 126 |
var zeroVal Sub |
| 127 |
ret := none.GetOrInsertDefault() |
| 128 |
require.True(t, none.HasValue()) |
| 129 |
assert.Equal(t, &zeroVal, ret) |
| 130 |
} |
| 131 |
|
| 132 |
func ExampleNone() { |
| 133 |
type Person struct { |
| 134 |
Name string |
| 135 |
Age int |
| 136 |
} |
| 137 |
|
| 138 |
opt := None[Person]() |
| 139 |
|
| 140 |
// A None has no value. |
| 141 |
fmt.Println(opt.HasValue()) |
| 142 |
fmt.Println(opt.Get()) |
| 143 |
|
| 144 |
// GetOrInsertDefault places the zero value |
| 145 |
// and returns it, allowing you to modify it. |
| 146 |
opt.GetOrInsertDefault().Name = "John Doe" |
| 147 |
fmt.Println(opt.HasValue()) |
| 148 |
fmt.Println(opt.Get()) |
| 149 |
|
| 150 |
// Output: |
| 151 |
// false |
| 152 |
// <nil> |
| 153 |
// true |
| 154 |
// &{John Doe 0} |
| 155 |
} |
| 156 |
|
| 157 |
func TestSome(t *testing.T) { |
| 158 |
some := Some(Sub{ |
| 159 |
Foo: "foobar", |
| 160 |
}) |
| 161 |
require.True(t, some.HasValue()) |
| 162 |
retGet := some.Get() |
| 163 |
assert.Equal(t, "foobar", retGet.Foo) |
| 164 |
|
| 165 |
retGetOrInsertDefault := some.GetOrInsertDefault() |
| 166 |
require.True(t, some.HasValue()) |
| 167 |
assert.Equal(t, retGet, retGetOrInsertDefault) |
| 168 |
} |
| 169 |
|
| 170 |
func ExampleSome() { |
| 171 |
type Person struct { |
| 172 |
Name string |
| 173 |
Age int |
| 174 |
} |
| 175 |
|
| 176 |
opt := Some(Person{ |
| 177 |
Name: "John Doe", |
| 178 |
Age: 42, |
| 179 |
}) |
| 180 |
|
| 181 |
// A Some has a value. |
| 182 |
fmt.Println(opt.HasValue()) |
| 183 |
fmt.Println(opt.Get()) |
| 184 |
|
| 185 |
// GetOrInsertDefault only returns a reference |
| 186 |
// to the inner value without modifying it. |
| 187 |
opt.GetOrInsertDefault().Name = "Jane Doe" |
| 188 |
fmt.Println(opt.HasValue()) |
| 189 |
fmt.Println(opt.Get()) |
| 190 |
|
| 191 |
// Output: |
| 192 |
// true |
| 193 |
// &{John Doe 42} |
| 194 |
// true |
| 195 |
// &{Jane Doe 42} |
| 196 |
} |
| 197 |
|
| 198 |
func TestDefault(t *testing.T) { |
| 199 |
defaultSub := Default(subDefault) |
| 200 |
require.False(t, defaultSub.HasValue()) |
| 201 |
require.Nil(t, defaultSub.Get()) |
| 202 |
|
| 203 |
ret := defaultSub.GetOrInsertDefault() |
| 204 |
require.True(t, defaultSub.HasValue()) |
| 205 |
assert.Equal(t, &subDefault, ret) |
| 206 |
} |
| 207 |
|
| 208 |
func ExampleDefault() { |
| 209 |
type Person struct { |
| 210 |
Name string |
| 211 |
Age int |
| 212 |
} |
| 213 |
|
| 214 |
opt := Default(Person{ |
| 215 |
Name: "John Doe", |
| 216 |
Age: 42, |
| 217 |
}) |
| 218 |
|
| 219 |
// A Default has no value. |
| 220 |
fmt.Println(opt.HasValue()) |
| 221 |
fmt.Println(opt.Get()) |
| 222 |
|
| 223 |
// GetOrInsertDefault places the default value |
| 224 |
// and returns it, allowing you to modify it. |
| 225 |
opt.GetOrInsertDefault().Age = 38 |
| 226 |
fmt.Println(opt.HasValue()) |
| 227 |
fmt.Println(opt.Get()) |
| 228 |
|
| 229 |
// Output: |
| 230 |
// false |
| 231 |
// <nil> |
| 232 |
// true |
| 233 |
// &{John Doe 38} |
| 234 |
} |
| 235 |
|
| 236 |
func TestUnmarshalOptional(t *testing.T) { |
| 237 |
tests := []struct { |
| 238 |
name string |
| 239 |
config map[string]any |
| 240 |
defaultCfg Config[Sub] |
| 241 |
expectedSub bool |
| 242 |
expectedFoo string |
| 243 |
}{ |
| 244 |
{ |
| 245 |
name: "none_no_config", |
| 246 |
defaultCfg: Config[Sub]{ |
| 247 |
Sub1: None[Sub](), |
| 248 |
}, |
| 249 |
expectedSub: false, |
| 250 |
}, |
| 251 |
{ |
| 252 |
name: "none_with_config", |
| 253 |
config: map[string]any{ |
| 254 |
"sub": map[string]any{ |
| 255 |
"foo": "bar", |
| 256 |
}, |
| 257 |
}, |
| 258 |
defaultCfg: Config[Sub]{ |
| 259 |
Sub1: None[Sub](), |
| 260 |
}, |
| 261 |
expectedSub: true, |
| 262 |
expectedFoo: "bar", // input overrides default |
| 263 |
}, |
| 264 |
{ |
| 265 |
// nil is treated as an empty map because of the hooks we use. |
| 266 |
name: "none_with_config_no_foo", |
| 267 |
config: map[string]any{ |
| 268 |
"sub": nil, |
| 269 |
}, |
| 270 |
defaultCfg: Config[Sub]{ |
| 271 |
Sub1: None[Sub](), |
| 272 |
}, |
| 273 |
expectedSub: false, |
| 274 |
}, |
| 275 |
{ |
| 276 |
name: "none_with_config_no_foo_empty_map", |
| 277 |
config: map[string]any{ |
| 278 |
"sub": map[string]any{}, |
| 279 |
}, |
| 280 |
defaultCfg: Config[Sub]{ |
| 281 |
Sub1: None[Sub](), |
| 282 |
}, |
| 283 |
expectedSub: true, |
| 284 |
expectedFoo: "", |
| 285 |
}, |
| 286 |
{ |
| 287 |
name: "default_no_config", |
| 288 |
defaultCfg: Config[Sub]{ |
| 289 |
Sub1: Default(subDefault), |
| 290 |
}, |
| 291 |
expectedSub: false, |
| 292 |
}, |
| 293 |
{ |
| 294 |
name: "default_with_config", |
| 295 |
config: map[string]any{ |
| 296 |
"sub": map[string]any{ |
| 297 |
"foo": "bar", |
| 298 |
}, |
| 299 |
}, |
| 300 |
defaultCfg: Config[Sub]{ |
| 301 |
Sub1: Default(subDefault), |
| 302 |
}, |
| 303 |
expectedSub: true, |
| 304 |
expectedFoo: "bar", // input overrides default |
| 305 |
}, |
| 306 |
{ |
| 307 |
name: "default_with_config_no_foo", |
| 308 |
config: map[string]any{ |
| 309 |
"sub": nil, |
| 310 |
}, |
| 311 |
defaultCfg: Config[Sub]{ |
| 312 |
Sub1: Default(subDefault), |
| 313 |
}, |
| 314 |
expectedSub: true, |
| 315 |
expectedFoo: "foobar", // default applies |
| 316 |
}, |
| 317 |
{ |
| 318 |
name: "default_with_config_no_foo_empty_map", |
| 319 |
config: map[string]any{ |
| 320 |
"sub": map[string]any{}, |
| 321 |
}, |
| 322 |
defaultCfg: Config[Sub]{ |
| 323 |
Sub1: Default(subDefault), |
| 324 |
}, |
| 325 |
expectedSub: true, |
| 326 |
expectedFoo: "foobar", // default applies |
| 327 |
}, |
| 328 |
{ |
| 329 |
name: "some_no_config", |
| 330 |
defaultCfg: Config[Sub]{ |
| 331 |
Sub1: Some(Sub{ |
| 332 |
Foo: "foobar", |
| 333 |
}), |
| 334 |
}, |
| 335 |
expectedSub: true, |
| 336 |
expectedFoo: "foobar", // value is not modified |
| 337 |
}, |
| 338 |
{ |
| 339 |
name: "some_with_config", |
| 340 |
config: map[string]any{ |
| 341 |
"sub": map[string]any{ |
| 342 |
"foo": "bar", |
| 343 |
}, |
| 344 |
}, |
| 345 |
defaultCfg: Config[Sub]{ |
| 346 |
Sub1: Some(Sub{ |
| 347 |
Foo: "foobar", |
| 348 |
}), |
| 349 |
}, |
| 350 |
expectedSub: true, |
| 351 |
expectedFoo: "bar", // input overrides previous value |
| 352 |
}, |
| 353 |
{ |
| 354 |
name: "some_with_config_no_foo", |
| 355 |
config: map[string]any{ |
| 356 |
"sub": nil, |
| 357 |
}, |
| 358 |
defaultCfg: Config[Sub]{ |
| 359 |
Sub1: Some(Sub{ |
| 360 |
Foo: "foobar", |
| 361 |
}), |
| 362 |
}, |
| 363 |
expectedSub: true, |
| 364 |
expectedFoo: "foobar", // default applies |
| 365 |
}, |
| 366 |
} |
| 367 |
|
| 368 |
for _, test := range tests { |
| 369 |
t.Run(test.name, func(t *testing.T) { |
| 370 |
cfg := test.defaultCfg |
| 371 |
conf := confmap.NewFromStringMap(test.config) |
| 372 |
require.NoError(t, conf.Unmarshal(&cfg)) |
| 373 |
require.Equal(t, test.expectedSub, cfg.Sub1.HasValue()) |
| 374 |
if test.expectedSub { |
| 375 |
require.Equal(t, test.expectedFoo, cfg.Sub1.Get().Foo) |
| 376 |
} |
| 377 |
}) |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
func TestAddFieldEnabledFeatureGate(t *testing.T) { |
| 382 |
tests := []struct { |
| 383 |
name string |
| 384 |
config map[string]any |
| 385 |
defaultCfg Config[Sub] |
| 386 |
expectedSub bool |
| 387 |
expectedFoo string |
| 388 |
}{ |
| 389 |
{ |
| 390 |
name: "none_with_enabled_true", |
| 391 |
config: map[string]any{ |
| 392 |
"sub": map[string]any{ |
| 393 |
"enabled": true, |
| 394 |
"foo": "bar", |
| 395 |
}, |
| 396 |
}, |
| 397 |
defaultCfg: Config[Sub]{ |
| 398 |
Sub1: None[Sub](), |
| 399 |
}, |
| 400 |
expectedSub: true, |
| 401 |
expectedFoo: "bar", |
| 402 |
}, |
| 403 |
{ |
| 404 |
name: "none_with_enabled_false", |
| 405 |
config: map[string]any{ |
| 406 |
"sub": map[string]any{ |
| 407 |
"enabled": false, |
| 408 |
"foo": "bar", |
| 409 |
}, |
| 410 |
}, |
| 411 |
defaultCfg: Config[Sub]{ |
| 412 |
Sub1: None[Sub](), |
| 413 |
}, |
| 414 |
expectedSub: false, |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "none_with_enabled_false_no_other_config", |
| 418 |
config: map[string]any{ |
| 419 |
"sub": map[string]any{ |
| 420 |
"enabled": false, |
| 421 |
}, |
| 422 |
}, |
| 423 |
defaultCfg: Config[Sub]{ |
| 424 |
Sub1: None[Sub](), |
| 425 |
}, |
| 426 |
expectedSub: false, |
| 427 |
}, |
| 428 |
{ |
| 429 |
name: "default_with_enabled_true", |
| 430 |
config: map[string]any{ |
| 431 |
"sub": map[string]any{ |
| 432 |
"enabled": true, |
| 433 |
"foo": "bar", |
| 434 |
}, |
| 435 |
}, |
| 436 |
defaultCfg: Config[Sub]{ |
| 437 |
Sub1: Default(subDefault), |
| 438 |
}, |
| 439 |
expectedSub: true, |
| 440 |
expectedFoo: "bar", |
| 441 |
}, |
| 442 |
{ |
| 443 |
name: "default_with_enabled_false", |
| 444 |
config: map[string]any{ |
| 445 |
"sub": map[string]any{ |
| 446 |
"enabled": false, |
| 447 |
"foo": "bar", |
| 448 |
}, |
| 449 |
}, |
| 450 |
defaultCfg: Config[Sub]{ |
| 451 |
Sub1: Default(subDefault), |
| 452 |
}, |
| 453 |
expectedSub: false, |
| 454 |
}, |
| 455 |
{ |
| 456 |
name: "default_with_enabled_false_no_other_config", |
| 457 |
config: map[string]any{ |
| 458 |
"sub": map[string]any{ |
| 459 |
"enabled": false, |
| 460 |
}, |
| 461 |
}, |
| 462 |
defaultCfg: Config[Sub]{ |
| 463 |
Sub1: Default(subDefault), |
| 464 |
}, |
| 465 |
expectedSub: false, |
| 466 |
}, |
| 467 |
{ |
| 468 |
name: "some_with_enabled_true", |
| 469 |
config: map[string]any{ |
| 470 |
"sub": map[string]any{ |
| 471 |
"enabled": true, |
| 472 |
"foo": "baz", |
| 473 |
}, |
| 474 |
}, |
| 475 |
defaultCfg: Config[Sub]{ |
| 476 |
Sub1: Some(Sub{ |
| 477 |
Foo: "foobar", |
| 478 |
}), |
| 479 |
}, |
| 480 |
expectedSub: true, |
| 481 |
expectedFoo: "baz", |
| 482 |
}, |
| 483 |
{ |
| 484 |
name: "some_with_enabled_false", |
| 485 |
config: map[string]any{ |
| 486 |
"sub": map[string]any{ |
| 487 |
"enabled": false, |
| 488 |
"foo": "baz", |
| 489 |
}, |
| 490 |
}, |
| 491 |
defaultCfg: Config[Sub]{ |
| 492 |
Sub1: Some(Sub{ |
| 493 |
Foo: "foobar", |
| 494 |
}), |
| 495 |
}, |
| 496 |
expectedSub: false, |
| 497 |
}, |
| 498 |
{ |
| 499 |
name: "some_with_enabled_false_no_other_config", |
| 500 |
config: map[string]any{ |
| 501 |
"sub": map[string]any{ |
| 502 |
"enabled": false, |
| 503 |
}, |
| 504 |
}, |
| 505 |
defaultCfg: Config[Sub]{ |
| 506 |
Sub1: Some(Sub{ |
| 507 |
Foo: "foobar", |
| 508 |
}), |
| 509 |
}, |
| 510 |
expectedSub: false, |
| 511 |
}, |
| 512 |
} |
| 513 |
|
| 514 |
oldVal := addEnabledFieldFeatureGate.IsEnabled() |
| 515 |
require.NoError(t, featuregate.GlobalRegistry().Set(addEnabledFieldFeatureGateID, true)) |
| 516 |
defer func() { require.NoError(t, featuregate.GlobalRegistry().Set(addEnabledFieldFeatureGateID, oldVal)) }() |
| 517 |
|
| 518 |
for _, test := range tests { |
| 519 |
t.Run(test.name, func(t *testing.T) { |
| 520 |
cfg := test.defaultCfg |
| 521 |
conf := confmap.NewFromStringMap(test.config) |
| 522 |
require.NoError(t, conf.Unmarshal(&cfg)) |
| 523 |
require.Equal(t, test.expectedSub, cfg.Sub1.HasValue()) |
| 524 |
if test.expectedSub { |
| 525 |
require.Equal(t, test.expectedFoo, cfg.Sub1.Get().Foo) |
| 526 |
} |
| 527 |
}) |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
func TestEnabledFalseResetsValue(t *testing.T) { |
| 532 |
oldVal := addEnabledFieldFeatureGate.IsEnabled() |
| 533 |
require.NoError(t, featuregate.GlobalRegistry().Set(addEnabledFieldFeatureGateID, true)) |
| 534 |
defer func() { require.NoError(t, featuregate.GlobalRegistry().Set(addEnabledFieldFeatureGateID, oldVal)) }() |
| 535 |
|
| 536 |
cfg := Config[Sub]{Sub1: Some(Sub{Foo: "initial"})} |
| 537 |
require.True(t, cfg.Sub1.HasValue()) |
| 538 |
|
| 539 |
cm := confmap.NewFromStringMap(map[string]any{ |
| 540 |
"sub": map[string]any{"enabled": false, "foo": "ignored"}, |
| 541 |
}) |
| 542 |
require.NoError(t, cm.Unmarshal(&cfg)) |
| 543 |
require.Equal(t, None[Sub](), cfg.Sub1) |
| 544 |
} |
| 545 |
|
| 546 |
func TestUnmarshalErrorEnabledInvalidType(t *testing.T) { |
| 547 |
oldVal := addEnabledFieldFeatureGate.IsEnabled() |
| 548 |
require.NoError(t, featuregate.GlobalRegistry().Set(addEnabledFieldFeatureGateID, true)) |
| 549 |
defer func() { require.NoError(t, featuregate.GlobalRegistry().Set(addEnabledFieldFeatureGateID, oldVal)) }() |
| 550 |
|
| 551 |
cm := confmap.NewFromStringMap(map[string]any{ |
| 552 |
"sub": map[string]any{ |
| 553 |
"enabled": "something", |
| 554 |
"foo": "bar", |
| 555 |
}, |
| 556 |
}) |
| 557 |
cfg := Config[Sub]{ |
| 558 |
Sub1: None[Sub](), |
| 559 |
} |
| 560 |
err := cm.Unmarshal(&cfg) |
| 561 |
require.ErrorContains(t, err, "unexpected type string for 'enabled': got 'something' value expected 'true' or 'false'") |
| 562 |
} |
| 563 |
|
| 564 |
func TestUnmarshalErrorEnabledField(t *testing.T) { |
| 565 |
cm := confmap.NewFromStringMap(map[string]any{ |
| 566 |
"enabled": true, |
| 567 |
}) |
| 568 |
// Use zero value to avoid panic on constructor. |
| 569 |
var none Optional[WithEnabled] |
| 570 |
require.Error(t, cm.Unmarshal(&none)) |
| 571 |
} |
| 572 |
|
| 573 |
func TestUnmarshalConfigPointer(t *testing.T) { |
| 574 |
cm := confmap.NewFromStringMap(map[string]any{ |
| 575 |
"sub": map[string]any{ |
| 576 |
"foo": "bar", |
| 577 |
}, |
| 578 |
}) |
| 579 |
|
| 580 |
var cfg Config[*Sub] |
| 581 |
err := cm.Unmarshal(&cfg) |
| 582 |
require.NoError(t, err) |
| 583 |
assert.True(t, cfg.Sub1.HasValue()) |
| 584 |
assert.Equal(t, "bar", (*cfg.Sub1.Get()).Foo) |
| 585 |
} |
| 586 |
|
| 587 |
func TestUnmarshalErr(t *testing.T) { |
| 588 |
cm := confmap.NewFromStringMap(map[string]any{ |
| 589 |
"field": "value", |
| 590 |
}) |
| 591 |
|
| 592 |
cfg := Config[Sub]{ |
| 593 |
Sub1: Default(subDefault), |
| 594 |
} |
| 595 |
|
| 596 |
assert.False(t, cfg.Sub1.HasValue()) |
| 597 |
|
| 598 |
err := cm.Unmarshal(&cfg) |
| 599 |
require.Error(t, err) |
| 600 |
require.ErrorContains(t, err, "has invalid keys: field") |
| 601 |
assert.False(t, cfg.Sub1.HasValue()) |
| 602 |
} |
| 603 |
|
| 604 |
type MyIntConfig struct { |
| 605 |
Val int `mapstructure:"my_int"` |
| 606 |
} |
| 607 |
type MyConfig struct { |
| 608 |
Optional[MyIntConfig] `mapstructure:",squash"` |
| 609 |
} |
| 610 |
|
| 611 |
var myIntDefault = MyIntConfig{ |
| 612 |
Val: 1, |
| 613 |
} |
| 614 |
|
| 615 |
func TestSquashedOptional(t *testing.T) { |
| 616 |
cm := confmap.NewFromStringMap(map[string]any{ |
| 617 |
"my_int": 42, |
| 618 |
}) |
| 619 |
|
| 620 |
cfg := MyConfig{ |
| 621 |
Default(myIntDefault), |
| 622 |
} |
| 623 |
|
| 624 |
err := cm.Unmarshal(&cfg) |
| 625 |
require.NoError(t, err) |
| 626 |
|
| 627 |
assert.True(t, cfg.HasValue()) |
| 628 |
assert.Equal(t, 42, cfg.Get().Val) |
| 629 |
} |
| 630 |
|
| 631 |
func confFromYAML(t *testing.T, yaml string) *confmap.Conf { |
| 632 |
t.Helper() |
| 633 |
cm, err := confmap.NewRetrievedFromYAML([]byte(yaml)) |
| 634 |
require.NoError(t, err) |
| 635 |
conf, err := cm.AsConf() |
| 636 |
require.NoError(t, err) |
| 637 |
return conf |
| 638 |
} |
| 639 |
|
| 640 |
func TestComparePointerUnmarshal(t *testing.T) { |
| 641 |
tests := []struct { |
| 642 |
yaml string |
| 643 |
}{ |
| 644 |
{yaml: ""}, |
| 645 |
{yaml: "sub: "}, |
| 646 |
{yaml: "sub: null"}, |
| 647 |
{yaml: "sub: {}"}, |
| 648 |
{yaml: "sub: {foo: bar}"}, |
| 649 |
} |
| 650 |
for _, test := range tests { |
| 651 |
t.Run(test.yaml, func(t *testing.T) { |
| 652 |
var optCfg Config[Sub] |
| 653 |
conf := confFromYAML(t, test.yaml) |
| 654 |
optErr := conf.Unmarshal(&optCfg) |
| 655 |
require.NoError(t, optErr) |
| 656 |
|
| 657 |
var ptrCfg struct { |
| 658 |
Sub1 *Sub `mapstructure:"sub"` |
| 659 |
} |
| 660 |
ptrErr := conf.Unmarshal(&ptrCfg) |
| 661 |
require.NoError(t, ptrErr) |
| 662 |
|
| 663 |
assert.Equal(t, optCfg.Sub1.Get(), ptrCfg.Sub1) |
| 664 |
}) |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
func TestOptionalMarshal(t *testing.T) { |
| 669 |
tests := []struct { |
| 670 |
name string |
| 671 |
value Config[Sub] |
| 672 |
expected map[string]any |
| 673 |
}{ |
| 674 |
{ |
| 675 |
name: "none (zero value)", |
| 676 |
value: Config[Sub]{}, |
| 677 |
expected: map[string]any{"sub": nil}, |
| 678 |
}, |
| 679 |
{ |
| 680 |
name: "none", |
| 681 |
value: Config[Sub]{Sub1: None[Sub]()}, |
| 682 |
expected: map[string]any{"sub": nil}, |
| 683 |
}, |
| 684 |
{ |
| 685 |
name: "default", |
| 686 |
value: Config[Sub]{Sub1: Default(subDefault)}, |
| 687 |
expected: map[string]any{"sub": nil}, |
| 688 |
}, |
| 689 |
{ |
| 690 |
name: "some", |
| 691 |
value: Config[Sub]{Sub1: Some(Sub{ |
| 692 |
Foo: "bar", |
| 693 |
})}, |
| 694 |
expected: map[string]any{ |
| 695 |
"sub": map[string]any{ |
| 696 |
"foo": "bar", |
| 697 |
}, |
| 698 |
}, |
| 699 |
}, |
| 700 |
} |
| 701 |
|
| 702 |
for _, test := range tests { |
| 703 |
t.Run(test.name, func(t *testing.T) { |
| 704 |
conf := confmap.New() |
| 705 |
require.NoError(t, conf.Marshal(test.value)) |
| 706 |
assert.Equal(t, test.expected, conf.ToStringMap()) |
| 707 |
}) |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
func TestComparePointerMarshal(t *testing.T) { |
| 712 |
type Wrap[T any] struct { |
| 713 |
// Note: passes without requiring "squash". |
| 714 |
Sub1 T `mapstructure:"sub"` |
| 715 |
} |
| 716 |
|
| 717 |
type WrapOmitEmpty[T any] struct { |
| 718 |
// Note: passes without requiring "squash", except with Default-flavored Optional values. |
| 719 |
Sub1 T `mapstructure:"sub,omitempty"` |
| 720 |
} |
| 721 |
|
| 722 |
tests := []struct { |
| 723 |
pointer *Sub |
| 724 |
optional Optional[Sub] |
| 725 |
skipOmitEmpty bool |
| 726 |
}{ |
| 727 |
{pointer: nil, optional: None[Sub]()}, |
| 728 |
{pointer: nil, optional: Default(subDefault), skipOmitEmpty: true}, // does not work with omitempty |
| 729 |
{pointer: &Sub{Foo: "bar"}, optional: Some(Sub{Foo: "bar"})}, |
| 730 |
} |
| 731 |
for _, test := range tests { |
| 732 |
t.Run(fmt.Sprintf("%v vs %v", test.pointer, test.optional), func(t *testing.T) { |
| 733 |
wrapPointer := Wrap[*Sub]{Sub1: test.pointer} |
| 734 |
confPointer := confmap.NewFromStringMap(nil) |
| 735 |
require.NoError(t, confPointer.Marshal(wrapPointer)) |
| 736 |
|
| 737 |
wrapOptional := Wrap[Optional[Sub]]{Sub1: test.optional} |
| 738 |
confOptional := confmap.NewFromStringMap(nil) |
| 739 |
require.NoError(t, confOptional.Marshal(wrapOptional)) |
| 740 |
|
| 741 |
assert.Equal(t, confPointer.ToStringMap(), confOptional.ToStringMap()) |
| 742 |
}) |
| 743 |
|
| 744 |
if test.skipOmitEmpty { |
| 745 |
continue |
| 746 |
} |
| 747 |
t.Run(fmt.Sprintf("%v vs %v (omitempty)", test.pointer, test.optional), func(t *testing.T) { |
| 748 |
wrapPointer := WrapOmitEmpty[*Sub]{Sub1: test.pointer} |
| 749 |
confPointer := confmap.NewFromStringMap(nil) |
| 750 |
require.NoError(t, confPointer.Marshal(wrapPointer)) |
| 751 |
|
| 752 |
wrapOptional := WrapOmitEmpty[Optional[Sub]]{Sub1: test.optional} |
| 753 |
confOptional := confmap.NewFromStringMap(nil) |
| 754 |
require.NoError(t, confOptional.Marshal(wrapOptional)) |
| 755 |
|
| 756 |
assert.Equal(t, confPointer.ToStringMap(), confOptional.ToStringMap()) |
| 757 |
}) |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
type invalid struct{} |
| 762 |
|
| 763 |
func (invalid) Validate() error { |
| 764 |
return errors.New("invalid") |
| 765 |
} |
| 766 |
|
| 767 |
var _ xconfmap.Validator = invalid{} |
| 768 |
|
| 769 |
type hasNested struct { |
| 770 |
CouldBe Optional[invalid] |
| 771 |
} |
| 772 |
|
| 773 |
func TestOptionalValidate(t *testing.T) { |
| 774 |
require.NoError(t, xconfmap.Validate(hasNested{ |
| 775 |
CouldBe: None[invalid](), |
| 776 |
})) |
| 777 |
require.NoError(t, xconfmap.Validate(hasNested{ |
| 778 |
CouldBe: Default(invalid{}), |
| 779 |
})) |
| 780 |
require.Error(t, xconfmap.Validate(hasNested{ |
| 781 |
CouldBe: Some(invalid{}), |
| 782 |
})) |
| 783 |
} |
| 784 |
|
| 785 |
type validatedConfig struct { |
| 786 |
Default Optional[optionalConfig] `mapstructure:"default"` |
| 787 |
Some Optional[someConfig] `mapstructure:"some"` |
| 788 |
} |
| 789 |
|
| 790 |
var _ xconfmap.Validator = (*optionalConfig)(nil) |
| 791 |
|
| 792 |
type optionalConfig struct { |
| 793 |
StringVal string `mapstructure:"string_val"` |
| 794 |
} |
| 795 |
|
| 796 |
func (n optionalConfig) Validate() error { |
| 797 |
if n.StringVal == "invalid" { |
| 798 |
return errors.New("field `string_val` cannot be set to `invalid`") |
| 799 |
} |
| 800 |
|
| 801 |
return nil |
| 802 |
} |
| 803 |
|
| 804 |
type someConfig struct { |
| 805 |
Nested Optional[optionalConfig] `mapstructure:"nested"` |
| 806 |
} |
| 807 |
|
| 808 |
func newDefaultValidatedConfig() validatedConfig { |
| 809 |
return validatedConfig{ |
| 810 |
Default: Default(optionalConfig{StringVal: "valid"}), |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
func newInvalidDefaultConfig() validatedConfig { |
| 815 |
return validatedConfig{ |
| 816 |
Default: Default(optionalConfig{StringVal: "invalid"}), |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
func TestOptionalFileValidate(t *testing.T) { |
| 821 |
cases := []struct { |
| 822 |
name string |
| 823 |
variant string |
| 824 |
cfg func() validatedConfig |
| 825 |
err error |
| 826 |
}{ |
| 827 |
{ |
| 828 |
name: "valid default with just key set and no subfields", |
| 829 |
variant: "implicit", |
| 830 |
cfg: newDefaultValidatedConfig, |
| 831 |
}, |
| 832 |
{ |
| 833 |
name: "valid default with keys set in default", |
| 834 |
variant: "explicit", |
| 835 |
cfg: newDefaultValidatedConfig, |
| 836 |
}, |
| 837 |
{ |
| 838 |
name: "invalid config", |
| 839 |
variant: "invalid", |
| 840 |
cfg: newDefaultValidatedConfig, |
| 841 |
err: errors.New("default: field `string_val` cannot be set to `invalid`\nsome: nested: field `string_val` cannot be set to `invalid`"), |
| 842 |
}, |
| 843 |
{ |
| 844 |
name: "invalid default throws an error", |
| 845 |
variant: "implicit", |
| 846 |
cfg: newInvalidDefaultConfig, |
| 847 |
err: errors.New("default: field `string_val` cannot be set to `invalid`"), |
| 848 |
}, |
| 849 |
{ |
| 850 |
name: "invalid default does not throw an error when key is not set", |
| 851 |
variant: "no_default", |
| 852 |
cfg: newInvalidDefaultConfig, |
| 853 |
}, |
| 854 |
{ |
| 855 |
name: "invalid default invalid default does not throw an error when the value is overridden", |
| 856 |
variant: "explicit", |
| 857 |
cfg: newInvalidDefaultConfig, |
| 858 |
}, |
| 859 |
} |
| 860 |
|
| 861 |
for _, tt := range cases { |
| 862 |
t.Run(tt.name, func(t *testing.T) { |
| 863 |
conf, err := confmaptest.LoadConf(fmt.Sprintf("testdata/validate_%s.yaml", tt.variant)) |
| 864 |
require.NoError(t, err) |
| 865 |
|
| 866 |
cfg := tt.cfg() |
| 867 |
|
| 868 |
err = conf.Unmarshal(&cfg) |
| 869 |
require.NoError(t, err) |
| 870 |
|
| 871 |
err = xconfmap.Validate(cfg) |
| 872 |
if tt.err == nil { |
| 873 |
require.NoError(t, err) |
| 874 |
} else { |
| 875 |
require.EqualError(t, err, tt.err.Error()) |
| 876 |
} |
| 877 |
}) |
| 878 |
} |
| 879 |
} |