Skip to content

Stringify non-string map keys in ToStringMapInt/Int64 reflect path - #359

Open
nikolauspschuetz wants to merge 1 commit into
spf13:masterfrom
nikolauspschuetz:fix-tostringmapint-non-string-key-panic
Open

Stringify non-string map keys in ToStringMapInt/Int64 reflect path#359
nikolauspschuetz wants to merge 1 commit into
spf13:masterfrom
nikolauspschuetz:fix-tostringmapint-non-string-key-panic

Conversation

@nikolauspschuetz

Copy link
Copy Markdown

ToStringMapIntE / ToStringMapInt64E panic on a map whose key type isn't string or any (e.g. map[int]int): the reflect fallback in toStringMapIntE does mVal.SetMapIndex(keyVal, ...) into a map[string]T, and reflect rejects the non-string key (value of type int is not assignable to type string).

This stringifies the key with ToString and assigns directly — matching the existing map[any]... cases — and adds a map[int]int regression case to TestStringMapInt.

Verified: the new case fails on current code and passes with the fix; the full cast suite stays green.

Developed with AI assistance (Claude Code); I directed, reviewed, and verified it locally.

The reflect fallback in toStringMapIntE wrote the original key into the
result map via reflect.SetMapIndex, which panics for a map whose key is
not a string (e.g. map[int]int), since the result map is map[string]T.
Convert the key with ToString and assign directly, matching the typed
map[any]... cases, and add a non-string-key test.
@CLAassistant

CLAassistant commented Aug 26, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@nikolauspschuetz
nikolauspschuetz marked this pull request as ready for review August 26, 2026 19:56
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.

2 participants