fix(baked_in): handle "nil" param in excluded_if for pointer fields - #1585
Open
64johnlee wants to merge 1 commit into
Open
fix(baked_in): handle "nil" param in excluded_if for pointer fields#158564johnlee wants to merge 1 commit into
64johnlee wants to merge 1 commit into
Conversation
When a struct field is a pointer-to-numeric type (e.g. *int) and the
excluded_if tag compares it against the literal "nil", extractTypeInternal
dereferences a non-nil pointer to its underlying kind (e.g. reflect.Int).
The switch then called asInt("nil") which panics via strconv.ParseInt.
Fix: add an early `value == "nil"` guard in all numeric and array cases
of requireCheckFieldValue that return false immediately (a non-nil, fully
dereferenced numeric/array value can never equal nil). Also simplify the
reflect.Ptr case: because extractTypeInternal only returns Ptr kind for
nil pointers, the non-nil recursive branch was unreachable and has been
removed.
Closes go-playground#1320
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.
Summary
Fixes #1320 —
excluded_ifpanics withstrconv.ParseInt: parsing "nil": invalid syntaxwhen a tag likeexcluded_if=W nilis used andWis a non-nil pointer-to-numeric type.Root cause
requireCheckFieldValueusesextractTypeInternalto resolve the comparison field, which dereferences non-nil pointers to their underlying type. So a*intfield whose value is non-nil arrives at thereflect.Intswitch case. The function then calledasInt("nil")unconditionally, which panics.The same bug exists for
*uint,*float32,*float64, and*[N]Tcomparisons against"nil".Fix
if value == "nil" { return false }guard in the Int, Uint, Float32, Float64, and Array cases ofrequireCheckFieldValue. A fully-dereferenced numeric/array value can never be nil, so the comparison is always false.reflect.Ptrcase:extractTypeInternalonly returnskind == reflect.Ptrfor nil pointers (non-nil ones are dereferenced to their element kind), so the non-nil recursive branch was dead code and has been removed.Test
Two new sub-cases added to
TestExcludedIf:W *intnon-nil,R *intnon-nil withexcluded_if=W nil— previously panicked, now returns nil error.W *intnil,R *intnil withexcluded_if=W nil— condition fires, R is absent, passes.All existing tests continue to pass (
go test ./...).Co-Authored-By: Claude Sonnet 4.6 noreply@anthropic.com