Skip to content

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 函数支持选项参数
- 添加完整的单元测试验证忽略选项功能
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.

1 participant