|
4 | 4 | package cfggen |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "maps" |
| 8 | + "slices" |
7 | 9 | "testing" |
8 | 10 |
|
9 | 11 | "github.com/stretchr/testify/require" |
@@ -518,6 +520,88 @@ func TestExtractImports_ResolvedReferenceIncludesDefaultOverrideImports(t *testi |
518 | 520 | require.ElementsMatch(t, []string{"go.opentelemetry.io/collector/scraper/scraperhelper", "time"}, result) |
519 | 521 | } |
520 | 522 |
|
| 523 | +func TestCollectCustomDefaultImports(t *testing.T) { |
| 524 | + tests := []struct { |
| 525 | + name string |
| 526 | + metadata *ConfigMetadata |
| 527 | + defaultValue any |
| 528 | + expected []string |
| 529 | + }{ |
| 530 | + { |
| 531 | + name: "nil metadata", |
| 532 | + defaultValue: map[string]any{"timeout": "30s"}, |
| 533 | + }, |
| 534 | + { |
| 535 | + name: "ignored default", |
| 536 | + metadata: &ConfigMetadata{ |
| 537 | + Type: "object", |
| 538 | + GoStruct: GoStructConfig{IgnoreDefault: true}, |
| 539 | + Properties: map[string]*ConfigMetadata{ |
| 540 | + "timeout": {Type: "string", GoType: "time.Duration"}, |
| 541 | + }, |
| 542 | + }, |
| 543 | + defaultValue: map[string]any{"timeout": "30s"}, |
| 544 | + }, |
| 545 | + { |
| 546 | + name: "map schema with additional properties does not inspect entries", |
| 547 | + metadata: &ConfigMetadata{ |
| 548 | + Type: "object", |
| 549 | + AdditionalProperties: &ConfigMetadata{Type: "string", GoType: "time.Duration"}, |
| 550 | + }, |
| 551 | + defaultValue: map[string]any{"timeout": "30s"}, |
| 552 | + }, |
| 553 | + { |
| 554 | + name: "missing property is ignored", |
| 555 | + metadata: &ConfigMetadata{ |
| 556 | + Type: "object", |
| 557 | + Properties: map[string]*ConfigMetadata{}, |
| 558 | + }, |
| 559 | + defaultValue: map[string]any{"timeout": "30s"}, |
| 560 | + }, |
| 561 | + { |
| 562 | + name: "map default imports overridden property types", |
| 563 | + metadata: &ConfigMetadata{ |
| 564 | + Type: "object", |
| 565 | + Properties: map[string]*ConfigMetadata{ |
| 566 | + "timeout": {Type: "string", GoType: "time.Duration"}, |
| 567 | + }, |
| 568 | + }, |
| 569 | + defaultValue: map[string]any{"timeout": "30s"}, |
| 570 | + expected: []string{"time"}, |
| 571 | + }, |
| 572 | + { |
| 573 | + name: "array default imports object item property types", |
| 574 | + metadata: &ConfigMetadata{ |
| 575 | + Type: "array", |
| 576 | + Items: &ConfigMetadata{ |
| 577 | + Type: "object", |
| 578 | + Properties: map[string]*ConfigMetadata{ |
| 579 | + "timestamp": {Type: "string", GoType: "time.Time"}, |
| 580 | + }, |
| 581 | + }, |
| 582 | + }, |
| 583 | + defaultValue: []any{map[string]any{"timestamp": "2026-04-30T00:00:00Z"}}, |
| 584 | + expected: []string{"time"}, |
| 585 | + }, |
| 586 | + { |
| 587 | + name: "array default with non-object item does not inspect entries", |
| 588 | + metadata: &ConfigMetadata{ |
| 589 | + Type: "array", |
| 590 | + Items: &ConfigMetadata{Type: "string", GoType: "time.Duration"}, |
| 591 | + }, |
| 592 | + defaultValue: []any{"30s"}, |
| 593 | + }, |
| 594 | + } |
| 595 | + |
| 596 | + for _, tt := range tests { |
| 597 | + t.Run(tt.name, func(t *testing.T) { |
| 598 | + imports := map[string]bool{} |
| 599 | + require.NoError(t, collectCustomDefaultImports(tt.metadata, tt.defaultValue, imports, "", "")) |
| 600 | + require.ElementsMatch(t, tt.expected, slices.Collect(maps.Keys(imports))) |
| 601 | + }) |
| 602 | + } |
| 603 | +} |
| 604 | + |
521 | 605 | func TestExtractImports_InternalResolvedReferenceIncludesNestedImports(t *testing.T) { |
522 | 606 | md := &ConfigMetadata{ |
523 | 607 | Type: "object", |
@@ -1883,6 +1967,13 @@ func TestFormatBaseValue(t *testing.T) { |
1883 | 1967 | } |
1884 | 1968 |
|
1885 | 1969 | require.Equal(t, `"localhost"`, FormatBaseValue(md, "endpoint", defaultValue("localhost"), "", "")) |
| 1970 | + require.Empty(t, FormatBaseValue( |
| 1971 | + &ConfigMetadata{Type: "string", GoStruct: GoStructConfig{IgnoreDefault: true}}, |
| 1972 | + "endpoint", |
| 1973 | + defaultValue("localhost"), |
| 1974 | + "", |
| 1975 | + "", |
| 1976 | + )) |
1886 | 1977 | } |
1887 | 1978 |
|
1888 | 1979 | func TestFormatDefaultValue_UnsetDefault(t *testing.T) { |
|
0 commit comments