conv

package
v0.0.50 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

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

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

func JSONToSliceE

func JSONToSliceE[S ~[]E, E any](data []byte) (S, 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 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. You can also use standard library golang.org/x/exp/maps#Keys.

func MapKeysVals

func MapKeysVals[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 MapToSlice

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

MapToSlice converts map keys and values to slice in indeterminate order.

func MapToSliceE

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

MapToSliceE converts keys and values of map to slice in indeterminate order with error.

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. You can also 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 Struct2Map

func Struct2Map(a any) map[string]any

Struct2Map 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 Struct2MapStr

func Struct2MapStr(obj any) map[string]string

Struct2MapStr 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 ToBoolSlice

func ToBoolSlice(a any) []bool

ToBoolSlice converts any type to []bool.

func ToBoolSliceE

func ToBoolSliceE(a any) ([]bool, error)

ToBoolSliceE converts any type slice or array to []bool with returned error.

func ToByteSlice

func ToByteSlice(a any) []byte

ToByteSlice converts an type slice or array to []byte. E.g. covert []string{"1", "2", "3"} to []byte{1, 2, 3}.

func ToByteSliceE

func ToByteSliceE(a any) ([]byte, error)

ToByteSliceE converts any type slice or array to []byte with returned error.

func ToDurationE

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

ToDurationE casts any type to time.Duration type.

func ToDurationSlice

func ToDurationSlice(a any) []time.Duration

ToDurationSlice converts any type slice or array to []time.Duration.

func ToDurationSliceE

func ToDurationSliceE(a any) ([]time.Duration, error)

ToDurationSliceE converts any type to []time.Duration with returned error.

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 ToInt16Slice

func ToInt16Slice(a any) []int16

ToInt16Slice converts any type slice or array to []int16. For example, covert []string{"1", "2", "3"} to []int16{1, 2, 3}.

func ToInt16SliceE

func ToInt16SliceE(a any) ([]int16, error)

ToInt16SliceE converts any type slice or array to []int16 with returned error.

func ToInt32E

func ToInt32E(i any) (int32, error)

ToInt32E casts any type to an int32 type.

func ToInt32Slice

func ToInt32Slice(a any) []int32

ToInt32Slice converts any type slice or array to []int32. For example, covert []string{"1", "2", "3"} to []int32{1, 2, 3}.

func ToInt32SliceE

func ToInt32SliceE(a any) ([]int32, error)

ToInt32SliceE converts any type slice or array []int32 with returned error.

func ToInt64E

func ToInt64E(i any) (int64, error)

ToInt64E casts any to an int64 type.

func ToInt64Slice

func ToInt64Slice(a any) []int64

ToInt64Slice converts any type slice or array to []int64 slice. For example, covert []string{"1", "2", "3"} to []int64{1, 2, 3}.

func ToInt64SliceE

func ToInt64SliceE(a any) ([]int64, error)

ToInt64SliceE converts any type slice or array to []int64 slice with returned error.

func ToInt8E

func ToInt8E(i any) (int8, error)

ToInt8E casts any type to an int8 type.

func ToInt8Slice

func ToInt8Slice(a any) []int8

ToInt8Slice converts any type slice or array to []int8. E.g. covert []string{"1", "2", "3"} to []int8{1, 2, 3}.

func ToInt8SliceE

func ToInt8SliceE(a any) ([]int8, error)

ToInt8SliceE converts any type slice or array to []int8 with returned error.

func ToIntE

func ToIntE(i any) (int, error)

ToIntE casts any type to an int type.

func ToIntSlice

func ToIntSlice(a any) []int

ToIntSlice converts any type slice or array to []int. E.g. covert []string{"1", "2", "3"} to []int{1, 2, 3}.

func ToIntSliceE

func ToIntSliceE(a any) ([]int, error)

ToIntSliceE converts any type slice or array to []int with returned error..

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 ToStrSlice

func ToStrSlice(a any) []string

ToStrSlice converts any type slice or array to []string. For example, covert []int{1, 2, 3} to []string{"1", "2", "3"}.

func ToStrSliceE

func ToStrSliceE(a any) ([]string, error)

ToStrSliceE converts any type slice or array to []string with returned error.

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 ToUint16Slice

func ToUint16Slice(a any) []uint16

ToUint16Slice converts any type slice or array to []uint16. For example, covert []string{"1", "2", "3"} to []uint16{1, 2, 3}.

func ToUint16SliceE

func ToUint16SliceE(a any) ([]uint16, error)

ToUint16SliceE converts any type slice or array to []uint16 slice with returned error.

func ToUint32E

func ToUint32E(i any) (uint32, error)

ToUint32E casts any type to a uint32 type.

func ToUint32Slice

func ToUint32Slice(a any) []uint32

ToUint32Slice converts any type slice or array to []uint32. For example, covert []string{"1", "2", "3"} to []uint32{1, 2, 3}.

func ToUint32SliceE

func ToUint32SliceE(a any) ([]uint32, error)

ToUint32SliceE converts any type slice or array to []uint32 slice with returned error.

func ToUint64E

func ToUint64E(i any) (uint64, error)

ToUint64E casts any type to a uint64 type.

func ToUint64Slice

func ToUint64Slice(a any) []uint64

ToUint64Slice converts any type slice or array to []uint64. For example, covert []string{"1", "2", "3"} to []uint64{1, 2, 3}.

func ToUint64SliceE

func ToUint64SliceE(a any) ([]uint64, error)

ToUint64SliceE converts any type slice or array to []uint64 with returned error.

func ToUint8E

func ToUint8E(i any) (uint8, error)

ToUint8E casts any type to a uint type.

func ToUint8Slice

func ToUint8Slice(a any) []uint8

ToUint8Slice converts any type slice or array to []uint8. E.g. covert []string{"1", "2", "3"} to []uint8{1, 2, 3}.

func ToUint8SliceE

func ToUint8SliceE(a any) ([]uint8, error)

ToUint8SliceE converts any type slice or array to []uint8 slice with returned error.

func ToUintE

func ToUintE(i any) (uint, error)

ToUintE casts any type to a uint type.

func ToUintSlice

func ToUintSlice(a any) []uint

ToUintSlice converts any type slice or array to []uint. For example, covert []string{"1", "2", "3"} to []uint{1, 2, 3}.

func ToUintSliceE

func ToUintSliceE(a any) ([]uint, error)

ToUintSliceE converts any type slice or array to []uint with returned error.

Types

This section is empty.

Jump to

Keyboard shortcuts

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