Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [reflection] Fix panic in IsEmpty when dealing with nil pointer #591

Merged
merged 1 commit into from
Mar 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,5 @@
}
]
},
"generated_at": "2025-03-28T11:37:49Z"
"generated_at": "2025-03-28T15:30:43Z"
}
1 change: 1 addition & 0 deletions changes/20250328152658.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
:bug: `[reflection]` Fix panic in `IsEmpty` when dealing with nil pointer
3 changes: 3 additions & 0 deletions utils/reflection/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ func IsEmpty(value any) bool {
return len(strings.TrimSpace(valueStr)) == 0
}
if valueStrPtr, ok := value.(*string); ok {
if valueStrPtr == nil {
return true
}
return len(strings.TrimSpace(*valueStrPtr)) == 0
}
if valueBool, ok := value.(bool); ok {
Expand Down
8 changes: 8 additions & 0 deletions utils/reflection/reflection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ func TestIsEmpty(t *testing.T) {
isEmpty: true,
differsFromAssertEmpty: true,
},
{
value: (*string)(nil),
isEmpty: true,
},
{
value: field.ToOptionalString(""),
isEmpty: true,
},
{
value: field.ToOptionalString(" "),
isEmpty: true,
Expand Down
Loading