Documentation ¶
Index ¶
- Constants
- Variables
- func AssignReflect(assigner reflect.Value, val reflect.Value, funcOpts ...FuncOption) (err error)
- func Clone(val reflect.Value) (res reflect.Value)
- func CloneInterface(input interface{}) (res interface{})
- func ExtractBool(val reflect.Value, fnOpts ...FuncOption) (res bool, err error)
- func ExtractComplex(val reflect.Value, fnOpts ...FuncOption) (result complex128, err error)
- func ExtractDuration(val reflect.Value, fnOpts ...FuncOption) (result time.Duration, err error)
- func ExtractFloat(val reflect.Value, fnOpts ...FuncOption) (result float64, err error)
- func ExtractIP(val reflect.Value, fnOpts ...FuncOption) (result net.IP, err error)
- func ExtractInt(val reflect.Value, fnOpts ...FuncOption) (result int64, err error)
- func ExtractString(val reflect.Value, fnOpts ...FuncOption) (result string, err error)
- func ExtractTime(val reflect.Value, fnOpts ...FuncOption) (result time.Time, err error)
- func ExtractURL(val reflect.Value, fnOpts ...FuncOption) (result *url.URL, err error)
- func ExtractUint(val reflect.Value, fnOpts ...FuncOption) (result uint64, err error)
- func GetBool(input interface{}, fnOpts ...FuncOption) (res bool)
- func GetChildElem(val reflect.Value) (res reflect.Value)
- func GetChildElemPtrKind(val reflect.Value) (res reflect.Kind)
- func GetChildElemPtrType(val reflect.Value) (typ reflect.Type)
- func GetChildElemPtrTypeOfType(input reflect.Type) (typ reflect.Type)
- func GetChildElemType(val reflect.Value) (typ reflect.Type)
- func GetChildElemTypeKind(val reflect.Value) (res reflect.Kind)
- func GetChildElemTypeOfType(input reflect.Type) (typ reflect.Type)
- func GetChildElemValueKind(val reflect.Value) (res reflect.Kind)
- func GetChildElemValueType(val reflect.Value) (typ reflect.Type)
- func GetChildNilElem(val reflect.Value) (res reflect.Value)
- func GetChildPtrElem(val reflect.Value) (res reflect.Value)
- func GetComplex(input interface{}, fnOpts ...FuncOption) (result complex128)
- func GetDuration(input interface{}, fnOpts ...FuncOption) (result time.Duration)
- func GetElem(val reflect.Value) (res reflect.Value)
- func GetElemKind(val reflect.Value) (res reflect.Kind)
- func GetElemType(val reflect.Value) (typ reflect.Type)
- func GetElemTypeOfType(input reflect.Type) (typ reflect.Type)
- func GetFloat(input interface{}, fnOpts ...FuncOption) (result float64)
- func GetIP(input interface{}, fnOpts ...FuncOption) (result net.IP)
- func GetInitChildElem(val reflect.Value) (res reflect.Value)
- func GetInitChildPtrElem(val reflect.Value) (res reflect.Value)
- func GetInitElem(val reflect.Value) (res reflect.Value)
- func GetInt(input interface{}, fnOpts ...FuncOption) (result int64)
- func GetKind(val reflect.Value) (res reflect.Kind)
- func GetNilElem(val reflect.Value) (res reflect.Value)
- func GetString(input interface{}, fnOpts ...FuncOption) (result string)
- func GetTime(input interface{}, fnOpts ...FuncOption) (result time.Time)
- func GetType(val reflect.Value) (typ reflect.Type)
- func GetURL(input interface{}, fnOpts ...FuncOption) (result *url.URL)
- func GetUint(input interface{}, fnOpts ...FuncOption) (result uint64)
- func InitNew(val reflect.Value) (res reflect.Value)
- func IsArrayZero(v reflect.Value) bool
- func IsInterfaceReflectZero(val interface{}) (result bool)
- func IsKindArray(kind reflect.Kind) bool
- func IsKindBool(kind reflect.Kind) bool
- func IsKindChan(kind reflect.Kind) bool
- func IsKindComplex(kind reflect.Kind) bool
- func IsKindFloat(kind reflect.Kind) bool
- func IsKindInt(kind reflect.Kind) bool
- func IsKindInterface(kind reflect.Kind) bool
- func IsKindList(kind reflect.Kind) bool
- func IsKindMap(kind reflect.Kind) bool
- func IsKindNil(kind reflect.Kind) bool
- func IsKindPtr(kind reflect.Kind) bool
- func IsKindSlice(kind reflect.Kind) bool
- func IsKindString(kind reflect.Kind) bool
- func IsKindStruct(kind reflect.Kind) bool
- func IsKindTypeElemable(kind reflect.Kind) bool
- func IsKindUint(kind reflect.Kind) bool
- func IsKindUnsafePointer(kind reflect.Kind) bool
- func IsKindValueBytesSlice(val reflect.Value) bool
- func IsKindValueElemable(kind reflect.Kind) bool
- func IsKindValueNil(val reflect.Value) bool
- func IsNil(val interface{}) bool
- func IsPtr(in interface{}) bool
- func IsPtrValueZero(val reflect.Value) bool
- func IsReflectZero(val reflect.Value) (result bool)
- func IsStructZero(v reflect.Value) bool
- func IsTimeZero(t time.Time) bool
- func IsTypeElemable(typ reflect.Type) (res bool)
- func IsTypeValueDuration(val reflect.Value) bool
- func IsTypeValueElemable(val reflect.Value) bool
- func IsTypeValueIP(val reflect.Value) bool
- func IsTypeValueTime(val reflect.Value) bool
- func IsTypeValueURL(val reflect.Value) bool
- func IsValueElemable(val reflect.Value) bool
- func IsValueElemableParentElem(res reflect.Value) bool
- func IsValueNil(val reflect.Value) bool
- func IsValueZero(v reflect.Value) bool
- func IsZero(k interface{}) bool
- func ParseTime(timeTxt string, fnOpts ...FuncOption) (result time.Time, err error)
- func RecoverFn(err *error)
- func SetReflectZero(val reflect.Value)
- func TryExtract(val reflect.Value, fnOpts ...FuncOption) (result interface{}, err error)
- func TryGet(input interface{}, fnOpts ...FuncOption) (result interface{})
- func UnwrapInterfaceValue(val reflect.Value) (res reflect.Value)
- type FuncAssigner
- type FuncOption
- func WithBaseSystem(base int) FuncOption
- func WithBitSize(intFloat, complex int) FuncOption
- func WithBlockChannel(input bool) FuncOption
- func WithConcurrency(input bool) FuncOption
- func WithCustomAssigner(fn FuncAssigner, continueAssignOnErr bool) FuncOption
- func WithDecoderConfig(cfg *mapstructure.DecoderConfig) FuncOption
- func WithFloatConfiguration(floatPrec int, floatFormat byte) FuncOption
- func WithIgnoreError(input bool) FuncOption
- func WithPanicRecoverer(input bool) FuncOption
- func WithTimeLayouts(timeLayouts ...string) FuncOption
- type IterArraySliceFn
- type IterChanFn
- type IterMapFn
- type IterStructFn
- type Option
- type Value
- func (s *Value) Assign(fnOpts ...FuncOption) *Value
- func (s *Value) Error() error
- func (s *Value) IterateArraySlice(fns ...IterArraySliceFn) *Value
- func (s *Value) IterateChan(fns ...IterChanFn) *Value
- func (s *Value) IterateMap(fns ...IterMapFn) *Value
- func (s *Value) IterateStruct(fns ...IterStructFn) *Value
- type Zeroable
Constants ¶
const ( ZeroTime0 = "0000-00-00 00:00:00" ZeroTime1 = "0001-01-01 00:00:00" )
List of constants for the zero time.
const ( // DefaultFloatPrecision specifies the default precision used in this package. // This is the default maximum precision. DefaultFloatPrecision = -1 // DefaultBitSize is the default bit size used for the conversion in this package. DefaultBitSize = 64 // DefaultComplexBitSize is the default bit size for the complex128 type. DefaultComplexBitSize = 128 // DefaultBaseSystem is the default base system used for decimal in this package. DefaultBaseSystem = 10 // DefaultFloatFormat is the default format used for formmating float value in string DefaultFloatFormat = 'g' )
Variables ¶
var ( TypeRuneSlice = reflect.TypeOf([]rune{}) TypeByteSlice = reflect.TypeOf([]byte{}) TypeTimePtr = reflect.TypeOf(new(time.Time)) TypeTime = reflect.TypeOf(time.Time{}) TypeDurationPtr = reflect.TypeOf(new(time.Duration)) TypeDuration = reflect.TypeOf(time.Duration(0)) TypeURLPtr = reflect.TypeOf(new(url.URL)) TypeURL = reflect.TypeOf(url.URL{}) TypeIPPtr = reflect.TypeOf(new(net.IP)) TypeIP = reflect.TypeOf(net.IP{}) )
List of reflect.Type used in this package
var (
ErrAssignerCantSet = errors.New("assigner doesn't have the ability to set the value")
)
List of all errors for reflecthelper.
Functions ¶
func AssignReflect ¶
AssignReflect assigns the val of the reflect.Value to the assigner. This function asserts that the assigner Kind is same as the val Kind.
func Clone ¶
Clone clones the current underlying value of val as a reflect.Value. Clone can't clone unexported struct field because it is inaccessible.
func CloneInterface ¶ added in v4.3.0
func CloneInterface(input interface{}) (res interface{})
CloneInterface is like Clone but it accepts input and returns output as interface{}.
func ExtractBool ¶
func ExtractBool(val reflect.Value, fnOpts ...FuncOption) (res bool, err error)
ExtractBool extract the underlying bool value from the val of reflect.Value.
func ExtractComplex ¶
func ExtractComplex(val reflect.Value, fnOpts ...FuncOption) (result complex128, err error)
ExtractComplex gets the underlying complex value from val of reflect.Value.
func ExtractDuration ¶ added in v4.1.0
ExtractDuration extracts time.Duration from val of reflect.Value.
func ExtractFloat ¶
func ExtractFloat(val reflect.Value, fnOpts ...FuncOption) (result float64, err error)
ExtractFloat extracts the underlying float value from val of reflect.Value.
func ExtractInt ¶
func ExtractInt(val reflect.Value, fnOpts ...FuncOption) (result int64, err error)
ExtractInt gets the underlying int value from val of reflect.Value.
func ExtractString ¶
func ExtractString(val reflect.Value, fnOpts ...FuncOption) (result string, err error)
ExtractString gets the underlying string value from val of reflect.Value.
func ExtractTime ¶
ExtractTime extracts time.Time from val of reflect.Value.
func ExtractURL ¶ added in v4.4.0
ExtractURL extracts *url.URL from val of reflect.Value.
func ExtractUint ¶
func ExtractUint(val reflect.Value, fnOpts ...FuncOption) (result uint64, err error)
ExtractUint extracts the underlying uint value from val of reflect.Value.
func GetBool ¶
func GetBool(input interface{}, fnOpts ...FuncOption) (res bool)
GetBool accepts input as interface{}. GetBool is ExtractBool without error.
func GetChildElem ¶
GetChildElem is similar with GetInitChildElem but without initialize the child elem.
func GetChildElemPtrKind ¶
GetChildElemPtrKind gets the child elements' (root child) ptr kind of the val of reflect.Value.
func GetChildElemPtrType ¶
GetChildElemPtrType returns the child elems' (root child) ptr type of the val of reflect.Value.
func GetChildElemPtrTypeOfType ¶
GetChildElemPtrTypeOfType returns the child elems' (root child) ptr type of the input of reflect.Type.
func GetChildElemType ¶
GetChildElemType returns the child elems' (root child) type of the val of reflect.Value.
func GetChildElemTypeKind ¶
GetChildElemTypeKind returns the child elems' (root child) kind of the type of val reflect.Value.
func GetChildElemTypeOfType ¶
GetChildElemTypeOfType returns the child elems' (root child) type of the input of reflect.Type.
func GetChildElemValueKind ¶
GetChildElemValueKind gets the child elements' (root child) kind of the val reflect.Value and it only works on ptr kind.
func GetChildElemValueType ¶
GetChildElemValueType returns the child elem's (root child) type of the val reflect.Value and it only works on ptr kind.
func GetChildNilElem ¶
GetChildNilElem is similar with GetChildElem but it uses GetNilElem function.
func GetChildPtrElem ¶
GetChildPtrElem is similar with GetChildElem but the function stops when the elem is ptr and the elem of that ptr is non ptr.
func GetComplex ¶
func GetComplex(input interface{}, fnOpts ...FuncOption) (result complex128)
GetComplex accepts input as interface{}. GetComplex is ExtractComplex without error.
func GetDuration ¶ added in v4.1.0
func GetDuration(input interface{}, fnOpts ...FuncOption) (result time.Duration)
GetDuration accepts input as interface{}. GetDuration is ExtractDuration without error.
func GetElem ¶
GetElem gets the elem of the pointer val without initialize the pointer val. GetElem is similar to GetInitElem but without initialization.
func GetElemKind ¶
GetElemKind gets the elem kind from the val of reflect.Value.
func GetElemType ¶
GetElemType returns the elem type of a val of reflect.Value.
func GetElemTypeOfType ¶
GetElemTypeOfType returns the elem type of the input of reflect.Type.
func GetFloat ¶
func GetFloat(input interface{}, fnOpts ...FuncOption) (result float64)
GetFloat accepts input as interface{}. GetFloat is ExtractFloat without error.
func GetIP ¶ added in v4.4.0
func GetIP(input interface{}, fnOpts ...FuncOption) (result net.IP)
GetIP accepts input as interface{}. GetIP is ExtractIP without error.
func GetInitChildElem ¶
GetInitChildElem gets the child elem (root child) if it is a pointer with an element of pointer. It also initializes the child elem if it is CanSet and IsNil.
func GetInitChildPtrElem ¶
GetInitChildPtrElem is similar with GetInitChildElem but the function stops when the elem is ptr and the elem of that ptr is non ptr.
func GetInitElem ¶
GetInitElem gets the element of a pointer value. It initialize the element of a pointer value if it is nil.
func GetInt ¶
func GetInt(input interface{}, fnOpts ...FuncOption) (result int64)
GetInt accepts input as interface{}. GetInt is ExtractInt without error.
func GetNilElem ¶
GetNilElem is similar with GetElem but it doesn't check if reflect.Ptr or reflect.Interface is nil.
func GetString ¶
func GetString(input interface{}, fnOpts ...FuncOption) (result string)
GetString accepts input as interface{}. GetString is ExtractString without error.
func GetTime ¶
func GetTime(input interface{}, fnOpts ...FuncOption) (result time.Time)
GetTime accepts input as interface{}. GetTime is ExtractTime without error.
func GetURL ¶ added in v4.4.0
func GetURL(input interface{}, fnOpts ...FuncOption) (result *url.URL)
GetURL accepts input as interface{}. GetURL is ExtractURL without error.
func GetUint ¶
func GetUint(input interface{}, fnOpts ...FuncOption) (result uint64)
GetUint accepts input as interface{}. GetUint is ExtractUint without error.
func IsInterfaceReflectZero ¶
func IsInterfaceReflectZero(val interface{}) (result bool)
IsInterfaceReflectZero checks whether the interface is nil. It also checks if the reflect.Value of val is reflect.Zero of it's type.
func IsKindArray ¶
IsKindArray checks whether the kind is array or not.
func IsKindBool ¶
IsKindBool checks whether the kind is bool or not.
func IsKindChan ¶
IsKindChan checks whether the input kind is reflect.Chan.
func IsKindComplex ¶
IsKindComplex checks whether the kind is complex or not.
func IsKindFloat ¶
IsKindFloat checks whether the kind is float or not.
func IsKindInterface ¶ added in v4.5.0
IsKindInterface checks whether the input kind is reflect.Interface.
func IsKindList ¶
IsKindList checks whether the kind is array or slice.
func IsKindSlice ¶
IsKindSlice checks whether the kind is slice or not.
func IsKindString ¶
IsKindString checks whether the kind is string or not.
func IsKindStruct ¶
IsKindStruct checks whether the input kind is reflect.Struct.
func IsKindTypeElemable ¶
IsKindTypeElemable checks the kind of reflect.Type that can call Elem method.
func IsKindUint ¶
IsKindUint checks whether the kind is uint or not.
func IsKindUnsafePointer ¶
IsKindUnsafePointer checks whether the kind is unsafe ptr or not.
func IsKindValueBytesSlice ¶
IsKindValueBytesSlice checks whether the val of reflect.Value is byte slice.
func IsKindValueElemable ¶
IsKindValueElemable checks the kind of reflect.Value that can call Elem method.
func IsKindValueNil ¶ added in v4.1.0
IsKindValueNil checks whether the input val of reflect.Value can call IsNil method.
func IsNil ¶ added in v4.1.0
func IsNil(val interface{}) bool
IsNil checks whether the input val is nil for any type.
func IsPtr ¶ added in v4.6.2
func IsPtr(in interface{}) bool
IsPtr checks whether the input interface{} is a reflect.Ptr or not. The pointer in golang can be represented by reflect.Ptr.
func IsPtrValueZero ¶
IsPtrValueZero overrides the default behavior for the reflect.Ptr case in the IsValueZero method.
func IsReflectZero ¶
IsReflectZero check if the val of reflect.Value is a reflect.Zero of it's type. This operation is solely based on reflect.Zero, not to check if the actual underlying value is zero. To check if the value of reflect.Value is zero, use IsZero or IsValueZero.
func IsStructZero ¶
IsStructZero checks if the struct is zero.
func IsTypeElemable ¶
IsTypeElemable checks wether the typ of reflect.Type can call Elem method.
func IsTypeValueDuration ¶ added in v4.1.0
IsTypeValueDuration checks whether the type of val reflect.Value is time.Duration or *time.Duration.
func IsTypeValueElemable ¶
IsTypeValueElemable checks if the type of the reflect.Value can call Elem.
func IsTypeValueIP ¶ added in v4.4.0
IsTypeValueIP checks whether the type of val reflect.Value is net.IP or *net.IP.
func IsTypeValueTime ¶ added in v4.1.0
IsTypeValueTime checks whether the type of val reflect.Value is time.Time or *time.Time.
func IsTypeValueURL ¶ added in v4.4.0
IsTypeValueURL checks whether the type of val reflect.Value is url.URL or *url.URL.
func IsValueElemable ¶
IsValueElemable checks whether the val of reflect.Value could call Elem method.
func IsValueElemableParentElem ¶
IsValueElemableParentElem checks whether the res have elemable kind for parent and elem.
func IsValueNil ¶ added in v4.1.0
IsValueNil checks whether the input val of reflect.Value is nil for any type.
func IsValueZero ¶
IsValueZero check the reflect.Value if it is zero based on it's kind.
func ParseTime ¶
func ParseTime(timeTxt string, fnOpts ...FuncOption) (result time.Time, err error)
ParseTime parses the timeTxt string to the time.Time using various formats.
func RecoverFn ¶
func RecoverFn(err *error)
RecoverFn is used to recover from panic situation by passing the pointer of the error.
func SetReflectZero ¶
SetReflectZero sets the val to the reflect.Zero of its type.
func TryExtract ¶
func TryExtract(val reflect.Value, fnOpts ...FuncOption) (result interface{}, err error)
TryExtract tries to extract the real value from the val of reflect.Value.
func TryGet ¶
func TryGet(input interface{}, fnOpts ...FuncOption) (result interface{})
TryGet accepts input as interface{}. TryGet is TryExtract without error.
func UnwrapInterfaceValue ¶
UnwrapInterfaceValue unwraps the elem of val reflect.Value with the kind reflect.Interface. If the val of reflect.Value contains multi level interface, then it unwraps until the child of val reflect.Value doesn't have the kind of reflect.Interface.
Types ¶
type FuncAssigner ¶ added in v4.6.0
FuncAssigner is a custom function to assign from the val of reflect.Value to the assigner of reflect.Value. This function will be passed with one more argument, that is option. This function will return the error from inside the function.
type FuncOption ¶
type FuncOption func(o *Option)
FuncOption is a function option to set the Option for function arguments.
func WithBaseSystem ¶
func WithBaseSystem(base int) FuncOption
WithBaseSystem sets the default base system of Option.
func WithBitSize ¶
func WithBitSize(intFloat, complex int) FuncOption
WithBitSize sets the bit size of integer and complex.
func WithBlockChannel ¶
func WithBlockChannel(input bool) FuncOption
WithBlockChannel toggles the blocking operation of receive from reflect.Value with kind reflect.Chan. WithBlockChannel will use the Recv() method instead of TryRecv(). The default behavior for this package is false.
func WithConcurrency ¶
func WithConcurrency(input bool) FuncOption
WithConcurrency toggles the concurrency mode in this package, especially in the iteration. This toggles to iterate array, slice, map, or struct elements in concurrent mode. The default behavior for this package is false.
func WithCustomAssigner ¶ added in v4.6.0
func WithCustomAssigner(fn FuncAssigner, continueAssignOnErr bool) FuncOption
WithCustomAssigner sets the custom function of assigning value to the Option. This function also accepts the continueAssignOnErr param to make sure that the assignment inside this package still continues even the custom assigner returns error. The custom assigner is only needed if the assignment process is complex.
func WithDecoderConfig ¶
func WithDecoderConfig(cfg *mapstructure.DecoderConfig) FuncOption
WithDecoderConfig assigns the mapstructure decoder config for map assignment. DecoderConfig will be assigned Result (output) in the process of assignment.
func WithFloatConfiguration ¶
func WithFloatConfiguration(floatPrec int, floatFormat byte) FuncOption
WithFloatConfiguration assign the float configuration to the option.
func WithIgnoreError ¶
func WithIgnoreError(input bool) FuncOption
WithIgnoreError toggles for ignoring error in the struct, slice, array, and map iteration. The default behavior for this package is false.
func WithPanicRecoverer ¶
func WithPanicRecoverer(input bool) FuncOption
WithPanicRecoverer toggles the panic recoverer in all of the packages' functions. The default behavior for this package is false.
func WithTimeLayouts ¶
func WithTimeLayouts(timeLayouts ...string) FuncOption
WithTimeLayouts sets the time layouts for the Option.
type IterArraySliceFn ¶
IterArraySliceFn is a function type to iterate each field of array or slice and returning an error if needed.
type IterChanFn ¶
IterChanFn is a function type to iterate each value received by a channel and returning an error if needed.
type IterMapFn ¶
IterMapFn is a function type to iterate each key and element of map and returning an error if needed.
type IterStructFn ¶
IterStructFn is a function type to iterate each field of structInput and returning an error if needed.
type Option ¶
type Option struct { // Affected by Default() method FloatPrecision int FloatFormat byte BitSize int ComplexBitSize int BaseSystem int TimeLayouts []string DecoderConfig *mapstructure.DecoderConfig // Not affected by Default() method IgnoreError bool RecoverPanic bool BlockChannelIteration bool ConcurrentMode bool FnAssigner FuncAssigner ContinueAssignOnError bool // contains filtered or unexported fields }
Option is a collection of argument options used in this package.
func NewDefaultOption ¶
func NewDefaultOption() *Option
NewDefaultOption initialize the new default option.
func (*Option) Assign ¶
func (o *Option) Assign(fnOpts ...FuncOption) *Option
Assign assigns the functional options to the Option.
type Value ¶
Value is a custom struct for representing reflect.Value.
func Cast ¶
func Cast(val reflect.Value, fnOpts ...FuncOption) (res Value)
Cast casts the val of reflect.Value to the Value of this package.
func (*Value) Assign ¶
func (s *Value) Assign(fnOpts ...FuncOption) *Value
Assign assigns the function options to the s.opt.
func (*Value) IterateArraySlice ¶
func (s *Value) IterateArraySlice(fns ...IterArraySliceFn) *Value
IterateArraySlice iterates the element of slice or array using the IterArraySliceFn.
func (*Value) IterateChan ¶
func (s *Value) IterateChan(fns ...IterChanFn) *Value
IterateChan iterates the received elements using IterChanFn.
func (*Value) IterateMap ¶
IterateMap iterates the element of map using the IterMapFn.
func (*Value) IterateStruct ¶
func (s *Value) IterateStruct(fns ...IterStructFn) *Value
IterateStruct iterates the struct field using the IterStructFn.