reflector

package
v0.5.6 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2022 License: Apache-2.0 Imports: 5 Imported by: 19

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTag added in v0.5.1

func ParseTag(tag string) (map[string]string, error)

ParseTag parses a golang struct tag into a map.

Types

type CallResult

type CallResult struct {
	Result []interface{}
	Error  error
}

CallResult is a wrapper of a method call result.

func (*CallResult) IsError

func (cr *CallResult) IsError() bool

IsError checks if the last value is a non-nil error.

type Obj

type Obj struct {
	ObjMetadata
	// contains filtered or unexported fields
}

Obj is a wrapper for golang values which need to be reflected. The value can be of any kind and any type.

func New

func New(obj interface{}) *Obj

New initializes a new Obj wrapper.

func NewFromType

func NewFromType(ty reflect.Type) *Obj

NewFromType creates a new Obj but using reflect.Type.

func (*Obj) Field

func (o *Obj) Field(fieldName string) *ObjField

Field get a field wrapper. Note that the field name can be invalid. You can check the field validity using ObjField.IsValid().

func (*Obj) Fields

func (o *Obj) Fields() []ObjField

Fields returns fields. Don't list fields inside Anonymous fields as distinct fields.

func (Obj) FieldsAll

func (o Obj) FieldsAll() []ObjField

FieldsAll returns fields. List both anonymous fields and fields declared inside anonymous fields.

func (Obj) FieldsAnonymous

func (o Obj) FieldsAnonymous() []ObjField

FieldsAnonymous returns only anonymous fields.

func (Obj) FieldsFlattened

func (o Obj) FieldsFlattened() []ObjField

FieldsFlattened returns fields. Will not list Anonymous fields but it will list fields declared in those anonymous fields.

func (Obj) FindDoubleFields

func (o Obj) FindDoubleFields() []string

FindDoubleFields checks if this object has declared multiple fields with a same name. (by checking recursively Anonymous fields and their fields)

func (*Obj) GetByIndex added in v0.5.3

func (o *Obj) GetByIndex(index int) (value interface{}, found bool)

GetByIndex returns a value by int index.

Works for arrays, slices and strins. Won't panic when index or kind is invalid.

func (*Obj) GetByKey added in v0.5.3

func (o *Obj) GetByKey(key interface{}) (value interface{}, found bool)

GetByKey returns a value by map key.

Won't panic when key is invalid or kind is not map.

func (*Obj) IsGettableByIndex added in v0.5.4

func (o *Obj) IsGettableByIndex() bool

IsGettableByIndex returns true if underlying type is array, slice or string

func (*Obj) IsMap added in v0.5.4

func (o *Obj) IsMap() bool

IsMap returns true if underlying type is map or a pointer to a map

func (Obj) IsPtr

func (o Obj) IsPtr() bool

IsPtr checks if the value is a pointer.

func (*Obj) IsSettableByIndex added in v0.5.4

func (o *Obj) IsSettableByIndex() bool

IsSettableByIndex returns true if underlying type is array, slice (but not string, since they are immutable)

func (*Obj) IsValid

func (o *Obj) IsValid() bool

IsValid checks if the underlying objects is valid. Nil is an invalid value, for example.

func (*Obj) Keys added in v0.5.3

func (o *Obj) Keys() ([]interface{}, error)

Keys return map keys in unspecified order.

func (Obj) Kind

func (o Obj) Kind() reflect.Kind

Kind returns the value's kind.

func (*Obj) Len added in v0.5.3

func (o *Obj) Len() int

Len returns object length. Works for arrays, channels, maps, slices and strings.

It doesn't panic for other types, returns 0 instead.

In case the value is a pointer, len checks the underlying value.

func (*Obj) Method

func (o *Obj) Method(name string) *ObjMethod

Method returns a new method wrapper. The method name can be invalid, check the method validity with ObjMethod.IsValid().

func (*Obj) Methods

func (o *Obj) Methods() []ObjMethod

Methods returns the list of all methods.

