Documentation ¶
Overview ¶
Package gconv 提供了强大且方便的任意类型变量转换功能。
本包应尽量减少对其他包的依赖。
Index ¶
- Variables
- func MapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) error
- func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error
- func Scan(params interface{}, pointer interface{}, ...) (err error)
- func ScanList(structSlice interface{}, structSlicePointer interface{}, bindToAttrName string, ...) (err error)
- func SliceAny别名(值 interface{}) []interface{}
- func SliceFloat32别名(any interface{}) []float32
- func SliceFloat64别名(any interface{}) []float64
- func SliceFloat别名(any interface{}) []float64
- func SliceIet32别名(any interface{}) []int32
- func SliceIne别名(any interface{}) []int
- func SliceInt64别名(any interface{}) []int64
- func SliceMapDeep别名(any interface{}) []map[string]interface{}
- func SliceMap别名(值 interface{}, option ...MapOption) []map[string]interface{}
- func SliceStruct别名(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
- func SliceStr别名(any interface{}) []string
- func SliceUint32别名(any interface{}) []uint32
- func SliceUint64别名(any interface{}) []uint64
- func SliceUint别名(any interface{}) []uint
- func String(any interface{}) string
- func Struct(params interface{}, pointer interface{}, ...) (err error)
- func StructTag(params interface{}, pointer interface{}, priorityTag string) (err error)
- func Structs(params interface{}, pointer interface{}, ...) (err error)
- func StructsTag(params interface{}, pointer interface{}, priorityTag string) (err error)
- func X取Map(值 interface{}, 选项 ...MapOption) map[string]interface{}
- func X取Map_递归(值 interface{}, 值标签 ...string) map[string]interface{}
- func X取Map数组(值 interface{}, 选项 ...MapOption) []map[string]interface{}
- func X取Map数组_递归(值 interface{}, 值标签 ...string) []map[string]interface{}
- func X取any指针(值 interface{}) *interface{}
- func X取any数组(值 interface{}) []interface{}
- func X取gtime时间类(值 interface{}, 格式 ...string) *时间类.Time
- func X取字符(值 interface{}) rune
- func X取字符数组(值 interface{}) []rune
- func X取字节(值 interface{}) byte
- func X取字节集(值 interface{}) []byte
- func X取小数32位(值 interface{}) float32
- func X取小数32位指针(值 interface{}) *float32
- func X取小数32位数组(值 interface{}) []float32
- func X取小数64位(值 interface{}) float64
- func X取小数64位指针(值 interface{}) *float64
- func X取小数64位数组(值 interface{}) []float64
- func X取小数数组(值 interface{}) []float64
- func X取布尔(值 interface{}) bool
- func X取布尔指针(值 interface{}) *bool
- func X取整数(值 interface{}) int
- func X取整数16位(值 interface{}) int16
- func X取整数16位指针(值 interface{}) *int16
- func X取整数32位(值 interface{}) int32
- func X取整数32位指针(值 interface{}) *int32
- func X取整数32位数组(值 interface{}) []int32
- func X取整数64位(值 interface{}) int64
- func X取整数64位指针(值 interface{}) *int64
- func X取整数64位数组(值 interface{}) []int64
- func X取整数8位(值 interface{}) int8
- func X取整数8位指针(值 interface{}) *int8
- func X取整数指针(值 interface{}) *int
- func X取整数数组(值 interface{}) []int
- func X取文本Map(值 interface{}, 选项 ...MapOption) map[string]string
- func X取文本Map_递归(值 interface{}, 值标签 ...string) map[string]string
- func X取文本指针(值 interface{}) *string
- func X取文本数组(any interface{}) []string
- func X取时长(值 interface{}) time.Duration
- func X取时间(any interface{}, 格式 ...string) time.Time
- func X取正整数(值 interface{}) uint
- func X取正整数16位(值 interface{}) uint16
- func X取正整数16位指针(值 interface{}) *uint16
- func X取正整数32位(值 interface{}) uint32
- func X取正整数32位指针(值 interface{}) *uint32
- func X取正整数32位数组(值 interface{}) []uint32
- func X取正整数64位(值 interface{}) uint64
- func X取正整数64位指针(值 interface{}) *uint64
- func X取正整数64位数组(值 interface{}) []uint64
- func X取正整数8位(值 interface{}) uint8
- func X取正整数8位指针(值 interface{}) *uint8
- func X取正整数指针(值 interface{}) *uint
- func X取正整数数组(any interface{}) []uint
- func X字节集到文本_非安全(字节集 []byte) string
- func X按参考值类型转换(值 interface{}, 参考值 interface{}, extraParams ...interface{}) interface{}
- func X按名称转换(值 interface{}, 类型名称 string, extraParams ...interface{}) interface{}
- func X文本到字节集_非安全(文本 string) []byte
- func X转换器注册(fn interface{}) (错误 error)
- type MapOption
Constants ¶
This section is empty.
Variables ¶
var ( // StructTagPriority 定义了 Map*/Struct* 函数的默认优先级标签。 // 注意,`gconv/param` 标签在旧版本包中使用。 // 强烈建议在未来使用简写标签 `c/p` 代替。 StructTagPriority = []string{ gtag.GConv, gtag.Param, gtag.GConvShort, gtag.ParamShort, gtag.Json, } )
Functions ¶
func MapToMap ¶
MapToMap 使用 reflect 将任意类型的 map 变量 `params` 转换为另一种 map 类型变量 `pointer` 详细实现请参考 doMapToMap 函数。
func Scan ¶
func Scan(params interface{}, pointer interface{}, paramKeyToAttrMap ...map[string]string) (err error)
Scan 自动检查 `pointer` 的类型并将 `params` 转换为 `pointer`。它支持将 `params` 转换为以下类型的 `pointer`: *map/*[]map/*[]*map/*struct/**struct/*[]struct/*[]*struct。
如果 `pointer` 类型为 *map,Scan 内部会调用函数 `doMapToMap` 进行转换。 如果 `pointer` 类型为 *[]map 或 *[]*map,Scan 内部会调用函数 `doMapToMaps` 进行转换。 如果 `pointer` 类型为 *struct 或 **struct,Scan 内部会调用函数 `doStruct` 进行转换。 如果 `pointer` 类型为 *[]struct 或 *[]*struct,Scan 内部会调用函数 `doStructs` 进行转换。
func ScanList ¶
func ScanList(structSlice interface{}, structSlicePointer interface{}, bindToAttrName string, relationAttrNameAndFields ...string) (err error)
ScanList 将 `structSlice` 转换为包含其他复杂结构体属性的结构体切片。 注意,参数 `structSlicePointer` 应该是 *[]struct 或 *[]*struct 类型。
使用示例 1:普通属性结构体关联:
定义 EntityUser 结构体,包含 Uid 和 Name 属性 定义 EntityUserDetail 结构体,包含 Uid 和 Address 属性 定义 EntityUserScores 结构体,包含 Id、Uid、Score 和 Course 属性 定义 Entity 结构体,包含 User(指向 EntityUser 的指针)、UserDetail(指向 EntityUserDetail 的指针)和 UserScores(EntityUserScores 的指针切片) var users []*Entity var userRecords = EntityUser{Uid: 1, Name:"john"} var detailRecords = EntityUserDetail{Uid: 1, Address: "chengdu"} var scoresRecords = EntityUserScores{Id: 1, Uid: 1, Score: 100, Course: "math"} ScanList(userRecords, &users, "User") ScanList(detailRecords, &users, "User", "uid") ScanList(scoresRecords, &users, "UserScores", "User", "uid:Uid") ScanList(scoresRecords, &users, "UserScores", "User", "uid")
使用示例 2:嵌入式属性结构体关联:
重新定义 EntityUser、EntityUserDetail 和 EntityUserScores 结构体 定义 Entity 结构体,其中包含嵌入的 EntityUser、UserDetail(EntityUserDetail 类型)和 UserScores(EntityUserScores 切片类型) var userRecords = EntityUser{Uid: 1, Name:"john"} var detailRecords = EntityUserDetail{Uid: 1, Address: "chengdu"} var scoresRecords = EntityUserScores{Id: 1, Uid: 1, Score: 100} ScanList(userRecords, &users) ScanList(detailRecords, &users, "UserDetail", "uid") ScanList(scoresRecords, &users, "UserScores", "uid")
示例代码中的 "User/UserDetail/UserScores" 参数用于指定当前结果将绑定的目标属性结构体。
示例代码中的 "uid" 是结果中的表字段名,而 "Uid" 是相关结构体属性名——不是目标绑定的属性名。在示例中,它是实体 "Entity" 中 "User" 的属性名 "Uid"。它会根据给定的 `relation` 参数自动计算 HasOne/HasMany 关系。
为了清晰理解此函数,请参阅示例或单元测试用例。
func SliceMapDeep别名 ¶
func SliceMapDeep别名(any interface{}) []map[string]interface{}
SliceMapDeep 是 MapsDeep 的别名。 已弃用:请改用 SliceMap。
func SliceMap别名 ¶
SliceMap 是 Maps 的别名。
func SliceStruct别名 ¶
func SliceStruct别名(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
SliceStruct 是 Structs 的别名。
func Struct ¶
func Struct(params interface{}, pointer interface{}, paramKeyToAttrMap ...map[string]string) (err error)
Struct 将参数键值对映射到相应结构体对象的属性上。 第三个参数 `mapping` 是可选的,表示自定义键名与属性名(大小写敏感)之间的映射规则。 注意:
- `params` 可以是任何类型的 map 或 struct,通常为 map 类型。
- `pointer` 应为 *struct/**struct 类型,即指向结构体对象或结构体指针的指针。
- 只有结构体对象的公共属性可以被映射。
- 如果 `params` 是一个 map,map 的键可以是小写的。 在映射过程中,会自动将键的首字母转换为大写进行匹配。 如果不匹配,则忽略该 map 键。
func StructTag ¶
StructTag 结构体在 Struct 的基础上增加了支持优先级标签功能,该功能用于获取 `params` 键值对中指定的标签,并映射到结构体属性名称。 参数 `priorityTag` 支持多个标签,多个标签之间可以通过字符 ',' 连接。
func Structs ¶
func Structs(params interface{}, pointer interface{}, paramKeyToAttrMap ...map[string]string) (err error)
Structs 将任何切片转换为给定的结构体切片。 也可以参考 Scan, Struct。
func StructsTag ¶
StructsTag的行为类似于Structs,但增加了对优先级标签功能的支持,该功能用于获取指定的标签,以便将`params`键值对映射到结构体属性名称。 参数`priorityTag`支持多个标签,这些标签可以使用字符','连接。
func X取Map ¶
Map 将任意变量 `value` 转换为 map[string]interface{} 类型。如果参数 `value` 不是 map、struct 或 *struct 类型,那么转换将会失败并返回 nil。
如果 `value` 是 struct 或 *struct 对象,第二个参数 `tags` 指定了优先检测的标签, 否则将按照 gconv、json、字段名的顺序进行检测。
func X取Map_递归 ¶
MapDeep 递归地执行 Map 函数,这意味着如果 `value` 的属性也是一个结构体(struct/*struct),则对该属性调用 Map 函数,并将其转换为 map[string]interface{} 类型的变量。 注意:已弃用,请改用 Map。
func X取Map数组 ¶
Maps 将 `value` 转换为 []map[string]interface{} 类型。 注意,如果 `value` 为 string 或 []byte 类型,它会自动检查并转换为 json 字符串到 []map。
func X取Map数组_递归 ¶
MapsDeep 递归地将`value`转换为[]map[string]interface{}。
TODO 完全实现对所有类型的递归转换。 废弃: 请改用Maps方法。
func X取gtime时间类 ¶
func X取gtime时间类(值 interface{}, 格式 ...string) *时间类.Time
GTime 将 `any` 类型转换为 *gtime.Time 类型。 参数 `format` 可用于指定 `any` 的格式。 它将返回与 formats 切片中第一个格式匹配的转换后的值。 如果未提供 `format`,当 `any` 为数值类型时,使用 gtime.NewFromTimeStamp 进行转换; 当 `any` 为字符串类型时,则使用 gtime.StrToTime 进行转换。
func X取布尔 ¶
func X取布尔(值 interface{}) bool
Bool将`any`转换为布尔值。 当`any`为:false、空字符串、0、"false"、"off"、"no"或空切片/映射时,返回false。
func X取文本Map_递归 ¶
MapStrStrDeep递归地将`value`转换为map[string]string类型。 注意,这种映射类型转换可能会涉及数据复制。 废弃: 请改用MapStrStr。
func X取时长 ¶
Duration 将 `any` 转换为 time.Duration 类型。 如果 `any` 是字符串,那么它会使用 time.ParseDuration 来进行转换。 如果 `any` 是数字类型,则将 `any` 视为纳秒进行转换。
func X字节集到文本_非安全 ¶
UnsafeBytesToStr 将 []byte 类型转换为 string 类型,且不进行内存拷贝操作。 注意:只有当你完全确定在未来绝不会再使用变量 `b` 时, 才可以使用这个不安全的函数以实现高性能的类型转换。
func X按参考值类型转换 ¶
func X按参考值类型转换(值 interface{}, 参考值 interface{}, extraParams ...interface{}) interface{}
ConvertWithRefer 将变量 `fromValue` 转换为由值 `referValue` 所引用的类型。
可选参数 `extraParams` 用于提供本次转换所需的额外必要参数。 它支持基于类型名称字符串的基本常见类型的转换。
func X按名称转换 ¶
func X按名称转换(值 interface{}, 类型名称 string, extraParams ...interface{}) interface{}
Convert 将变量 `fromValue` 转换为类型 `toTypeName`,其中 `toTypeName` 由字符串指定。
可选参数 `extraParams` 用于提供此次转换所需的额外必要参数。 它支持基于类型名称字符串的基本常见类型的转换。
func X文本到字节集_非安全 ¶
UnsafeStrToBytes 将字符串转换为 []byte,不进行内存拷贝。 注意,如果你确定在将来绝对不会使用 `s` 变量, 你可以使用这个不安全函数来实现高性能的类型转换。
Types ¶
Source Files ¶
- gconv.go
- gconv_convert.go
- gconv_converter.go
- gconv_float.go
- gconv_int.go
- gconv_interface.go
- gconv_map.go
- gconv_maps.go
- gconv_maptomap.go
- gconv_maptomaps.go
- gconv_ptr.go
- gconv_scan.go
- gconv_slice_any.go
- gconv_slice_float.go
- gconv_slice_int.go
- gconv_slice_str.go
- gconv_slice_uint.go
- gconv_struct.go
- gconv_structs.go
- gconv_time.go
- gconv_uint.go
- gconv_unsafe.go