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

Add map key matcher #9

Open
andig opened this issue Aug 11, 2024 · 0 comments
Open

Add map key matcher #9

andig opened this issue Aug 11, 2024 · 0 comments

Comments

@andig
Copy link

andig commented Aug 11, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant