It seems neither mergo nor mapstructure currently provide this ability. Made a small addition to allow arbitrarily matching map keys:
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
}
Default
var matchKey = func(a, b string) bool {
return strings.Compare(a, b) == 0
}
but also
var matchKey = strings.EqualFold
It seems neither
mergonormapstructurecurrently provide this ability. Made a small addition to allow arbitrarily matching map keys:Default
but also