internal

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2021 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package conv provides a group of functions to convert between primitive types, maps, slices and structs.

Index

Constants

View Source
const (
	// StringSliceSep is used by SplitString() as the separator.
	StringSliceSep = "~"
)

Variables

This section is empty.

Functions

func CaseInsensitiveNameIndexer

func CaseInsensitiveNameIndexer(m map[string]interface{}, key string) (value interface{}, ok bool)

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

func ConverToBool(v interface{}) (bool, error)

ConverToBool is equivalent to Conv{}.ConverToBool() .

func Convert

func Convert(src interface{}, dst interface{}) error

Convert is equivalent to Conv{}.Convert() .

func ConvertMapToMap

func ConvertMapToMap(m interface{}, typ reflect.Type) (interface{}, error)

ConvertMapToMap is equivalent to Conv{}.ConvertMapToMap() .

func ConvertMapToStruct

func ConvertMapToStruct(m map[string]interface{}, typ reflect.Type) (interface{}, error)

ConvertMapToStruct is equivalent to Conv{}.ConvertMapToStruct() .

func ConvertSimpleToString

func ConvertSimpleToString(v interface{}) (string, error)

ConvertSimpleToString is equivalent to Conv{}.ConvertSimpleToString() .

func ConvertSliceToSlice

func ConvertSliceToSlice(src interface{}, dstSliceTyp reflect.Type) (interface{}, error)

ConvertSliceToSlice is equivalent to Conv{}.ConvertSliceToSlice() .

func ConvertStringToPrimitive

func ConvertStringToPrimitive(v string, dstKind reflect.Kind) (interface{}, error)

ConvertStringToPrimitive is equivalent to Conv{}.ConvertStringToPrimitive() .

func ConvertStringToSlice

func ConvertStringToSlice(v string, primitiveSliceType reflect.Type) (interface{}, error)

ConvertStringToSlice is equivalent to Conv{}.ConvertStringToSlice() .

func ConvertStructToMap

func ConvertStructToMap(v interface{}) (map[string]interface{}, error)

ConvertStructToMap is equivalent to Conv{}.ConvertStructToMap() .

func ConvertStructToStruct

func ConvertStructToStruct(v interface{}, typ reflect.Type) (interface{}, error)

ConvertStructToStruct is equivalent to Conv{}.ConvertStructToStruct() .

func ConvertType

func ConvertType(v interface{}, typ reflect.Type) (interface{}, error)

ConvertType is equivalent to Conv{}.ConvertType() .

func DefaultSplitString

func DefaultSplitString(v string) []string

DefaultSplitString spilit a string by '~'. This is the default value for Conv.SplitString() when it is nil.

func DefaultStringToTime

func DefaultStringToTime(v string) (time.Time, error)

DefaultStringToTime parses the time using the time.RFC3339Nano format.

func DefaultTimeToString

func DefaultTimeToString(t time.Time) (string, error)

DefaultTimeToString formats time using the time.RFC3339 format.

func IsPrimitiveKind

func IsPrimitiveKind(k reflect.Kind) bool

IsPrimitiveKind returns true if the given Kind is bool/int*/uint*/float*/complex*/string .

func IsPrimitiveType

func IsPrimitiveType(t reflect.Type) bool

IsPrimitiveType returns true if the given type is bool/int*/uint*/float*/complex*/string .

func IsSimpleType

func IsSimpleType(t reflect.Type) bool

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

func (c Conv) ConverToBool(v interface{}) (bool, error)

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

func (c Conv) Convert(src interface{}, dst interface{}) error

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

func (c Conv) ConvertMapToMap(m interface{}, typ reflect.Type) (interface{}, error)

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

func (c Conv) ConvertMapToStruct(m map[string]interface{}, typ reflect.Type) (interface{}, error)

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

func (c Conv) ConvertSimpleToString(v interface{}) (string, error)

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

func (c Conv) ConvertSliceToSlice(src interface{}, dstSliceTyp reflect.Type) (interface{}, error)

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

func (c Conv) ConvertStringToPrimitive(v string, dstKind reflect.Kind) (interface{}, error)

ConvertStringToPrimitive converts a string to a primitive type (which IsPrimitiveType() returns true).

func (Conv) ConvertStringToSlice

func (c Conv) ConvertStringToSlice(v string, simpleSliceType reflect.Type) (interface{}, error)

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

func (c Conv) ConvertStructToMap(v interface{}) (map[string]interface{}, error)

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

func (c Conv) ConvertStructToStruct(v interface{}, typ reflect.Type) (interface{}, error)

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

func (c Conv) ConvertType(v interface{}, typ reflect.Type) (interface{}, error)

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

Jump to

Keyboard shortcuts

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