reflection

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: May 2, 2022 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package structs contains various utilities functions to work with structs.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultTagName is the default tag name for struct fields which provides
	// a more granular to tweak certain structs. Lookup the necessary functions
	// for more info.
	DefaultTagName = "structs" // struct's field default tag name
)

Functions

func Copy

func Copy(toValue interface{}, fromValue interface{}) (err error)

Copy copy things

func CreateFuncForCodePtr

func CreateFuncForCodePtr(outFuncPtr interface{}, codePtr uintptr)

CreateFuncForCodePtr is given a code pointer and creates a function value that uses that pointer. The outFun argument should be a pointer to a function of the proper type (e.g. the address of a local variable), and will be set to the result function value.

func FillMap

func FillMap(s interface{}, out map[string]interface{})

FillMap is the same as Map. Instead of returning the output, it fills the given map.

func FindFuncWithName

func FindFuncWithName(name string) (uintptr, error)

FindFuncWithName searches through the moduledata table created by the linker and returns the function's code pointer. If the function was not found, it returns an error. Since the data structures here are not exported, we copy them below (and they need to stay in sync or else things will fail catastrophically).

func GetField

func GetField(obj interface{}, name string) (interface{}, error)

GetField returns the value of the provided obj field. obj can whether be a structure or pointer to structure.

func GetFieldKind

func GetFieldKind(obj interface{}, name string) (reflect.Kind, error)

GetFieldKind returns the kind of the provided obj field. obj can whether be a structure or pointer to structure.

func GetFieldTag

func GetFieldTag(obj interface{}, fieldName, tagKey string) (string, error)

GetFieldTag returns the provided obj field tag value. obj can whether be a structure or pointer to structure.

func GetFunc

func GetFunc(outFuncPtr interface{}, name string) error

GetFunc gets the function defined by the given fully-qualified name. The outFuncPtr parameter should be a pointer to a function with the appropriate type (e.g. the address of a local variable), and is set to a new function value that calls the specified function. If the specified function does not exist, outFuncPtr is not set and an error is returned.

func HasField

func HasField(obj interface{}, name string) (bool, error)

HasField checks if the provided field name is part of a struct. obj can whether be a structure or pointer to structure.

func HasZero

func HasZero(s interface{}) bool

HasZero returns true if any field is equal to a zero value. For more info refer to Struct types HasZero() method. It panics if s's kind is not struct.

func Indirect

func Indirect(v reflect.Value, decodingNull bool) reflect.Value

func IsPointer

func IsPointer(obj interface{}) bool

func IsSimpleType

func IsSimpleType(kind reflect.Kind) bool

func IsStruct

func IsStruct(s interface{}) bool

IsStruct returns true if the given variable is a struct or a pointer to struct.

func IsStructureType

func IsStructureType(kind reflect.Kind) bool

func IsZero

func IsZero(s interface{}) bool

IsZero returns true if all fields is equal to a zero value. For more info refer to Struct types IsZero() method. It panics if s's kind is not struct.

func Items

func Items(obj interface{}) (map[string]interface{}, error)

Items returns the field - value struct pairs as a map. obj can whether be a structure or pointer to structure.

func Map

func Map(s interface{}) map[string]interface{}

Map converts the given struct to a map[string]interface{}. For more info refer to Struct types Map() method. It panics if s's kind is not struct.

func Name

func Name(s interface{}) string

Name returns the structs's type name within its package. It returns an empty string for unnamed types. It panics if s's kind is not struct.

func Names

func Names(s interface{}) []string

Names returns a slice of field names. For more info refer to Struct types Names() method. It panics if s's kind is not struct.

func ReflectSimpleValue

func ReflectSimpleValue(str string, typ reflect.Type) (val reflect.Value, err error)

func SetField

func SetField(obj interface{}, name string, value interface{}) error

SetField sets the provided obj field with provided value. obj param has to be a pointer to a struct, otherwise it will soundly fail. Provided value type should match with the struct field you're trying to set.

func StructName

func StructName(s interface{}) string

Returns the type name of a struct

func Tags

func Tags(obj interface{}, key string) (map[string]string, error)

Tags lists the struct tag fields. obj can whether be a structure or pointer to structure.

func Values

func Values(s interface{}) []interface{}

Values converts the given struct to a []interface{}. For more info refer to Struct types Values() method. It panics if s's kind is not struct.

Types

type Bitvector

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

type DiffResult

type DiffResult struct {
	Field string      `json:"field"`
	Old   interface{} `json:"old"`
	New   interface{} `json:"new"`
}

func Diff

func Diff(original, changed interface{}) (res []DiffResult)

type Field

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

Field represents a single struct field that encapsulates high level functions around the field.

func Fields

func Fields(s interface{}) []*Field

Fields returns a slice of *Field. For more info refer to Struct types Fields() method. It panics if s's kind is not struct.

func (*Field) Field

func (f *Field) Field(name string) *Field

Field returns the field from a nested struct. It panics if the nested struct is not exported or if the field was not found.

func (*Field) FieldOk

func (f *Field) FieldOk(name string) (*Field, bool)

Field returns the field from a nested struct. The boolean returns true if the field was found. It panics if the nested struct is not exported or if the field was not found.

func (*Field) Fields

func (f *Field) Fields() []*Field

Fields returns a slice of Fields. This is particular handy to get the fields of a nested struct . A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field *http.Request `structs:"-"`

It panics if field is not exported or if field's kind is not struct

func (*Field) IsEmbedded

func (f *Field) IsEmbedded() bool

IsEmbedded returns true if the given field is an anonymous field (embedded)

