kconv

package
v0.2.117 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 Struct

func Struct[T any](params interface{}, pointer T, mapping ...map[string]string) (res T)

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[T any](params interface{}, pointer T, priorityTag string) (res T)

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 StructTagWithError

func StructTagWithError[T any](params interface{}, pointer T, priorityTag string) (res T, err error)

func StructWithError

func StructWithError[T any](params interface{}, pointer T, mapping ...map[string]string) (res T, err error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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