reflecthelper

package module
v4.6.2 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2022 License: GPL-3.0 Imports: 13 Imported by: 3

README

بِسْمِ اللّٰهِ الرَّحْمٰنِ الرَّحِيْمِ


السَّلاَمُ عَلَيْكُمْ وَرَحْمَةُ اللهِ وَبَرَكَاتُهُ


ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ

ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ


اللَّهُمَّ صَلِّ عَلَى مُحَمَّدٍ ، وَعَلَى آلِ مُحَمَّدٍ ، كَمَا صَلَّيْتَ عَلَى إِبْرَاهِيمَ وَعَلَى آلِ إِبْرَاهِيمَ ، إِنَّكَ حَمِيدٌ مَجِيدٌ ، اللَّهُمَّ بَارِكْ عَلَى مُحَمَّدٍ ، وَعَلَى آلِ مُحَمَّدٍ ، كَمَا بَارَكْتَ عَلَى إِبْرَاهِيمَ ، وَعَلَى آلِ إِبْرَاهِيمَ ، إِنَّكَ حَمِيدٌ مَجِيدٌ

reflecthelper

Coverage Status CircleCI Go Report Card

Reflecthelper is a collection of helper function that works with Reflect in go-lang.

Documentation

Index

Constants

View Source
const (
	ZeroTime0 = "0000-00-00 00:00:00"
	ZeroTime1 = "0001-01-01 00:00:00"
)

List of constants for the zero time.

View Source
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

View Source
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

View Source
var (
	ErrAssignerCantSet = errors.New("assigner doesn't have the ability to set the value")
)

List of all errors for reflecthelper.

Functions

func AssignReflect

func AssignReflect(assigner reflect.Value, val reflect.Value, funcOpts ...FuncOption) (err error)

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

func Clone(val reflect.Value) (res reflect.Value)

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

func ExtractDuration(val reflect.Value, fnOpts ...FuncOption) (result time.Duration, err error)

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 ExtractIP added in v4.4.0

func ExtractIP(val reflect.Value, fnOpts ...FuncOption) (result net.IP, err error)

ExtractIP extracts net.IP 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

func ExtractTime(val reflect.Value, fnOpts ...FuncOption) (result time.Time, err error)

ExtractTime extracts time.Time from val of reflect.Value.

func ExtractURL added in v4.4.0

func ExtractURL(val reflect.Value, fnOpts ...FuncOption) (result *url.URL, err error)

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

func GetChildElem(val reflect.Value) (res reflect.Value)

GetChildElem is similar with GetInitChildElem but without initialize the child elem.

func GetChildElemPtrKind

func GetChildElemPtrKind(val reflect.Value) (res reflect.Kind)

GetChildElemPtrKind gets the child elements' (root child) ptr kind of the val of reflect.Value.

func GetChildElemPtrType

func GetChildElemPtrType(val reflect.Value) (typ reflect.Type)

GetChildElemPtrType returns the child elems' (root child) ptr type of the val of reflect.Value.

func GetChildElemPtrTypeOfType

func GetChildElemPtrTypeOfType(input reflect.Type) (typ reflect.Type)

GetChildElemPtrTypeOfType returns the child elems' (root child) ptr type of the input of reflect.Type.

func GetChildElemType

func GetChildElemType(val reflect.Value) (typ reflect.Type)

GetChildElemType returns the child elems' (root child) type of the val of reflect.Value.

func GetChildElemTypeKind

func GetChildElemTypeKind(val reflect.Value) (res reflect.Kind)

GetChildElemTypeKind returns the child elems' (root child) kind of the type of val reflect.Value.

func GetChildElemTypeOfType

func GetChildElemTypeOfType(input reflect.Type) (typ reflect.Type)

GetChildElemTypeOfType returns the child elems' (root child) type of the input of reflect.Type.

func GetChildElemValueKind

func GetChildElemValueKind(val reflect.Value) (res reflect.Kind)

GetChildElemValueKind gets the child elements' (root child) kind of the val reflect.Value and it only works on ptr kind.

func GetChildElemValueType

func GetChildElemValueType(val reflect.Value) (typ reflect.Type)

GetChildElemValueType returns the child elem's (root child) type of the val reflect.Value and it only works on ptr kind.

