reflection

package
v0.0.0-...-8bfda83 Latest Latest
Warning

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

Go to latest
Published: May 12, 2015 License: MIT Imports: 8 Imported by: 35

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// TypeOfError is the built-in error type
	TypeOfError = reflect.TypeOf((*error)(nil)).Elem()

	// TypeOfInterface is the type of an empty interface{}
	TypeOfInterface = reflect.TypeOf((*interface{})(nil)).Elem()
)

Built-in types

View Source
var DefaultValuerType = reflect.TypeOf((*DefaultValuer)(nil)).Elem()
View Source
var DontVisitMapType = reflect.TypeOf((*DontVisitMap)(nil)).Elem()
View Source
var DontVisitStructType = reflect.TypeOf((*DontVisitStruct)(nil)).Elem()

Functions

func AppendDefaultSliceElement

func AppendDefaultSliceElement(slice reflect.Value) reflect.Value

AppendDefaultSliceElement appends a field to slice with the value returned by GetDefaultValue for the former last slice element, or the zero value of the type if the slice was empty.

func CanStringToValueOfType

func CanStringToValueOfType(t reflect.Type) bool

func CheckFunctionSignature

func CheckFunctionSignature(f interface{}, args, results []reflect.Type) error

func CheckFunctionSignatureKind

func CheckFunctionSignatureKind(f interface{}, args, results []reflect.Kind) error

func CopyExportedStructFields

func CopyExportedStructFields(src, dstPtr interface{}) (copied int)

CopyExportedStructFields copies all exported struct fields from src that are assignable to their name siblings at dstPtr to dstPtr. src can be a struct or a pointer to a struct, dstPtr must be a pointer to a struct.

func CopyExportedStructFieldsVal

func CopyExportedStructFieldsVal(src, dst reflect.Value) (copied int)

func DeleteDefaultSliceElements

func DeleteDefaultSliceElements(slice interface{}) interface{}

DeleteDefaultSliceElements deletes slice elements where IsDefaultValue returns true.

func DeleteDefaultSliceElementsVal

func DeleteDefaultSliceElementsVal(slice reflect.Value) reflect.Value

DeleteDefaultSliceElementsVal deletes slice elements where IsDefaultValue returns true.

func DereferenceValue

func DereferenceValue(v reflect.Value) (result reflect.Value, ok bool)

DereferenceValue recursively dereferences v if it is a pointer or interface. It will return ok == false if nil is encountered.

func ExportedStructFields

func ExportedStructFields(v reflect.Value) map[string]reflect.Value

ExportedStructFields returns a map from exported struct field names to values, inlining anonymous sub-structs so that their field names are available at the base level. Example:

type A struct {
	X int
}
type B Struct {
	A
	Y int
}
// Yields X and Y instead of A and Y:
InlineAnonymousStructFields(reflect.ValueOf(B{}))

func FindFlattenedStructField

func FindFlattenedStructField(t reflect.Type, matchFunc MatchStructFieldFunc) *reflect.StructField

func GenericSlice

func GenericSlice(sliceOrArray interface{}) []interface{}

func GetDefaultValue

func GetDefaultValue(v reflect.Value) reflect.Value

GetDefaultValue returns the result of the GetDefault() method if v or v pointer implements DefaultValuer. Else the zero value of v.Type() will be returned. Used to clone types that have internal state that represents a default value that is not deep zero.

func GetStruct

func GetStruct(s interface{}) reflect.Value

GetStruct returns reflect.Value for the struct found in s. s can be a struct, a struct pointer or a reflect.Value of a struct or struct pointer.

func Implements

func Implements(v reflect.Value, interfaceType reflect.Type) bool

Implements returns if the type of v or a pointer to the type of v implements an interfaceType.

func IsDefault

func IsDefault(value interface{}) bool

IsDefault return true if value is nil, or the result of IsDefaultValue for the reflect.Value of value.

func IsDefaultValue

func IsDefaultValue(v reflect.Value) bool

