Reflect

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2023 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

This section is empty.

Variables

View Source
var ErrValue = errors.New("you should pass in a struct or a pointer to a struct")

Functions

func Addr added in v0.4.2

func Addr(typ reflect.Type) uintptr

func Eface

func Eface(in any) *eface

func Fields

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

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 Type

func Type(typ reflect.Type) uintptr

Types

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 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 Tag

type Tag struct {
	Tag    string
	Fields []Tag
}

func (Tag) String added in v0.4.2

func (t Tag) String() string

type Tags added in v0.4.2

type Tags []Tag

func (Tags) String added in v0.4.2

func (t Tags) String() string

Jump to

Keyboard shortcuts

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