reflectlite

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const Ptr = Pointer

Ptr is the old name for the Pointer kind.

Variables

This section is empty.

Functions

func Copy added in v0.37.0

func Copy(dst, src Value) int

Copy copies the contents of src into dst until either dst has been filled or src has been exhausted.

func DeepEqual added in v0.37.0

func DeepEqual(x, y interface{}) bool

DeepEqual reports whether x and y are “deeply equal”, defined as follows. Two values of identical type are deeply equal if one of the following cases applies. Values of distinct types are never deeply equal.

Array values are deeply equal when their corresponding elements are deeply equal.

Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.

Func values are deeply equal if both are nil; otherwise they are not deeply equal.

Interface values are deeply equal if they hold deeply equal concrete values.

Map values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they are the same map object or their corresponding keys (matched using Go equality) map to deeply equal values.

Pointer values are deeply equal if they are equal using Go's == operator or if they point to deeply equal values.

Slice values are deeply equal when all of the following are true: they are both nil or both non-nil, they have the same length, and either they point to the same initial entry of the same underlying array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal. Note that a non-nil empty slice and a nil slice (for example, []byte{} and []byte(nil)) are not deeply equal.

Other values - numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.

In general DeepEqual is a recursive relaxation of Go's == operator. However, this idea is impossible to implement without some inconsistency. Specifically, it is possible for a value to be unequal to itself, either because it is of func type (uncomparable in general) or because it is a floating-point NaN value (not equal to itself in floating-point comparison), or because it is an array, struct, or interface containing such a value. On the other hand, pointer values are always equal to themselves, even if they point at or contain such problematic values, because they compare equal using Go's == operator, and that is a sufficient condition to be deeply equal, regardless of content. DeepEqual has been defined so that the same short-cut applies to slices and maps: if x and y are the same slice or the same map, they are deeply equal regardless of content.

As DeepEqual traverses the data values it may find a cycle. The second and subsequent times that DeepEqual compares two pointer values that have been compared before, it treats the values as equal rather than examining the values to which they point. This ensures that DeepEqual terminates.

func Swapper

func Swapper(slice interface{}) func(i, j int)

Types

type ChanDir added in v0.37.0

type ChanDir int

ChanDir represents a channel type's direction.

const (
	RecvDir ChanDir             = 1 << iota // <-chan
	SendDir                                 // chan<-
	BothDir = RecvDir | SendDir             // chan
)

type Kind

type Kind uint8
const (
	Invalid Kind = iota
	Bool
	Int
	Int8
	Int16
	Int32
	Int64
	Uint
	Uint8
	Uint16
	Uint32
	Uint64
	Uintptr
	Float32
	Float64
	Complex64
	Complex128
	String
	UnsafePointer
	Chan
	Interface
	Pointer
	Slice
	Array
	Func
	Map
	Struct
)

Copied from reflect/type.go https://golang.org/src/reflect/type.go?s=8302:8316#L217 These constants must match basicTypes and the typeKind* constants in compiler/interface.go

func (Kind) String added in v0.37.0

func (k Kind) String() string

type MapIter added in v0.37.0

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

func (*MapIter) Key added in v0.37.0

func (it *MapIter) Key() Value

func (*MapIter) Next added in v0.37.0

func (it *MapIter) Next() bool

func (*MapIter) Reset added in v0.37.0

func (iter *MapIter) Reset(v Value)

func (*MapIter) Value added in v0.37.0

func (it *MapIter) Value() Value

type RawType added in v0.37.0

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

The base type struct. All type structs start with this.

func (*RawType) Align added in v0.37.0

func (t *RawType) Align() int

Align returns the alignment of this type. It is similar to calling unsafe.Alignof.

func (*RawType) AssignableTo added in v0.37.0

func (t *RawType) AssignableTo(u Type) bool

AssignableTo returns whether a value of type t can be assigned to a variable of type u.

func (*RawType) Bits added in v0.37.0

func (t *RawType) Bits() int

