Lance Add
de9d3c2b3c
feat(util/gconv): Add OmitEmpty and OmitNil options to Scan function (#4584)
## 改进内容
- 扩展 `ScanOption`/`StructOption` 结构体,添加 `OmitEmpty bool` 字段:当设置为 true
时,跳过空值(如空字符串、零值等)的赋值;添加 `OmitNil bool` 字段:当设置为 true 时,跳过 nil 值的赋值;
- 添加 `ScanWithOptions` 函数,支持通过 `ScanOption` 参数使用新选项
- 原有的 `Scan` 函数行为完全不变
- 通过 `NewConverter` 创建的转换器也支持新功能
## 使用示例
### 基本用法
```go
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: "old@example.com"}
err := gconv.ScanWithOptions(user, &person, gconv.ScanOption{
OmitEmpty: true,
OmitNil: true,
})
// 结果: person.Name 保持 "zhangsan",person.Age 变为 25,person.Email 保持 "old@example.com"
```
后续可以将`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`
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-16 10:19:02 +08:00
..
2026-01-16 10:19:02 +08:00
2025-08-28 16:53:19 +08:00
2025-03-06 23:04:26 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-11-10 21:40:35 +08:00
2025-11-10 21:40:35 +08:00
2025-03-06 23:04:26 +08:00
2025-03-06 23:04:26 +08:00
2025-08-28 16:53:19 +08:00
2025-08-29 10:32:30 +08:00
2026-01-16 10:19:02 +08:00
2025-08-28 16:53:19 +08:00
2025-12-08 11:18:45 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-03-06 23:04:26 +08:00
2025-03-14 18:23:07 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2026-01-15 10:21:45 +08:00
2021-10-11 21:41:56 +08:00
2021-01-17 21:46:25 +08:00
2021-01-17 21:46:25 +08:00
2021-01-17 21:46:25 +08:00
2021-01-17 21:46:25 +08:00
2025-08-28 16:53:19 +08:00
2025-12-08 11:18:45 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2024-09-12 21:59:38 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2026-01-15 21:24:35 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2024-05-20 19:40:30 +08:00
2026-01-16 10:19:02 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2025-08-28 16:53:19 +08:00
2024-09-23 16:45:55 +08:00
2025-08-28 16:53:19 +08:00
2024-05-20 19:40:30 +08:00
2025-11-10 21:40:35 +08:00