Repository navigation
Convert rebuilt map keys to the map's key type before MapIndex - #1652
Open
januththedev wants to merge 1 commit into
Open
januththedev wants to merge 1 commit into
januththedev wants to merge 1 commit into
Conversation
januththedev
force-pushed
the
fix/named-map-key-index
branch
from
October 3, 2026 10:13
7d0d98b to
41014e7
Compare
Developed with AI assistance, disclosed here per CONTRIBUTING.md.
januththedev
force-pushed
the
fix/named-map-key-index
branch
from
October 3, 2026 11:22
41014e7 to
714b7e2
Compare
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.
A named map key type panics cross-field validation instead of returning "not found"
Description
util.go:237, insidegetStructFieldOKInternal,case reflect.Map:. The function resolves aField[key]namespace. It parseskeyout of the namespace, rebuilds it as a built-in value based oncurrent.Type().Key().Kind(), and passes it straight toreflect.Value.MapIndex:A map key declared as a named type (
type LangCode string) has the samereflect.Kindas its underlying type, so it lands in the matching branch — butreflect.ValueOf(key)yields astring, andstringis not assignable tovalidator.LangCode.MapIndexpanics rather than reporting "not found".Why that's wrong
The function's own contract (
util.go:244-247) is to "return found=false instead of panicking to handle cases like ValidateMap where cross-field validators (required_if, etc.) can't navigate non-struct parents."A named key type is a perfectly ordinary, already-supported map shape — the repo's own test at
validator_test.go:11928uses exactlytype LangCode string; type Label map[LangCode]stringwithdive,keys. Only the namespace-resolution path is missing the conversion. The result is that aValidate.Struct()call takes down the caller's process (an unrecovered panic, noerrorreturned) purely because of a user-declared map key type.Reachable from every cross-field validator —
eqfield,nefield,required_if,excluded_if, and so on — and fromGetStructFieldOK2()/GetStructFieldOKAdvanced2in user-registered custom validators.Reproduction
The fix
A
mapIndexOfhelper that converts the rebuilt key to the map's real key type when they differ, and returns an invalidreflect.Valuewhen the key cannot be represented at all — which the caller already funnels intofound=falseviaExtractType→kind == reflect.Invalid. All 14current.MapIndex(...)call sites route through it.For plain
map[string]…/map[int]…thekey.Type() != ktcheck is false, so behaviour is bit-for-bit unchanged and no conversion cost is added.Tests
TestCrossFieldNamedMapKeyTypeinvalidator_test.go(63 lines), coveringeqfieldandnefieldover a map with a named key type, asserting a clean validation error rather than a panic.--- PASS.go test ./...: 25/25 packagesok, exit 0. Main package 290--- PASS, 0--- FAIL, 1 pre-existing Windows-only skip. Baseline on unmodified HEAD was 25/25 packagesokwith 289 passes and zero pre-existing failures, so the delta is exactly my one new test.go vet ./clean.BenchmarkStructSimpleSuccess24 B/op / 2 allocs/op both before and after). ns/op deltas are machine noise on this shared box, and none of those benchmarks exercise the map path anyway.go test -racecannot run in my environment (gcc: fatal error: cannot execute 'cc1'), a missing C toolchain unrelated to this change.Upstream status
I could not complete this check —
gh issue list,gh pr list,gh api, and a direct fetch toapi.github.comall failed with a TLS handshake timeout from my sandbox. What I can confirm locally: the clone is a single-commit checkout at3487bab, so the defect is present at current upstream HEAD, and the panic is reachable purely from the public API with no unusual configuration. If you know of an existing report, point me at it and I will close this one.Related defects found but deliberately left out of this change
util.go:137— a negative index in a namespace (eqfield=B[-1]) panics withreflect: slice index out of range;arrIdxis never checked for< 0, since the guard above only checks the upper bound.cache.go:225-234—dive,keys,<tags>with noendkeyssilently swallows every remaining tag into thekeyssub-chain instead of panicking withkeysTagNotDefined, sodive,keys,eq=endkeys,min=10loses themin=10(verified). Theendkeysbranch four lines below does panic, so the two are inconsistent.util.go:172-241— the map branch is 14 copy-pasted blocks; any new key kind (uintptr,complex, interface keys) silently falls into thedefaultstring branch and panics rather than degrading tofound=false. My helper is the natural place to collapse that, but it is a larger refactor than this fix should be.