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(anyInput any) bool
- func Byte(anyInput any) byte
- func Bytes(anyInput any) []byte
- func Convert(fromValue any, toTypeName string, extraParams ...any) any
- func ConvertWithRefer(fromValue any, referValue any, extraParams ...any) any
- func Duration(anyInput any) time.Duration
- func Float32(anyInput any) float32
- func Float32s(anyInput any) []float32
- func Float64(anyInput any) float64
- func Float64s(anyInput any) []float64
- func Floats(anyInput any) []float64
- func GTime(anyInput any, format ...string) *gtime.Time
- func Int(anyInput any) int
- func Int16(anyInput any) int16
- func Int32(anyInput any) int32
- func Int32s(anyInput any) []int32
- func Int64(anyInput any) int64
- func Int64s(anyInput any) []int64
- func Int8(anyInput any) int8
- func Interfaces(anyInput any) []any
- func Ints(anyInput any) []int
- func Map(value any, option ...MapOption) map[string]any
- func MapDeep(value any, tags ...string) map[string]any
- func MapStrStr(value any, option ...MapOption) map[string]string
- func MapStrStrDeep(value any, tags ...string) map[string]string
- func MapToMap(params any, pointer any, mapping ...map[string]string) error
- func MapToMaps(params any, pointer any, mapping ...map[string]string) error
- func Maps(value any, option ...MapOption) []map[string]any
- func MapsDeep(value any, tags ...string) []map[string]any
- func PtrAny(anyInput any) *any
- func PtrBool(anyInput any) *bool
- func PtrFloat32(anyInput any) *float32
- func PtrFloat64(anyInput any) *float64
- func PtrInt(anyInput any) *int
- func PtrInt16(anyInput any) *int16
- func PtrInt32(anyInput any) *int32
- func PtrInt64(anyInput any) *int64
- func PtrInt8(anyInput any) *int8
- func PtrString(anyInput any) *string
- func PtrUint(anyInput any) *uint
- func PtrUint16(anyInput any) *uint16
- func PtrUint32(anyInput any) *uint32
- func PtrUint64(anyInput any) *uint64
- func PtrUint8(anyInput any) *uint8
- func RegisterAnyConverterFunc(f AnyConvertFunc, types ...reflect.Type)
- func RegisterConverter(fn any) (err error)
- func RegisterTypeConverterFunc(fn any) (err error)
- func Rune(anyInput any) rune
- func Runes(anyInput any) []rune
- func Scan(srcValue any, dstPointer any, paramKeyToAttrMap ...map[string]string) (err error)
- func ScanList(structSlice any, structSlicePointer any, bindToAttrName string, ...) (err error)
- func SliceAny(anyInput any) []any
- func SliceFloat(anyInput any) []float64
- func SliceFloat32(anyInput any) []float32
- func SliceFloat64(anyInput any) []float64
- func SliceInt(anyInput any) []int
- func SliceInt32(anyInput any) []int32
- func SliceInt64(anyInput any) []int64
- func SliceMap(anyInput any, option ...MapOption) []map[string]any
- func SliceMapDeep(anyInput any) []map[string]any
- func SliceStr(anyInput any) []string
- func SliceStruct(params any, pointer any, mapping ...map[string]string) (err error)
- func SliceUint(anyInput any) []uint
- func SliceUint32(anyInput any) []uint32
- func SliceUint64(anyInput any) []uint64
- func String(anyInput any) string
- func Strings(anyInput any) []string
- func Struct(params any, pointer any, paramKeyToAttrMap ...map[string]string) (err error)
- func StructTag(params any, pointer any, priorityTag string) (err error)
- func Structs(params any, pointer any, paramKeyToAttrMap ...map[string]string) (err error)
- func StructsTag(params any, pointer any, priorityTag string) (err error)
- func Time(anyInput any, format ...string) time.Time
- func Uint(anyInput any) uint
- func Uint16(anyInput any) uint16
- func Uint32(anyInput any) uint32
- func Uint32s(anyInput any) []uint32
- func Uint64(anyInput any) uint64
- func Uint64s(anyInput any) []uint64
- func Uint8(anyInput any) uint8
- func Uints(anyInput any) []uint
- func UnsafeBytesToStr(b []byte) string
- func UnsafeStrToBytes(s string) []byte
- type AnyConvertFunc
- type ConvertOption
- type Converter
- type ConverterForBasic
- type ConverterForConvert
- type ConverterForFloat
- type ConverterForInt
- type ConverterForMap
- type ConverterForRegister
- type ConverterForSlice
- type ConverterForStruct
- type ConverterForTime
- type ConverterForUint
- type IUnmarshalValue
- type MapOption
- type ScanOption
- type SliceMapOption
- type SliceOption
- type StructOption
- type StructsOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bool ¶
Bool converts `any` to bool. It returns false if `any` is: false, "", 0, "false", "off", "no", empty slice/map.
func Convert ¶
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 ¶ added in v2.5.4
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 Map ¶
Map converts any variable `value` to map[string]any. 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]any 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]any. Note that it automatically checks and converts json string to []map if `value` is string/[]byte.
func MapsDeep ¶
MapsDeep converts `value` to []map[string]any recursively.
TODO completely implement the recursive converting for all types. Deprecated: used Maps instead.
func PtrFloat32 ¶ added in v2.2.1
PtrFloat32 creates and returns a float32 pointer variable to this value.
func PtrFloat64 ¶ added in v2.2.1
PtrFloat64 creates and returns a float64 pointer variable to this value.
func PtrInt16 ¶ added in v2.2.1
PtrInt16 creates and returns an int16 pointer variable to this value.
func PtrInt32 ¶ added in v2.2.1
PtrInt32 creates and returns an int32 pointer variable to this value.
func PtrInt64 ¶ added in v2.2.1
PtrInt64 creates and returns an int64 pointer variable to this value.
func PtrString ¶ added in v2.2.1
PtrString creates and returns a string pointer variable to this value.
func PtrUint16 ¶ added in v2.2.1
PtrUint16 creates and returns an uint16 pointer variable to this value.
func PtrUint32 ¶ added in v2.2.1
PtrUint32 creates and returns an uint32 pointer variable to this value.
func PtrUint64 ¶ added in v2.2.1
PtrUint64 creates and returns an uint64 pointer variable to this value.
func PtrUint8 ¶ added in v2.2.1
PtrUint8 creates and returns an uint8 pointer variable to this value.
func RegisterAnyConverterFunc ¶ added in v2.9.0
func RegisterAnyConverterFunc(f AnyConvertFunc, types ...reflect.Type)
RegisterAnyConverterFunc registers custom type converting function for specified type.
func RegisterConverter ¶ added in v2.5.2
RegisterConverter registers custom converter. Deprecated: use RegisterTypeConverterFunc instead for clear
func RegisterTypeConverterFunc ¶ added in v2.9.0
RegisterTypeConverterFunc registers custom converter.
func Scan ¶
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 ScanList ¶
func ScanList( structSlice any, structSlicePointer any, 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 ¶
SliceMapDeep is alias of MapsDeep. Deprecated: used SliceMap instead.
func SliceStruct ¶
SliceStruct is alias of Structs.
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 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 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 AnyConvertFunc ¶ added in v2.9.0
type AnyConvertFunc = structcache.AnyConvertFunc
AnyConvertFunc is the function type for converting any to specified type.
type ConvertOption ¶ added in v2.9.0
type ConvertOption = converter.ConvertOption
ConvertOption is the option for converting.
type Converter ¶ added in v2.9.0
type Converter interface {
ConverterForConvert
ConverterForRegister
ConverterForInt
ConverterForUint
ConverterForTime
ConverterForFloat
ConverterForMap
ConverterForSlice
ConverterForStruct
ConverterForBasic
}
Converter is the manager for type converting.
func NewConverter ¶ added in v2.9.0
func NewConverter() Converter
NewConverter creates and returns management object for type converting.
type ConverterForBasic ¶ added in v2.9.0
type ConverterForBasic interface {
Scan(srcValue, dstPointer any, option ...ScanOption) (err error)
String(anyInput any) (string, error)
Bool(anyInput any) (bool, error)
Rune(anyInput any) (rune, error)
}
ConverterForBasic is the basic converting interface.
type ConverterForConvert ¶ added in v2.9.0
type ConverterForConvert interface {
ConvertWithRefer(fromValue, referValue any, option ...ConvertOption) (any, error)
ConvertWithTypeName(fromValue any, toTypeName string, option ...ConvertOption) (any, error)
}
ConverterForConvert is the converting interface for custom converting.
type ConverterForFloat ¶ added in v2.9.0
type ConverterForFloat interface {
Float32(v any) (float32, error)
Float64(v any) (float64, error)
}
ConverterForFloat is the converting interface for float.
type ConverterForInt ¶ added in v2.9.0
type ConverterForInt interface {
Int(v any) (int, error)
Int8(v any) (int8, error)
Int16(v any) (int16, error)
Int32(v any) (int32, error)
Int64(v any) (int64, error)
}
ConverterForInt is the converting interface for integer.
type ConverterForMap ¶ added in v2.9.0
type ConverterForMap interface {
Map(v any, option ...MapOption) (map[string]any, error)
MapStrStr(v any, option ...MapOption) (map[string]string, error)
}
ConverterForMap is the converting interface for map.
type ConverterForRegister ¶ added in v2.9.0
type ConverterForRegister interface {
RegisterTypeConverterFunc(f any) error
RegisterAnyConverterFunc(f AnyConvertFunc, types ...reflect.Type)
}
ConverterForRegister is the converting interface for custom converter registration.
type ConverterForSlice ¶ added in v2.9.0
type ConverterForSlice interface {
Bytes(v any) ([]byte, error)
Runes(v any) ([]rune, error)
SliceAny(v any, option ...SliceOption) ([]any, error)
SliceFloat32(v any, option ...SliceOption) ([]float32, error)
SliceFloat64(v any, option ...SliceOption) ([]float64, error)
SliceInt(v any, option ...SliceOption) ([]int, error)
SliceInt32(v any, option ...SliceOption) ([]int32, error)
SliceInt64(v any, option ...SliceOption) ([]int64, error)
SliceUint(v any, option ...SliceOption) ([]uint, error)
SliceUint32(v any, option ...SliceOption) ([]uint32, error)
SliceUint64(v any, option ...SliceOption) ([]uint64, error)
SliceStr(v any, option ...SliceOption) ([]string, error)
SliceMap(v any, option ...SliceMapOption) ([]map[string]any, error)
}
ConverterForSlice is the converting interface for slice.
type ConverterForStruct ¶ added in v2.9.0
type ConverterForStruct interface {
Struct(params, pointer any, option ...StructOption) (err error)
Structs(params, pointer any, option ...StructsOption) (err error)
}
ConverterForStruct is the converting interface for struct.
type ConverterForTime ¶ added in v2.9.0
type ConverterForTime interface {
Time(v any, format ...string) (time.Time, error)
Duration(v any) (time.Duration, error)
GTime(v any, format ...string) (*gtime.Time, error)
}
ConverterForTime is the converting interface for time.
type ConverterForUint ¶ added in v2.9.0
type ConverterForUint interface {
Uint(v any) (uint, error)
Uint8(v any) (uint8, error)
Uint16(v any) (uint16, error)
Uint32(v any) (uint32, error)
Uint64(v any) (uint64, error)
}
ConverterForUint is the converting interface for unsigned integer.
type IUnmarshalValue ¶ added in v2.7.4
type IUnmarshalValue = localinterface.IUnmarshalValue
IUnmarshalValue is the interface for custom defined types customizing value assignment. Note that only pointer can implement interface IUnmarshalValue.
type ScanOption ¶ added in v2.9.0
type ScanOption = converter.ScanOption
ScanOption is the option for the Scan function.
type SliceMapOption ¶ added in v2.9.0
type SliceMapOption = converter.SliceMapOption
SliceMapOption is the option for SliceMap function.
type SliceOption ¶ added in v2.9.0
type SliceOption = converter.SliceOption
SliceOption is the option for Slice type converting.
type StructOption ¶ added in v2.9.0
type StructOption = converter.StructOption
StructOption is the option for Struct converting.
type StructsOption ¶ added in v2.9.0
type StructsOption = converter.StructsOption
StructsOption is the option for Structs function.
Source Files
¶
- gconv.go
- gconv_basic.go
- gconv_convert.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
|
|
|
converter
Package converter provides converting utilities for any types of variables.
|
Package converter provides converting utilities for any types of variables. |
|
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. |