Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case-insensitively merging template maps #15351

Merged
merged 2 commits into from
Aug 11, 2024
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
55 changes: 55 additions & 0 deletions util/templates/merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package templates

import (
"reflect"
"strings"
)

// https://github.com/peterbourgon/mergemap

const mergeMaxDepth = 100

var matchKey = strings.EqualFold

// mergeMaps recursively merges other into target using matchKey for key comparison
func mergeMaps(other map[string]any, target map[string]any) error {
// return mergo.Map(&target, other, mergo.WithOverride)
// return util.DecodeOther(other, target)
merge(target, other, 0)
return nil
}

func merge(dst, src map[string]any, depth int) map[string]any {
if depth > mergeMaxDepth {
panic("too deep!")
}
for key, srcVal := range src {
for k := range dst {
if matchKey(k, key) {
// overwrite key
key = k

srcMap, srcMapOk := mapify(srcVal)
dstMap, dstMapOk := mapify(dst[k])
if srcMapOk && dstMapOk {
srcVal = merge(dstMap, srcMap, depth+1)
}
break
}
}
dst[key] = srcVal
}
return dst
}

func mapify(i any) (map[string]any, bool) {
value := reflect.ValueOf(i)
if value.Kind() == reflect.Map {
m := map[string]any{}
for _, k := range value.MapKeys() {
m[k.String()] = value.MapIndex(k).Interface()
}
return m, true
}
return map[string]any{}, false
}
32 changes: 32 additions & 0 deletions util/templates/merge_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package templates

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestMergeMaps(t *testing.T) {
target := map[string]any{
"foo": "bar",
"nested": map[string]any{
"bar": "baz",
},
}
other := map[string]any{
"Foo": 1,
"Nested": map[string]any{
"Bar": 2,
},
"baz": 3,
}

require.NoError(t, mergeMaps(other, target))
require.Equal(t, map[string]any{
"foo": 1,
"nested": map[string]any{
"bar": 2,
},
"baz": 3,
}, target)
}
5 changes: 2 additions & 3 deletions util/templates/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"strings"
"text/template"

"github.com/evcc-io/evcc/util"
"github.com/go-sprout/sprout"
)

Expand Down Expand Up @@ -267,9 +266,9 @@ func (t *Template) RenderProxyWithValues(values map[string]interface{}, lang str
}

// RenderResult renders the result template to instantiate the proxy
func (t *Template) RenderResult(renderMode int, other map[string]interface{}) ([]byte, map[string]interface{}, error) {
func (t *Template) RenderResult(renderMode int, other map[string]any) ([]byte, map[string]any, error) {
values := t.Defaults(renderMode)
if err := util.DecodeOther(other, &values); err != nil {
if err := mergeMaps(other, values); err != nil {
return nil, values, err
}

Expand Down