reflect

package
v3.10.55 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2024 License: Apache-2.0 Imports: 10 Imported by: 19

Documentation

Index

Constants

View Source
const (
	// SplitToken used to detect path components
	SplitToken = "."
	// IndexCloseChar used to detect index end
	IndexCloseChar = "]"
	// IndexOpenChar used to detect index start
	IndexOpenChar = "["
)

Variables

View Source
var (
	// ErrMalformedIndex returns when index key have invalid format
	ErrMalformedIndex = errors.New("malformed index key")
	// ErrInvalidIndexUsage returns when index key usage error
	ErrInvalidIndexUsage = errors.New("invalid index key usage")
	// ErrKeyNotFound returns when key not found
	ErrKeyNotFound = errors.New("unable to find the key")
	// ErrBadJSONPath returns when path have invalid syntax
	ErrBadJSONPath = errors.New("bad path: must start with $ and have more then 2 chars")
)
View Source
var (
	// ErrInvalidStruct happens when passed not struct and not struct pointer
	ErrInvalidStruct = errors.New("invalid struct specified")
	// ErrInvalidValue happens when passed invalid value for field
	ErrInvalidValue = errors.New("invalid value specified")
	// ErrNotFound happens when struct field not found
	ErrNotFound = errors.New("struct field not found")
)
View Source
var ErrInvalidParam = errors.New("invalid url query param provided")

ErrInvalidParam specifies invalid url query params

Functions

func CopyDefaults

func CopyDefaults(a, b interface{})

CopyDefaults for a from b a and b should be pointers to the same kind of struct

func CopyFrom

func CopyFrom(a, b interface{})

CopyFrom sets the public members of a from b a and b should be pointers to structs a can be a different type from b Only the Fields which have the same name and assignable type on a and b will be set.

func Equal added in v3.10.29

func Equal(src interface{}, dst interface{}, excptFields ...string) bool

func FieldName

func FieldName(name string) string

FieldName returns map field name that can be looked up in struct field

func FlattenMap

func FlattenMap(a map[string]interface{}) map[string]interface{}

FlattenMap expand key.subkey to nested map

func IsEmpty

func IsEmpty(v reflect.Value) bool

IsEmpty returns true if value empty

func IsZero

func IsZero(src interface{}) bool

IsZero returns true if struct is zero (not have any defined values)

func Lookup

func Lookup(i interface{}, path string) (reflect.Value, error)

Lookup performs a lookup into a value, using a path of keys. The key should match with a Field or a MapIndex. For slice you can use the syntax key[index] to access a specific index. If one key owns to a slice and an index is not specificied the rest of the path will be apllied to evaley value of the slice, and the value will be merged into a slice.

func Merge

func Merge(dst interface{}, mp map[string]interface{}, opts ...Option) error

Merge merges map[string]interface{} to destination struct

func SetFieldByPath added in v3.8.5

func SetFieldByPath(src interface{}, dst interface{}, path string) error

SetFieldByPath set struct field by its path

func StructFieldByName

func StructFieldByName(src interface{}, tkey string) (interface{}, error)

StructFieldByName get struct field by its name

func StructFieldByPath

func StructFieldByPath(src interface{}, path string) (interface{}, error)

StructFieldByPath get struct field by its path

func StructFieldByTag

func StructFieldByTag(src interface{}, tkey string, tval string) (interface{}, error)

StructFieldByTag get struct field by tag key and its value

func StructFieldsMap

func StructFieldsMap(src interface{}) (map[string]interface{}, error)

StructFieldsMap returns struct map[string]interface{} or error

func StructURLValues

func StructURLValues(src interface{}, pref string, tags []string) (url.Values, error)

StructURLValues get struct fields via url.Values

func URLMap

func URLMap(query string) (map[string]interface{}, error)

URLMap returns map of url query params

func Zero

func Zero(src interface{}) (interface{}, error)

Zero creates new zero interface

func ZeroFieldByPath added in v3.8.4

func ZeroFieldByPath(src interface{}, path string) error

ZeroFieldByPath clean struct field by its path

Types

type Option

type Option func(*Options)

Option func signature

func SliceAppend

func SliceAppend(b bool) Option

SliceAppend sets the option

func Tags

func Tags(t []string) Option

Tags sets the merge tags for lookup

type Options

type Options struct {
	// Tags specifies tags to lookup
	Tags []string
	// SliceAppend controls slice appending
	SliceAppend bool
}

Options for merge

type StructField

type StructField struct {
	Value reflect.Value
	Path  string
	Field reflect.StructField
}

StructField contains struct field path its value and field

func StructFields

func StructFields(src interface{}) ([]StructField, error)

StructFields returns slice of struct fields

Jump to

Keyboard shortcuts

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