cast2

package module
v0.0.0-...-79057ee Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 9 Imported by: 1

README

cast

GoDoc Build Status Go Report Card

Easy and safe casting from one struct to another in Go

Don’t Panic! ... Cast

What is Cast?

Cast is a library to convert between different go types in a consistent and easy way.

Cast provides simple functions to easily convert a struct to a another, an struct into a map, etc. Cast does this intelligently when an obvious conversion is possible. It doesn’t make any attempts to guess what you meant, for example you can only convert a string to an int when it is a string representation of an int such as “8”. Cast2 is used to supplement Cast's deficiency in aaa conversion

Why use Cast?

Usage

Example ‘ToMap’:
Example ‘ToMapByTag’:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelString

func CamelString(s string) string

CamelString 蛇形转驼峰 @description xx_yy to XxYx xx_y_y to XxYY @date 2020/7/30 @param s要转换的字符串 @return string

func CopyStruct

func CopyStruct[T1 any, T2 any](original T1, aim T2) T2

CopyStruct 匹配到及赋值

func CopyStructAdv

func CopyStructAdv[T1 any, T2 any](original T1, aim T2, c FieldConversionConfig) T2

CopyStructAdv 将目标字段映射赋值

func CreateList

func CreateList[T1 any, T2 any](sourceList []T2, c FieldConversionConfig) []T1

CreateList 使用一个list创建另外一个list

func CreateStruct

func CreateStruct[T2 any, T1 any](original T1) T2

func ErgodicObj

func ErgodicObj(obj interface{}, fn func(fieldName string))

func FormatSelf

func FormatSelf[T any](info T) T

func GetColumn

func GetColumn[TField any, TList any](list []TList, key string) []TField

func GetStructKeyKind

func GetStructKeyKind[T any](data T, key string) reflect.Kind

func GetStructValue

func GetStructValue[T any](data T, field string) any

func InStrings

func InStrings(target string, arr []string) bool

InStrings 判断字符串是否在字符串数组中

func InStringsSorted

func InStringsSorted(target string, arr []string) bool

InStringsSorted 判断字符串是否在已排序的字符串数组中

func LoadList

func LoadList[T1 any, T2 any](baseList []T1, baseListKey string, newList []T2, newListKey string, c FieldConversionConfig) []T1

func SetStructValue

func SetStructValue[T any, T2 any](data T, key string, value T2) T

SetStructValue 按属性key给结构体赋值

func SnakeString

func SnakeString(s string) string

SnakeString 驼峰转蛇形 snake string @description XxYy to xx_yy , XxYY to xx_y_y @param s 需要转换的字符串 @return string

func StructHaveField

func StructHaveField[T any](data T, field string) bool

func StructValue

func StructValue[T any](data T, field string) any

func To

func To[T any](sourceValue any) (aim T, err error)

func ToListMap

func ToListMap[TKey comparable, T any](list []T, keyName string) map[TKey]T

ToListMap 将list转换成指定key为下标的map

func ToMap

func ToMap(obj interface{}) map[string]interface{}

func ToMapByTag

func ToMapByTag(obj interface{}, tagName string) map[string]interface{}

func ToMapByTagJson

func ToMapByTagJson(obj interface{}) map[string]interface{}

func ToSlice

func ToSlice[T any](s, sep string) (list2 []T, err error)

func ToString

func ToString(v any) string

ToString 将对象转字符串,不考虑异常情况

func Unmarshal

func Unmarshal[T any](oldData string) T

Unmarshal 将原字符串值转对象,不考虑错误情况

Types

type FieldConversionConfig

type FieldConversionConfig struct {
	PartialConversionFields []string          //需要进行部分转换的字段,未设置则进行全部转换(源字段)
	ReplaceField            map[string]string //需要进行的字段替换配置(源字段:新字段)
}

Jump to

Keyboard shortcuts

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