Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gconfig/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func reduceAny(in any, dimensions []*dimension, dIndex int) (any, error) {
}

func reduce(in map[string]any, dimensions []*dimension, dIndex int) (any, error) {
if len(in) == 0 {
return in, nil // nothing to reduce.
}
if dIndex+1 > len(dimensions) {
return in, nil
}
Expand Down
42 changes: 42 additions & 0 deletions gconfig/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,48 @@ func TestGetters(t *testing.T) {
}
}

func TestRegressions_EmptyObjects(t *testing.T) {
cfg, err := gconfig.NewBuilder().
WithDimension("d1", internal.D1b).
WithDimension("d2", internal.D2e).
FromFile(testFS, "internal/test.yaml")
require.NoError(t, err)

t.Run("emptyObject as empy map", func(t *testing.T) {
result, err := gconfig.Get[map[string]string](cfg, "regressions.emptyObject")
assert.NoError(t, err)
assert.Empty(t, result)
})

t.Run("emptyObject as empty slice", func(t *testing.T) {
result, err := gconfig.Get[[]string](cfg, "regressions.emptyObject")
assert.NoError(t, err)
assert.Empty(t, result)
})

t.Run("anotherEmptyObject as empty map", func(t *testing.T) {
result, err := gconfig.Get[map[string]string](cfg, "regressions.anotherEmptyObject")
assert.NoError(t, err)
assert.Empty(t, result)
})

type structWithValues struct {
Field1 string `yaml:"field1"`
Field2 int `yaml:"field2"`
}
t.Run("anotherEmptyObject as struct", func(t *testing.T) {
result, err := gconfig.Get[structWithValues](cfg, "regressions.anotherEmptyObject")
assert.NoError(t, err)
assert.Equal(t, structWithValues{}, result)
})
t.Run("emptyObject as struct", func(t *testing.T) {
result, err := gconfig.Get[structWithValues](cfg, "regressions.emptyObject")
assert.NoError(t, err)
assert.Equal(t, structWithValues{}, result)
})

}

func testGetter[T any](expected T, defaultV T) tester {
return func(t *testing.T, cfg *gconfig.Config, key string) {
t.Run(fmt.Sprintf("Get[%T]", *new(T)), func(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions gconfig/internal/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ struct:
D1a: "D1a name"
D1b: "D1b name"
default: "default"

regressions:
emptyObject:
anotherEmptyObject: {}