Skip to content

Commit

Permalink
fix case insensitive (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong authored Nov 17, 2023
1 parent c1c436a commit f5b3121
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
10 changes: 7 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,21 @@ func (c *Config) Watch(ctx context.Context) error { //nolint:cyclop,funlen,gocog
"provider", provider.watcher,
)

// Find the onChanges should be triggered.
oldValues := provider.values
// Both oldValues and newValues are merged to empty map to convert to lower case.
oldValues := make(map[string]any)
maps.Merge(oldValues, provider.values)
newValues := make(map[string]any)
maps.Merge(newValues, values)
provider.values = values

// Find the onChanges should be triggered.
onChanges := func() []func(*Config) {
c.onChangesMutex.RLock()
defer c.onChangesMutex.RUnlock()

var callbacks []func(*Config)
for path, onChanges := range c.onChanges {
if sub(oldValues, path, c.delimiter) != nil || sub(values, path, c.delimiter) != nil {
if sub(oldValues, path, c.delimiter) != nil || sub(newValues, path, c.delimiter) != nil {
callbacks = append(callbacks, onChanges...)
}
}
Expand Down
6 changes: 3 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ func TestConfig_Watch(t *testing.T) {
var value string
assert.NoError(t, config.Unmarshal("config", &value))
newValue.Store(value)
})
watcher.change(map[string]any{"config": "changed"})
}, "config")
watcher.change(map[string]any{"Config": "changed"})
assert.Equal(t, "changed", newValue.Load())
}

type mapWatcher chan map[string]any

func (m mapWatcher) Load() (map[string]any, error) {
return map[string]any{"config": "string"}, nil
return map[string]any{"Config": "string"}, nil
}

func (m mapWatcher) Watch(ctx context.Context, fn func(map[string]any)) error {
Expand Down
16 changes: 9 additions & 7 deletions internal/maps/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ func Merge(dst, src map[string]any) {
// Ensure key is lower case since the path is case-insensitive.
key = strings.ToLower(key)

// Direct override if the dstVal is not map[string]any.
dstMap, succeed := dst[key].(map[string]any)
if !succeed {
// Direct override if the srcVal is not map[string]any.
srcMap, srcOk := srcVal.(map[string]any)
if !srcOk {
dst[key] = srcVal

continue
}

// Direct override if the srcVal is not map[string]any.
srcMap, succeed := srcVal.(map[string]any)
if !succeed {
dst[key] = srcVal
// Direct override if the dstVal is not map[string]any.
dstMap, dstOk := dst[key].(map[string]any)
if !dstOk {
values := make(map[string]any)
Merge(values, srcMap)
dst[key] = values

continue
}
Expand Down
6 changes: 6 additions & 0 deletions internal/maps/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ func TestMerge(t *testing.T) {
dst: map[string]any{"a": 1},
expected: map[string]any{"a": map[string]any{"x": 2}},
},
{
description: "mix case",
src: map[string]any{"a": map[string]any{"X": 2}},
dst: map[string]any{},
expected: map[string]any{"a": map[string]any{"x": 2}},
},
}

for _, testcase := range testcases {
Expand Down

0 comments on commit f5b3121

Please sign in to comment.