func GetChildNilElem

func GetChildNilElem(val reflect.Value) (res reflect.Value)

GetChildNilElem is similar with GetChildElem but it uses GetNilElem function.

func GetChildPtrElem

func GetChildPtrElem(val reflect.Value) (res reflect.Value)

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

func GetElem(val reflect.Value) (res reflect.Value)

GetElem gets the elem of the pointer val without initialize the pointer val. GetElem is similar to GetInitElem but without initialization.

func GetElemKind

func GetElemKind(val reflect.Value) (res reflect.Kind)

GetElemKind gets the elem kind from the val of reflect.Value.

func GetElemType

func GetElemType(val reflect.Value) (typ reflect.Type)

GetElemType returns the elem type of a val of reflect.Value.

func GetElemTypeOfType

func GetElemTypeOfType(input reflect.Type) (typ reflect.Type)

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

func GetInitChildElem(val reflect.Value) (res reflect.Value)

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

func GetInitChildPtrElem(val reflect.Value) (res reflect.Value)

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

func GetInitElem(val reflect.Value) (res reflect.Value)

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 GetKind

func GetKind(val reflect.Value) (res reflect.Kind)

GetKind gets the kind of the val of reflect.Value.

func GetNilElem

func GetNilElem(val reflect.Value) (res reflect.Value)

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 GetType

func GetType(val reflect.Value) (typ reflect.Type)

GetType is a wrapper for val.Type() to safely extract type if it is valid.

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 InitNew

func InitNew(val reflect.Value) (res reflect.Value)

InitNew initializes a new reflect.Value with reflect.Type of val.

func IsArrayZero

func IsArrayZero(v reflect.Value) bool

IsArrayZero checks if the array is empty.

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

func IsKindArray(kind reflect.Kind) bool

IsKindArray checks whether the kind is array or not.

func IsKindBool

func IsKindBool(kind reflect.Kind) bool

IsKindBool checks whether the kind is bool or not.

func IsKindChan

func IsKindChan(kind reflect.Kind) bool

IsKindChan checks whether the input kind is reflect.Chan.

func IsKindComplex

func IsKindComplex(kind reflect.Kind) bool

IsKindComplex checks whether the kind is complex or not.

func IsKindFloat

func IsKindFloat(kind reflect.Kind) bool

IsKindFloat checks whether the kind is float or not.

func IsKindInt

func IsKindInt(kind reflect.Kind) bool

IsKindInt checks whether the kind is int or not.

func IsKindInterface added in v4.5.0

func IsKindInterface(kind reflect.Kind) bool

IsKindInterface checks whether the input kind is reflect.Interface.

func IsKindList

func IsKindList(kind reflect.Kind) bool

IsKindList checks whether the kind is array or slice.

func IsKindMap

func IsKindMap(kind reflect.Kind) bool

IsKindMap checks whether the input kind is reflect.Map.

func IsKindNil added in v4.1.0

func IsKindNil(kind reflect.Kind) bool

IsKindNil checks whether the input kind can call IsNil method.

func IsKindPtr

func IsKindPtr(kind reflect.Kind) bool

IsKindPtr checks whether the input kind is reflect.Ptr.

func IsKindSlice

func IsKindSlice(kind reflect.Kind) bool

IsKindSlice checks whether the kind is slice or not.

func IsKindString

func IsKindString(kind reflect.Kind) bool

IsKindString checks whether the kind is string or not.

func IsKindStruct

func IsKindStruct(kind reflect.Kind) bool

IsKindStruct checks whether the input kind is reflect.Struct.

func IsKindTypeElemable

func IsKindTypeElemable(kind reflect.Kind) bool

IsKindTypeElemable checks the kind of reflect.Type that can call Elem method.

func IsKindUint

func IsKindUint(kind reflect.Kind) bool

IsKindUint checks whether the kind is uint or not.

func IsKindUnsafePointer

func IsKindUnsafePointer(kind reflect.Kind) bool

IsKindUnsafePointer checks whether the kind is unsafe ptr or not.

func IsKindValueBytesSlice

func IsKindValueBytesSlice(val reflect.Value) bool

IsKindValueBytesSlice checks whether the val of reflect.Value is byte slice.

func IsKindValueElemable

