reflect

package
v1.2.117 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: MIT Imports: 12 Imported by: 9

Documentation

Index

Constants

View Source
const PtrSize = unsafe.Sizeof(uintptr(0)) // an ideal const, sizeof *void, as 4 << (^uintptr(0) >> 63)

Variables

This section is empty.

Functions

func DumpTypeInfoBFS

func DumpTypeInfoBFS(t reflect.Type) string

func DumpTypeInfoDFS

func DumpTypeInfoDFS(t reflect.Type) string

func DumpValueInfoBFS

func DumpValueInfoBFS(v reflect.Value) string

func DumpValueInfoDFS

func DumpValueInfoDFS(v reflect.Value) string

func FieldByNames added in v1.2.7

func FieldByNames(v reflect.Value, names ...string) (x reflect.Value, ok bool)

FieldByNames returns the struct field with the given names. It returns the zero Value if no field was found. It panics if v's Kind is not struct.

func FollowTypePointer

func FollowTypePointer(v reflect.Type) reflect.Type

func FollowValuePointer

func FollowValuePointer(v reflect.Value) reflect.Value

func IsEmptyValue

func IsEmptyValue(v reflect.Value) bool

IsEmptyValue reports whether v is empty value for its type.

The zero value is:

0 for numeric types, false for the boolean type, and "" (the empty string) for strings, and {} (the empty struct) for structs, and untyped nil or typed nil or len == 0 for maps, slices, pointers, functions, interfaces, and channels. Code borrowed from https://github.com/golang/go/blob/go1.22.0/src/encoding/json/encode.go#L306

func IsFieldExported

func IsFieldExported(sf reflect.StructField) bool

func IsNil added in v1.2.91

func IsNil(v any) bool

IsNil reports whether its argument v is a nil interface value or an untyped nil. Note that IsNil is not always equivalent to a regular comparison with nil in Go. It is equivalent to:

