Documentation ¶
Overview ¶
Package gconv implements powerful and convenient converting functionality for any types of variables.
This package should keep much less dependencies with other packages.
Index ¶
- Variables
- 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 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{}, tags ...string) map[string]interface{}
- func MapDeep(value interface{}, tags ...string) map[string]interface{}
- func MapStrStr(value interface{}, tags ...string) 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 MapToMapsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error
- func Maps(value interface{}, tags ...string) []map[string]interface{}
- func MapsDeep(value interface{}, tags ...string) []map[string]interface{}
- func Rune(any interface{}) rune
- func Runes(any interface{}) []rune
- func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
- func ScanDeep(params interface{}, pointer interface{}, mapping ...map[string]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{}) []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{}, mapping ...map[string]string) (err error)
- func StructDeep(params interface{}, pointer interface{}, mapping ...map[string]string) error
- func StructTag(params interface{}, pointer interface{}, priorityTag string) (err error)
- func Structs(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)
- func StructsDeep(params interface{}, pointer interface{}, mapping ...map[string]string) (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
Constants ¶
This section is empty.
Variables ¶
var ( // StructTagPriority defines the default priority tags for Map*/Struct* functions. // Note, the "gconv", "param", "params" tags are used by old version of package. // It is strongly recommended using short tag "c" or "p" instead in the future. StructTagPriority = []string{"gconv", "param", "params", "c", "p", "json"} )
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 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 Float32s ¶ added in v1.10.1
func Float32s(any interface{}) []float32
Float32s converts `any` to []float32.
func Float64s ¶ added in v1.10.1
func Float64s(any interface{}) []float64
Float64s converts `any` to []float64.
func GTime ¶
GTime converts `any` to *gtime.Time. The parameter `format` can be used to specify the format of `any`. If no `format` given, it converts `any` using gtime.NewFromTimeStamp if `any` is numeric, or using gtime.StrToTime if `any` is string.
func Int32s ¶ added in v1.10.1
func Int32s(any interface{}) []int32
Int32s converts `any` to []int32.
func Int64s ¶ added in v1.10.1
func Int64s(any interface{}) []int64
Int64s converts `any` to []int64.
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 `tags` specifies the most priority tags that will be detected, otherwise it detects the tags 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. Also see Map.
func MapStrStr ¶ added in v1.10.1
MapStrStr converts `value` to map[string]string. Note that there might be data copy for this map type converting.
func MapStrStrDeep ¶ added in v1.10.1
MapStrStrDeep converts `value` to map[string]string recursively. Note that there might be data copy for this map type converting.
func MapToMap ¶ added in v1.9.3
MapToMap converts any map type variable `params` to another map type variable `pointer` using reflect. See doMapToMap.
func MapToMaps ¶ added in v1.9.3
MapToMaps converts any slice type variable `params` to another map slice type variable `pointer`. See doMapToMaps.
func MapToMapsDeep ¶ added in v1.9.3
MapToMapsDeep converts any slice type variable `params` to another map slice type variable `pointer` recursively. Deprecated, use MapToMaps instead.
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.
func Scan ¶ added in v1.13.2
Scan automatically checks the type of `pointer` and converts `params` to `pointer`. It supports `pointer` with type of `*map/*[]map/*[]*map/*struct/**struct/*[]struct/*[]*struct` for converting.
It calls function `doMapToMap` internally if `pointer` is type of *map for converting. It calls function `doMapToMaps` internally if `pointer` is type of *[]map/*[]*map for converting. It calls function `doStruct` internally if `pointer` is type of *struct/**struct for converting. It calls function `doStructs` internally if `pointer` is type of *[]struct/*[]*struct for converting.
func ScanDeep ¶ added in v1.13.2
ScanDeep automatically calls StructDeep or StructsDeep function according to the type of parameter `pointer` to implement the converting.
It calls function StructDeep if `pointer` is type of *struct/**struct to do the converting. It calls function StructsDeep if `pointer` is type of *[]struct/*[]*struct to do the converting. Deprecated, use Scan instead.
func SliceFloat32 ¶ added in v1.10.1
func SliceFloat32(any interface{}) []float32
SliceFloat32 is alias of Float32s.
func SliceFloat64 ¶ added in v1.10.1
func SliceFloat64(any interface{}) []float64
SliceFloat64 is alias of Float64s.
func SliceInt32 ¶ added in v1.10.1
func SliceInt32(any interface{}) []int32
SliceInt32 is alias of Int32s.
func SliceInt64 ¶ added in v1.10.1
func SliceInt64(any interface{}) []int64
SliceInt is alias of Int64s.
func SliceMapDeep ¶
func SliceMapDeep(any interface{}) []map[string]interface{}
SliceMapDeep is alias of MapsDeep.
func SliceStruct ¶
SliceStruct is alias of Structs.
func SliceUint ¶ added in v1.9.7
func SliceUint(any interface{}) []uint
SliceUint is alias of Uints.
func SliceUint32 ¶ added in v1.10.1
func SliceUint32(any interface{}) []uint32
SliceUint32 is alias of Uint32s.
func SliceUint64 ¶ added in v1.10.1
func SliceUint64(any interface{}) []uint64
SliceUint64 is alias of Uint64s.
func String ¶
func String(any interface{}) string
String converts `any` to string. It's most commonly used converting function.
func Struct ¶
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 StructDeep ¶
StructDeep do Struct function recursively. Deprecated, use Struct instead.
func StructTag ¶ added in v1.15.4
StructTag acts as Struct but also with support for priority tag feature, which retrieves the specified tags for `params` key-value items to struct attribute names mapping. The parameter `priorityTag` supports multiple tags that can be joined with char ','.
func StructsDeep ¶
StructsDeep converts any slice to given struct slice recursively. Deprecated, use Structs instead.
func StructsTag ¶ added in v1.15.4
StructsTag acts as Structs but also with support for priority tag feature, which retrieves the specified tags for `params` key-value items to struct attribute names mapping. The parameter `priorityTag` supports multiple tags that can be joined with char ','.
func Uint32s ¶ added in v1.10.1
func Uint32s(any interface{}) []uint32
Uint32s converts `any` to []uint32.
func Uint64s ¶ added in v1.10.1
func Uint64s(any interface{}) []uint64
Uint64s converts `any` to []uint64.
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 ¶
This section is empty.