func IsKindValueElemable(kind reflect.Kind) bool

IsKindValueElemable checks the kind of reflect.Value that can call Elem method.

func IsKindValueNil added in v4.1.0

func IsKindValueNil(val reflect.Value) bool

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

func IsPtrValueZero(val reflect.Value) bool

IsPtrValueZero overrides the default behavior for the reflect.Ptr case in the IsValueZero method.

func IsReflectZero

func IsReflectZero(val reflect.Value) (result bool)

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

func IsStructZero(v reflect.Value) bool

IsStructZero checks if the struct is zero.

func IsTimeZero

func IsTimeZero(t time.Time) bool

IsTimeZero checks if the time is zero.

func IsTypeElemable

func IsTypeElemable(typ reflect.Type) (res bool)

IsTypeElemable checks wether the typ of reflect.Type can call Elem method.

func IsTypeValueDuration added in v4.1.0

func IsTypeValueDuration(val reflect.Value) bool

IsTypeValueDuration checks whether the type of val reflect.Value is time.Duration or *time.Duration.

func IsTypeValueElemable

func IsTypeValueElemable(val reflect.Value) bool

IsTypeValueElemable checks if the type of the reflect.Value can call Elem.

func IsTypeValueIP added in v4.4.0

func IsTypeValueIP(val reflect.Value) bool

IsTypeValueIP checks whether the type of val reflect.Value is net.IP or *net.IP.

func IsTypeValueTime added in v4.1.0

func IsTypeValueTime(val reflect.Value) bool

IsTypeValueTime checks whether the type of val reflect.Value is time.Time or *time.Time.

func IsTypeValueURL added in v4.4.0

func IsTypeValueURL(val reflect.Value) bool

IsTypeValueURL checks whether the type of val reflect.Value is url.URL or *url.URL.

func IsValueElemable

func IsValueElemable(val reflect.Value) bool

IsValueElemable checks whether the val of reflect.Value could call Elem method.

func IsValueElemableParentElem

func IsValueElemableParentElem(res reflect.Value) bool

IsValueElemableParentElem checks whether the res have elemable kind for parent and elem.

func IsValueNil added in v4.1.0

func IsValueNil(val reflect.Value) bool

IsValueNil checks whether the input val of reflect.Value is nil for any type.

func IsValueZero

func IsValueZero(v reflect.Value) bool

IsValueZero check the reflect.Value if it is zero based on it's kind.

func IsZero

func IsZero(k interface{}) bool

IsZero returns false if k is nil or has a zero value

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

func SetReflectZero(val reflect.Value)

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

func UnwrapInterfaceValue(val reflect.Value) (res reflect.Value)

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

type FuncAssigner func(assigner reflect.Value, val reflect.Value, o *Option) (err error)

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

type IterArraySliceFn func(arrSliceInput reflect.Value, index int, field reflect.Value) error

IterArraySliceFn is a function type to iterate each field of array or slice and returning an error if needed.

type IterChanFn

type IterChanFn func(chanInput reflect.Value, recv reflect.Value) error

IterChanFn is a function type to iterate each value received by a channel and returning an error if needed.

type IterMapFn

type IterMapFn func(mapInput reflect.Value, key reflect.Value, value reflect.Value) error

IterMapFn is a function type to iterate each key and element of map and returning an error if needed.

type IterStructFn

type IterStructFn func(structInput reflect.Value, field reflect.Value) error

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 NewOption

func NewOption() *Option

NewOption initialize the new empty option.

func (*Option) Assign

func (o *Option) Assign(fnOpts ...FuncOption) *Option

Assign assigns the functional options to the Option.

func (*Option) Clone

func (o *Option) Clone() *Option

Clone clones the current option to the new memory address.

func (*Option) Default

func (o *Option) Default() *Option

Default sets the default value of all variables in Option.

type Value

type Value struct {
	reflect.Value
	// contains filtered or unexported fields
}

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

func (s *Value) Error() error

Error returns the error contained within the Value.

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

func (s *Value) IterateMap(fns ...IterMapFn) *Value

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.

type Zeroable

type Zeroable interface {
	IsZero() bool
}

Zeroable is a contract to specifies the Zero attribute of a custom type.

Jump to

Keyboard shortcuts

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