func (*Obj) SetByIndex added in v0.5.3

func (o *Obj) SetByIndex(index int, val interface{}) error

SetByIndex sets a slice value by key.

func (*Obj) SetByKey added in v0.5.3

func (o *Obj) SetByKey(key interface{}, val interface{}) (err error)

SetByKey sets a map value by key.

func (Obj) String

func (o Obj) String() string

func (Obj) Type

func (o Obj) Type() reflect.Type

Type returns the value type. If kind is invalid, this will return a zero filled reflect.Type.

type ObjField

type ObjField struct {
	ObjFieldMetadata
	// contains filtered or unexported fields
}

ObjField is a wrapper for the object's field.

func (*ObjField) Get

func (of *ObjField) Get() (interface{}, error)

Get gets the field value of error if field is invalid).

func (*ObjField) IsAnonymous

func (of *ObjField) IsAnonymous() bool

IsAnonymous checks if this is an anonymous (embedded) field.

func (*ObjField) IsExported

func (of *ObjField) IsExported() bool

IsExported returns true if the name starts with uppercase (i.e. field is public).

func (*ObjField) IsSettable

func (of *ObjField) IsSettable() bool

IsSettable checks if this field is settable.

func (*ObjField) IsValid

func (of *ObjField) IsValid() bool

IsValid checks if the fields is valid.

func (*ObjField) Kind

func (of *ObjField) Kind() reflect.Kind

Kind returns the field's kind.

func (*ObjField) Name

func (of *ObjField) Name() string

Name returns the field's name.

func (*ObjField) Set

func (of *ObjField) Set(value interface{}) error

Set sets a value for this field or error if field is invalid (or not settable).

func (*ObjField) Tag

func (of *ObjField) Tag(tag string) (string, error)

Tag returns the value of this specific tag or error if the field is invalid.

func (*ObjField) TagExpanded

func (of *ObjField) TagExpanded(tag string) ([]string, error)

TagExpanded returns the tag value "expanded" with commas.

func (*ObjField) Tags

func (of *ObjField) Tags() (map[string]string, error)

Tags returns the map of all fields or error for invalid field.

func (*ObjField) TagsString added in v0.5.6

func (of *ObjField) TagsString() (string, error)

TagsString returns the complete tags string (everything inside “)

func (*ObjField) Type

func (of *ObjField) Type() reflect.Type

Type returns the field's type.

type ObjFieldMetadata

type ObjFieldMetadata struct {
	// contains filtered or unexported fields
}

ObjFieldMetadata contains data which is always unique per Type/Field.

type ObjMetadata

type ObjMetadata struct {
	// contains filtered or unexported fields
}

ObjMetadata contains data which is always unique per Type.

func (*ObjMetadata) IsStructOrPtrToStruct

func (om *ObjMetadata) IsStructOrPtrToStruct() bool

IsStructOrPtrToStruct checks if the value is a struct or a pointer to a struct.

type ObjMethod

type ObjMethod struct {
	ObjMethodMetadata
	// contains filtered or unexported fields
}

ObjMethod is a wrapper for an object method. The name of the method can be invalid.

func (*ObjMethod) Call

func (om *ObjMethod) Call(args ...interface{}) (*CallResult, error)

Call calls this method. Note that in the error returning value is not the error from the method call.

func (*ObjMethod) InTypes

func (om *ObjMethod) InTypes() []reflect.Type

InTypes returns an slice with this method's input types.

func (*ObjMethod) IsValid

func (om *ObjMethod) IsValid() bool

IsValid returns this method's validity.

func (*ObjMethod) Name

func (om *ObjMethod) Name() string

Name returns the method's name.

func (*ObjMethod) OutTypes

func (om *ObjMethod) OutTypes() []reflect.Type

OutTypes returns an slice with this method's output types.

type ObjMethodMetadata

type ObjMethodMetadata struct {
	// contains filtered or unexported fields
}

ObjMethodMetadata contains data which is always unique per Type/Method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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