Bits returns the number of bits that this type uses. It is only valid for arithmetic types (integers, floats, and complex numbers). For other types, it will panic.

func (*RawType) ChanDir added in v0.37.0

func (t *RawType) ChanDir() ChanDir

func (*RawType) Comparable added in v0.37.0

func (t *RawType) Comparable() bool

Comparable returns whether values of this type can be compared to each other.

func (*RawType) Elem added in v0.37.0

func (t *RawType) Elem() Type

Elem returns the element type for channel, slice and array types, the pointed-to value for pointer types, and the key type for map types.

func (*RawType) Field added in v0.37.0

func (t *RawType) Field(i int) StructField

Field returns the type of the i'th field of this struct type. It panics if t is not a struct type.

func (*RawType) FieldAlign added in v0.37.0

func (t *RawType) FieldAlign() int

FieldAlign returns the alignment if this type is used in a struct field. It is currently an alias for Align() but this might change in the future.

func (*RawType) FieldByIndex added in v0.37.0

func (t *RawType) FieldByIndex(index []int) StructField

func (*RawType) FieldByName added in v0.37.0

func (t *RawType) FieldByName(name string) (StructField, bool)

func (*RawType) FieldByNameFunc added in v0.37.0

func (t *RawType) FieldByNameFunc(match func(string) bool) (StructField, bool)

func (*RawType) Implements added in v0.37.0

func (t *RawType) Implements(u Type) bool

func (*RawType) Key added in v0.37.0

func (t *RawType) Key() Type

func (*RawType) Kind added in v0.37.0

func (t *RawType) Kind() Kind

func (*RawType) Len added in v0.37.0

func (t *RawType) Len() int

Len returns the number of elements in this array. It panics of the type kind is not Array.

func (*RawType) Name added in v0.37.0

func (t *RawType) Name() string

func (*RawType) NumField added in v0.37.0

func (t *RawType) NumField() int

NumField returns the number of fields of a struct type. It panics for other type kinds.

func (*RawType) NumMethod added in v0.37.0

func (t *RawType) NumMethod() int

func (RawType) OverflowComplex added in v0.37.0

func (t RawType) OverflowComplex(x complex128) bool

OverflowComplex reports whether the complex128 x cannot be represented by type t. It panics if t's Kind is not Complex64 or Complex128.

func (RawType) OverflowFloat added in v0.37.0

func (t RawType) OverflowFloat(x float64) bool

OverflowFloat reports whether the float64 x cannot be represented by type t. It panics if t's Kind is not Float32 or Float64.

func (RawType) OverflowInt added in v0.37.0

func (t RawType) OverflowInt(x int64) bool

OverflowInt reports whether the int64 x cannot be represented by type t. It panics if t's Kind is not Int, Int8, Int16, Int32, or Int64.

func (RawType) OverflowUint added in v0.37.0

func (t RawType) OverflowUint(x uint64) bool

OverflowUint reports whether the uint64 x cannot be represented by type t. It panics if t's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.

func (*RawType) PkgPath added in v0.37.0

func (t *RawType) PkgPath() string

func (*RawType) Size added in v0.37.0

func (t *RawType) Size() uintptr

Size returns the size in bytes of a given type. It is similar to unsafe.Sizeof.

func (*RawType) String added in v0.37.0

func (t *RawType) String() string

type StructField added in v0.37.0

type StructField struct {
	// Name indicates the field name.
	Name string

	// PkgPath is the package path where the struct containing this field is
	// declared for unexported fields, or the empty string for exported fields.
	PkgPath string

	Type      Type
	Tag       StructTag // field tag string
	Offset    uintptr
	Index     []int // index sequence for Type.FieldByIndex
	Anonymous bool
}

A StructField describes a single field in a struct. This must be kept in sync with reflect.StructField.

func VisibleFields added in v0.37.0

func VisibleFields(t Type) []StructField

VisibleFields returns all the visible fields in t, which must be a struct type. A field is defined as visible if it's accessible directly with a FieldByName call. The returned fields include fields inside anonymous struct members and unexported fields. They follow the same order found in the struct, with anonymous fields followed immediately by their promoted fields.