IsDefaultValue calls SmartCastDefaultValuer to tedect if v in any kind implements DefaultValuer. If this is the case, then DefaultValuer.IsDefault will be returned. If v does not implement DefaultValuer, then the result of a comparison with the zero value of the type will be returned.

func IsExportedField

func IsExportedField(structField reflect.StructField) bool

func IsExportedName

func IsExportedName(name string) bool

func IsNilOrWrappedNil

func IsNilOrWrappedNil(i interface{}) bool

IsNilOrWrappedNil returns if i is nil, or wraps a nil pointer in a non nil interface.

func NewInstance

func NewInstance(prototype interface{}) interface{}

Creates a new zero valued instance of prototype

func Reset

func Reset(resultRef interface{})

Reset sets all elements of the object pointed to by resultRef to their default or zero values. But it works different from simply zeroing out everything, here are the exceptions: If resultRef is a pointer to a pointer, then the pointed to pointer will be reset to a new instance If resultRef is a pointer to a map, then the map will be reset to a new empty one. All other types pointed to by resultRef will be set to their default zero values.

func SetSliceLengh

func SetSliceLengh(slice reflect.Value, length int) reflect.Value

SetSliceLengh sets the length of a slice by sub-slicing a slice that's too long, or appending empty fields with the result of AppendDefaultSliceElement.

func SetStructZero

func SetStructZero(structVal reflect.Value)

SetStructZero sets all elements of a struct to their zero values.

func SmartCopy

func SmartCopy(source, resultRef interface{})

SmartCopy copies struct or map fields from source to equally named struct or map fields of resultRef, by dereferencing source and resultRef if necessary to find a matching assignable type. All fields of the object referenced by resultPtr will be set to their default values before copying from source by calling Reset(). SmartCopy is typically used for iterators with a method Next(resultRef interface{}) bool.

func Sort

func Sort(slice, compareFunc interface{})

Sort will sort slice according to compareFunc using reflection. slice can be a slice of any element type including interface{}. compareFunc must have two arguments that are assignable from the slice element type or pointers to such a type. The result of compareFunc must be a bool indicating if the first argument is less than the second. If the element type of slice is interface{}, then the type of the compareFunc arguments can be any type and dynamic casting from the interface value or its address will be attempted.

func StringToValueOfType

func StringToValueOfType(s string, t reflect.Type) (interface{}, error)

func VisitStruct

func VisitStruct(strct interface{}, visitor StructVisitor) error

VisitStruct visits recursively all exported fields of a struct and reports them via StructVisitor methods. If a StructVisitor method returns an error, the visitation is aborted and the error returned as result. Pointers and interfaces are dereferenced silently until a non nil value is found. Structs that are embedded anonymously are inlined so that their fields are reported as fields of the embedding struct at the same depth. Anonymous struct fields that are not structs themselves are omitted. Struct fields with the tag gostart:"-" are ignored.

Example (DeepAnonymousStructFields)
type A struct {
	A0 interface{}
	A1 int
}
type B struct {
	B0 bool
	A
	B1 bool
}
type C struct {
	A
	C0 string
	*B
	C1 string
}
type exampleStruct struct {
	C
}
val := &exampleStruct{
	C: C{
		A:  A{A0: 0, A1: 1},
		C0: "C0",
		B: &B{
			B0: false,
			A:  A{A0: 0, A1: 1},
			B1: true,
		},
		C1: "C1",
	},
}
VisitStruct(val, NewStdLogStructVisitor())
Output:

BeginStruct(reflection.exampleStruct)
  StructField(0: A0 int = 0)
  StructField(1: A1 int = 1)
  StructField(2: C0 string = "C0")
  StructField(3: B0 bool = false)
  StructField(4: A0 int = 0)
  StructField(5: A1 int = 1)
  StructField(6: B1 bool = true)
  StructField(7: C1 string = "C1")
