Documentation
¶
Overview ¶
Package henge is a type conversion library for Golang.
変化 (Henge) means "Appearing with a different figure." in Japanese. Henge as the name implies can easily convert to different types.
Example (MethodChain) ¶
i, err := New("1.25").Float().Int().Result() if err != nil { return } fmt.Println(i)
Output: 1
Example (Struct) ¶
type UserRequest struct { Name *string Age *int } type User struct { Name string // *string to string Age string // *int to string } name := "Alice" age := 30 var user User if err := New(UserRequest{Name: &name, Age: &age}).Convert(&user); err != nil { return } fmt.Printf("%#v\n", user)
Output: henge.User{Name:"Alice", Age:"30"}
Index ¶
- Variables
- func ToBool(i interface{}, fs ...func(*ConverterOpts)) bool
- func ToBoolPtr(i interface{}, fs ...func(*ConverterOpts)) *bool
- func ToFloat(i interface{}, fs ...func(*ConverterOpts)) float64
- func ToFloatPtr(i interface{}, fs ...func(*ConverterOpts)) *float64
- func ToInt(i interface{}, fs ...func(*ConverterOpts)) int64
- func ToIntPtr(i interface{}, fs ...func(*ConverterOpts)) *int64
- func ToString(i interface{}, fs ...func(*ConverterOpts)) string
- func ToStringPtr(i interface{}, fs ...func(*ConverterOpts)) *string
- func ToUint(i interface{}, fs ...func(*ConverterOpts)) uint64
- func ToUintPtr(i interface{}, fs ...func(*ConverterOpts)) *uint64
- func WithFloatFormat(fmt byte, prec int) func(*ConverterOpts)
- func WithMapFilter(cond func(k interface{}, v interface{}) bool) func(*ConverterOpts)
- func WithMapMaxDepth(maxDepth uint) func(*ConverterOpts)
- func WithRoundingFunc(f RoundingFunc) func(*ConverterOpts)
- func WithoutNilMapKey() func(*ConverterOpts)
- func WithoutNilMapValue() func(*ConverterOpts)
- func WithoutZeroMapKey() func(*ConverterOpts)
- func WithoutZeroMapValue() func(*ConverterOpts)
- type AfterCallback
- type BeforeCallback
- type BoolConverter
- func (c *BoolConverter) Convert(out interface{}) error
- func (c *BoolConverter) Error() error
- func (c *BoolConverter) InstanceGet(key string) interface{}
- func (c *BoolConverter) InstanceSet(key string, value interface{})
- func (c *BoolConverter) InstanceSetValues(m map[string]interface{})
- func (c *BoolConverter) Ptr() *BoolPtrConverter
- func (c *BoolConverter) Result() (bool, error)
- func (c *BoolConverter) Value() bool
- type BoolPtrConverter
- func (c *BoolPtrConverter) Error() error
- func (c *BoolPtrConverter) InstanceGet(key string) interface{}
- func (c *BoolPtrConverter) InstanceSet(key string, value interface{})
- func (c *BoolPtrConverter) InstanceSetValues(m map[string]interface{})
- func (c *BoolPtrConverter) Result() (*bool, error)
- func (c *BoolPtrConverter) Value() *bool
- type ConvertError
- type Converter
- type ConverterOpts
- type FloatConverter
- func (c *FloatConverter) Convert(out interface{}) error
- func (c *FloatConverter) Error() error
- func (c *FloatConverter) InstanceGet(key string) interface{}
- func (c *FloatConverter) InstanceSet(key string, value interface{})
- func (c *FloatConverter) InstanceSetValues(m map[string]interface{})
- func (c *FloatConverter) Int() *IntegerConverter
- func (c *FloatConverter) Ptr() *FloatPtrConverter
- func (c *FloatConverter) Result() (float64, error)
- func (c *FloatConverter) Uint() *UnsignedIntegerConverter
- func (c *FloatConverter) Value() float64
- type FloatPtrConverter
- func (c *FloatPtrConverter) Error() error
- func (c *FloatPtrConverter) InstanceGet(key string) interface{}
- func (c *FloatPtrConverter) InstanceSet(key string, value interface{})
- func (c *FloatPtrConverter) InstanceSetValues(m map[string]interface{})
- func (c *FloatPtrConverter) Result() (*float64, error)
- func (c *FloatPtrConverter) Value() *float64
- type FloatSliceConverter
- func (c *FloatSliceConverter) Error() error
- func (c *FloatSliceConverter) InstanceGet(key string) interface{}
- func (c *FloatSliceConverter) InstanceSet(key string, value interface{})
- func (c *FloatSliceConverter) InstanceSetValues(m map[string]interface{})
- func (c *FloatSliceConverter) Result() ([]float64, error)
- func (c *FloatSliceConverter) Value() []float64
- type IntegerConverter
- func (c *IntegerConverter) Convert(out interface{}) error
- func (c *IntegerConverter) Error() error
- func (c *IntegerConverter) InstanceGet(key string) interface{}
- func (c *IntegerConverter) InstanceSet(key string, value interface{})
- func (c *IntegerConverter) InstanceSetValues(m map[string]interface{})
- func (c *IntegerConverter) Ptr() *IntegerPtrConverter
- func (c *IntegerConverter) Result() (int64, error)
- func (c *IntegerConverter) Value() int64
- type IntegerPtrConverter
- func (c *IntegerPtrConverter) Error() error
- func (c *IntegerPtrConverter) InstanceGet(key string) interface{}
- func (c *IntegerPtrConverter) InstanceSet(key string, value interface{})
- func (c *IntegerPtrConverter) InstanceSetValues(m map[string]interface{})
- func (c *IntegerPtrConverter) Result() (*int64, error)
- func (c *IntegerPtrConverter) Value() *int64
- type IntegerSliceConverter
- func (c *IntegerSliceConverter) Error() error
- func (c *IntegerSliceConverter) InstanceGet(key string) interface{}
- func (c *IntegerSliceConverter) InstanceSet(key string, value interface{})
- func (c *IntegerSliceConverter) InstanceSetValues(m map[string]interface{})
- func (c *IntegerSliceConverter) Result() ([]int64, error)
- func (c *IntegerSliceConverter) Value() []int64
- type JSONMapConverter
- func (c *JSONMapConverter) Error() error
- func (c *JSONMapConverter) InstanceGet(key string) interface{}
- func (c *JSONMapConverter) InstanceSet(key string, value interface{})
- func (c *JSONMapConverter) InstanceSetValues(m map[string]interface{})
- func (c *JSONMapConverter) Result() (map[string]interface{}, error)
- func (c *JSONMapConverter) Value() map[string]interface{}
- type MapConverter
- func (c *MapConverter) Convert(out interface{}) error
- func (c *MapConverter) Error() error
- func (c *MapConverter) InstanceGet(key string) interface{}
- func (c *MapConverter) InstanceSet(key string, value interface{})
- func (c *MapConverter) InstanceSetValues(m map[string]interface{})
- func (c *MapConverter) Result() (map[interface{}]interface{}, error)
- func (c *MapConverter) Value() map[interface{}]interface{}
- type RoundingFunc
- type SliceConverter
- func (c *SliceConverter) Convert(out interface{}) error
- func (c *SliceConverter) Error() error
- func (c *SliceConverter) FloatSlice() *FloatSliceConverter
- func (c *SliceConverter) InstanceGet(key string) interface{}
- func (c *SliceConverter) InstanceSet(key string, value interface{})
- func (c *SliceConverter) InstanceSetValues(m map[string]interface{})
- func (c *SliceConverter) IntSlice() *IntegerSliceConverter
- func (c *SliceConverter) Result() ([]interface{}, error)
- func (c *SliceConverter) StringSlice() *StringSliceConverter
- func (c *SliceConverter) UintSlice() *UnsignedIntegerSliceConverter
- func (c *SliceConverter) Value() []interface{}
- type StringConverter
- func (c *StringConverter) Convert(out interface{}) error
- func (c *StringConverter) Error() error
- func (c *StringConverter) InstanceGet(key string) interface{}
- func (c *StringConverter) InstanceSet(key string, value interface{})
- func (c *StringConverter) InstanceSetValues(m map[string]interface{})
- func (c *StringConverter) Ptr() *StringPtrConverter
- func (c *StringConverter) Result() (string, error)
- func (c *StringConverter) Value() string
- type StringPtrConverter
- func (c *StringPtrConverter) Error() error
- func (c *StringPtrConverter) InstanceGet(key string) interface{}
- func (c *StringPtrConverter) InstanceSet(key string, value interface{})
- func (c *StringPtrConverter) InstanceSetValues(m map[string]interface{})
- func (c *StringPtrConverter) Result() (*string, error)
- func (c *StringPtrConverter) Value() *string
- type StringSliceConverter
- func (c *StringSliceConverter) Error() error
- func (c *StringSliceConverter) InstanceGet(key string) interface{}
- func (c *StringSliceConverter) InstanceSet(key string, value interface{})
- func (c *StringSliceConverter) InstanceSetValues(m map[string]interface{})
- func (c *StringSliceConverter) Result() ([]string, error)
- func (c *StringSliceConverter) Value() []string
- type StructConverter
- type UnsignedIntegerConverter
- func (c *UnsignedIntegerConverter) Convert(out interface{}) error
- func (c *UnsignedIntegerConverter) Error() error
- func (c *UnsignedIntegerConverter) InstanceGet(key string) interface{}
- func (c *UnsignedIntegerConverter) InstanceSet(key string, value interface{})
- func (c *UnsignedIntegerConverter) InstanceSetValues(m map[string]interface{})
- func (c *UnsignedIntegerConverter) Ptr() *UnsignedIntegerPtrConverter
- func (c *UnsignedIntegerConverter) Result() (uint64, error)
- func (c *UnsignedIntegerConverter) Value() uint64
- type UnsignedIntegerPtrConverter
- func (c *UnsignedIntegerPtrConverter) Error() error
- func (c *UnsignedIntegerPtrConverter) InstanceGet(key string) interface{}
- func (c *UnsignedIntegerPtrConverter) InstanceSet(key string, value interface{})
- func (c *UnsignedIntegerPtrConverter) InstanceSetValues(m map[string]interface{})
- func (c *UnsignedIntegerPtrConverter) Result() (*uint64, error)
- func (c *UnsignedIntegerPtrConverter) Value() *uint64
- type UnsignedIntegerSliceConverter
- func (c *UnsignedIntegerSliceConverter) Error() error
- func (c *UnsignedIntegerSliceConverter) InstanceGet(key string) interface{}
- func (c *UnsignedIntegerSliceConverter) InstanceSet(key string, value interface{})
- func (c *UnsignedIntegerSliceConverter) InstanceSetValues(m map[string]interface{})
- func (c *UnsignedIntegerSliceConverter) Result() ([]uint64, error)
- func (c *UnsignedIntegerSliceConverter) Value() []uint64
- type ValueConverter
- func (c *ValueConverter) Bool() *BoolConverter
- func (c *ValueConverter) BoolPtr() *BoolPtrConverter
- func (c *ValueConverter) Convert(out interface{}) error
- func (c *ValueConverter) Error() error
- func (c *ValueConverter) Float() *FloatConverter
- func (c *ValueConverter) FloatPtr() *FloatPtrConverter
- func (c *ValueConverter) FloatSlice() *FloatSliceConverter
- func (c *ValueConverter) FloatrPtr() *FloatPtrConverter
- func (c *ValueConverter) InstanceGet(key string) interface{}
- func (c *ValueConverter) InstanceSet(key string, value interface{})
- func (c *ValueConverter) InstanceSetValues(m map[string]interface{})
- func (c *ValueConverter) Int() *IntegerConverter
- func (c *ValueConverter) IntPtr() *IntegerPtrConverter
- func (c *ValueConverter) IntSlice() *IntegerSliceConverter
- func (c *ValueConverter) JSONMap() *JSONMapConverter
- func (c *ValueConverter) Map() *MapConverter
- func (c *ValueConverter) Model(t interface{}) *ValueConverter
- func (c *ValueConverter) Result() (interface{}, error)
- func (c *ValueConverter) Slice() *SliceConverter
- func (c *ValueConverter) String() *StringConverter
- func (c *ValueConverter) StringPtr() *StringPtrConverter
- func (c *ValueConverter) StringSlice() *StringSliceConverter
- func (c *ValueConverter) Struct() *StructConverter
- func (c *ValueConverter) Uint() *UnsignedIntegerConverter
- func (c *ValueConverter) UintPtr() *UnsignedIntegerPtrConverter
- func (c *ValueConverter) UintSlice() *UnsignedIntegerSliceConverter
- func (c *ValueConverter) Value() interface{}
Examples ¶
- Package (MethodChain)
- Package (Struct)
- New
- ValueConverter.Bool
- ValueConverter.Convert
- ValueConverter.Float
- ValueConverter.Int
- ValueConverter.Map
- ValueConverter.Map (Nested)
- ValueConverter.Model
- ValueConverter.Slice
- ValueConverter.String
- ValueConverter.Struct
- ValueConverter.Uint
- WithFloatFormat
- WithMapFilter
- WithMapMaxDepth
- WithRoundingFunc
- WithoutNilMapKey
- WithoutNilMapValue
- WithoutZeroMapKey
- WithoutZeroMapValue
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidValue is an error when the source is an invalid value. // Refer: reflect.IsValid ErrInvalidValue = errors.New("invalid value") // ErrUnsupportedType is an error when the source is an unsupported type. ErrUnsupportedType = errors.New("unsupported type") // ErrOverflow is an error if an overflow occurs during conversion. ErrOverflow = errors.New("overflows") // ErrNegativeNumber is an error if converting a negative number to an unsigned type. ErrNegativeNumber = errors.New("negative number") )
Functions ¶
func ToBool ¶ added in v1.2.0
func ToBool(i interface{}, fs ...func(*ConverterOpts)) bool
ToBool is equiv to New(i, fs...).Bool().Value()
func ToBoolPtr ¶ added in v1.2.0
func ToBoolPtr(i interface{}, fs ...func(*ConverterOpts)) *bool
ToBoolPtr is equiv to New(i, fs...).BoolPtr().Value()
func ToFloat ¶ added in v1.2.0
func ToFloat(i interface{}, fs ...func(*ConverterOpts)) float64
ToFloat is equiv to New(i, fs...).Float().Value()
func ToFloatPtr ¶ added in v1.2.0
func ToFloatPtr(i interface{}, fs ...func(*ConverterOpts)) *float64
ToFloatPtr is equiv to New(i, fs...).FloatPtr().Value()
func ToInt ¶ added in v1.2.0
func ToInt(i interface{}, fs ...func(*ConverterOpts)) int64
ToInt is equiv to New(i, fs...).Int().Value()
func ToIntPtr ¶ added in v1.2.0
func ToIntPtr(i interface{}, fs ...func(*ConverterOpts)) *int64
ToIntPtr is equiv to New(i, fs...).IntPtr().Value()
func ToString ¶ added in v1.2.0
func ToString(i interface{}, fs ...func(*ConverterOpts)) string
ToString is equiv to New(i, fs...).String().Value()
func ToStringPtr ¶ added in v1.2.0
func ToStringPtr(i interface{}, fs ...func(*ConverterOpts)) *string
ToStringPtr is equiv to New(i, fs...).StringPtr().Value()
func ToUint ¶ added in v1.2.0
func ToUint(i interface{}, fs ...func(*ConverterOpts)) uint64
ToUint is equiv to New(i, fs...).Uint().Value()
func ToUintPtr ¶ added in v1.2.0
func ToUintPtr(i interface{}, fs ...func(*ConverterOpts)) *uint64
ToUintPtr is equiv to New(i, fs...).UintPtr().Value()
func WithFloatFormat ¶ added in v0.5.0
func WithFloatFormat(fmt byte, prec int) func(*ConverterOpts)
WithFloatFormat is an option when converting from float to string. Ref: strconv.FormatFloat
Example ¶
fmt.Printf( "Default: %v\n", New(0.0125).String().Value(), ) fmt.Printf( "WithFloatFormat('e', 2): %v\n", New(0.0125, WithFloatFormat('e', 2)).String().Value(), )
Output: Default: 0.0125 WithFloatFormat('e', 2): 1.25e-02
func WithMapFilter ¶ added in v0.7.0
func WithMapFilter(cond func(k interface{}, v interface{}) bool) func(*ConverterOpts)
WithMapFilter is an option when converting to map.
If you specify multiple filters, it will be copied only if all filters return true. By default, it copies everything.
Example ¶
type Value struct { X string } in := map[string]*Value{"a": {X: "a"}, "b": nil} fmt.Printf( "Default: %v\n", New(in).Map().Value(), ) fmt.Printf( "Except when value is nil: %v\n", New(in, WithMapFilter(func(k interface{}, v interface{}) bool { r := reflect.ValueOf(v) return r.Kind() != reflect.Ptr || !r.IsNil() })).Map().Value(), ) fmt.Printf( "Multiple Filters: %v\n", New(in, WithMapFilter(func(k interface{}, v interface{}) bool { return true }), WithMapFilter(func(k interface{}, v interface{}) bool { return false }), ).Map().Value(), )
Output: Default: map[a:map[X:a] b:<nil>] Except when value is nil: map[a:map[X:a]] Multiple Filters: map[]
func WithMapMaxDepth ¶ added in v0.7.0
func WithMapMaxDepth(maxDepth uint) func(*ConverterOpts)
WithMapMaxDepth is an option when converting to map.
By default, all structs are converted to maps. It can be used when converting only the top-level.
Example ¶
type Nested struct { Y string } type Value struct { Nested X string } in := map[string]Value{"a": {X: "a", Nested: Nested{Y: "y"}}} fmt.Printf( "Default: %v\n", New(in).Map().Value(), ) fmt.Printf( "WithMapMaxDepth(1): %v\n", New(in, WithMapMaxDepth(1)).Map().Value(), )
Output: Default: map[a:map[Nested:map[Y:y] X:a]] WithMapMaxDepth(1): map[a:map[Nested:{y} X:a]]
func WithRoundingFunc ¶ added in v1.1.0
func WithRoundingFunc(f RoundingFunc) func(*ConverterOpts)
WithRoundingFunc is an option when converting from float to integer (or unsigned integer). It specify the rounding method from float to nearest integer. By default, it use math.Floor.
Example ¶
fmt.Println("int") fmt.Printf( "Default: %v\n", New(1.25).Int().Value(), ) fmt.Printf( "WithRoundingFunc(math.Ceil): %v\n", New(1.25, WithRoundingFunc(math.Ceil)).Int().Value(), ) fmt.Println() fmt.Println("uint") fmt.Printf( "Default: %v\n", New(1.25).Uint().Value(), ) fmt.Printf( "WithRoundingFunc(math.Ceil): %v\n", New(1.25, WithRoundingFunc(math.Ceil)).Uint().Value(), )
Output: int Default: 1 WithRoundingFunc(math.Ceil): 2 uint Default: 1 WithRoundingFunc(math.Ceil): 2
func WithoutNilMapKey ¶ added in v0.7.0
func WithoutNilMapKey() func(*ConverterOpts)
WithoutNilMapKey is an option when converting to map.
When it used, it will not copy if the key is nil.
Example ¶
in := map[interface{}]interface{}{ nil: "a", (*string)(nil): "b", "": "c", } fmt.Printf( "Default: %v\n", New(in).Map().Value(), ) fmt.Printf( "Except when the key is nil: %v\n", New(in, WithoutNilMapKey()).Map().Value(), )
Output: Default: map[<nil>:a <nil>:b :c] Except when the key is nil: map[:c]
func WithoutNilMapValue ¶ added in v0.7.0
func WithoutNilMapValue() func(*ConverterOpts)
WithoutNilMapValue is an option when converting to map.
When it used, it will not copy if the value is nil.
Example ¶
in := map[interface{}]interface{}{ "a": nil, "b": (*string)(nil), "c": "", } fmt.Printf( "Default: %v\n", New(in).Map().Value(), ) fmt.Printf( "Except when the value is nil: %v\n", New(in, WithoutNilMapValue()).Map().Value(), )
Output: Default: map[a:<nil> b:<nil> c:] Except when the value is nil: map[c:]
func WithoutZeroMapKey ¶ added in v0.7.0
func WithoutZeroMapKey() func(*ConverterOpts)
WithoutZeroMapKey is an option when converting to map.
When it used, it will not copy if the key is zero.
Example ¶
in := map[interface{}]interface{}{ nil: "a", (*string)(nil): "b", "": "c", "d": "d", } fmt.Printf( "Default: %v\n", New(in).Map().Value(), ) fmt.Printf( "Except when the key is zero: %v\n", New(in, WithoutZeroMapKey()).Map().Value(), )
Output: Default: map[<nil>:a <nil>:b :c d:d] Except when the key is zero: map[d:d]
func WithoutZeroMapValue ¶ added in v0.7.0
func WithoutZeroMapValue() func(*ConverterOpts)
WithoutZeroMapValue is an option when converting to map.
When it used, it will not copy if the value is zero.
Example ¶
in := map[interface{}]interface{}{ "a": nil, "b": (*string)(nil), "c": "", "d": "d", } fmt.Printf( "Default: %v\n", New(in).Map().Value(), ) fmt.Printf( "Except when the value is zero: %v\n", New(in, WithoutZeroMapValue()).Map().Value(), )
Output: Default: map[a:<nil> b:<nil> c: d:d] Except when the value is zero: map[d:d]
Types ¶
type AfterCallback ¶ added in v0.3.0
AfterCallback is a callback that is executed after the conversion from a struct to the struct.
type BeforeCallback ¶ added in v0.5.0
BeforeCallback is a callback that is executed before the conversion from a struct to the struct.
type BoolConverter ¶ added in v0.5.0
type BoolConverter struct {
// contains filtered or unexported fields
}
BoolConverter is a converter that converts a bool type to another type.
func (*BoolConverter) Convert ¶ added in v0.5.0
func (c *BoolConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*BoolConverter) Error ¶ added in v0.5.0
func (c *BoolConverter) Error() error
Error returns an error if the conversion fails
func (*BoolConverter) InstanceGet ¶ added in v0.5.0
func (c *BoolConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*BoolConverter) InstanceSet ¶ added in v0.5.0
func (c *BoolConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*BoolConverter) InstanceSetValues ¶ added in v0.5.0
func (c *BoolConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*BoolConverter) Ptr ¶ added in v0.5.0
func (c *BoolConverter) Ptr() *BoolPtrConverter
Ptr converts the input to ptr type.
func (*BoolConverter) Result ¶ added in v0.5.0
func (c *BoolConverter) Result() (bool, error)
Result returns the conversion result and error.
func (*BoolConverter) Value ¶ added in v0.5.0
func (c *BoolConverter) Value() bool
Value returns the conversion result.
type BoolPtrConverter ¶ added in v0.5.0
type BoolPtrConverter struct {
// contains filtered or unexported fields
}
BoolPtrConverter is a converter that converts a pointer of bool type to another type.
func (*BoolPtrConverter) Error ¶ added in v0.5.0
func (c *BoolPtrConverter) Error() error
Error returns an error if the conversion fails.
func (*BoolPtrConverter) InstanceGet ¶ added in v0.5.0
func (c *BoolPtrConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*BoolPtrConverter) InstanceSet ¶ added in v0.5.0
func (c *BoolPtrConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*BoolPtrConverter) InstanceSetValues ¶ added in v0.5.0
func (c *BoolPtrConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*BoolPtrConverter) Result ¶ added in v0.5.0
func (c *BoolPtrConverter) Result() (*bool, error)
Result returns the conversion result and error.
func (*BoolPtrConverter) Value ¶ added in v0.5.0
func (c *BoolPtrConverter) Value() *bool
Value returns the conversion result.
type ConvertError ¶ added in v0.5.0
type ConvertError struct { Field string SrcType reflect.Type DstType reflect.Type Value interface{} Err error }
ConvertError is an error that shows where the error occurred during conversion.
func (*ConvertError) Error ¶ added in v0.5.0
func (e *ConvertError) Error() string
func (*ConvertError) Unwrap ¶ added in v0.5.0
func (e *ConvertError) Unwrap() error
type Converter ¶ added in v0.5.0
type Converter interface { InstanceGet(key string) interface{} InstanceSet(key string, value interface{}) InstanceSetValues(m map[string]interface{}) }
Converter is an interface that has common functions of all Converters.
type ConverterOpts ¶ added in v0.5.0
type ConverterOpts struct {
// contains filtered or unexported fields
}
ConverterOpts are options for the conversion.
type FloatConverter ¶ added in v0.5.0
type FloatConverter struct {
// contains filtered or unexported fields
}
FloatConverter is a converter that converts a float type to another type.
func (*FloatConverter) Convert ¶ added in v0.5.0
func (c *FloatConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*FloatConverter) Error ¶ added in v0.5.0
func (c *FloatConverter) Error() error
Error returns an error if the conversion fails
func (*FloatConverter) InstanceGet ¶ added in v0.5.0
func (c *FloatConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*FloatConverter) InstanceSet ¶ added in v0.5.0
func (c *FloatConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*FloatConverter) InstanceSetValues ¶ added in v0.5.0
func (c *FloatConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*FloatConverter) Int ¶ added in v0.5.0
func (c *FloatConverter) Int() *IntegerConverter
Int converts the input to int type.
func (*FloatConverter) Ptr ¶ added in v0.5.0
func (c *FloatConverter) Ptr() *FloatPtrConverter
Ptr converts the input to ptr type.
func (*FloatConverter) Result ¶ added in v0.5.0
func (c *FloatConverter) Result() (float64, error)
Result returns the conversion result and error.
func (*FloatConverter) Uint ¶ added in v0.5.0
func (c *FloatConverter) Uint() *UnsignedIntegerConverter
Uint converts the input to uint type.
func (*FloatConverter) Value ¶ added in v0.5.0
func (c *FloatConverter) Value() float64
Value returns the conversion result.
type FloatPtrConverter ¶ added in v0.5.0
type FloatPtrConverter struct {
// contains filtered or unexported fields
}
FloatPtrConverter is a converter that converts a pointer of float type to another type.
func (*FloatPtrConverter) Error ¶ added in v0.5.0
func (c *FloatPtrConverter) Error() error
Error returns an error if the conversion fails.
func (*FloatPtrConverter) InstanceGet ¶ added in v0.5.0
func (c *FloatPtrConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*FloatPtrConverter) InstanceSet ¶ added in v0.5.0
func (c *FloatPtrConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*FloatPtrConverter) InstanceSetValues ¶ added in v0.5.0
func (c *FloatPtrConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*FloatPtrConverter) Result ¶ added in v0.5.0
func (c *FloatPtrConverter) Result() (*float64, error)
Result returns the conversion result and error.
func (*FloatPtrConverter) Value ¶ added in v0.5.0
func (c *FloatPtrConverter) Value() *float64
Value returns the conversion result.
type FloatSliceConverter ¶ added in v0.5.0
type FloatSliceConverter struct {
// contains filtered or unexported fields
}
FloatSliceConverter is a converter that converts a slice of float type to another type.
func (*FloatSliceConverter) Error ¶ added in v0.5.0
func (c *FloatSliceConverter) Error() error
Error returns an error if the conversion fails.
func (*FloatSliceConverter) InstanceGet ¶ added in v0.5.0
func (c *FloatSliceConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*FloatSliceConverter) InstanceSet ¶ added in v0.5.0
func (c *FloatSliceConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*FloatSliceConverter) InstanceSetValues ¶ added in v0.5.0
func (c *FloatSliceConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*FloatSliceConverter) Result ¶ added in v0.5.0
func (c *FloatSliceConverter) Result() ([]float64, error)
Result returns the conversion result and error.
func (*FloatSliceConverter) Value ¶ added in v0.5.0
func (c *FloatSliceConverter) Value() []float64
Value returns the conversion result.
type IntegerConverter ¶ added in v0.5.0
type IntegerConverter struct {
// contains filtered or unexported fields
}
IntegerConverter is a converter that converts an integer type to another type.
func (*IntegerConverter) Convert ¶ added in v0.5.0
func (c *IntegerConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*IntegerConverter) Error ¶ added in v0.5.0
func (c *IntegerConverter) Error() error
Error returns an error if the conversion fails.
func (*IntegerConverter) InstanceGet ¶ added in v0.5.0
func (c *IntegerConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*IntegerConverter) InstanceSet ¶ added in v0.5.0
func (c *IntegerConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*IntegerConverter) InstanceSetValues ¶ added in v0.5.0
func (c *IntegerConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*IntegerConverter) Ptr ¶ added in v0.5.0
func (c *IntegerConverter) Ptr() *IntegerPtrConverter
Ptr converts the input to ptr type.
func (*IntegerConverter) Result ¶ added in v0.5.0
func (c *IntegerConverter) Result() (int64, error)
Result returns the conversion result and error.
func (*IntegerConverter) Value ¶ added in v0.5.0
func (c *IntegerConverter) Value() int64
Value returns the conversion result.
type IntegerPtrConverter ¶ added in v0.5.0
type IntegerPtrConverter struct {
// contains filtered or unexported fields
}
IntegerPtrConverter is a converter that converts a pointer of integer type to another type.
func (*IntegerPtrConverter) Error ¶ added in v0.5.0
func (c *IntegerPtrConverter) Error() error
Error returns an error if the conversion fails.
func (*IntegerPtrConverter) InstanceGet ¶ added in v0.5.0
func (c *IntegerPtrConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*IntegerPtrConverter) InstanceSet ¶ added in v0.5.0
func (c *IntegerPtrConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*IntegerPtrConverter) InstanceSetValues ¶ added in v0.5.0
func (c *IntegerPtrConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*IntegerPtrConverter) Result ¶ added in v0.5.0
func (c *IntegerPtrConverter) Result() (*int64, error)
Result returns the conversion result and error.
func (*IntegerPtrConverter) Value ¶ added in v0.5.0
func (c *IntegerPtrConverter) Value() *int64
Value returns the conversion result.
type IntegerSliceConverter ¶ added in v0.5.0
type IntegerSliceConverter struct {
// contains filtered or unexported fields
}
IntegerSliceConverter is a converter that converts a slice of integer type to another type.
func (*IntegerSliceConverter) Error ¶ added in v0.5.0
func (c *IntegerSliceConverter) Error() error
Error returns an error if the conversion fails.
func (*IntegerSliceConverter) InstanceGet ¶ added in v0.5.0
func (c *IntegerSliceConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*IntegerSliceConverter) InstanceSet ¶ added in v0.5.0
func (c *IntegerSliceConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*IntegerSliceConverter) InstanceSetValues ¶ added in v0.5.0
func (c *IntegerSliceConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*IntegerSliceConverter) Result ¶ added in v0.5.0
func (c *IntegerSliceConverter) Result() ([]int64, error)
Result returns the conversion result and error
func (*IntegerSliceConverter) Value ¶ added in v0.5.0
func (c *IntegerSliceConverter) Value() []int64
Value returns the conversion result.
type JSONMapConverter ¶ added in v1.0.0
type JSONMapConverter struct {
// contains filtered or unexported fields
}
JSONMapConverter is a converter that converts a json map type to another type.
func (*JSONMapConverter) Error ¶ added in v1.0.0
func (c *JSONMapConverter) Error() error
Error returns an error if the conversion fails.
func (*JSONMapConverter) InstanceGet ¶ added in v1.0.0
func (c *JSONMapConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*JSONMapConverter) InstanceSet ¶ added in v1.0.0
func (c *JSONMapConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*JSONMapConverter) InstanceSetValues ¶ added in v1.0.0
func (c *JSONMapConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*JSONMapConverter) Result ¶ added in v1.0.0
func (c *JSONMapConverter) Result() (map[string]interface{}, error)
Result returns the conversion result and error.
func (*JSONMapConverter) Value ¶ added in v1.0.0
func (c *JSONMapConverter) Value() map[string]interface{}
Value returns the conversion result.
type MapConverter ¶ added in v0.5.0
type MapConverter struct {
// contains filtered or unexported fields
}
MapConverter is a converter that converts a map type to another type.
func (*MapConverter) Convert ¶ added in v0.5.0
func (c *MapConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*MapConverter) Error ¶ added in v0.5.0
func (c *MapConverter) Error() error
Error returns an error if the conversion fails.
func (*MapConverter) InstanceGet ¶ added in v0.5.0
func (c *MapConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*MapConverter) InstanceSet ¶ added in v0.5.0
func (c *MapConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*MapConverter) InstanceSetValues ¶ added in v0.5.0
func (c *MapConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*MapConverter) Result ¶ added in v0.5.0
func (c *MapConverter) Result() (map[interface{}]interface{}, error)
Result returns the conversion result and error.
func (*MapConverter) Value ¶ added in v0.5.0
func (c *MapConverter) Value() map[interface{}]interface{}
Value returns the conversion result.
type RoundingFunc ¶ added in v1.1.0
RoundingFunc is a function that rounds from float to nearest integer. e.g. math.Floor
type SliceConverter ¶ added in v0.5.0
type SliceConverter struct {
// contains filtered or unexported fields
}
SliceConverter is a converter that converts a slice type to another type.
func (*SliceConverter) Convert ¶ added in v0.5.0
func (c *SliceConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*SliceConverter) Error ¶ added in v0.5.0
func (c *SliceConverter) Error() error
Error returns an error if the conversion fails.
func (*SliceConverter) FloatSlice ¶ added in v0.5.0
func (c *SliceConverter) FloatSlice() *FloatSliceConverter
FloatSlice converts the input to slice of float type.
func (*SliceConverter) InstanceGet ¶ added in v0.5.0
func (c *SliceConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*SliceConverter) InstanceSet ¶ added in v0.5.0
func (c *SliceConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*SliceConverter) InstanceSetValues ¶ added in v0.5.0
func (c *SliceConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*SliceConverter) IntSlice ¶ added in v0.5.0
func (c *SliceConverter) IntSlice() *IntegerSliceConverter
IntSlice converts the input to slice of int type.
func (*SliceConverter) Result ¶ added in v0.5.0
func (c *SliceConverter) Result() ([]interface{}, error)
Result returns the conversion result and error
func (*SliceConverter) StringSlice ¶ added in v0.5.0
func (c *SliceConverter) StringSlice() *StringSliceConverter
StringSlice converts the input to slice of string type.
func (*SliceConverter) UintSlice ¶ added in v0.5.0
func (c *SliceConverter) UintSlice() *UnsignedIntegerSliceConverter
UintSlice converts the input to slice of uint type.
func (*SliceConverter) Value ¶ added in v0.5.0
func (c *SliceConverter) Value() []interface{}
Value returns the conversion result.
type StringConverter ¶ added in v0.5.0
type StringConverter struct {
// contains filtered or unexported fields
}
StringConverter is a converter that converts a string type to another type.
func (*StringConverter) Convert ¶ added in v0.5.0
func (c *StringConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*StringConverter) Error ¶ added in v0.5.0
func (c *StringConverter) Error() error
Error returns an error if the conversion fails.
func (*StringConverter) InstanceGet ¶ added in v0.5.0
func (c *StringConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*StringConverter) InstanceSet ¶ added in v0.5.0
func (c *StringConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*StringConverter) InstanceSetValues ¶ added in v0.5.0
func (c *StringConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*StringConverter) Ptr ¶ added in v0.5.0
func (c *StringConverter) Ptr() *StringPtrConverter
Ptr converts the input to ptr type.
func (*StringConverter) Result ¶ added in v0.5.0
func (c *StringConverter) Result() (string, error)
Result returns the conversion result and error.
func (*StringConverter) Value ¶ added in v0.5.0
func (c *StringConverter) Value() string
Value returns the conversion result.
type StringPtrConverter ¶ added in v0.5.0
type StringPtrConverter struct {
// contains filtered or unexported fields
}
StringPtrConverter is a converter that converts a pointer of string type to another type.
func (*StringPtrConverter) Error ¶ added in v0.5.0
func (c *StringPtrConverter) Error() error
Error returns an error if the conversion fails.
func (*StringPtrConverter) InstanceGet ¶ added in v0.5.0
func (c *StringPtrConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*StringPtrConverter) InstanceSet ¶ added in v0.5.0
func (c *StringPtrConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*StringPtrConverter) InstanceSetValues ¶ added in v0.5.0
func (c *StringPtrConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*StringPtrConverter) Result ¶ added in v0.5.0
func (c *StringPtrConverter) Result() (*string, error)
Result returns the conversion result and error.
func (*StringPtrConverter) Value ¶ added in v0.5.0
func (c *StringPtrConverter) Value() *string
Value returns the conversion result.
type StringSliceConverter ¶ added in v0.5.0
type StringSliceConverter struct {
// contains filtered or unexported fields
}
StringSliceConverter is a converter that converts a slice of string type to another type.
func (*StringSliceConverter) Error ¶ added in v0.5.0
func (c *StringSliceConverter) Error() error
Error returns an error if the conversion fails.
func (*StringSliceConverter) InstanceGet ¶ added in v0.5.0
func (c *StringSliceConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*StringSliceConverter) InstanceSet ¶ added in v0.5.0
func (c *StringSliceConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*StringSliceConverter) InstanceSetValues ¶ added in v0.5.0
func (c *StringSliceConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*StringSliceConverter) Result ¶ added in v0.5.0
func (c *StringSliceConverter) Result() ([]string, error)
Result returns the conversion result and error.
func (*StringSliceConverter) Value ¶ added in v0.5.0
func (c *StringSliceConverter) Value() []string
Value returns the conversion result.
type StructConverter ¶ added in v0.5.0
type StructConverter struct {
// contains filtered or unexported fields
}
StructConverter is a converter that converts a struct type to another type.
func (*StructConverter) Convert ¶ added in v0.5.0
func (c *StructConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*StructConverter) InstanceGet ¶ added in v0.5.0
func (c *StructConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*StructConverter) InstanceSet ¶ added in v0.5.0
func (c *StructConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*StructConverter) InstanceSetValues ¶ added in v0.5.0
func (c *StructConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
type UnsignedIntegerConverter ¶ added in v0.5.0
type UnsignedIntegerConverter struct {
// contains filtered or unexported fields
}
UnsignedIntegerConverter is a converter that converts an unsigned integer type to another type.
func (*UnsignedIntegerConverter) Convert ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
func (*UnsignedIntegerConverter) Error ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) Error() error
Error returns an error if the conversion fails.
func (*UnsignedIntegerConverter) InstanceGet ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*UnsignedIntegerConverter) InstanceSet ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*UnsignedIntegerConverter) InstanceSetValues ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*UnsignedIntegerConverter) Ptr ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) Ptr() *UnsignedIntegerPtrConverter
Ptr converts the input to ptr type.
func (*UnsignedIntegerConverter) Result ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) Result() (uint64, error)
Result returns the conversion result and error.
func (*UnsignedIntegerConverter) Value ¶ added in v0.5.0
func (c *UnsignedIntegerConverter) Value() uint64
Value returns the conversion result.
type UnsignedIntegerPtrConverter ¶ added in v0.5.0
type UnsignedIntegerPtrConverter struct {
// contains filtered or unexported fields
}
UnsignedIntegerPtrConverter is a converter that converts a pointer of uint type to another type.
func (*UnsignedIntegerPtrConverter) Error ¶ added in v0.5.0
func (c *UnsignedIntegerPtrConverter) Error() error
Error returns an error if the conversion fails.
func (*UnsignedIntegerPtrConverter) InstanceGet ¶ added in v0.5.0
func (c *UnsignedIntegerPtrConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*UnsignedIntegerPtrConverter) InstanceSet ¶ added in v0.5.0
func (c *UnsignedIntegerPtrConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*UnsignedIntegerPtrConverter) InstanceSetValues ¶ added in v0.5.0
func (c *UnsignedIntegerPtrConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*UnsignedIntegerPtrConverter) Result ¶ added in v0.5.0
func (c *UnsignedIntegerPtrConverter) Result() (*uint64, error)
Result returns the conversion result and error.
func (*UnsignedIntegerPtrConverter) Value ¶ added in v0.5.0
func (c *UnsignedIntegerPtrConverter) Value() *uint64
Value returns the conversion result.
type UnsignedIntegerSliceConverter ¶ added in v0.5.0
type UnsignedIntegerSliceConverter struct {
// contains filtered or unexported fields
}
UnsignedIntegerSliceConverter is a converter that converts a slice of uint type to another type.
func (*UnsignedIntegerSliceConverter) Error ¶ added in v0.5.0
func (c *UnsignedIntegerSliceConverter) Error() error
Error returns an error if the conversion fails.
func (*UnsignedIntegerSliceConverter) InstanceGet ¶ added in v0.5.0
func (c *UnsignedIntegerSliceConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*UnsignedIntegerSliceConverter) InstanceSet ¶ added in v0.5.0
func (c *UnsignedIntegerSliceConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*UnsignedIntegerSliceConverter) InstanceSetValues ¶ added in v0.5.0
func (c *UnsignedIntegerSliceConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*UnsignedIntegerSliceConverter) Result ¶ added in v0.5.0
func (c *UnsignedIntegerSliceConverter) Result() ([]uint64, error)
Result returns the conversion result and error.
func (*UnsignedIntegerSliceConverter) Value ¶ added in v0.5.0
func (c *UnsignedIntegerSliceConverter) Value() []uint64
Value returns the conversion result.
type ValueConverter ¶ added in v0.5.0
type ValueConverter struct {
// contains filtered or unexported fields
}
ValueConverter is a converter that converts an interface type to another type.
func New ¶ added in v0.5.0
func New(i interface{}, fs ...func(*ConverterOpts)) *ValueConverter
New returns a new ValueConverter
Example ¶
i, err := New("-12").Int().Result() fmt.Printf("New(\"-12\").Int().Result() = (%#v, %#v)\n", i, err) err = New("abc").Int().Error() fmt.Printf("New(\"abc\").Error() = %#v\n", err.Error()) ip := New("-12").Int().Ptr().Value() fmt.Printf("*New(\"-12\").Int().Ptr().Value() = %#v", *ip)
Output: New("-12").Int().Result() = (-12, <nil>) New("abc").Error() = "Failed to convert from string to int64: fields=, value=\"abc\", error=strconv.ParseInt: parsing \"abc\": invalid syntax" *New("-12").Int().Ptr().Value() = -12
func (*ValueConverter) Bool ¶ added in v0.5.0
func (c *ValueConverter) Bool() *BoolConverter
Bool converts the input to bool type.
Example ¶
fmt.Println("int64 to bool") fmt.Printf("%v -> %v\n", math.MinInt64, New(math.MinInt64).Bool().Value()) fmt.Printf("%v -> %v\n", 0, New(0).Bool().Value()) fmt.Println() fmt.Println("uint64 to bool") fmt.Printf("%v -> %v\n", uint64(math.MaxUint64), New(uint64(math.MaxUint64)).Bool().Value()) fmt.Printf("%v -> %v\n", 0, New(0).Bool().Value()) fmt.Println() fmt.Println("float64 to bool") fmt.Printf("%v -> %v\n", -1*math.MaxFloat64, New(-1*math.MaxFloat64).Bool().Value()) fmt.Printf("%v -> %v\n", 0.0, New(0.0).Bool().Value()) fmt.Println() fmt.Println("bool to bool") fmt.Printf("%v -> %v\n", true, New(true).Bool().Value()) fmt.Printf("%v -> %v\n", false, New(false).Bool().Value()) fmt.Println() fmt.Println("string to bool") fmt.Printf("%#v -> %v\n", "aaaa", New("aaaa").Bool().Value()) fmt.Printf("%#v -> %v\n", "0", New("0").Bool().Value()) fmt.Printf("%#v -> %v\n", "", New("").Bool().Value())
Output: int64 to bool -9223372036854775808 -> true 0 -> false uint64 to bool 18446744073709551615 -> true 0 -> false float64 to bool -1.7976931348623157e+308 -> true 0 -> false bool to bool true -> true false -> false string to bool "aaaa" -> true "0" -> true "" -> false
func (*ValueConverter) BoolPtr ¶ added in v0.5.0
func (c *ValueConverter) BoolPtr() *BoolPtrConverter
BoolPtr converts the input to pointer of bool type.
func (*ValueConverter) Convert ¶ added in v0.5.0
func (c *ValueConverter) Convert(out interface{}) error
Convert converts the input to the out type and assigns it. If the conversion fails, the method returns an error.
Example ¶
fmt.Println("int") var i8 int8 if err := New(math.MaxInt16).Convert(&i8); err != nil { fmt.Println(err) } else { fmt.Println(i8) } if err := New(math.MinInt16).Convert(&i8); err != nil { fmt.Println(err) } else { fmt.Println(i8) } if err := New("24").Convert(&i8); err != nil { fmt.Println(err) } else { fmt.Println(i8) } var i16 int16 if err := New(math.MaxInt32).Convert(&i16); err != nil { fmt.Println(err) } else { fmt.Println(i16) } if err := New(math.MinInt32).Convert(&i16); err != nil { fmt.Println(err) } else { fmt.Println(i16) } if err := New("24").Convert(&i16); err != nil { fmt.Println(err) } else { fmt.Println(i16) } var i32 int32 if err := New(math.MaxInt64).Convert(&i32); err != nil { fmt.Println(err) } else { fmt.Println(i32) } if err := New(math.MinInt64).Convert(&i32); err != nil { fmt.Println(err) } else { fmt.Println(i32) } if err := New("24").Convert(&i32); err != nil { fmt.Println(err) } else { fmt.Println(i32) } var i64 int64 if err := New(math.MaxInt64).Convert(&i64); err != nil { fmt.Println(err) } else { fmt.Println(i64) } if err := New(math.MinInt64).Convert(&i64); err != nil { fmt.Println(err) } else { fmt.Println(i64) } if err := New("24").Convert(&i64); err != nil { fmt.Println(err) } else { fmt.Println(i64) } fmt.Println() fmt.Println("uint") var u8 uint8 if err := New(math.MaxUint16).Convert(&u8); err != nil { fmt.Println(err) } else { fmt.Println(u8) } if err := New("24").Convert(&u8); err != nil { fmt.Println(err) } else { fmt.Println(u8) } var u16 uint16 if err := New(math.MaxUint32).Convert(&u16); err != nil { fmt.Println(err) } else { fmt.Println(u16) } if err := New("24").Convert(&u16); err != nil { fmt.Println(err) } else { fmt.Println(u16) } var u32 uint32 if err := New(uint64(math.MaxUint64)).Convert(&u32); err != nil { fmt.Println(err) } else { fmt.Println(u32) } if err := New("24").Convert(&u32); err != nil { fmt.Println(err) } else { fmt.Println(u32) } var u64 uint64 if err := New(uint64(math.MaxUint64)).Convert(&u64); err != nil { fmt.Println(err) } else { fmt.Println(u64) } if err := New("24").Convert(&u64); err != nil { fmt.Println(err) } else { fmt.Println(u64) } fmt.Println() fmt.Println("float") var f32 float32 if err := New(math.MaxFloat64).Convert(&f32); err != nil { fmt.Println(err) } else { fmt.Println(f32) } if err := New("24").Convert(&f32); err != nil { fmt.Println(err) } else { fmt.Println(f32) } var f64 float64 if err := New(math.MaxFloat64).Convert(&f64); err != nil { fmt.Println(err) } else { fmt.Println(f64) } if err := New("24").Convert(&f64); err != nil { fmt.Println(err) } else { fmt.Println(f64) } fmt.Println() fmt.Println("string") var s string if err := New(123).Convert(&s); err != nil { fmt.Println(err) } else { fmt.Println(s) }
Output: int Failed to convert from int64 to int8: fields=, value=32767, error=overflows Failed to convert from int64 to int8: fields=, value=-32768, error=overflows 24 Failed to convert from int64 to int16: fields=, value=2147483647, error=overflows Failed to convert from int64 to int16: fields=, value=-2147483648, error=overflows 24 Failed to convert from int64 to int32: fields=, value=9223372036854775807, error=overflows Failed to convert from int64 to int32: fields=, value=-9223372036854775808, error=overflows 24 9223372036854775807 -9223372036854775808 24 uint Failed to convert from uint64 to uint8: fields=, value=0xffff, error=overflows 24 Failed to convert from uint64 to uint16: fields=, value=0xffffffff, error=overflows 24 Failed to convert from uint64 to uint32: fields=, value=0xffffffffffffffff, error=overflows 24 18446744073709551615 24 float Failed to convert from float64 to float32: fields=, value=1.7976931348623157e+308, error=overflows 24 1.7976931348623157e+308 24 string 123
func (*ValueConverter) Error ¶ added in v0.5.0
func (c *ValueConverter) Error() error
Error returns an error if the conversion fails.
func (*ValueConverter) Float ¶ added in v0.5.0
func (c *ValueConverter) Float() *FloatConverter
Float converts the input to float type.
Example ¶
fmt.Println("int64 to float64") fmt.Printf("%v -> %v\n", math.MaxInt64, New(math.MaxInt64).Float().Value()) fmt.Printf("%v -> %v\n", math.MinInt64, New(math.MinInt64).Float().Value()) fmt.Println() fmt.Println("uint64 to float64") fmt.Printf("%v -> %v\n", uint64(math.MaxUint64), New(uint64(math.MaxUint64)).Float().Value()) fmt.Println() fmt.Println("float64 to float64") fmt.Printf("%v -> %v\n", math.MaxFloat64, New(math.MaxFloat64).Float().Value()) fmt.Printf("%v -> %v\n", math.SmallestNonzeroFloat64, New(math.SmallestNonzeroFloat64).Float().Value()) fmt.Printf("%v -> %v\n", -1*math.MaxFloat64, New(-1*math.MaxFloat64).Float().Value()) fmt.Println() fmt.Println("bool to float64") fmt.Printf("%v -> %v\n", true, New(true).Float().Value()) fmt.Printf("%v -> %v\n", false, New(false).Float().Value()) fmt.Println() fmt.Println("string to float64") fmt.Printf("%#v -> %v\n", strconv.FormatFloat(math.MaxFloat64, 'f', 10, 64), New(strconv.FormatFloat(math.MaxFloat64, 'f', 10, 64)).Float().Value()) fmt.Printf("%#v -> %v\n", strconv.FormatFloat(1.79769e+308, 'e', 10, 64), New(strconv.FormatFloat(1.79769e+308, 'e', 10, 64)).Float().Value()) fmt.Printf("%#v -> %v\n", strconv.FormatFloat(math.MaxFloat64, 'e', 10, 64), New(strconv.FormatFloat(math.MaxFloat64, 'e', 10, 64)).Float().Value()) fmt.Printf("%#v\n", New("1.1.1").Float().Error().Error())
Output: int64 to float64 9223372036854775807 -> 9.223372036854776e+18 -9223372036854775808 -> -9.223372036854776e+18 uint64 to float64 18446744073709551615 -> 1.8446744073709552e+19 float64 to float64 1.7976931348623157e+308 -> 1.7976931348623157e+308 5e-324 -> 5e-324 -1.7976931348623157e+308 -> -1.7976931348623157e+308 bool to float64 true -> 1 false -> 0 string to float64 "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000" -> 1.7976931348623157e+308 "1.7976900000e+308" -> 1.79769e+308 "1.7976931349e+308" -> +Inf "Failed to convert from string to float64: fields=, value=\"1.1.1\", error=strconv.ParseFloat: parsing \"1.1.1\": invalid syntax"
func (*ValueConverter) FloatPtr ¶ added in v1.2.0
func (c *ValueConverter) FloatPtr() *FloatPtrConverter
FloatPtr converts the input to pointer of float type.
func (*ValueConverter) FloatSlice ¶ added in v0.5.0
func (c *ValueConverter) FloatSlice() *FloatSliceConverter
FloatSlice converts the input to slice of fload type.
func (*ValueConverter) FloatrPtr ¶ added in v0.5.0
func (c *ValueConverter) FloatrPtr() *FloatPtrConverter
FloatrPtr is a deprecated method. Please use FloatPtr.
func (*ValueConverter) InstanceGet ¶ added in v0.5.0
func (c *ValueConverter) InstanceGet(key string) interface{}
InstanceGet returns the value saved using InstanceSet.
func (*ValueConverter) InstanceSet ¶ added in v0.5.0
func (c *ValueConverter) InstanceSet(key string, value interface{})
InstanceSet saves the value by specifying the key.
func (*ValueConverter) InstanceSetValues ¶ added in v0.5.0
func (c *ValueConverter) InstanceSetValues(m map[string]interface{})
InstanceSetValues saves multiple key-value pairs.
func (*ValueConverter) Int ¶ added in v0.5.0
func (c *ValueConverter) Int() *IntegerConverter
Int converts the input to int type.
Example ¶
fmt.Println("int64 to int64") fmt.Printf("%v -> %v\n", math.MaxInt64, New(math.MaxInt64).Int().Value()) fmt.Printf("%v -> %v\n", math.MinInt64, New(math.MinInt64).Int().Value()) fmt.Println() fmt.Println("uint64 to int64") fmt.Printf("%v -> %v\n", uint64(123), New(uint64(123)).Int().Value()) fmt.Printf("%#v\n", New(uint64(math.MaxUint64)).Int().Error().Error()) fmt.Println() fmt.Println("float64 to int64") fmt.Printf("%v -> %v\n", 1.25, New(1.25).Int().Value()) fmt.Printf("%v -> %v\n", -1.25, New(-1.25).Int().Value()) fmt.Printf("%v -> %v\n", math.MaxInt32, New(float64(math.MaxInt32)).Int().Value()) fmt.Printf("%#v\n", New(float64(math.MaxInt64)).Int().Error().Error()) fmt.Printf("%#v\n", New(math.MaxFloat64).Int().Error().Error()) fmt.Println() fmt.Println("bool to int64") fmt.Printf("%v -> %v\n", true, New(true).Int().Value()) fmt.Printf("%v -> %v\n", false, New(false).Int().Value()) fmt.Println() fmt.Println("string to int64") fmt.Printf("\"%v\" -> %v\n", math.MaxInt64, New(strconv.FormatInt(math.MaxInt64, 10)).Int().Value()) fmt.Printf("%#v\n", New("1.5").Int().Error().Error()) fmt.Printf("%#v\n", New("-1.5").Int().Error().Error())
Output: int64 to int64 9223372036854775807 -> 9223372036854775807 -9223372036854775808 -> -9223372036854775808 uint64 to int64 123 -> 123 "Failed to convert from uint64 to int64: fields=, value=0xffffffffffffffff, error=overflows" float64 to int64 1.25 -> 1 -1.25 -> -2 2147483647 -> 2147483647 "Failed to convert from float64 to int64: fields=, value=9.223372036854776e+18, error=overflows" "Failed to convert from float64 to int64: fields=, value=1.7976931348623157e+308, error=overflows" bool to int64 true -> 1 false -> 0 string to int64 "9223372036854775807" -> 9223372036854775807 "Failed to convert from string to int64: fields=, value=\"1.5\", error=strconv.ParseInt: parsing \"1.5\": invalid syntax" "Failed to convert from string to int64: fields=, value=\"-1.5\", error=strconv.ParseInt: parsing \"-1.5\": invalid syntax"
func (*ValueConverter) IntPtr ¶ added in v0.5.0
func (c *ValueConverter) IntPtr() *IntegerPtrConverter
IntPtr converts the input to pointer of int type.
func (*ValueConverter) IntSlice ¶ added in v0.5.0
func (c *ValueConverter) IntSlice() *IntegerSliceConverter
IntSlice converts the input to slice of int type.
func (*ValueConverter) JSONMap ¶ added in v1.0.0
func (c *ValueConverter) JSONMap() *JSONMapConverter
JSONMap converts the input to json map type.
func (*ValueConverter) Map ¶ added in v0.5.0
func (c *ValueConverter) Map() *MapConverter
Map converts the input to map type.
Example ¶
type In struct { A int B string } var in = In{ A: 125, B: "25", } var s2 map[string]int err := New(in).Map().Convert(&s2) if err != nil { return } fmt.Printf("%#v\n", s2)
Output: map[string]int{"A":125, "B":25}
Example (Nested) ¶
type Nested2 struct { Z int } type Nested1 struct { Nested2 X string Y int } type In struct { A int B Nested1 } in := In{A: 1, B: Nested1{X: "x", Y: 2, Nested2: Nested2{Z: 3}}} m, err := New(in, WithMapMaxDepth(1)).Map().Result() if err != nil { fmt.Println(err) } else { fmt.Println(m) } m, err = New(in).Map().Result() if err != nil { fmt.Println(err) } else { fmt.Println(m) }
Output: map[A:1 B:map[Nested2:{3} X:x Y:2]] map[A:1 B:map[Nested2:map[Z:3] X:x Y:2]]
func (*ValueConverter) Model ¶ added in v0.6.0
func (c *ValueConverter) Model(t interface{}) *ValueConverter
Model converts the input to the specified type.
Example ¶
type In struct { X string } type Out struct { X int } in := In{X: "125"} if out, ok := New(in).Model(&Out{}).Value().(*Out); ok { fmt.Printf("%#v -> %#v", in, out) }
Output: henge.In{X:"125"} -> &henge.Out{X:125}
func (*ValueConverter) Result ¶ added in v0.5.0
func (c *ValueConverter) Result() (interface{}, error)
Result returns the conversion result and error.
func (*ValueConverter) Slice ¶ added in v0.5.0
func (c *ValueConverter) Slice() *SliceConverter
Slice converts the input to slice type.
Example ¶
var r1 []string if err := New([...]int{1, 2, 3}).Slice().Convert(&r1); err != nil { fmt.Println(err) } else { fmt.Printf("%#v -> %#v\n", [...]int{1, 2, 3}, r1) } var r2 [2]string if err := New([]int{1, 2, 3}).Slice().Convert(&r2); err != nil { fmt.Println(err) } else { fmt.Printf("%#v -> %#v\n", []int{1, 2, 3}, r2) } var r3 []int if err := New([]string{"1", "a"}).Slice().Convert(&r3); err != nil { fmt.Println(err) } else { fmt.Printf("%#v -> %#v\n", []string{"1", "a"}, r3) } type In struct { A string } type Out struct { A int } var out []Out if err := New([]In{{A: "123"}, {A: "234"}}).Convert(&out); err != nil { fmt.Println(err) } else { fmt.Printf("%#v -> %#v\n", []In{{A: "123"}, {A: "234"}}, out) }
Output: [3]int{1, 2, 3} -> []string{"1", "2", "3"} []int{1, 2, 3} -> [2]string{"1", "2"} Failed to convert from string to int: fields=[1], value="a", error=strconv.ParseInt: parsing "a": invalid syntax []henge.In{henge.In{A:"123"}, henge.In{A:"234"}} -> []henge.Out{henge.Out{A:123}, henge.Out{A:234}}
func (*ValueConverter) String ¶ added in v0.5.0
func (c *ValueConverter) String() *StringConverter
String converts the input to string type.
Example ¶
fmt.Println("int64 to string") fmt.Printf("%v -> %#v\n", math.MaxInt64, New(math.MaxInt64).String().Value()) fmt.Printf("%v -> %#v\n", math.MinInt64, New(math.MinInt64).String().Value()) fmt.Println() fmt.Println("uint64 to string") fmt.Printf("%v -> %#v\n", uint64(math.MaxUint64), New(uint64(math.MaxUint64)).String().Value()) fmt.Println() fmt.Println("float64 to string") fmt.Printf("%v -> %#v\n", 2.5, New(2.5).String().Value()) fmt.Printf("%v -> %#v\n", math.MaxFloat64, New(math.MaxFloat64).String().Value()) fmt.Printf("%v -> %#v\n", math.MaxFloat64, New(math.MaxFloat64, WithFloatFormat('e', 2)).String().Value()) fmt.Printf("%v -> %#v\n", math.SmallestNonzeroFloat64, New(math.SmallestNonzeroFloat64).String().Value()) fmt.Printf("%v -> %#v\n", -1*math.MaxFloat64, New(-1*math.MaxFloat64).String().Value()) fmt.Println() fmt.Println("bool to string") fmt.Printf("%v -> %#v\n", true, New(true).String().Value()) fmt.Printf("%v -> %#v\n", false, New(false).String().Value()) fmt.Println() fmt.Println("string to string") fmt.Printf("%v -> %v\n", "aaa", New("aaa").String().Value())
Output: int64 to string 9223372036854775807 -> "9223372036854775807" -9223372036854775808 -> "-9223372036854775808" uint64 to string 18446744073709551615 -> "18446744073709551615" float64 to string 2.5 -> "2.5" 1.7976931348623157e+308 -> "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" 1.7976931348623157e+308 -> "1.80e+308" 5e-324 -> "0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005" -1.7976931348623157e+308 -> "-179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" bool to string true -> "true" false -> "false" string to string aaa -> aaa
func (*ValueConverter) StringPtr ¶ added in v0.5.0
func (c *ValueConverter) StringPtr() *StringPtrConverter
StringPtr converts the input to pointer of string type.
func (*ValueConverter) StringSlice ¶ added in v0.5.0
func (c *ValueConverter) StringSlice() *StringSliceConverter
StringSlice converts the input to slice of string type.
func (*ValueConverter) Struct ¶ added in v0.5.0
func (c *ValueConverter) Struct() *StructConverter
Struct converts the input to struct type.
Example ¶
type In struct { A int B string } type Out struct { A string B *int } var in = In{ A: 125, B: "25", } var out Out if err := New(in).Struct().Convert(&out); err != nil { fmt.Println(err) } else { fmt.Printf("A = %#v\n", out.A) fmt.Printf("B = %#v\n", *out.B) }
Output: A = "125" B = 25
func (*ValueConverter) Uint ¶ added in v0.5.0
func (c *ValueConverter) Uint() *UnsignedIntegerConverter
Uint converts the input to uint type.
Example ¶
fmt.Println("int64 to uint64") fmt.Printf("%v -> %v\n", math.MaxInt64, New(math.MaxInt64).Uint().Value()) fmt.Printf("%#v\n", New(math.MinInt64).Uint().Error().Error()) fmt.Println() fmt.Println("uint64 to uint64") fmt.Printf("%v -> %v\n", uint64(math.MaxUint64), New(uint64(math.MaxUint64)).Uint().Value()) fmt.Println() fmt.Println("float64 to uint64") fmt.Printf("%v -> %v\n", 1.25, New(1.25).Uint().Value()) fmt.Printf("%#v\n", New(-1.25).Uint().Error().Error()) fmt.Printf("%v -> %v\n", math.MaxUint32, New(float64(math.MaxUint32)).Uint().Value()) fmt.Printf("%#v\n", New(float64(math.MaxUint64)).Uint().Error().Error()) fmt.Printf("%#v\n", New(math.MaxFloat64).Uint().Error().Error()) fmt.Println() fmt.Println("bool to uint64") fmt.Printf("%v -> %v\n", true, New(true).Uint().Value()) fmt.Printf("%v -> %v\n", false, New(false).Uint().Value()) fmt.Println() fmt.Println("string to uint64") fmt.Printf("\"%v\" -> %v\n", uint64(math.MaxUint64), New(strconv.FormatUint(math.MaxUint64, 10)).Uint().Value()) fmt.Printf("%#v\n", New("1.5").Uint().Error().Error()) fmt.Printf("%#v\n", New("-2").Uint().Error().Error())
Output: int64 to uint64 9223372036854775807 -> 9223372036854775807 "Failed to convert from int to uint64: fields=, value=-9223372036854775808, error=negative number" uint64 to uint64 18446744073709551615 -> 18446744073709551615 float64 to uint64 1.25 -> 1 "Failed to convert from float64 to uint64: fields=, value=-1.25, error=negative number" 4294967295 -> 4294967295 "Failed to convert from float64 to uint64: fields=, value=1.8446744073709552e+19, error=overflows" "Failed to convert from float64 to uint64: fields=, value=1.7976931348623157e+308, error=overflows" bool to uint64 true -> 1 false -> 0 string to uint64 "18446744073709551615" -> 18446744073709551615 "Failed to convert from string to uint64: fields=, value=\"1.5\", error=strconv.ParseUint: parsing \"1.5\": invalid syntax" "Failed to convert from string to uint64: fields=, value=\"-2\", error=strconv.ParseUint: parsing \"-2\": invalid syntax"
func (*ValueConverter) UintPtr ¶ added in v0.5.0
func (c *ValueConverter) UintPtr() *UnsignedIntegerPtrConverter
UintPtr converts the input to pointer of uint type.
func (*ValueConverter) UintSlice ¶ added in v0.5.0
func (c *ValueConverter) UintSlice() *UnsignedIntegerSliceConverter
UintSlice converts the input to slice of uint type.
func (*ValueConverter) Value ¶ added in v0.5.0
func (c *ValueConverter) Value() interface{}
Value returns the conversion result.