Files
gf/util/gconv/gconv_scan.go

60 lines
2.4 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gconv
// Scan automatically checks the type of `pointer` and converts `params` to `pointer`.
// It supports various types of parameter conversions, including:
// 1. Basic types (int, string, float, etc.)
// 2. Pointer types
// 3. Slice types
// 4. Map types
// 5. Struct types
//
// The `paramKeyToAttrMap` parameter is used for mapping between attribute names and parameter keys.
// TODO: change `paramKeyToAttrMap` to `ScanOption` to be more scalable; add `DeepCopy` option for `ScanOption`.
func Scan(srcValue any, dstPointer any, paramKeyToAttrMap ...map[string]string) (err error) {
option := ScanOption{
ContinueOnError: true,
}
2023-11-22 21:05:39 +08:00
if len(paramKeyToAttrMap) > 0 {
option.ParamKeyToAttrMap = paramKeyToAttrMap[0]
2021-11-05 01:15:11 +08:00
}
return defaultConverter.Scan(srcValue, dstPointer, option)
2021-11-05 01:15:11 +08:00
}
// ScanWithOptions automatically checks the type of `dstPointer` and converts `srcValue` to `dstPointer`.
// It is the same as Scan function, but accepts one or more ScanOption values for additional conversion control.
//
// When using ScanWithOptions, the term "omit" means that the assignment from the source to the destination
// is skipped, so the existing value in the destination field is preserved.
//
// - option.OmitEmpty, when set to true, skips assignment of empty source values (for example: empty strings,
// zero numeric values, zero time values, empty slices or maps), preserving any existing non-empty values
// in the destination.
//
// - option.OmitNil, when set to true, skips assignment of nil source values, preserving the existing values
// in the destination when the source contains nil.
//
// Example:
//
// type User struct {
// Name string
// Email string
// }
//
// dst := &User{Name: "Alice", Email: "alice@example.com"}
// src := map[string]any{
// "Name": "",
// "Email": nil,
// }
//
// // With OmitEmpty and OmitNil, empty and nil values in src will not overwrite dst.
// err := ScanWithOptions(src, dst, ScanOption{OmitEmpty: true, OmitNil: true})
func ScanWithOptions(srcValue any, dstPointer any, option ...ScanOption) (err error) {
return defaultConverter.Scan(srcValue, dstPointer, option...)
}