fix(orm): properly handle zero value scanning for pointer destinations #5270
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.
Description
This PR fixes a critical issue in the
getValueInterfacefunction withinsqlxwhere NULL values and zero values from the database were not being correctly scanned into destination struct fields.The Problem
Previously, when unmarshaling database rows into Go structs:
string,int)"", number0,false) had incorrect behavior when scanning into pointer fieldsThe Solution
Enhanced the
getValueInterfacefunction insqlx/orm.goto properly handle:nil) and zero values (allocates pointer to zero value)Tests Added
Comprehensive test coverage added in
sqlx/orm_test.go:1. Pointer Field Handling
TestUnmarshalRowsZeroValueStructPtr: Tests zero values and NULL values in mixed pointer/non-pointer structsTestUnmarshalRowsAllNullStructPtrFields: Tests all-NULL scenarios for pointer-only structs2. SQL Null Types
TestUnmarshalRowsWithSqlNullTypes: Tests allsql.Nulltypes (String, Int64, Float64, Bool) with NULL valuesTestUnmarshalRowsSqlNullWithMixedData: Tests mixed basic types and sql.Null typesTestUnmarshalRowsSqlNullTime: Specialized test forsql.NullTimetype3. Edge Cases and Zero Value Scenarios
TestUnmarshalRowsSqlNullWithEmptyValues: Distinguishes between empty values and NULL valuesTestUnmarshalRowsSqlNullStringEmptyVsNull: Specifically tests empty string vs NULL string handlingTest Coverage
The new tests validate:
nilfor pointer fieldssql.Null*types handle NULL correctlysql.NullTimeChanges
sqlx/orm.go::getValueInterfacefunctionImpact