Documentation ¶
Overview ¶
Package gconv implements powerful and convenient converting functionality for any types of variables.
This package should keep much fewer dependencies with other packages.
Index ¶
- func Bool(any interface{}) bool
- func Byte(any interface{}) byte
- func Bytes(any interface{}) []byte
- func Convert(fromValue interface{}, toTypeName string, extraParams ...interface{}) interface{}
- func ConvertWithRefer(fromValue interface{}, referValue interface{}, extraParams ...interface{}) interface{}
- func Duration(any interface{}) time.Duration
- func Float32(any interface{}) float32
- func Float32s(any interface{}) []float32
- func Float64(any interface{}) float64
- func Float64s(any interface{}) []float64
- func Floats(any interface{}) []float64
- func GTime(any interface{}, format ...string) *gtime.Time
- func Int(any interface{}) int
- func Int16(any interface{}) int16
- func Int32(any interface{}) int32
- func Int32s(any interface{}) []int32
- func Int64(any interface{}) int64
- func Int64s(any interface{}) []int64
- func Int8(any interface{}) int8
- func Interfaces(any interface{}) []interface{}
- func Ints(any interface{}) []int
- func Map(value interface{}, option ...MapOption) map[string]interface{}
- func MapDeep(value interface{}, tags ...string) map[string]interface{}
- func MapStrStr(value interface{}, option ...MapOption) map[string]string
- func MapStrStrDeep(value interface{}, tags ...string) map[string]string
- func MapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) error
- func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error
- func Maps(value interface{}, option ...MapOption) []map[string]interface{}
- func MapsDeep(value interface{}, tags ...string) []map[string]interface{}
- func PtrAny(any interface{}) *interface{}
- func PtrBool(any interface{}) *bool
- func PtrFloat32(any interface{}) *float32
- func PtrFloat64(any interface{}) *float64
- func PtrInt(any interface{}) *int
- func PtrInt16(any interface{}) *int16
- func PtrInt32(any interface{}) *int32
- func PtrInt64(any interface{}) *int64
- func PtrInt8(any interface{}) *int8
- func PtrString(any interface{}) *string
- func PtrUint(any interface{}) *uint
- func PtrUint16(any interface{}) *uint16
- func PtrUint32(any interface{}) *uint32
- func PtrUint64(any interface{}) *uint64
- func PtrUint8(any interface{}) *uint8
- func RegisterConverter(fn interface{}) (err error)
- func Rune(any interface{}) rune
- func Runes(any interface{}) []rune
- func Scan(srcValue interface{}, dstPointer interface{}, ...) (err error)
- func ScanList(structSlice interface{}, structSlicePointer interface{}, bindToAttrName string, ...) (err error)
- func SliceAny(any interface{}) []interface{}
- func SliceFloat(any interface{}) []float64
- func SliceFloat32(any interface{}) []float32
- func SliceFloat64(any interface{}) []float64
- func SliceInt(any interface{}) []int
- func SliceInt32(any interface{}) []int32
- func SliceInt64(any interface{}) []int64
- func SliceMap(any interface{}, option ...MapOption) []map[string]interface{}
- func SliceMapDeep(any interface{}) []map[string]interface{}
- func SliceStr(any interface{}) []string
- func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
- func SliceUint(any interface{}) []uint
- func SliceUint32(any interface{}) []uint32
- func SliceUint64(any interface{}) []uint64
- func String(any interface{}) string
- func Strings(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 Time(any interface{}, format ...string) time.Time
- func Uint(any interface{}) uint
- func Uint16(any interface{}) uint16
- func Uint32(any interface{}) uint32
- func Uint32s(any interface{}) []uint32
- func Uint64(any interface{}) uint64
- func Uint64s(any interface{}) []uint64
- func Uint8(any interface{}) uint8
- func Uints(any interface{}) []uint
- func UnsafeBytesToStr(b []byte) string
- func UnsafeStrToBytes(s string) []byte
- type IUnmarshalValue
- type MapOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
func Bool(any interface{}) bool
Bool converts `any` to bool. It returns false if `any` is: false, "", 0, "false", "off", "no", empty slice/map.
func Convert ¶
func Convert(fromValue interface{}, toTypeName string, extraParams ...interface{}) interface{}
Convert converts the variable `fromValue` to the type `toTypeName`, the type `toTypeName` is specified by string.
The optional parameter `extraParams` is used for additional necessary parameter for this conversion. It supports common basic types conversion as its conversion based on type name string.
func ConvertWithRefer ¶
func ConvertWithRefer(fromValue interface{}, referValue interface{}, extraParams ...interface{}) interface{}
ConvertWithRefer converts the variable `fromValue` to the type referred by value `referValue`.
The optional parameter `extraParams` is used for additional necessary parameter for this conversion. It supports common basic types conversion as its conversion based on type name string.
func Duration ¶
Duration converts `any` to time.Duration. If `any` is string, then it uses time.ParseDuration to convert it. If `any` is numeric, then it converts `any` as nanoseconds.
func GTime ¶
GTime converts `any` to *gtime.Time. The parameter `format` can be used to specify the format of `any`. It returns the converted value that matched the first format of the formats slice. If no `format` given, it converts `any` using gtime.NewFromTimeStamp if `any` is numeric, or using gtime.StrToTime if `any` is string.
func Interfaces ¶
func Interfaces(any interface{}) []interface{}
Interfaces converts `any` to []interface{}.
func Map ¶
Map converts any variable `value` to map[string]interface{}. If the parameter `value` is not a map/struct/*struct type, then the conversion will fail and returns nil.
If `value` is a struct/*struct object, the second parameter `priorityTagAndFieldName` specifies the most priority priorityTagAndFieldName that will be detected, otherwise it detects the priorityTagAndFieldName in order of: gconv, json, field name.
func MapDeep ¶
MapDeep does Map function recursively, which means if the attribute of `value` is also a struct/*struct, calls Map function on this attribute converting it to a map[string]interface{} type variable. Deprecated: used Map instead.
func MapStrStr ¶
MapStrStr converts `value` to map[string]string. Note that there might be data copy for this map type converting.
func MapStrStrDeep ¶
MapStrStrDeep converts `value` to map[string]string recursively. Note that there might be data copy for this map type converting. Deprecated: used MapStrStr instead.
func MapToMap ¶
MapToMap converts any map type variable `params` to another map type variable `pointer` using reflect. See doMapToMap.
func MapToMaps ¶
MapToMaps converts any slice type variable `params` to another map slice type variable `pointer`. See doMapToMaps.
func Maps ¶
Maps converts `value` to []map[string]interface{}. Note that it automatically checks and converts json string to []map if `value` is string/[]byte.
func MapsDeep ¶
MapsDeep converts `value` to []map[string]interface{} recursively.
TODO completely implement the recursive converting for all types. Deprecated: used Maps instead.
func PtrAny ¶
func PtrAny(any interface{}) *interface{}
PtrAny creates and returns an interface{} pointer variable to this value.
func PtrBool ¶
func PtrBool(any interface{}) *bool
PtrBool creates and returns a bool pointer variable to this value.
func PtrFloat32 ¶
func PtrFloat32(any interface{}) *float32
PtrFloat32 creates and returns a float32 pointer variable to this value.
func PtrFloat64 ¶
func PtrFloat64(any interface{}) *float64
PtrFloat64 creates and returns a float64 pointer variable to this value.
func PtrInt ¶
func PtrInt(any interface{}) *int
PtrInt creates and returns an int pointer variable to this value.
func PtrInt16 ¶
func PtrInt16(any interface{}) *int16
PtrInt16 creates and returns an int16 pointer variable to this value.
func PtrInt32 ¶
func PtrInt32(any interface{}) *int32
PtrInt32 creates and returns an int32 pointer variable to this value.
func PtrInt64 ¶
func PtrInt64(any interface{}) *int64
PtrInt64 creates and returns an int64 pointer variable to this value.
func PtrInt8 ¶
func PtrInt8(any interface{}) *int8
PtrInt8 creates and returns an int8 pointer variable to this value.
func PtrString ¶
func PtrString(any interface{}) *string
PtrString creates and returns a string pointer variable to this value.
func PtrUint ¶
func PtrUint(any interface{}) *uint
PtrUint creates and returns an uint pointer variable to this value.
func PtrUint16 ¶
func PtrUint16(any interface{}) *uint16
PtrUint16 creates and returns an uint16 pointer variable to this value.
func PtrUint32 ¶
func PtrUint32(any interface{}) *uint32
PtrUint32 creates and returns an uint32 pointer variable to this value.
func PtrUint64 ¶
func PtrUint64(any interface{}) *uint64
PtrUint64 creates and returns an uint64 pointer variable to this value.
func PtrUint8 ¶
func PtrUint8(any interface{}) *uint8
PtrUint8 creates and returns an uint8 pointer variable to this value.
func RegisterConverter ¶
func RegisterConverter(fn interface{}) (err error)
RegisterConverter to register custom converter. It must be registered before you use this custom converting feature. It is suggested to do it in boot procedure of the process.
Note:
- The parameter `fn` must be defined as pattern `func(T1) (T2, error)`. It will convert type `T1` to type `T2`.
- The `T1` should not be type of pointer, but the `T2` should be type of pointer.
func Scan ¶
func Scan(srcValue interface{}, dstPointer interface{}, paramKeyToAttrMap ...map[string]string) (err error)
Scan automatically checks the type of `pointer` and converts `params` to `pointer`. It supports `pointer` in type of `*map/*[]map/*[]*map/*struct/**struct/*[]struct/*[]*struct` for converting.
TODO change `paramKeyToAttrMap` to `ScanOption` to be more scalable; add `DeepCopy` option for `ScanOption`.
func ScanList ¶
func ScanList(structSlice interface{}, structSlicePointer interface{}, bindToAttrName string, relationAttrNameAndFields ...string) (err error)
ScanList converts `structSlice` to struct slice which contains other complex struct attributes. Note that the parameter `structSlicePointer` should be type of *[]struct/*[]*struct.
Usage example 1: Normal attribute struct relation:
type EntityUser struct { Uid int Name string } type EntityUserDetail struct { Uid int Address string } type EntityUserScores struct { Id int Uid int Score int Course string } type Entity struct { User *EntityUser UserDetail *EntityUserDetail UserScores []*EntityUserScores }
var users []*Entity var userRecords = EntityUser{Uid: 1, Name:"john"} var detailRecords = EntityUser{Uid: 1, Address: "chengdu"} var scoresRecords = EntityUser{Id: 1, Uid: 1, Score: 100, Course: "math"} ScanList(userRecords, &users, "User") ScanList(userRecords, &users, "User", "uid") ScanList(detailRecords, &users, "UserDetail", "User", "uid:Uid") ScanList(scoresRecords, &users, "UserScores", "User", "uid:Uid") ScanList(scoresRecords, &users, "UserScores", "User", "uid")
Usage example 2: Embedded attribute struct relation:
type EntityUser struct { Uid int Name string } type EntityUserDetail struct { Uid int Address string } type EntityUserScores struct { Id int Uid int Score int } type Entity struct { EntityUser UserDetail EntityUserDetail UserScores []EntityUserScores }
var userRecords = EntityUser{Uid: 1, Name:"john"} var detailRecords = EntityUser{Uid: 1, Address: "chengdu"} var scoresRecords = EntityUser{Id: 1, Uid: 1, Score: 100, Course: "math"} ScanList(userRecords, &users) ScanList(detailRecords, &users, "UserDetail", "uid") ScanList(scoresRecords, &users, "UserScores", "uid")
The parameters "User/UserDetail/UserScores" in the example codes specify the target attribute struct that current result will be bound to.
The "uid" in the example codes is the table field name of the result, and the "Uid" is the relational struct attribute name - not the attribute name of the bound to target. In the example codes, it's attribute name "Uid" of "User" of entity "Entity". It automatically calculates the HasOne/HasMany relationship with given `relation` parameter.
See the example or unit testing cases for clear understanding for this function.
func SliceMapDeep ¶
func SliceMapDeep(any interface{}) []map[string]interface{}
SliceMapDeep is alias of MapsDeep. Deprecated: used SliceMap instead.
func SliceStruct ¶
SliceStruct is alias of Structs.
func String ¶
func String(any interface{}) string
String converts `any` to string. It's most commonly used converting function.
func Struct ¶
func Struct(params interface{}, pointer interface{}, paramKeyToAttrMap ...map[string]string) (err error)
Struct maps the params key-value pairs to the corresponding struct object's attributes. The third parameter `mapping` is unnecessary, indicating the mapping rules between the custom key name and the attribute name(case-sensitive).
Note:
- The `params` can be any type of map/struct, usually a map.
- The `pointer` should be type of *struct/**struct, which is a pointer to struct object or struct pointer.
- Only the public attributes of struct object can be mapped.
- If `params` is a map, the key of the map `params` can be lowercase. It will automatically convert the first letter of the key to uppercase in mapping procedure to do the matching. It ignores the map key, if it does not match.
func StructTag ¶
StructTag acts as Struct but also with support for priority tag feature, which retrieves the specified priorityTagAndFieldName for `params` key-value items to struct attribute names mapping. The parameter `priorityTag` supports multiple priorityTagAndFieldName that can be joined with char ','.
func Structs ¶
func Structs(params interface{}, pointer interface{}, paramKeyToAttrMap ...map[string]string) (err error)
Structs converts any slice to given struct slice. Also see Scan, Struct.
func StructsTag ¶
StructsTag acts as Structs but also with support for priority tag feature, which retrieves the specified priorityTagAndFieldName for `params` key-value items to struct attribute names mapping. The parameter `priorityTag` supports multiple priorityTagAndFieldName that can be joined with char ','.
func UnsafeBytesToStr ¶
UnsafeBytesToStr converts []byte to string without memory copy. Note that, if you completely sure you will never use `b` variable in the feature, you can use this unsafe function to implement type conversion in high performance.
func UnsafeStrToBytes ¶
UnsafeStrToBytes converts string to []byte without memory copy. Note that, if you completely sure you will never use `s` variable in the feature, you can use this unsafe function to implement type conversion in high performance.
Types ¶
type IUnmarshalValue ¶
type IUnmarshalValue = localinterface.IUnmarshalValue
IUnmarshalValue is the interface for custom defined types customizing value assignment. Note that only pointer can implement interface IUnmarshalValue.
type MapOption ¶
type MapOption struct { // Deep marks doing Map function recursively, which means if the attribute of given converting value // is also a struct/*struct, it automatically calls Map function on this attribute converting it to // a map[string]interface{} type variable. Deep bool // OmitEmpty ignores the attributes that has json `omitempty` tag. OmitEmpty bool // Tags specifies the converted map key name by struct tag name. Tags []string }
MapOption specifies the option for map converting.
Source Files ¶
- gconv.go
- gconv_convert.go
- gconv_converter.go
- gconv_float.go
- gconv_int.go
- gconv_map.go
- gconv_maps.go
- gconv_maptomap.go
- gconv_maptomaps.go
- gconv_ptr.go
- gconv_scan.go
- gconv_scan_list.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
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
localinterface
Package localinterface defines some interfaces for converting usage.
|
Package localinterface defines some interfaces for converting usage. |
structcache
Package structcache provides struct and field info cache feature to enhance performance for struct converting.
|
Package structcache provides struct and field info cache feature to enhance performance for struct converting. |