Documentation
¶
Overview ¶
Package conv provides a group of functions to convert between primitive types, maps, slices and structs.
Index ¶
- Constants
- func CaseInsensitiveNameIndexer(m map[string]interface{}, key string) (value interface{}, ok bool)
- func ConverToBool(v interface{}) (bool, error)
- func Convert(src interface{}, dst interface{}) error
- func ConvertMapToMap(m interface{}, typ reflect.Type) (interface{}, error)
- func ConvertMapToStruct(m map[string]interface{}, typ reflect.Type) (interface{}, error)
- func ConvertSimpleToString(v interface{}) (string, error)
- func ConvertSliceToSlice(src interface{}, dstSliceTyp reflect.Type) (interface{}, error)
- func ConvertStringToPrimitive(v string, dstKind reflect.Kind) (interface{}, error)
- func ConvertStringToSlice(v string, primitiveSliceType reflect.Type) (interface{}, error)
- func ConvertStructToMap(v interface{}) (map[string]interface{}, error)
- func ConvertStructToStruct(v interface{}, typ reflect.Type) (interface{}, error)
- func ConvertType(v interface{}, typ reflect.Type) (interface{}, error)
- func DefaultSplitString(v string) []string
- func DefaultStringToTime(v string) (time.Time, error)
- func DefaultTimeToString(t time.Time) (string, error)
- func IsPrimitiveKind(k reflect.Kind) bool
- func IsPrimitiveType(t reflect.Type) bool
- func IsSimpleType(t reflect.Type) bool
- type Conv
- func (c Conv) ConverToBool(v interface{}) (bool, error)
- func (c Conv) Convert(src interface{}, dst interface{}) error
- func (c Conv) ConvertMapToMap(m interface{}, typ reflect.Type) (interface{}, error)
- func (c Conv) ConvertMapToStruct(m map[string]interface{}, typ reflect.Type) (interface{}, error)
- func (c Conv) ConvertSimpleToString(v interface{}) (string, error)
- func (c Conv) ConvertSliceToSlice(src interface{}, dstSliceTyp reflect.Type) (interface{}, error)
- func (c Conv) ConvertStringToPrimitive(v string, dstKind reflect.Kind) (interface{}, error)
- func (c Conv) ConvertStringToSlice(v string, simpleSliceType reflect.Type) (interface{}, error)
- func (c Conv) ConvertStructToMap(v interface{}) (map[string]interface{}, error)
- func (c Conv) ConvertStructToStruct(v interface{}, typ reflect.Type) (interface{}, error)
- func (c Conv) ConvertType(v interface{}, typ reflect.Type) (interface{}, error)
Constants ¶
const (
// StringSliceSep is used by SplitString() as the separator.
StringSliceSep = "~"
)
Variables ¶
This section is empty.
Functions ¶
func CaseInsensitiveNameIndexer ¶
CaseInsensitiveNameIndexer indexes a map and compares the keys case-insensitively. It compares keys with strings.EqualFold() , and returns on the first key for which EqualFold() is true.
func ConverToBool ¶
ConverToBool is equivalent to Conv{}.ConverToBool() .
func Convert ¶
func Convert(src interface{}, dst interface{}) error
Convert is equivalent to Conv{}.Convert() .
func ConvertMapToMap ¶
ConvertMapToMap is equivalent to Conv{}.ConvertMapToMap() .
func ConvertMapToStruct ¶
ConvertMapToStruct is equivalent to Conv{}.ConvertMapToStruct() .
func ConvertSimpleToString ¶
ConvertSimpleToString is equivalent to Conv{}.ConvertSimpleToString() .
func ConvertSliceToSlice ¶
ConvertSliceToSlice is equivalent to Conv{}.ConvertSliceToSlice() .
func ConvertStringToPrimitive ¶
ConvertStringToPrimitive is equivalent to Conv{}.ConvertStringToPrimitive() .
func ConvertStringToSlice ¶
ConvertStringToSlice is equivalent to Conv{}.ConvertStringToSlice() .
func ConvertStructToMap ¶
ConvertStructToMap is equivalent to Conv{}.ConvertStructToMap() .
func ConvertStructToStruct ¶
ConvertStructToStruct is equivalent to Conv{}.ConvertStructToStruct() .
func ConvertType ¶
ConvertType is equivalent to Conv{}.ConvertType() .
func DefaultSplitString ¶
DefaultSplitString spilit a string by '~'. This is the default value for Conv.SplitString() when it is nil.
func DefaultStringToTime ¶
DefaultStringToTime parses the time using the time.RFC3339Nano format.
func DefaultTimeToString ¶
DefaultTimeToString formats time using the time.RFC3339 format.
func IsPrimitiveKind ¶
IsPrimitiveKind returns true if the given Kind is bool/int*/uint*/float*/complex*/string .
func IsPrimitiveType ¶
IsPrimitiveType returns true if the given type is bool/int*/uint*/float*/complex*/string .
func IsSimpleType ¶
IsSimpleType returns true if the given type IsPrimitiveType() or is time.Time .
Types ¶
type Conv ¶
type Conv struct {
// SplitString is the function used to split the string into elements of the slice, when converting a string to a slice.
// Set this field if need to customize the procedure.
// If this field is nil, the function DefaultSplitString() will be used.
SplitString func(v string) []string
// NameIndexer is the function used to match names when converting from map to struct or from struct to struct.
// If the given name is match, the function returns the value from the source map with @ok=true;
// otherwise returns (nil, false) .
// If it returns OK, the value from the source map will be converted into the destination struct
// using Conv.ConvertType() .
//
// When converting a map to a struct, each field name of the struct will be indexed using this function.
// When converting a struct to another, field names and values from the souce struct will be put into a map,
// then each field name of the destination struct will be indexed with the map.
//
// If this function is nil, the Go built-in indexer for maps will be used.
// The build-in indexer is like:
// v, ok := m[name]
//
// If a case-insensitive indexer is needed, use the CaseInsensitiveNameIndexer function.
//
NameIndexer func(m map[string]interface{}, name string) (v interface{}, ok bool)
// TimeToString formats the given time.
// Set this field if need to customize the procedure.
// If this field is nil, the function DefaultTimeToString() will be used.
TimeToString func(t time.Time) (string, error)
// StringToTime parses the given string and returns the time it represends.
// Set this field if need to customize the procedure.
// If this field is nil, the function DefaultStringToTime() will be used.
StringToTime func(v string) (time.Time, error)
}
Conv provides a group of functions to convert between primitive types, time.Time, maps, slices and structs. A new instance with default values has default conversion behavior.
Conv{}.ConvertType(...)
Don't call functions that does not start with 'Convert' directly, they are for configuration and are called internally by other functions with names which start with 'Convert', such as Convert()/ConvertType()/ConvertStringToSlice() .
func (Conv) ConverToBool ¶
ConverToBool converts the value to bool.
Rules: nil: as false; numbers/time.Time: zero as false, non-zero as true; string: same as strconv.ParseBool() ; other values are not supported, the function returns false and an error.
func (Conv) Convert ¶
Convert is like Conv.ConvertType() , but receives a pointer instead of a type. It stores the result in the value pointed to by dst. If dst is not a pointer, the function panics an error.
func (Conv) ConvertMapToMap ¶
ConvertMapToMap converts a map to another map. If the source value is nil, the function returns a nil map of the destination type without any error.
All keys and values in the map are converted using Conv.ConvertType() .
func (Conv) ConvertMapToStruct ¶
ConvertMapToStruct converts a map[string]interface{} to a struct.
Each exported field of the struct is indexed from the map by name using Conv.NameIndexer() , if the name exists, the corresponding value is converted using Conv.ConvertType() .
func (Conv) ConvertSimpleToString ¶
ConvertSimpleToString converts the given value to a string. The value must be a simple type, for which IsSimpleType() returns true.
Conv.StringToTime() is used to format times. Specially, booleans are converted to 0/1, not the default foramt true/false.
func (Conv) ConvertSliceToSlice ¶
ConvertSliceToSlice converts a slice to another slice. Each element will be converted using Conv.ConvertType() . If the source value is nil, returns nil and an error.
func (Conv) ConvertStringToPrimitive ¶
ConvertStringToPrimitive converts a string to a primitive type (which IsPrimitiveType() returns true).
func (Conv) ConvertStringToSlice ¶
ConvertStringToSlice converts a string to a slice. The elements of the slice must be simple type, for which IsSimpleType() returns true.
Conv.SplitString() is used to split the string.
func (Conv) ConvertStructToMap ¶
ConvertStructToMap is like json.Unmashal(json.Marshal(v), &someMap) . It converts a struct to map[string]interface{} .
Each value of exported field will be processed recursively with an internal function f() , which:
- Simple types (which IsSimpleType() returns true) will be cloned into the map directly.
- Slices:
- A nil/empty slices is converted to an empty slice with cap=0.
- A non-empty slice is converted to another slice, each element is process with f() , all elements must be the same type.
- Maps:
- A nil map are converted to nil of map[string]interface{} .
- A non-nil map is converted to map[string]interface{} , keys are processed with Conv.ConvertType() , values with f() .
- Structs are converted to map[string]interface{} using Conv.ConvertStructToMap() .
- For pointers, the values pointed to are converted with f() .
Other types not listed are not supported and will result in an error.
func (Conv) ConvertStructToStruct ¶
ConvertStructToStruct converts a struct to another. If the given value is nil, returns nil and an error.
When converting, all fields of the source struct is to be stored in a map[string]interface{} , then each field of the destination struct is indexed from the map by name using Conv.NameIndexer() , if the name exists, the value is converted using Conv.ConvertType() .
This function can be used to deep-clone a struct.
func (Conv) ConvertType ¶
ConvertType is the core function of Conv . It converts the given value to the destination type.
Currently these conversions are supported:
simple -> simple * use Conv.ConvertSimpleToSimple()
string -> simple * use Conv.ConvertStringToPrimitive(), or Conv.StringToTime() for time values.
string -> []simple * use Conv.ConvertStringToSlice()
map[string]any -> struct * use Conv.ConvertMapToStruct()
map[any]any -> map[any]any * use Conv.ConvertMapToMap()
[]any -> []any * use Conv.ConvertType() recursively
struct -> map[string]interface{} * use Conv.ConvertStructToMap()
struct -> struct * use Conv.ConvertStructToStuct()
'any' generally means interface{} .
typ can be a type of pointer, the conversion of the underlying type must be supported.
This function can be used to deep-clone a struct, e.g.
clone, err := ConvertType(src, reflect.TypeOf(src))