Reflect

package
v0.7.4 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: MIT Imports: 6 Imported by: 0

README

Reflect

更好的反射

使用
package Reflect_test

import (
	"fmt"
	"testing"

	"github.com/Drelf2018/TypeGo/Reflect"
)

type Struct1 struct {
	Struct2 struct {
		d1 string `ref:"1"`
		d2 int64  `ref:"14"`
	} `ref:"514"`

	Struct3 struct {
		d3 bool    `ref:"19"`
		d4 float64 `ref:"19"`
	} `ref:"810"`

	D5 *Struct1 `ref:"Struct1"`
	D6 string

	Struct4 struct {
		d7 *Struct1 `ref:"Struct7"`
	} `ref:"Struct4"`
}

func TestTag(t *testing.T) {
	tag := Reflect.NewTagStruct("ref")
	v := tag.Get(&Struct1{})
	for idx, val := range v {
		fmt.Printf("#%d: %v\n", idx, val)
	}
}
命令
go test github.com/Drelf2018/TypeGo/Reflect -v
控制台
=== RUN   TestTag
#0: Tag(514, [Tag(1), Tag(14)])
#1: Tag(810, [Tag(19), Tag(19)])
#2: Tag(Struct1)
#3: Tag()
#4: Tag(Struct4, [Tag(Struct7)])
--- PASS: TestTag (0.00s)
PASS
ok      github.com/Drelf2018/TypeGo/Reflect     0.031s

Documentation

Index

Constants

View Source
const (
	NOALIAS       NoAlias       = 1 << iota
	PTRALIAS      PtrAlias      = 1 << iota
	SLICEALIAS    SliceAlias    = 1 << iota
	SLICEPTRALIAS SlicePtrAlias = 1 << iota
)

Variables

This section is empty.

Functions

func Addr added in v0.4.2

func Addr(typ reflect.Type) uintptr

func Eface

func Eface(in any) *eface

func FieldOf added in v0.7.0

func FieldOf(elem reflect.Type) []reflect.StructField

func Fields

func Fields(elem reflect.Type) Chan.Chan[reflect.StructField]

func IsEmbeddedMethod added in v0.7.2

func IsEmbeddedMethod(v reflect.Method) bool

func MethodFuncOf added in v0.7.1

func MethodFuncOf(value reflect.Value) map[string]any

func MethodOf added in v0.7.1

func MethodOf(value reflect.Value) map[reflect.Method]reflect.Value

func Pointer added in v0.5.1

func Pointer(elem reflect.Type) []uintptr

func Ptr

func Ptr(in any) uintptr

func Slice added in v0.5.1

func Slice(elem reflect.Type) []uintptr

func SlicePtr added in v0.6.0

func SlicePtr(elem reflect.Type) []uintptr

func Type

func Type(typ reflect.Type) uintptr

func ValuesOf added in v0.7.2

func ValuesOf(in ...any) []reflect.Value

func Zero added in v0.7.4

func Zero(typ reflect.Type) reflect.Value

Zero returns a Value representing the zero value for the specified type. The result is addressable and settable.

Types

type Alias added in v0.7.0

type Alias interface {
	Ptr(reflect.Type) []uintptr
}

type Aliases added in v0.7.0

type Aliases []Alias

func (Aliases) Ptr added in v0.7.0

func (as Aliases) Ptr(elem reflect.Type) (u []uintptr)

type ErrValue

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

func (ErrValue) Error added in v0.7.0

func (e ErrValue) Error() string

type Field

type Field struct {
	Index int
	Name  string
	Tag   string
}

type ForFields added in v0.7.0

type ForFields[V any] func(*Map[[]V], reflect.StructField, reflect.Type) V

func (ForFields[V]) Parse added in v0.7.0

func (f ForFields[V]) Parse(ref *Map[[]V], elem reflect.Type) []V

type Map added in v0.7.0

type Map[V any] struct {
	Alias
	Parser[V]
	// contains filtered or unexported fields
}

func NewMap added in v0.7.0

func NewMap[P Parser[V], V any](p P, alias ...Alias) *Map[V]

