conv

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MulanPSL-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package conv provides utility functions related to type conversions, such as to int using ToAny[int](any), to int set using ToSet[int](any), to a slice or array to set type (map[bool]struct{}), map keys and values to slice in random order etc.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JsonToSlice

func JsonToSlice[S ~[]E, E any](data []byte) S

JsonToSlice converts the JSON-encoded data to any type slice with no error returned.

func JsonToSliceE

func JsonToSliceE[S ~[]E, E any](data []byte) (s S, err error)

JsonToSliceE converts the JSON-encoded data to any type slice. E.g. a JSON value ["foo", "bar", "baz"] can be converted to []string{"foo", "bar", "baz"} when calling JsonToSliceE[[]string](`["foo", "bar", "baz"]`).

func MapKeyVals

func MapKeyVals[K comparable, V any, M ~map[K]V](m M) ([]K, []V)

MapKeyVals returns two slice of all the keys and values in m. The keys and values are returned in an indeterminate order.

func MapKeys

func MapKeys[K comparable, V any, M ~map[K]V](m M) []K

MapKeys returns a slice of all the keys in m. The keys returned are in indeterminate order. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Keys.

func MapToSlice

func MapToSlice(a any) (ks, vs any)

MapToSlice converts map keys and values to slice in indeterminate order. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Keys and Values.

func MapToSliceE

func MapToSliceE(a any) (ks, vs any, err error)

MapToSliceE converts keys and values of map to slice in indeterminate order with error. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Keys and Values.

func MapVals

func MapVals[K comparable, V any, M ~map[K]V](m M) []V

MapVals returns a slice of all the values in m. The values returned are in indeterminate order. Deprecated: As of Go 1.18, please use standard library golang.org/x/exp/maps#Values.

func SplitStrToSet

func SplitStrToSet(s string, sep string) map[string]struct{}

SplitStrToSet convert a string to map set after split

func SplitStrToSlice

func SplitStrToSlice[T any](s, sep string) []T

SplitStrToSlice splits a string to a slice by the specified separator.

func SplitStrToSliceE

func SplitStrToSliceE[T any](s, sep string) ([]T, error)

SplitStrToSliceE splits a string to a slice by the specified separator and returns an error if occurred. Note that this function is implemented through 1.18 generics, so the element type needs to be specified when calling it, e.g. SplitStrToSliceE[int]("1,2,3", ",").

func StructToMap

func StructToMap(a any) map[string]any

StructToMap converts struct to map[string]any. Such as struct{I int, S string}{I: 1, S: "a"} to map["I":1 "S":"a"]. Note that unexported fields of struct can't be converted.

func StructToMapStr

func StructToMapStr(a any) map[string]string

StructToMapStr converts struct to map[string]string. Such as struct{I int, S string}{I: 1, S: "a"} to map["I":"1" "S":"a"]. Note that unexported fields of struct can't be converted.

func ToAny

func ToAny[T any](a any) T

ToAny converts one type to another type.

func ToAnyE

func ToAnyE[T any](a any) (T, error)

ToAnyE converts one type to another and returns an error if error occurred.

func ToBoolE

func ToBoolE(a any) (bool, error)

ToBoolE casts any type to a bool type.

func ToDurationE

func ToDurationE(i any) (time.Duration, error)

ToDurationE casts any type to time.Duration type.

func ToFloat32E

func ToFloat32E(i any) (float32, error)

ToFloat32E casts any type to a float32 type.

func ToFloat64E

func ToFloat64E(i any) (float64, error)

ToFloat64E casts any type to a float64 type.

func ToInt16E

func ToInt16E(i any) (int16, error)

ToInt16E casts any type to an int16 type.

func ToInt32E

func ToInt32E(i any) (int32, error)

ToInt32E casts any type to an int32 type.

func ToInt64E

func ToInt64E(i any) (int64, error)

ToInt64E casts any to an int64 type.

func ToInt8E

func ToInt8E(i any) (int8, error)

ToInt8E casts any type to an int8 type.

func ToIntE

func ToIntE(i any) (int, error)

ToIntE casts any type to an int type.

func ToMapStr

func ToMapStr(a any) map[string]string

ToMapStr converts any type to a map[string]string type.

func ToMapStrE

func ToMapStrE(a any) (map[string]string, error)

ToMapStrE converts any type to a map[string]string type.

func ToSet

func ToSet[T comparable](a any) map[T]struct{}

ToSet converts a slice or array to map[T]struct{}. An error will be returned if an error occurred.

func ToSetE

func ToSetE[T comparable](a any) (map[T]struct{}, error)

ToSetE converts a slice or array to map[T]struct{} and returns an error if occurred. Note that the the element type of input don't need to be equal to the map key type. For example, []uint64{1, 2, 3} can be converted to map[uint64]struct{}{1:{},2:{},3:{}} and also can be converted to map[string]struct{}{"1":{},"2":{},"3":{}} if you want. Note that this function is implemented through 1.18 generics, so the element type needs to be specified when calling it, e.g. ToSetE[int]([]int{1,2,3}).

func ToSlice

func ToSlice[T any](a any) []T

ToSlice converts any type slice or array to the specified type slice.

func ToSliceE

func ToSliceE[T any](a any) ([]T, error)

ToSliceE converts any type slice or array to the specified type slice. An error will be returned if an error occurred.

func ToStringE

func ToStringE(i any) (string, error)

ToStringE casts any type to a string type.

func ToUint16E

func ToUint16E(i any) (uint16, error)

ToUint16E casts any type to a uint16 type.

func ToUint32E

func ToUint32E(i any) (uint32, error)

ToUint32E casts any type to a uint32 type.

func ToUint64E

func ToUint64E(i any) (uint64, error)

ToUint64E casts any type to a uint64 type.

func ToUint8E

func ToUint8E(i any) (uint8, error)

ToUint8E casts any type to a uint type.

func ToUintE

func ToUintE(i any) (uint, error)

ToUintE casts any type to a uint type.

Types

This section is empty.

Jump to

Keyboard shortcuts

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