gconv

package
v2.9.2 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2025 License: MIT Imports: 12 Imported by: 1,262

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(anyInput any) bool

Bool converts `any` to bool. It returns false if `any` is: false, "", 0, "false", "off", "no", empty slice/map.

func Byte

func Byte(anyInput any) byte

Byte converts `any` to byte.

func Bytes

func Bytes(anyInput any) []byte

Bytes converts `any` to []byte.

func Convert

func Convert(fromValue any, toTypeName string, extraParams ...any) any

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

func ConvertWithRefer(fromValue any, referValue any, extraParams ...any) any

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

func Duration(anyInput any) 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(anyInput any) float32

Float32 converts `any` to float32.

func Float32s

func Float32s(anyInput any) []float32

Float32s converts `any` to []float32.

func Float64

func Float64(anyInput any) float64

Float64 converts `any` to float64.

func Float64s

func Float64s(anyInput any) []float64

Float64s converts `any` to []float64.

func Floats

func Floats(anyInput any) []float64

Floats converts `any` to []float64.

func GTime

func GTime(anyInput any, format ...string) *gtime.Time

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 Int

func Int(anyInput any) int

Int converts `any` to int.

func Int16

func Int16(anyInput any) int16

Int16 converts `any` to int16.

func Int32

func Int32(anyInput any) int32

Int32 converts `any` to int32.

func Int32s

func Int32s(anyInput any) []int32

Int32s converts `any` to []int32.

func Int64

func Int64(anyInput any) int64

Int64 converts `any` to int64.

func Int64s

func Int64s(anyInput any) []int64

Int64s converts `any` to []int64.

func Int8

func Int8(anyInput any) int8

Int8 converts `any` to int8.

func Interfaces

func Interfaces(anyInput any) []any

Interfaces converts `any` to []any.

func Ints

func Ints(anyInput any) []int

Ints converts `any` to []int.

func Map

func Map(value any, option ...MapOption) map[string]any

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

func MapDeep(value any, tags ...string) map[string]any

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

func MapStrStr(value any, option ...MapOption) 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 any, 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. Deprecated: used MapStrStr instead.

func MapToMap

func MapToMap(params any, pointer any, 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 any, pointer any, 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 any, option ...MapOption) []map[string]any

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

func MapsDeep(value any, tags ...string) []map[string]any

MapsDeep converts `value` to []map[string]any recursively.

TODO completely implement the recursive converting for all types. Deprecated: used Maps instead.

func PtrAny added in v2.2.1

func PtrAny(anyInput any) *any

PtrAny creates and returns an any pointer variable to this value.

func PtrBool added in v2.2.1

func PtrBool(anyInput any) *bool

PtrBool creates and returns a bool pointer variable to this value.

func PtrFloat32 added in v2.2.1

func PtrFloat32(anyInput any) *float32

PtrFloat32 creates and returns a float32 pointer variable to this value.

func PtrFloat64 added in v2.2.1

func PtrFloat64(anyInput any) *float64

PtrFloat64 creates and returns a float64 pointer variable to this value.

func PtrInt added in v2.2.1

func PtrInt(anyInput any) *int

PtrInt creates and returns an int pointer variable to this value.

func PtrInt16 added in v2.2.1

func PtrInt16(anyInput any) *int16

PtrInt16 creates and returns an int16 pointer variable to this value.

func PtrInt32 added in v2.2.1

func PtrInt32(anyInput any) *int32

PtrInt32 creates and returns an int32 pointer variable to this value.

func PtrInt64 added in v2.2.1

func PtrInt64(anyInput any) *int64

PtrInt64 creates and returns an int64 pointer variable to this value.

func PtrInt8 added in v2.2.1

func PtrInt8(anyInput any) *int8

PtrInt8 creates and returns an int8 pointer variable to this value.

func PtrString added in v2.2.1

func PtrString(anyInput any) *string

PtrString creates and returns a string pointer variable to this value.

func PtrUint added in v2.2.1

func PtrUint(anyInput any) *uint

PtrUint creates and returns an uint pointer variable to this value.

func PtrUint16 added in v2.2.1

func PtrUint16(anyInput any) *uint16

