reflection

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2021 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign(value interface{}, out interface{}) bool

Assign simply performs a reflective replacement of the value, making sure to try to properly handle pointers.

func BindingName

func BindingName(field reflect.StructField) string

BindingName just returns the name of the field/attribute on the struct unless it has a `json` tag defined. If so, it will use the remapped name for this field instead.

type Foo struct {
    A string
    B string `json:"hello"
}

The binding name for the first attribute is "A", but the binding name for the other is "hello".

func FindField

func FindField(structType reflect.Type, name string) (reflect.StructField, bool)

FindField looks up the struct field attribute for the given field on the given struct.

func FlattenPointerType

func FlattenPointerType(t reflect.Type) reflect.Type

FlattenPointerType looks at the reflective type and if it's a pointer it will flatten it to the type it is a pointer for (e.g. "*string"->"string"). If it's already a non-pointer then we will leave this type as-is.

func IsNil

func IsNil(value reflect.Value) bool

IsNil returns true if the given value's type is both nil-able and nil.

Types

type StructAttribute

type StructAttribute struct {
	// Name is the binding name of the struct value. If there was a JSON tag on the
	// struct field, it should be that value. Otherwise, it's just the struct field's name.
	Name string
	// Value is the runtime value of this field on the struct you ran through "ToAttributes()"
	Value interface{}
}

StructAttribute represents a single key/value pair for a field on a struct.

func (StructAttribute) Matches

func (attr StructAttribute) Matches(name string) bool

Matches determines if there is a case-insensitive match between this name and the field.

type StructAttributes

type StructAttributes []*StructAttribute

StructAttributes maintains a list of attribute names/values for some source struct.

func ToAttributes

func ToAttributes(item interface{}) StructAttributes

ToAttributes accepts a struct (probably your service request) and returns a list of the key/value pairs for the attribute name/values. This is recursive, so nested structs will be included as the 'Value' of the necessary attributes.

func (StructAttributes) Find

func (attrs StructAttributes) Find(name string) *StructAttribute

Find looks up the struct attribute with the given name. This search is CASE-INSENSITIVE in order to match how the standard library handles JSON attribute.

func (StructAttributes) Remove

func (attrs StructAttributes) Remove(name string) StructAttributes

Remove performs a case-insensitive search for the attribute w/ that name and removes it from the list. The new slice without the matching attribute is returned.

Jump to

Keyboard shortcuts

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