Support any map type in the keys function#482
Open
ricardbejarano wants to merge 1 commit into
Open
Conversation
The keys function was typed as keys(...map[string]interface{}), so Go's
template engine could only pass it dicts of that exact type. Calling keys
on any other map (e.g. map[string]struct{...} or map[string]int) failed
with a type mismatch.
Accept ...interface{} and collect keys reflectively, stringifying each
key. Existing dict (map[string]interface{}) usage is unchanged.
Fixes Masterminds#455
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #455.
Problem
keyswas declared askeys(dicts ...map[string]interface{}). Go'stext/templatereflection can only pass a value to that parameter when it is exactlymap[string]interface{}, so callingkeyson any other map type failed:This is surprising:
keysconceptually works on any map.Fix
keysnow accepts...interface{}and collects the keys reflectively, stringifying each key.dictvalues (map[string]interface{}) behave exactly as before, so existing templates are unaffected; non-map arguments are skipped rather than causing a hard type error.Tests
Added
TestKeysWithTypedMaps, which exerciseskeysonmap[string]struct{...}andmap[string]intthrough the template engine. The existingTestKeyscases (which coverdict/map[string]interface{}) continue to pass unchanged.Docs
Updated
docs/dicts.mdto note thatkeysaccepts any map type, not onlydict.CHANGELOG.mdis compiled at release time keyed by the merged PR number, so I've left it untouched.