For each element e of the returned slice, the corresponding field can be retrieved from a value v of type t by calling v.FieldByIndex(e.Index).

func (StructField) IsExported added in v0.37.0

func (f StructField) IsExported() bool

IsExported reports whether the field is exported.

type StructTag added in v0.37.0

type StructTag string

A StructTag is the tag string in a struct field.

func (StructTag) Get added in v0.37.0

func (tag StructTag) Get(key string) string

Get returns the value associated with key in the tag string.

func (StructTag) Lookup added in v0.37.0

func (tag StructTag) Lookup(key string) (value string, ok bool)

Lookup returns the value associated with key in the tag string.

type Type

type Type interface {
	// These should match the reflectlite.Type implementation in Go.
	AssignableTo(u Type) bool
	Comparable() bool
	Elem() Type
	Implements(u Type) bool
	Kind() Kind
	Name() string
	PkgPath() string
	Size() uintptr
	String() string

	// Additional methods shared with reflect.Type.
	Align() int
	Field(i int) StructField
	Key() Type
	Len() int
	NumField() int
	NumMethod() int
}

Type represents the minimal interface for a Go type.

func ArrayOf added in v0.37.0

func ArrayOf(n int, t Type) Type

func FuncOf added in v0.37.0

func FuncOf(in, out []Type, variadic bool) Type

func MapOf added in v0.37.0

func MapOf(key, value Type) Type

func PointerTo added in v0.37.0

func PointerTo(t Type) Type

func PtrTo added in v0.37.0

func PtrTo(t Type) Type

func SliceOf added in v0.37.0

func SliceOf(t Type) Type

func StructOf added in v0.37.0

func StructOf([]StructField) Type

func TypeFor added in v0.37.0

func TypeFor[T any]() Type

TypeFor returns the Type that represents the type argument T.

func TypeOf

func TypeOf(i interface{}) Type

type TypeError added in v0.37.0

type TypeError struct {
	Method string
}

TypeError is the error that is used in a panic when invoking a method on a type that is not applicable to that type.

func (*TypeError) Error added in v0.37.0

func (e *TypeError) Error() string

type Value

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

func Append added in v0.37.0

func Append(v Value, x ...Value) Value

Append appends the values x to a slice s and returns the resulting slice. As in Go, each x's value must be assignable to the slice's element type.

func AppendSlice added in v0.37.0

func AppendSlice(s, t Value) Value

AppendSlice appends a slice t to a slice s and returns the resulting slice. The slices s and t must have the same element type.

func Indirect added in v0.37.0

func Indirect(v Value) Value

func MakeMap added in v0.37.0

func MakeMap(typ Type) Value

MakeMap creates a new map with the specified type.

func MakeMapWithSize added in v0.37.0

func MakeMapWithSize(typ Type, n int) Value

MakeMapWithSize creates a new map with the specified type and initial space for approximately n elements.

func MakeSlice added in v0.37.0

func MakeSlice(typ Type, len, cap int) Value

func New added in v0.37.0

func New(typ Type) Value

New is the reflect equivalent of the new(T) keyword, returning a pointer to a new value of the given type.

func NewAt added in v0.37.0

func NewAt(typ Type, p unsafe.Pointer) Value

func ValueOf

func ValueOf(i interface{}) Value

func Zero added in v0.37.0

func Zero(typ Type) Value

func (Value) Addr added in v0.37.0

func (v Value) Addr() Value

func (Value) Bool added in v0.37.0

func (v Value) Bool() bool

func (Value) Bytes added in v0.37.0

func (v Value) Bytes() []byte

func (Value) Call added in v0.37.0

func (v Value) Call(in []Value) []Value

func (Value) CallSlice added in v0.37.0

func (v Value) CallSlice(in []Value) []Value

func (Value) CanAddr added in v0.37.0

func (v Value) CanAddr() bool

func (Value) CanComplex added in v0.37.0

func (v Value) CanComplex() bool

