feat(util/gconv): Add OmitEmpty and OmitNil options to Scan function#4584
Merged
hailaz merged 7 commits intogogf:masterfrom Jan 16, 2026
Merged
feat(util/gconv): Add OmitEmpty and OmitNil options to Scan function#4584hailaz merged 7 commits intogogf:masterfrom
hailaz merged 7 commits intogogf:masterfrom
Conversation
- 在 ScanOption 和 StructOption 中添加 OmitEmpty 和 OmitNil 配置项 - 实现空值和 nil 值的忽略转换逻辑 - 新增 ScanWithOptions 函数支持选项参数 - 添加完整的单元测试验证忽略选项功能
Contributor
There was a problem hiding this comment.
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
OmitEmptyandOmitNilboolean fields toScanOptionandStructOptionstructs - Introduced
ScanWithOptionsfunction to expose the new options to users - Implemented early-return logic in
bindVarToStructFieldto 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.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
- 添加TestScan_AllOmitEmpty函数测试OmitEmpty选项功能 - 验证空值不会覆盖目标结构体的原有值 - 测试字符串、整型和邮箱字段的空值处理逻辑
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
改进内容
ScanOption/StructOption结构体,添加OmitEmpty bool字段:当设置为 true 时,跳过空值(如空字符串、零值等)的赋值;添加OmitNil bool字段:当设置为 true 时,跳过 nil 值的赋值;ScanWithOptions函数,支持通过ScanOption参数使用新选项Scan函数行为完全不变NewConverter创建的转换器也支持新功能使用示例
基本用法
后续可以将
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