PtrUint16 creates and returns an uint16 pointer variable to this value.

func PtrUint32 added in v2.2.1

func PtrUint32(anyInput any) *uint32

PtrUint32 creates and returns an uint32 pointer variable to this value.

func PtrUint64 added in v2.2.1

func PtrUint64(anyInput any) *uint64

PtrUint64 creates and returns an uint64 pointer variable to this value.

func PtrUint8 added in v2.2.1

func PtrUint8(anyInput any) *uint8

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

func RegisterConverter(fn any) (err error)

RegisterConverter registers custom converter. Deprecated: use RegisterTypeConverterFunc instead for clear

func RegisterTypeConverterFunc added in v2.9.0

func RegisterTypeConverterFunc(fn any) (err error)

RegisterTypeConverterFunc registers custom converter.

func Rune

func Rune(anyInput any) rune

Rune converts `any` to rune.

func Runes

func Runes(anyInput any) []rune

Runes converts `any` to []rune.

func Scan

func Scan(srcValue any, dstPointer any, paramKeyToAttrMap ...map[string]string) (err error)

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 SliceAny

func SliceAny(anyInput any) []any

SliceAny is alias of Interfaces.

func SliceFloat

func SliceFloat(anyInput any) []float64

SliceFloat is alias of Floats.

func SliceFloat32

func SliceFloat32(anyInput any) []float32

SliceFloat32 is alias of Float32s.

func SliceFloat64

func SliceFloat64(anyInput any) []float64

SliceFloat64 is alias of Float64s.

func SliceInt

func SliceInt(anyInput any) []int

SliceInt is alias of Ints.

func SliceInt32

func SliceInt32(anyInput any) []int32

SliceInt32 is alias of Int32s.

func SliceInt64

func SliceInt64(anyInput any) []int64

SliceInt64 is alias of Int64s.

func SliceMap

func SliceMap(anyInput any, option ...MapOption) []map[string]any

SliceMap is alias of Maps.

func SliceMapDeep

func SliceMapDeep(anyInput any) []map[string]any

SliceMapDeep is alias of MapsDeep. Deprecated: used SliceMap instead.

func SliceStr

func SliceStr(anyInput any) []string

SliceStr is alias of Strings.

func SliceStruct

func SliceStruct(params any, pointer any, mapping ...map[string]string) (err error)

SliceStruct is alias of Structs.

func SliceUint

func SliceUint(anyInput any) []uint

SliceUint is alias of Uints.

func SliceUint32

func SliceUint32(anyInput any) []uint32

SliceUint32 is alias of Uint32s.

func SliceUint64

func SliceUint64(anyInput any) []uint64

SliceUint64 is alias of Uint64s.

func String

func String(anyInput any) string

String converts `any` to string. It's most commonly used converting function.

func Strings

func Strings(anyInput any) []string

Strings converts `any` to []string.

func Struct

func Struct(params any, pointer any, 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:

  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 any, pointer any, priorityTag string) (err error)

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 any, pointer any, paramKeyToAttrMap ...map[string]string) (err error)

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

func StructsTag

func StructsTag(params any, pointer any, priorityTag string) (err error)

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 Time

func Time(anyInput any, format ...string) time.Time

Time converts `any` to time.Time.

func Uint

func Uint(anyInput any) uint

Uint converts `any` to uint.

func Uint16

func Uint16(anyInput any) uint16

Uint16 converts `any` to uint16.

func Uint32

func Uint32(anyInput any) uint32

Uint32 converts `any` to uint32.

func Uint32s

func Uint32s(anyInput any) []uint32

Uint32s converts `any` to []uint32.

func Uint64

func Uint64(anyInput any) uint64

Uint64 converts `any` to uint64.

func Uint64s

func Uint64s(anyInput any) []uint64

Uint64s converts `any` to []uint64.

func Uint8

func Uint8(anyInput any) uint8

Uint8 converts `any` to uint8.

func Uints

func Uints(anyInput any) []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

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

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 MapOption added in v2.6.0

type MapOption = converter.MapOption

MapOption specifies the option for map converting.

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.

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.

Jump to

Keyboard shortcuts

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