Reflect

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2023 License: MIT Imports: 4 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 string `ref:"Reflect"`
}

func TestTag(t *testing.T) {
	tag := Reflect.NewTagStruct("ref")
	v := tag.Get(Struct1{})
	for idx, val := range v {
		fmt.Printf("#%d: {Ptr: %v, Val: %v}\n  val.Ptr: %v\n", idx, val.Ptr, val.Val, tag.Ptr(val.Ptr))
	}
}
命令
go test github.com/Drelf2018/TypeGo/Reflect -v
控制台
=== RUN   TestTag
#0: {Ptr: 16922208, Val: {514 [{1 []} {14 []}]}}
  val.Ptr: [{16866560 {1 []}} {16867136 {14 []}}]
#1: {Ptr: 16922336, Val: {810 [{19 []} {19 []}]}}
  val.Ptr: [{16866624 {19 []}} {16866880 {19 []}}]
#2: {Ptr: 16866560, Val: {Reflect []}}
  val.Ptr: []
--- PASS: TestTag (0.00s)
PASS
ok      github.com/Drelf2018/TypeGo/Reflect     0.027s

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 Eface

func Eface(in any) *eface

func Field

func Field(field reflect.StructField) uintptr

func Fields

func Fields(v any) Chan.Chan[reflect.StructField]

func Ptr

func Ptr(in any) uintptr

func Type

func Type(typ reflect.Type) uintptr

Types

type Reflect

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

func New

func New[V any](parse func(self *Reflect[V], field reflect.StructField) V) *Reflect[V]

func NewTag

func NewTag(tag string) *Reflect[string]

func NewTagStruct

func NewTagStruct(tag string) *Reflect[Tag]

func NewTagWithName

func NewTagWithName(tag string) *Reflect[[2]string]

func (*Reflect[V]) Clear

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

func (*Reflect[V]) Get

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

func (*Reflect[V]) Ptr

func (r *Reflect[V]) Ptr(in uintptr) Values[V]

type Tag

type Tag struct {
	Tag    string
	Fields []Tag
}

type Value

type Value[V any] struct {
	Ptr uintptr
	Val V
}

type Values

type Values[V any] []Value[V]

func (Values[V]) Values

func (vs Values[V]) Values() []V

Jump to

Keyboard shortcuts

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