CanComplex reports whether Complex can be used without panicking.

func (Value) CanConvert added in v0.37.0

func (v Value) CanConvert(t Type) bool

func (Value) CanFloat added in v0.37.0

func (v Value) CanFloat() bool

CanFloat reports whether Float can be used without panicking.

func (Value) CanInt added in v0.37.0

func (v Value) CanInt() bool

CanInt reports whether Uint can be used without panicking.

func (Value) CanInterface added in v0.37.0

func (v Value) CanInterface() bool

func (Value) CanSet added in v0.37.0

func (v Value) CanSet() bool

func (Value) CanUint added in v0.37.0

func (v Value) CanUint() bool

CanUint reports whether Uint can be used without panicking.

func (Value) Cap added in v0.37.0

func (v Value) Cap() int

Cap returns the capacity of this value for arrays, channels and slices. For other types, it panics.

func (Value) Clear added in v0.37.0

func (v Value) Clear()

Clear clears the contents of a map or zeros the contents of a slice

It panics if v's Kind is not Map or Slice.

func (Value) Comparable added in v0.37.0

func (v Value) Comparable() bool

func (Value) Complex added in v0.37.0

func (v Value) Complex() complex128

func (Value) Convert added in v0.37.0

func (v Value) Convert(t Type) Value

func (Value) Elem added in v0.37.0

func (v Value) Elem() Value

func (Value) Equal added in v0.37.0

func (v Value) Equal(u Value) bool

Equal reports true if v is equal to u. For two invalid values, Equal will report true. For an interface value, Equal will compare the value within the interface. Otherwise, If the values have different types, Equal will report false. Otherwise, for arrays and structs Equal will compare each element in order, and report false if it finds non-equal elements. During all comparisons, if values of the same type are compared, and the type is not comparable, Equal will panic.

Copyright 2009 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

func (Value) Field added in v0.37.0

func (v Value) Field(i int) Value

Field returns the value of the i'th field of this struct.

func (Value) FieldByIndex added in v0.37.0

func (v Value) FieldByIndex(index []int) Value

FieldByIndex returns the nested field corresponding to index.

func (Value) FieldByIndexErr added in v0.37.0

func (v Value) FieldByIndexErr(index []int) (Value, error)

FieldByIndexErr returns the nested field corresponding to index.

func (Value) FieldByName added in v0.37.0

func (v Value) FieldByName(name string) Value

func (Value) FieldByNameFunc added in v0.37.0

func (v Value) FieldByNameFunc(match func(string) bool) Value

func (Value) Float added in v0.37.0

func (v Value) Float() float64

func (Value) Float32 added in v0.37.0

func (v Value) Float32() float32

func (Value) Grow added in v0.37.0

func (v Value) Grow(n int)

Grow increases the slice's capacity, if necessary, to guarantee space for another n elements. After Grow(n), at least n elements can be appended to the slice without another allocation.

It panics if v's Kind is not a Slice or if n is negative or too large to allocate the memory.

func (Value) Index added in v0.37.0

func (v Value) Index(i int) Value

func (Value) Int added in v0.37.0

func (v Value) Int() int64

func (Value) Interface added in v0.37.0

func (v Value) Interface() interface{}

func (Value) IsNil added in v0.37.0

func (v Value) IsNil() bool

IsNil returns whether the value is the nil value. It panics if the value Kind is not a channel, map, pointer, function, slice, or interface.

func (Value) IsValid added in v0.37.0

func (v Value) IsValid() bool

func (Value) IsZero added in v0.37.0

func (v Value) IsZero() bool

IsZero reports whether v is the zero value for its type. It panics if the argument is invalid.

func (Value) Kind added in v0.37.0

func (v Value) Kind() Kind

func (Value) Len added in v0.37.0

func (v Value) Len() int

Len returns the length of this value for slices, strings, arrays, channels, and maps. For other types, it panics.

func (Value) MapIndex added in v0.37.0

func (v Value) MapIndex(key Value) Value

func (Value) MapKeys added in v0.37.0

