gconv

package
v2.0.0-rc3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 23, 2022 License: MIT Imports: 18 Imported by: 884

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

Constants

This section is empty.

Variables

View Source
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/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 Byte

func Byte(any interface{}) byte

Byte converts `any` to byte.

func Bytes

func Bytes(any interface{}) []byte

Bytes converts `any` to []byte.

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

func Duration(any interface{}) time.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 Float32

func Float32(any interface{}) float32

Float32 converts `any` to float32.

func Float32s

func Float32s(any interface{}) []float32

Float32s converts `any` to []float32.

func Float64

func Float64(any interface{}) float64

Float64 converts `any` to float64.

func Float64s

func Float64s(any interface{}) []float64

Float64s converts `any` to []float64.

func Floats

func Floats(any interface{}) []float64

Floats converts `any` to []float64.

func GTime

func GTime(any interface{}, format ...string) *gtime.Time

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 Int

func Int(any interface{}) int

Int converts `any` to int.

func Int16

func Int16(any interface{}) int16

Int16 converts `any` to int16.

func Int32

func Int32(any interface{}) int32

Int32 converts `any` to int32.

func Int32s

func Int32s(any interface{}) []int32

Int32s converts `any` to []int32.

func Int64

func Int64(any interface{}) int64

Int64 converts `any` to int64.

func Int64s

func Int64s(any interface{}) []int64

Int64s converts `any` to []int64.

func Int8

func Int8(any interface{}) int8

Int8 converts `any` to int8.

func Interfaces

func Interfaces(any interface{}) []interface{}

Interfaces converts `any` to []interface{}.

func Ints

func Ints(any interface{}) []int

Ints converts `any` to []int.

func Map

func Map(value interface{}, tags ...string) map[string]interface{}

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

func MapDeep(value interface{}, tags ...string) map[string]interface{}

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

func MapStrStr(value interface{}, tags ...string) map[string]string

MapStrStr converts `value` to map[string]string. Note that there might be data copy for this map type converting.

func MapStrStrDeep

func MapStrStrDeep(value interface{}, tags ...string) map[string]string

MapStrStrDeep converts `value` to map[string]string recursively. Note that there might be data copy for this map type converting.

func MapToMap

func MapToMap(params interface{}, pointer interface{}, mapping ...map[string]string) error

MapToMap converts any map type variable `params` to another map type variable `pointer` using reflect. See doMapToMap.

func MapToMaps

func MapToMaps(params interface{}, pointer interface{}, mapping ...map[string]string) error

MapToMaps converts any slice type variable `params` to another map slice type variable `pointer`. See doMapToMaps.

func Maps

func Maps(value interface{}, tags ...string) []map[string]interface{}

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

func MapsDeep(value interface{}, tags ...string) []map[string]interface{}

MapsDeep converts `value` to []map[string]interface{} recursively.

TODO completely implement the recursive converting for all types.

func Rune

func Rune(any interface{}) rune

Rune converts `any` to rune.

func Runes

func Runes(any interface{}) []rune

Runes converts `any` to []rune.

func Scan

func Scan(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)

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 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 ScanList(records, &users, "User") ScanList(records, &users, "User", "uid") ScanList(records, &users, "UserDetail", "User", "uid:Uid") ScanList(records, &users, "UserScores", "User", "uid:Uid") ScanList(records, &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 users []*Entity ScanList(records, &users) ScanList(records, &users, "UserDetail", "uid") ScanList(records, &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 SliceAny

func SliceAny(any interface{}) []interface{}

SliceAny is alias of Interfaces.

func SliceFloat

func SliceFloat(any interface{}) []float64

SliceFloat is alias of Floats.

func SliceFloat32

func SliceFloat32(any interface{}) []float32

SliceFloat32 is alias of Float32s.

func SliceFloat64

func SliceFloat64(any interface{}) []float64

SliceFloat64 is alias of Float64s.

func SliceInt

func SliceInt(any interface{}) []int

SliceInt is alias of Ints.

func SliceInt32

func SliceInt32(any interface{}) []int32

SliceInt32 is alias of Int32s.

func SliceInt64

func SliceInt64(any interface{}) []int64

SliceInt64 is alias of Int64s.

func SliceMap

func SliceMap(any interface{}) []map[string]interface{}

SliceMap is alias of Maps.

func SliceMapDeep

func SliceMapDeep(any interface{}) []map[string]interface{}

SliceMapDeep is alias of MapsDeep.

func SliceStr

func SliceStr(any interface{}) []string

SliceStr is alias of Strings.

func SliceStruct

func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)

SliceStruct is alias of Structs.

func SliceUint

func SliceUint(any interface{}) []uint

SliceUint is alias of Uints.

func SliceUint32

func SliceUint32(any interface{}) []uint32

SliceUint32 is alias of Uint32s.

func SliceUint64

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 Strings

func Strings(any interface{}) []string

Strings converts `any` to []string.

func Struct

func Struct(params interface{}, pointer interface{}, mapping ...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:

  1. The `params` can be any type of map/struct, usually a map.
  2. The `pointer` should be type of *struct/**struct, which is a pointer to struct object or struct pointer.
  3. Only the public attributes of struct object can be mapped.
  4. 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

func StructTag(params interface{}, pointer interface{}, priorityTag string) (err error)

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 Structs

func Structs(params interface{}, pointer interface{}, mapping ...map[string]string) (err error)

Structs converts any slice to given struct slice. Also see Scan, Struct.

func StructsTag

func StructsTag(params interface{}, pointer interface{}, priorityTag string) (err error)

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 Time

func Time(any interface{}, format ...string) time.Time

Time converts `any` to time.Time.

func Uint

func Uint(any interface{}) uint

Uint converts `any` to uint.

func Uint16

func Uint16(any interface{}) uint16

Uint16 converts `any` to uint16.

func Uint32

func Uint32(any interface{}) uint32

Uint32 converts `any` to uint32.

func Uint32s

func Uint32s(any interface{}) []uint32

Uint32s converts `any` to []uint32.

func Uint64

func Uint64(any interface{}) uint64

Uint64 converts `any` to uint64.

func Uint64s

func Uint64s(any interface{}) []uint64

Uint64s converts `any` to []uint64.

func Uint8

func Uint8(any interface{}) uint8

Uint8 converts `any` to uint8.

func Uints

func Uints(any interface{}) []uint

Uints converts `any` to []uint.

func UnsafeBytesToStr

func UnsafeBytesToStr(b []byte) string

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

func UnsafeStrToBytes(s string) []byte

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL