Skip to content

fix(dict): support generic map types in keys, values, hasKey, pick, omit#2

Open
bejaratommy wants to merge 1 commit intomasterfrom
fix/keys-generic-map-types
Open

fix(dict): support generic map types in keys, values, hasKey, pick, omit#2
bejaratommy wants to merge 1 commit intomasterfrom
fix/keys-generic-map-types

Conversation

@bejaratommy
Copy link
Copy Markdown
Owner

Summary

Fixes Masterminds#455 (upstream: Masterminds#455)

The keys, values, hasKey, pick, and omit functions previously only accepted map[string]interface{}. Passing any other map type (e.g. map[string]string, map[string]int) caused a template execution error.

Changes

  • Updated keys, values, hasKey, pick, and omit in dict.go to use reflect to accept any map type whose key is string
  • Values are surfaced as interface{} in returned collections, consistent with existing behavior
  • Non-string-keyed maps panic with a descriptive message (same pattern as other sprig errors)
  • Added regression tests for each function using map[string]string and map[string]int

Testing

go test ./... -run 'TestKeys|TestValues|TestHasKey|TestPick|TestOmit'
ok  github.com/Masterminds/sprig/v3  0.21s

All existing tests continue to pass. The only pre-existing failure is TestRound (floating-point precision on this platform), which is unrelated to this change.

The keys, values, hasKey, pick, and omit functions previously only
accepted map[string]interface{}. This caused a runtime panic when any
other map type (e.g. map[string]string, map[string]int) was passed.

Use reflect to accept any map with string keys, converting values to
interface{} for the returned collections. Non-map inputs are silently
ignored, consistent with how the rest of the dict functions handle
bad input in templates.

Fixes Masterminds#455

Signed-off-by: Tommy <tommy@bejara.net>
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

Successfully merging this pull request may close these issues.

keys function does not work with map[string]struct

1 participant