func (*Map[V]) Get added in v0.7.0

func (r *Map[V]) Get(in any) V

func (*Map[V]) GetType added in v0.7.0

func (r *Map[V]) GetType(elem reflect.Type) (v V, ok bool)

func (*Map[V]) Gets added in v0.7.0

func (r *Map[V]) Gets(in ...any) []V

func (*Map[V]) Init added in v0.7.0

func (r *Map[V]) Init(in ...any) *Map[V]

func (*Map[V]) MustGetType added in v0.7.0

func (r *Map[V]) MustGetType(elem reflect.Type) (v V)

func (*Map[V]) Ptr added in v0.7.0

func (r *Map[V]) Ptr(in uintptr) (v V, ok bool)

type NoAlias added in v0.7.0

type NoAlias int

func (NoAlias) Ptr added in v0.7.0

func (NoAlias) Ptr(elem reflect.Type) []uintptr

type Option added in v0.5.1

type Option[V any] func(*Reflect[V])

func WithAlias added in v0.5.1

func WithAlias[V any](f func(elem reflect.Type) []uintptr) Option[V]

func WithSlice added in v0.5.1

func WithSlice[V any](in ...any) Option[V]

type Parser added in v0.7.0

type Parser[V any] interface {
	Parse(*Map[V], reflect.Type) V
}

type PtrAlias added in v0.7.0

type PtrAlias int

func (PtrAlias) Ptr added in v0.7.0

func (PtrAlias) Ptr(elem reflect.Type) []uintptr

type Reflect

type Reflect[V any] struct {
	Alias func(elem reflect.Type) []uintptr
	Parse func(self *Reflect[V], field reflect.StructField, elem reflect.Type) V
	// contains filtered or unexported fields
}

func New

func New[V any](parse func(self *Reflect[V], field reflect.StructField, elem reflect.Type) V, options ...Option[V]) (r *Reflect[V])

func NewTag

func NewTag(tag string, options ...Option[string]) *Reflect[string]

func NewTagStruct

func NewTagStruct(tag string, options ...Option[Tag]) *Reflect[Tag]

func (*Reflect[V]) Clear

func (r *Reflect[V]) Clear()

func (*Reflect[V]) Get

func (r *Reflect[V]) Get(in any) (v []V)

func (*Reflect[V]) GetType added in v0.4.2

func (r *Reflect[V]) GetType(elem reflect.Type, v *[]V) bool

func (*Reflect[V]) Init added in v0.5.1

func (r *Reflect[V]) Init(in ...any)

func (*Reflect[V]) Ptr

func (r *Reflect[V]) Ptr(in uintptr, v *[]V) (ok bool)

type SliceAlias added in v0.7.0

type SliceAlias int

func (SliceAlias) Ptr added in v0.7.0

func (SliceAlias) Ptr(elem reflect.Type) []uintptr

type SlicePtrAlias added in v0.7.0

type SlicePtrAlias int

func (SlicePtrAlias) Ptr added in v0.7.0

func (SlicePtrAlias) Ptr(elem reflect.Type) []uintptr

type Tag

type Tag struct {
	Tag    string
	Fields []Tag
}

func (Tag) String added in v0.4.2

func (t Tag) String() string

type TagParser added in v0.7.0

type TagParser[T interface {
	Insert(field Field, ok bool, fields []*T, array *[]*T)
}] string

func (TagParser[T]) Parse added in v0.7.0

func (tag TagParser[T]) Parse(ref *Map[[]*T], elem reflect.Type) []*T

type Tags added in v0.4.2

type Tags []Tag

func (Tags) String added in v0.4.2

func (t Tags) String() string

type Value

type Value struct {
	reflect.Value
}

func ValueOf added in v0.7.3

func ValueOf(i any) Value

func (Value) Call added in v0.7.2

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

func (Value) CallAny added in v0.7.2

func (v Value) CallAny(in ...any) []reflect.Value

func (Value) CallAnySlice added in v0.7.2

func (v Value) CallAnySlice(in ...any) []reflect.Value

func (Value) CallSlice added in v0.7.2

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

Jump to

Keyboard shortcuts

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