EndStruct(reflection.exampleStruct)
Example (DeepStructFields)
type A struct {
	A0 interface{}
	A1 int
}
type B struct {
	B0 bool
	A  A
	B1 bool
}
type C struct {
	A  A
	C0 string
	B  *B
	C1 string
}
type exampleStruct struct {
	C C
}
val := &exampleStruct{
	C: C{
		A:  A{A0: 0, A1: 1},
		C0: "C0",
		B: &B{
			B0: false,
			A:  A{A0: 0, A1: 1},
			B1: true,
		},
		C1: "C1",
	},
}
VisitStruct(val, NewStdLogStructVisitor())
Output:

BeginStruct(reflection.exampleStruct)
  StructField(0: C reflection.C)
  BeginStruct(reflection.C)
    StructField(0: A reflection.A)
    BeginStruct(reflection.A)
      StructField(0: A0 int = 0)
      StructField(1: A1 int = 1)
    EndStruct(reflection.A)
    StructField(1: C0 string = "C0")
    StructField(2: B reflection.B)
    BeginStruct(reflection.B)
      StructField(0: B0 bool = false)
      StructField(1: A reflection.A)
      BeginStruct(reflection.A)
        StructField(0: A0 int = 0)
        StructField(1: A1 int = 1)
      EndStruct(reflection.A)
      StructField(2: B1 bool = true)
    EndStruct(reflection.B)
    StructField(3: C1 string = "C1")
  EndStruct(reflection.C)
EndStruct(reflection.exampleStruct)
Example (FlatAnonymousStructFields)
type A struct {
	A0 int
	A1 interface{}
	A2 int
}
type B struct {
	B0 bool
	B1 bool
}
type exampleStruct struct {
	A
	*B
}
val := &exampleStruct{
	A: A{A0: 0, A1: 1, A2: 2},
	B: &B{B0: false, B1: true},
}
VisitStruct(val, NewStdLogStructVisitor())
Output:

BeginStruct(reflection.exampleStruct)
  StructField(0: A0 int = 0)
  StructField(1: A1 int = 1)
  StructField(2: A2 int = 2)
  StructField(3: B0 bool = false)
  StructField(4: B1 bool = true)
EndStruct(reflection.exampleStruct)
Example (FlatStructWithSliceAndArray)
type exampleStruct struct {
	X      int
	hidden bool
	Slice  []int
	Array  [3]int
}
val := &exampleStruct{
	X:     9,
	Slice: []int{1, 2},
	Array: [...]int{1, 2, 3},
}
VisitStruct(val, NewStdLogStructVisitor())
Output:

BeginStruct(reflection.exampleStruct)
  StructField(0: X int = 9)
  StructField(1: Slice []int)
  BeginSlice([]int)
    SliceField(0: int = 1)
    SliceField(1: int = 2)
  EndSlice([]int)
  StructField(2: Array [3]int)
  BeginArray([3]int)
    ArrayField(0: int = 1)
    ArrayField(1: int = 2)
    ArrayField(2: int = 3)
  EndArray([3]int)
EndStruct(reflection.exampleStruct)
Example (LimitDepth)
type A struct {
	A0 interface{}
	A1 int
}
type B struct {
	B0 bool
	A  A
	B1 bool
}
type C struct {
	A  A
	C0 string
	B  *B
	C1 string
}
type exampleStruct struct {
	C C
}
val := &exampleStruct{
	C: C{
		A:  A{A0: 0, A1: 1},
		C0: "C0",
		B: &B{
			B0: false,
			A:  A{A0: 0, A1: 1},
			B1: true,
		},
		C1: "C1",
	},
}
VisitStructDepth(val, NewStdLogStructVisitor(), 2)
Output:

BeginStruct(reflection.exampleStruct)
  StructField(0: C reflection.C)
  BeginStruct(reflection.C)
    StructField(0: A reflection.A)
    BeginStruct(reflection.A)
    EndStruct(reflection.A)
    StructField(1: C0 string = "C0")
    StructField(2: B reflection.B)
    BeginStruct(reflection.B)
    EndStruct(reflection.B)
    StructField(3: C1 string = "C1")
  EndStruct(reflection.C)