func (*Field) IsExported

func (f *Field) IsExported() bool

IsExported returns true if the given field is exported.

func (*Field) IsTime

func (f *Field) IsTime() bool

func (*Field) IsZero

func (f *Field) IsZero() bool

IsZero returns true if the given field is not initialized (has a zero value). It panics if the field is not exported.

func (*Field) Kind

func (f *Field) Kind() reflect.Kind

Kind returns the fields kind, such as "string", "map", "bool", etc ..

func (*Field) Name

func (f *Field) Name() string

Name returns the name of the given field

func (*Field) Set

func (f *Field) Set(val interface{}) error

Set sets the field to given value v. It returns an error if the field is not settable (not addressable or not exported) or if the given value's type doesn't match the fields type.

func (*Field) Tag

func (f *Field) Tag(key string) string

Tag returns the value associated with key in the tag string. If there is no such key in the tag, Tag returns the empty string.

func (*Field) Value

func (f *Field) Value() interface{}

Value returns the underlying value of of the field. It panics if the field is not exported.

func (*Field) Zero

func (f *Field) Zero() error

Zero sets the field to its zero value. It returns an error if the field is not settable (not addressable or not exported).

type Func

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

Convenience struct for modifying the underlying code pointer of a function value. The actual struct has other values, but always starts with a code pointer.

type Functab

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

type Method

type Method struct {
	Name string
	Func reflect.Value
	// contains filtered or unexported fields
}

func (*Method) Call

func (m *Method) Call(args ...reflect.Value) (res []interface{})

func (*Method) In

func (m *Method) In(i int) reflect.Type

func (*Method) NumIn

func (m *Method) NumIn() int

func (*Method) NumOut

func (m *Method) NumOut() int

func (*Method) Out

func (m *Method) Out(i int) reflect.Type

type Moduledata

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

nolint:golint

var Firstmoduledata Moduledata

nolint:golint go:linkname Firstmoduledata runtime.firstmoduledata

type PtabEntry

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

type Struct

type Struct struct {
	TagName string
	// contains filtered or unexported fields
}

Struct encapsulates a struct type to provide several high level functions around the struct.

func New

func New(s interface{}) *Struct

New returns a new *Struct with the struct s. It panics if the s's kind is not struct.

func (*Struct) Field

func (s *Struct) Field(name string) *Field

Field returns a new Field struct that provides several high level functions around a single struct field entity. It panics if the field is not found.

func (*Struct) FieldOk

func (s *Struct) FieldOk(name string) (*Field, bool)

Field returns a new Field struct that provides several high level functions around a single struct field entity. The boolean returns true if the field was found.

func (*Struct) Fields

func (s *Struct) Fields() []*Field

Fields returns a slice of Fields. A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

It panics if s's kind is not struct.

func (*Struct) FillMap

func (s *Struct) FillMap(out map[string]interface{})

FillMap is the same as Map. Instead of returning the output, it fills the given map.

func (*Struct) HasZero

func (s *Struct) HasZero() bool

HasZero returns true if a field in a struct is not initialized (zero value). A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected. It panics if s's kind is not struct.

func (*Struct) IsZero

func (s *Struct) IsZero() bool

IsZero returns true if all fields in a struct is a zero value (not initialized) A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected. It panics if s's kind is not struct.

func (*Struct) Map

func (s *Struct) Map() map[string]interface{}

Map converts the given struct to a map[string]interface{}, where the keys of the map are the field names and the values of the map the associated values of the fields. The default key string is the struct field name but can be changed in the struct field's tag value. The "structs" key in the struct's field tag value is the key name. Example:

// Field appears in map as key "myName".
Name string `structs:"myName"`

A tag value with the content of "-" ignores that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

A tag value with the content of "string" uses the stringer to get the value. Example:

// The value will be output of Animal's String() func.
// Map will panic if Animal does not implement String().
Field *Animal `structs:"field,string"`

A tag value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Field is not processed further by this package.
Field time.Time     `structs:"myName,omitnested"`
Field *http.Request `structs:",omitnested"`

A tag value with the option of "omitempty" ignores that particular field if the field value is empty. Example:

// Field appears in map as key "myName", but the field is
// skipped if empty.
Field string `structs:"myName,omitempty"`

// Field appears in map as key "Field" (the default), but
// the field is skipped if empty.
Field string `structs:",omitempty"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected.

func (*Struct) Methods

func (s *Struct) Methods() []*Method

func (*Struct) Name

func (s *Struct) Name() string

Name returns the structs's type name within its package. For more info refer to Name() function.

func (*Struct) Names

func (s *Struct) Names() []string

Names returns a slice of field names. A struct tag with the content of "-" ignores the checking of that particular field. Example:

// Field is ignored by this package.
Field bool `structs:"-"`

It panics if s's kind is not struct.

func (*Struct) Values

func (s *Struct) Values() []interface{}

Values converts the given s struct's field values to a []interface{}. A struct tag with the content of "-" ignores the that particular field. Example:

// Field is ignored by this package.
Field int `structs:"-"`

A value with the option of "omitnested" stops iterating further if the type is a struct. Example:

// Fields is not processed further by this package.
Field time.Time     `structs:",omitnested"`
Field *http.Request `structs:",omitnested"`

A tag value with the option of "omitempty" ignores that particular field and is not added to the values if the field value is empty. Example:

// Field is skipped if empty
Field string `structs:",omitempty"`

Note that only exported fields of a struct can be accessed, non exported fields will be neglected.

type Textsect

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

nolint:golint

Jump to

Keyboard shortcuts

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