Skip to content

Commit

Permalink
make map key case sensitive configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Jun 10, 2024
1 parent dbd3ce0 commit c52104a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.2.0] - 2024-05-02

### Changed

- [Breaking] The map key is case insensitive now. If you would like to keep it case sensitive.
please add konf.WithMapKeyCaseSensitive option (#365).

## [1.1.1] - 2024-05-02

### Fixed
Expand Down
13 changes: 7 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ type Config struct {
nocopy internal.NoCopy[Config]

// Options.
caseSensitive bool
delimiter string
logger *slog.Logger
onStatus func(loader Loader, changed bool, err error)
converter convert.Converter
caseSensitive bool
mapKeyCaseSensitive bool
delimiter string
logger *slog.Logger
onStatus func(loader Loader, changed bool, err error)
converter convert.Converter

// Loaded configuration.
values map[string]any
Expand Down Expand Up @@ -162,7 +163,7 @@ func (c *Config) delim() string {

func (c *Config) transformKeys(m map[string]any) {
if !c.caseSensitive {
maps.TransformKeys(m, strings.ToLower)
maps.TransformKeys(m, strings.ToLower, c.mapKeyCaseSensitive)
}
}

Expand Down
10 changes: 10 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ func TestConfig_Unmarshal(t *testing.T) {
{
description: "config for map",
loaders: []konf.Loader{mapLoader{"Config": "struct"}},
assert: func(config *konf.Config) {
var value map[string]string
assert.NoError(t, config.Unmarshal("", &value))
assert.Equal(t, "struct", value["config"])
},
},
{
description: "config for map (case sensitive)",
loaders: []konf.Loader{mapLoader{"Config": "struct"}},
opts: []konf.Option{konf.WithMapKeyCaseSensitive()},
assert: func(config *konf.Config) {
var value map[string]string
assert.NoError(t, config.Unmarshal("", &value))
Expand Down
10 changes: 7 additions & 3 deletions internal/maps/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

package maps

func TransformKeys(src map[string]interface{}, keyMap func(string) string) {
func TransformKeys(src map[string]interface{}, keyMap func(string) string, mapKeyCaseSensitive bool) {
if src == nil || keyMap == nil {
return
}
for key, value := range src {
if m, ok := value.(map[string]interface{}); ok {
TransformKeys(m, keyMap)
TransformKeys(m, keyMap, mapKeyCaseSensitive)
}
newKey := keyMap(key)
if newKey != key {
delete(src, key)
src[newKey] = Pack(key, value)
if mapKeyCaseSensitive {
src[newKey] = Pack(key, value)
} else {
src[newKey] = value
}
}
}
}
20 changes: 14 additions & 6 deletions internal/maps/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ func TestTransformKeys(t *testing.T) {
t.Parallel()

testcases := []struct {
description string
src map[string]any
keyMap func(string) string
expected map[string]any
description string
src map[string]any
keyMap func(string) string
mapKeyCaseSensitive bool
expected map[string]any
}{
{
description: "nil map",
Expand All @@ -33,7 +34,14 @@ func TestTransformKeys(t *testing.T) {
description: "transform keys",
src: map[string]any{"A": map[string]any{"X": 1, "y": 2}},
keyMap: strings.ToLower,
expected: map[string]any{"a": maps.Pack("A", map[string]any{"x": maps.Pack("X", 1), "y": 2})},
expected: map[string]any{"a": map[string]any{"x": 1, "y": 2}},
},
{
description: "transform keys",
src: map[string]any{"A": map[string]any{"X": 1, "y": 2}},
keyMap: strings.ToLower,
mapKeyCaseSensitive: true,
expected: map[string]any{"a": maps.Pack("A", map[string]any{"x": maps.Pack("X", 1), "y": 2})},
},
}

Expand All @@ -42,7 +50,7 @@ func TestTransformKeys(t *testing.T) {
t.Run(tc.description, func(t *testing.T) {
t.Parallel()

maps.TransformKeys(tc.src, tc.keyMap)
maps.TransformKeys(tc.src, tc.keyMap, tc.mapKeyCaseSensitive)
assert.Equal(t, tc.expected, tc.src)
})
}
Expand Down
7 changes: 7 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ func WithCaseSensitive() Option {
}
}

// WithMapKeyCaseSensitive enables the case sensitivity of the map keys.
func WithMapKeyCaseSensitive() Option {
return func(options *options) {
options.mapKeyCaseSensitive = true
}
}

type (
// Option configures a Config with specific options.
Option func(*options)
Expand Down

0 comments on commit c52104a

Please sign in to comment.