var typedNil any = (v's underlying type)(nil)
return v == nil || v == typedNil

For example, if v was created by set with `var p *int` or calling IsNil((*int)(nil)), i==nil will be false but IsNil will be true.

func IsNilObject deprecated

func IsNilObject(i any) (result bool)

Deprecated: Use IsNil instead.

func IsNilType

func IsNilType(v reflect.Type) (result bool)

nil, unknown type

func IsNilValue

func IsNilValue(v reflect.Value) bool

IsNilValue reports whether v is untyped nil or typed nil for its type.

func IsValidTagKey

func IsValidTagKey(tagKey string) bool

IsValidTagKey checks if key of struct tag, like `json:"name,omitempty"`, is valid

func IsZeroValue

func IsZeroValue(v reflect.Value) bool

IsZeroValue reports whether v is zero value for its type. Zero values are variables declared without an explicit initial value are given their zero value.

The zero value is:

0 for numeric types, false for the boolean type, and "" (the empty string) for strings. untyped nil or typed nil for maps, slices, pointers, functions, interfaces, and channels.

func SetFieldByNames added in v1.2.7

func SetFieldByNames(v reflect.Value, names []string, x reflect.Value) (ok bool)

SetFieldByNames assigns x to the value v. It panics if CanSet returns false. As in Go, x's value must be assignable to type of v's son, grandson, etc

func Truncate

func Truncate(v any, f func(v any) bool, n int) any

Truncate reset bytes and string at each run of value c satisfying f(c), useful for dump into log if some field is huge v is truncated in place return interface{} same as truncated v for stream-like api

func TruncateBytes

func TruncateBytes(v any, n int) any

TruncateBytes reset bytes, useful for dump into log if some field is huge v is truncated in place return interface{} same as truncated v for stream-like api

func TruncateString

func TruncateString(v any, n int) any

TruncateString reset string, useful for dump into log if some field is huge v is truncated in place return interface{} same as truncated v for stream-like api

func TypeByStructFieldIndex

func TypeByStructFieldIndex(t reflect.Type, index []int) reflect.Type

t[i][j][...], t[i] is t's ith field, t[i][j] is t[i]'s jth field

func UnTypeNil added in v1.2.91

func UnTypeNil(v any) any

UnTypeNil returns its argument v or nil if and only if v is a nil interface value or an untyped nil.

func ValueByStructFieldIndex

func ValueByStructFieldIndex(v reflect.Value, index []int) reflect.Value

v[i][j][...], v[i] is v's ith field, v[i][j] is v[i]'s jth field

func Walk

func Walk(t reflect.Type, visitedOnce bool, do func(s reflect.Type, sf reflect.StructField) (stop bool))

Walk walks down v

func WalkTypeBFS

func WalkTypeBFS(typ reflect.Type, handler FieldTypeInfoHandler)

Breadth First Search

func WalkTypeDFS

func WalkTypeDFS(typ reflect.Type, handler FieldTypeInfoHandler)

Wid First Search

func WalkValueBFS

func WalkValueBFS(val reflect.Value, handler FieldValueInfoHandler)

func WalkValueDFS

func WalkValueDFS(val reflect.Value, handler FieldValueInfoHandler)

Types

type FieldTypeInfo

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

A field represents a single field found in a struct.

func (FieldTypeInfo) Depth

func (info FieldTypeInfo) Depth() int

func (FieldTypeInfo) Index

func (info FieldTypeInfo) Index() []int

func (FieldTypeInfo) MiddleNodes

func (info FieldTypeInfo) MiddleNodes() []any

func (FieldTypeInfo) String

func (info FieldTypeInfo) String() string

func (FieldTypeInfo) StructField

func (info FieldTypeInfo) StructField() (reflect.StructField, bool)

type FieldTypeInfoHandler

type FieldTypeInfoHandler interface {
	Handler(info FieldTypeInfo) (goon bool)
}

type FieldTypeInfoHandlerFunc

type FieldTypeInfoHandlerFunc func(info FieldTypeInfo) (goon bool)

func (FieldTypeInfoHandlerFunc) Handler

func (f FieldTypeInfoHandlerFunc) Handler(info FieldTypeInfo) (goon bool)

type FieldValueInfo

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

FieldValueInfo represents a single field found in a struct.

func (FieldValueInfo) Depth

func (info FieldValueInfo) Depth() int

func (FieldValueInfo) Index

func (info FieldValueInfo) Index() []int

func (FieldValueInfo) MiddleNodes

func (info FieldValueInfo) MiddleNodes() []any

func (*FieldValueInfo) String

func (info *FieldValueInfo) String() string

func (FieldValueInfo) StructField

func (info FieldValueInfo) StructField() (reflect.StructField, bool)

func (FieldValueInfo) Value

func (info FieldValueInfo) Value() reflect.Value

type FieldValueInfoHandler

type FieldValueInfoHandler interface {
	Handler(info FieldValueInfo) (goon bool)
}

type FieldValueInfoHandlerFunc

type FieldValueInfoHandlerFunc func(info FieldValueInfo) (goon bool)

func (FieldValueInfoHandlerFunc) Handler

func (f FieldValueInfoHandlerFunc) Handler(info FieldValueInfo) (goon bool)

type StructTag

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

A StructTag is the tag string in a struct field, as reflect.StructTag .

By convention, tag strings are a concatenation of optionally space-separated key:"value" pairs. Each key is a non-empty string consisting of non-control characters other than space (U+0020 ' '), quote (U+0022 '"'), and colon (U+003A ':'). Each value is quoted using U+0022 '"' characters and Go string literal syntax.

func ParseAstStructTag

func ParseAstStructTag(tag string) (*StructTag, error)

ParseAstStructTag parses a single struct field tag of AST and returns the set of subTags. This code is based on the validateStructTag code in package tag `json:"name,omitempty"`, field.Tag.Value returned by AST

func ParseStructTag

func ParseStructTag(tag string) (*StructTag, error)

ParseStructTag parses a single struct field tag and returns the set of subTags. This code is based on the validateStructTag code in package tag json:"name,omitempty", reflect.StructField.Tag returned by reflect

func (*StructTag) AddOptions

func (t *StructTag) AddOptions(key string, options ...string)

AddOptions adds the given option for the given key. It appends to any existing options associated with key.

func (StructTag) AstString

func (t StructTag) AstString() string

AstString reassembles the subTags into a valid literal ast tag field representation key is random. tag `json:"name,omitempty"`, field.Tag.Value returned by AST

func (*StructTag) Delete

func (t *StructTag) Delete(keys ...string)

Delete deletes the tag for the given keys

func (*StructTag) DeleteOptions

func (t *StructTag) DeleteOptions(key string, options ...string)

DeleteOptions deletes the given options for the given key

func (StructTag) Get

func (t StructTag) Get(key string) (tag SubStructTag, ok bool)

Get returns the tag associated with the given key. If the key is present in the tag the value (which may be empty) is returned. Otherwise the returned value will be the empty string. The ok return value reports whether the tag exists or not (which the return value is nil).

func (StructTag) Keys

func (t StructTag) Keys() []string

Keys returns a slice of subTags.

func (StructTag) OrderKeys

func (t StructTag) OrderKeys() []string

OrderKeys returns a slice of subTags with original order.

func (StructTag) OrderedAstString

func (t StructTag) OrderedAstString() string

OrderedAstString reassembles the subTags into a valid literal ast tag field representation key is in the original order. tag `json:"name,omitempty"`, field.Tag.Value returned by AST

func (StructTag) OrderedString

func (t StructTag) OrderedString() string

OrderedString reassembles the subTags into a valid literal tag field representation key is in the original order. tag json:"name,omitempty", reflect.StructField.Tag returned by reflect

func (StructTag) OrderedTags

func (t StructTag) OrderedTags() []SubStructTag

Tags returns a slice of subTags with original order.

func (StructTag) SelectAstString

func (t StructTag) SelectAstString(keys ...string) string

SelectAstString reassembles the subTags selected by keys into a valid literal tag field representation tag json:"name,omitempty", reflect.StructField.Tag returned by reflect

func (StructTag) SelectString

func (t StructTag) SelectString(keys ...string) string

SelectString reassembles the subTags selected by keys into a valid literal tag field representation tag json:"name,omitempty", reflect.StructField.Tag returned by reflect

func (StructTag) SelectedTags

func (t StructTag) SelectedTags(keys ...string) []SubStructTag

SelectedTags returns a slice of subTags in keys order.

func (*StructTag) Set

func (t *StructTag) Set(subTag SubStructTag) error

Set sets the given tag. If the tag key already exists it'll override it

func (*StructTag) SetName

func (t *StructTag) SetName(key string, name string)

SetName sets the given name for the given key.

func (StructTag) SortedAstString

func (t StructTag) SortedAstString() string

SortedAstString reassembles the subTags into a valid literal ast tag field representation key is sorted by keys in increasing order. tag `json:"name,omitempty"`, field.Tag.Value returned by AST

func (StructTag) SortedKeys

func (t StructTag) SortedKeys() []string

SortedKeys returns a slice of subTags sorted by keys in increasing order.

func (StructTag) SortedString

func (t StructTag) SortedString() string

SortedString reassembles the subTags into a valid literal tag field representation key is sorted by keys in increasing order. tag json:"name,omitempty", reflect.StructField.Tag returned by reflect

func (StructTag) SortedTags

func (t StructTag) SortedTags() []SubStructTag

OrderedTags returns a slice of subTags sorted by keys in increasing order.

func (StructTag) String

func (t StructTag) String() string

String reassembles the subTags into a valid literal tag field representation key is random. tag json:"name,omitempty", reflect.StructField.Tag returned by reflect

func (StructTag) Tags

func (t StructTag) Tags() []SubStructTag

Tags returns a slice of subTags with original order.

type SubStructTag

type SubStructTag struct {
	// Key is the tag key, such as json, xml, etc.
	// i.e: `json:"foo,omitempty". Here key is: "json"
	Key string

	// Name is a part of the value
	// i.e: `json:"foo,omitempty". Here name is: "foo"
	Name string

	// Options is a part of the value. It contains a slice of tag options i.e:
	// `json:"foo,omitempty". Here options is: ["omitempty"]
	Options []string
}

SubStructTag defines a single struct's string literal tag

func (*SubStructTag) AddOptions

func (t *SubStructTag) AddOptions(opts ...string)

AddOptions adds the given option. It ignores any duplicated option.

func (*SubStructTag) DeleteOptions

func (t *SubStructTag) DeleteOptions(opts ...string)

DeleteOptions removes the given option. It ignores any option not found.

func (*SubStructTag) GoString

func (t *SubStructTag) GoString() string

GoString implements the fmt.GoStringer interface

func (*SubStructTag) HasOption

func (t *SubStructTag) HasOption(opt string) bool

HasOption returns true if the given option is available in options

func (*SubStructTag) String

func (t *SubStructTag) String() string

String reassembles the tag into a valid tag field representation

func (*SubStructTag) Value

func (t *SubStructTag) Value() string

Value returns the raw value of the tag, i.e. if the tag is `json:"foo,omitempty", the Value is "foo,omitempty"

type TagOptions

type TagOptions string

TagOptions is the string following a comma in a struct field's "json" tag, or the empty string. It does not include the leading comma.

func ParseTagOptions

func ParseTagOptions(tag string) (tagName string, opts TagOptions)

ParseTagOptions splits a struct field's json tag into its name and comma-separated options.

func (TagOptions) Contains

func (o TagOptions) Contains(optionName string) bool

Contains reports whether a comma-separated list of options contains a particular substr flag. substr must be surrounded by a string boundary or commas.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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