func (v Value) MapKeys() []Value

func (Value) MapRange added in v0.37.0

func (v Value) MapRange() *MapIter

func (Value) Method added in v0.37.0

func (v Value) Method(i int) Value

func (Value) MethodByName added in v0.37.0

func (v Value) MethodByName(name string) Value

func (Value) NumField added in v0.37.0

func (v Value) NumField() int

NumField returns the number of fields of this struct. It panics for other value types.

func (Value) NumMethod added in v0.37.0

func (v Value) NumMethod() int

func (Value) OverflowFloat added in v0.37.0

func (v Value) OverflowFloat(x float64) bool

OverflowFloat reports whether the float64 x cannot be represented by v's type. It panics if v's Kind is not Float32 or Float64.

func (Value) OverflowInt added in v0.37.0

func (v Value) OverflowInt(x int64) bool

OverflowInt reports whether the int64 x cannot be represented by v's type. It panics if v's Kind is not Int, Int8, Int16, Int32, or Int64.

func (Value) OverflowUint added in v0.37.0

func (v Value) OverflowUint(x uint64) bool

OverflowUint reports whether the uint64 x cannot be represented by v's type. It panics if v's Kind is not Uint, Uintptr, Uint8, Uint16, Uint32, or Uint64.

func (Value) Pointer added in v0.37.0

func (v Value) Pointer() uintptr

Pointer returns the underlying pointer of the given value for the following types: chan, map, pointer, unsafe.Pointer, slice, func.

func (Value) RawType added in v0.37.0

func (v Value) RawType() *RawType

Internal function only, do not use.

RawType returns the raw, underlying type code. It is used in the runtime package and needs to be exported for the runtime package to access it.

func (Value) Recv added in v0.37.0

func (v Value) Recv() (x Value, ok bool)

func (Value) Set added in v0.37.0

func (v Value) Set(x Value)

func (Value) SetBool added in v0.37.0

func (v Value) SetBool(x bool)

func (Value) SetBytes added in v0.37.0

func (v Value) SetBytes(x []byte)

func (Value) SetCap added in v0.37.0

func (v Value) SetCap(n int)

func (Value) SetComplex added in v0.37.0

func (v Value) SetComplex(x complex128)

func (Value) SetFloat added in v0.37.0

func (v Value) SetFloat(x float64)

func (Value) SetInt added in v0.37.0

func (v Value) SetInt(x int64)

func (Value) SetIterKey added in v0.37.0

func (v Value) SetIterKey(iter *MapIter)

func (Value) SetIterValue added in v0.37.0

func (v Value) SetIterValue(iter *MapIter)

func (Value) SetLen added in v0.37.0

func (v Value) SetLen(n int)

func (Value) SetMapIndex added in v0.37.0

func (v Value) SetMapIndex(key, elem Value)

func (Value) SetString added in v0.37.0

func (v Value) SetString(x string)

func (Value) SetUint added in v0.37.0

func (v Value) SetUint(x uint64)

func (Value) SetZero added in v0.37.0

func (v Value) SetZero()

func (Value) Slice added in v0.37.0

func (v Value) Slice(i, j int) Value

func (Value) Slice3 added in v0.37.0

func (v Value) Slice3(i, j, k int) Value

func (Value) String added in v0.37.0

func (v Value) String() string

func (Value) Type added in v0.37.0

func (v Value) Type() Type

func (Value) Uint added in v0.37.0

func (v Value) Uint() uint64

func (Value) UnsafeAddr added in v0.37.0

func (v Value) UnsafeAddr() uintptr

func (Value) UnsafePointer added in v0.37.0

func (v Value) UnsafePointer() unsafe.Pointer

UnsafePointer returns the underlying pointer of the given value for the following types: chan, map, pointer, unsafe.Pointer, slice, func.

type ValueError

type ValueError struct {
	Method string
	Kind   Kind
}

func (*ValueError) Error added in v0.37.0

func (e *ValueError) Error() string

Jump to

Keyboard shortcuts

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