EndStruct(reflection.exampleStruct)
Example (SimpleFlatPtrsStruct)
type exampleStruct struct {
	X *int
	Y **float32
	Z *string
	N **string
	I interface{}
}
val := &exampleStruct{
	X: new(int),
	Y: new(*float32),
	Z: nil,
	N: new(*string),
	I: new(int),
}
*val.X = 1
*val.Y = new(float32)
**val.Y = 2
VisitStruct(val, NewStdLogStructVisitor())
Output:

BeginStruct(reflection.exampleStruct)
  StructField(0: X int = 1)
  StructField(1: Y float32 = 2)
  StructField(2: I int = 0)
EndStruct(reflection.exampleStruct)
Example (SimpleFlatStruct)
type exampleStruct struct {
	X      int
	Y      float32
	Z      string
	hidden string
}
VisitStruct(&exampleStruct{X: 1}, NewStdLogStructVisitor())
VisitStruct(exampleStruct{Y: 2}, NewStdLogStructVisitor())
Output:

BeginStruct(reflection.exampleStruct)
  StructField(0: X int = 1)
  StructField(1: Y float32 = 0)
  StructField(2: Z string = "")
EndStruct(reflection.exampleStruct)
BeginStruct(reflection.exampleStruct)
  StructField(0: X int = 0)
  StructField(1: Y float32 = 2)
  StructField(2: Z string = "")
EndStruct(reflection.exampleStruct)
Example (SliceOfStructInAnonymousStruct)
type emailIdentity struct {
	Address     string
	Description string
}
type user struct {
	Name  string
	Email []emailIdentity
}
type person struct {
	user
	ExtraInfo string
}
val := &person{
	user: user{
		Name: "Erik Unger",
		Email: []emailIdentity{
			emailIdentity{Address: "erik@erikunger.com", Description: "Test"},
		},
	},
	ExtraInfo: "info",
}
VisitStruct(val, NewStdLogStructVisitor())
Output:

BeginStruct(reflection.person)
  StructField(0: Name string = "Erik Unger")
  StructField(1: Email []reflection.emailIdentity)
  BeginSlice([]reflection.emailIdentity)
    SliceField(0: reflection.emailIdentity)
    BeginStruct(reflection.emailIdentity)
      StructField(0: Address string = "erik@erikunger.com")
      StructField(1: Description string = "Test")
    EndStruct(reflection.emailIdentity)
  EndSlice([]reflection.emailIdentity)
  StructField(2: ExtraInfo string = "info")
EndStruct(reflection.person)

func VisitStructDepth

func VisitStructDepth(strct interface{}, visitor StructVisitor, maxDepth int) error

VisitStructDepth is identical to VisitStruct except that its recursive depth is limited to maxDepth with the first depth level being zero. If maxDepth is -1, then the recursive depth is unlimited (VisitStruct).

Types

type DefaultValuer

type DefaultValuer interface {
	IsDefault() bool
	GetDefault() interface{}
}

func SmartCastDefaultValuer

func SmartCastDefaultValuer(v reflect.Value) (DefaultValuer, bool)

type DontVisitMap

type DontVisitMap interface {
	DontVisitMap()
}

DontVisitMap implementations won't be visited as maps by VisitStruct

type DontVisitStruct

type DontVisitStruct interface {
	DontVisitStruct()
}

DontVisitStruct implementations won't be visited as structs by VisitStruct

type LogStructVisitor

type LogStructVisitor struct {
	Logger *log.Logger
}

LogStructVisitor can be used for testing and debugging VisitStruct()

func NewStdLogStructVisitor

func NewStdLogStructVisitor() *LogStructVisitor

func (*LogStructVisitor) ArrayField

func (self *LogStructVisitor) ArrayField(depth int, v reflect.Value, index int) error

func (*LogStructVisitor) BeginArray

func (self *LogStructVisitor) BeginArray(depth int, v reflect.Value) error

func (*LogStructVisitor) BeginMap

func (self *LogStructVisitor) BeginMap(depth int, v reflect.Value) error

func (*LogStructVisitor) BeginSlice

func (self *LogStructVisitor) BeginSlice(depth int, v reflect.Value) error

func (*LogStructVisitor) BeginStruct

