Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@lerity-yao
Copy link
Contributor

  • Resolve issue where zero values (0, "", false) could not be scanned into pointer fields
  • Ensure proper handling of both NULL values and actual zero values
  • Update pointer field assignment logic in unmarshal/getValueInterface functions

Description

This PR fixes a critical issue in the getValueInterface function within sqlx where 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:

  • NULL values could not be properly assigned to basic non-pointer fields (e.g., string, int)
  • Zero values (empty string "", number 0, false) had incorrect behavior when scanning into pointer fields
  • The distinction between actual NULL values and intentional zero values was not maintained

The Solution

Enhanced the getValueInterface function in sqlx/orm.go to properly handle:

  • Conversion of database NULL values to the zero value of destination basic types
  • Correct allocation and assignment of zero values to pointer fields
  • Proper distinction between NULL (sets pointer to 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 structs
  • TestUnmarshalRowsAllNullStructPtrFields: Tests all-NULL scenarios for pointer-only structs

2. SQL Null Types

  • TestUnmarshalRowsWithSqlNullTypes: Tests all sql.Null types (String, Int64, Float64, Bool) with NULL values
  • TestUnmarshalRowsSqlNullWithMixedData: Tests mixed basic types and sql.Null types
  • TestUnmarshalRowsSqlNullTime: Specialized test for sql.NullTime type

3. Edge Cases and Zero Value Scenarios

  • TestUnmarshalRowsSqlNullWithEmptyValues: Distinguishes between empty values and NULL values
  • TestUnmarshalRowsSqlNullStringEmptyVsNull: Specifically tests empty string vs NULL string handling

Test Coverage

The new tests validate:

  • ✅ NULL → zero value conversion for basic types
  • ✅ NULL → nil for pointer fields
  • ✅ Zero values → allocated pointers with correct values
  • ✅ All sql.Null* types handle NULL correctly
  • ✅ Mixed structs with both pointer and non-pointer fields
  • ✅ Time types with sql.NullTime
  • ✅ Empty strings vs NULL strings distinction

Changes

  1. Core Fix: Refactored sqlx/orm.go::getValueInterface function
  2. Tests: Added 7 comprehensive test functions with multiple test cases
  3. Validation: All existing tests continue to pass

Impact

  • Prevents data loss when scanning NULL values into structs
  • Maintains backward compatibility with existing code
  • Provides robust handling for database NULL semantics

@codecov
Copy link

codecov bot commented Nov 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

- Resolve issue where zero values (0, "", false) could not be scanned into pointer fields
- Ensure proper handling of both NULL values and actual zero values
- Update pointer field assignment logic in unmarshal functions
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR aims to fix zero value and NULL value handling when scanning database rows into Go struct fields with pointer types. The changes modify the getValueInterface function in core/stores/sqlx/orm.go and add comprehensive test coverage for pointer fields, SQL null types, and edge cases involving zero values vs NULL values.

Key Changes:

  • Modified getValueInterface function to change how pointer fields are handled during scanning
  • Added 7 comprehensive test functions covering pointer fields, sql.Null types, and zero/NULL value distinctions
  • Added helper functions stringPtr and int64Ptr for test convenience

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
core/stores/sqlx/orm.go Modified getValueInterface to use value.Addr() for pointer type checks and returns
core/stores/sqlx/orm_test.go Added comprehensive tests for pointer fields, SQL null types, zero values, and helper functions

@kevwan kevwan added this pull request to the merge queue Nov 16, 2025
Merged via the queue into zeromicro:master with commit c706517 Nov 16, 2025
12 checks passed
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