reflections

package
v0.0.0-...-e4caca2 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexportedField = errors.New("unexported field")

ErrUnexportedField indicates that an operation failed as a result of applying to a non-exported struct field.

View Source
var ErrUnsupportedType = errors.New("unsupported type")

ErrUnsupportedType indicates that the provided type doesn't support the requested reflection operation.

Functions

func Fields

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

Fields returns the struct fields names list. The `obj` parameter can either be a structure or pointer to structure.

func FieldsDeep

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

FieldsDeep returns "flattened" fields.

Note that FieldsDeept treats fields from anonymous inner structs as normal fields.

func GetField

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

GetField returns the value of the provided obj field. The `obj` can either 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. The `obj` can either be a structure or pointer to structure.

func GetFieldNameByTagValue

func GetFieldNameByTagValue(obj interface{}, tagKey, tagValue string) (string, error)

GetFieldNameByTagValue looks up a field with a matching `{tagKey}:"{tagValue}"` tag in the provided `obj` item. The `obj` parameter must be a `struct`, or a `pointer` to one. If the `obj` parameter doesn't have a field tagged with the `tagKey`, and the matching `tagValue`, this function returns an error.

func GetFieldTag

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

GetFieldTag returns the provided obj field tag value. The `obj` parameter can either be a structure or pointer to structure.

func GetFieldType

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

GetFieldType returns the kind of the provided obj field. The `obj` can either be a structure or pointer to structure.

func HasField

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

HasField checks if the provided `obj` struct has field named `name`. The `obj` can either be a structure or pointer to structure.

func Items

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

Items returns the field:value struct pairs as a map. The `obj` parameter can either be a structure or pointer to structure.

func ItemsDeep

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

ItemsDeep returns "flattened" items. Note that ItemsDeep will treat fields from anonymous inner structs as normal fields.

func SetField

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

SetField sets the provided obj field with provided value.

The `obj` parameter must be a pointer to a struct, otherwise it soundly fails. The provided `value` type should match with the struct field being set.

func Tags

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

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

func TagsDeep

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

TagsDeep returns "flattened" tags. Note that TagsDeep treats fields from anonymous inner structs as normal fields.

Types

This section is empty.

Jump to

Keyboard shortcuts

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