func (self *LogStructVisitor) BeginStruct(depth int, v reflect.Value) error

func (*LogStructVisitor) EndArray

func (self *LogStructVisitor) EndArray(depth int, v reflect.Value) error

func (*LogStructVisitor) EndMap

func (self *LogStructVisitor) EndMap(depth int, v reflect.Value) error

func (*LogStructVisitor) EndSlice

func (self *LogStructVisitor) EndSlice(depth int, v reflect.Value) error

func (*LogStructVisitor) EndStruct

func (self *LogStructVisitor) EndStruct(depth int, v reflect.Value) error

func (*LogStructVisitor) MapField

func (self *LogStructVisitor) MapField(depth int, v reflect.Value, key string, index int) error

func (*LogStructVisitor) SliceField

func (self *LogStructVisitor) SliceField(depth int, v reflect.Value, index int) error

func (*LogStructVisitor) StructField

func (self *LogStructVisitor) StructField(depth int, v reflect.Value, f reflect.StructField, index int) error

type MatchStructFieldFunc

type MatchStructFieldFunc func(field *reflect.StructField) bool

type ModifySliceStructVisitor

type ModifySliceStructVisitor func(depth int, v reflect.Value) (reflect.Value, error)

ModifySliceStructVisitor is a StructVisitor that calls its self function value in BeginSlice() and ignores all other StructVisitor methos. It can be used to modify the length of slices in complex structs.

func (ModifySliceStructVisitor) ArrayField

func (self ModifySliceStructVisitor) ArrayField(depth int, v reflect.Value, index int) error

func (ModifySliceStructVisitor) BeginArray

func (self ModifySliceStructVisitor) BeginArray(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) BeginMap

func (self ModifySliceStructVisitor) BeginMap(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) BeginSlice

func (self ModifySliceStructVisitor) BeginSlice(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) BeginStruct

func (self ModifySliceStructVisitor) BeginStruct(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) EndArray

func (self ModifySliceStructVisitor) EndArray(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) EndMap

func (self ModifySliceStructVisitor) EndMap(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) EndSlice

func (self ModifySliceStructVisitor) EndSlice(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) EndStruct

func (self ModifySliceStructVisitor) EndStruct(depth int, v reflect.Value) error

func (ModifySliceStructVisitor) MapField

func (self ModifySliceStructVisitor) MapField(depth int, v reflect.Value, key string, index int) error

func (ModifySliceStructVisitor) ModifySlice

func (self ModifySliceStructVisitor) ModifySlice(depth int, v reflect.Value) (reflect.Value, error)

func (ModifySliceStructVisitor) SliceField

func (self ModifySliceStructVisitor) SliceField(depth int, v reflect.Value, index int) error

func (ModifySliceStructVisitor) StructField

func (self ModifySliceStructVisitor) StructField(depth int, v reflect.Value, f reflect.StructField, index int) error

type SortCompareFunc

type SortCompareFunc struct {
	CompareFunc reflect.Value
	ArgType     reflect.Type
	PtrArgs     bool
}

func NewSortCompareFunc

func NewSortCompareFunc(compareFunc interface{}) (*SortCompareFunc, error)

func (*SortCompareFunc) Sort

func (self *SortCompareFunc) Sort(slice interface{}) error

type StructVisitor

type StructVisitor interface {
	BeginStruct(depth int, v reflect.Value) error
	StructField(depth int, v reflect.Value, f reflect.StructField, index int) error
	EndStruct(depth int, v reflect.Value) error

	BeginSlice(depth int, v reflect.Value) error
	SliceField(depth int, v reflect.Value, index int) error
	EndSlice(depth int, v reflect.Value) error

	BeginArray(depth int, v reflect.Value) error
	ArrayField(depth int, v reflect.Value, index int) error
	EndArray(depth int, v reflect.Value) error

	BeginMap(depth int, v reflect.Value) error
	MapField(depth int, v reflect.Value, key string, index int) error
	EndMap(depth int, v reflect.Value) error
}

Jump to

Keyboard shortcuts

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