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

Skip to content

feat(util/gconv): Add OmitEmpty and OmitNil options to Scan function#4584

Merged
hailaz merged 7 commits intogogf:masterfrom
LanceAdd:feature/gconv
Jan 16, 2026
Merged

feat(util/gconv): Add OmitEmpty and OmitNil options to Scan function#4584
hailaz merged 7 commits intogogf:masterfrom
LanceAdd:feature/gconv

Conversation

@LanceAdd
Copy link
Member

@LanceAdd LanceAdd commented Jan 1, 2026

改进内容

  • 扩展 ScanOption/StructOption 结构体,添加 OmitEmpty bool 字段:当设置为 true 时,跳过空值(如空字符串、零值等)的赋值;添加 OmitNil bool 字段:当设置为 true 时,跳过 nil 值的赋值;
  • 添加 ScanWithOptions 函数,支持通过 ScanOption 参数使用新选项
  • 原有的 Scan 函数行为完全不变
  • 通过 NewConverter 创建的转换器也支持新功能

使用示例

基本用法

type User struct {
    Name  *string
    Age   int
    Email string
}

type Person struct {
    Name  string
    Age   int
    Email string
}

user := User{Name: nil, Age: 25, Email: ""}
person := Person{Name: "zhangsan", Age: 0, Email: "[email protected]"}

err := gconv.ScanWithOptions(user, &person, gconv.ScanOption{
    OmitEmpty: true,
    OmitNil: true,
})
// 结果: person.Name 保持 "zhangsan",person.Age 变为 25,person.Email 保持 "[email protected]"

后续可以将func Scan(srcValue any, dstPointer any, paramKeyToAttrMap ...map[string]string) (err error)func ScanWithOptions(srcValue any, dstPointer any, option ...ScanOption) (err error)直接用func Scan(srcValue any, dstPointer any, option ...ScanOption) (err error)代替,ScanOption里已经包含了paramKeyToAttrMap map[string]string

- 在 ScanOption 和 StructOption 中添加 OmitEmpty 和 OmitNil 配置项
- 实现空值和 nil 值的忽略转换逻辑
- 新增 ScanWithOptions 函数支持选项参数
- 添加完整的单元测试验证忽略选项功能
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 adds optional value-omission capabilities to the gconv.Scan function by introducing OmitEmpty and OmitNil options. When enabled, OmitEmpty skips assignment of empty values (empty strings, zero values), and OmitNil skips assignment of nil values, allowing destination struct fields to preserve their existing values. The original Scan function behavior remains unchanged for backward compatibility.

Key Changes

  • Added OmitEmpty and OmitNil boolean fields to ScanOption and StructOption structs
  • Introduced ScanWithOptions function to expose the new options to users
  • Implemented early-return logic in bindVarToStructField to skip assignments when omit conditions are met

Reviewed changes

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

File Description
util/gconv/internal/converter/converter_struct.go Added OmitEmpty and OmitNil fields to StructOption; implemented omission logic in bindVarToStructField
util/gconv/internal/converter/converter_scan.go Added OmitEmpty and OmitNil fields to ScanOption; propagated options to StructOption in both Struct and Structs conversion paths
util/gconv/gconv_scan.go Added ScanWithOptions function as new public API; exported ScanOption type alias
util/gconv/gconv_z_unit_scan_omit_test.go Added comprehensive test suite covering OmitEmpty, OmitNil, combined options, and backward compatibility scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

LanceAdd and others added 5 commits January 6, 2026 09:33
- 添加TestScan_AllOmitEmpty函数测试OmitEmpty选项功能
- 验证空值不会覆盖目标结构体的原有值
- 测试字符串、整型和邮箱字段的空值处理逻辑
@gqcn gqcn changed the title feat(gconv): Add OmitEmpty and OmitNil options to Scan function feat(util/gconv): Add OmitEmpty and OmitNil options to Scan function Jan 13, 2026
Copy link
Member

@gqcn gqcn left a comment

Choose a reason for hiding this comment

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

/lgtm

@hailaz hailaz merged commit de9d3c2 into gogf:master Jan 16, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants