reflectMap

package module
v0.0.0-...-868fe4c Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 5 Imported by: 3

README

reflectMap

反射字典,以及更好的反射。

原项目 Reflect

使用
type Attachment struct {
	CollegeName string
	Name        string `xpath:"./a//text()"`
	Url         string `xpath:"./a/@href"`
}

type College struct {
	Name        string `xpath:"./td[1]//span/text()" gorm:"primaryKey"`
	Url         string `xpath:"./td[2]//a/@href"`
	Temp        string
	Attachments []Attachment `xpath:"//td//span[a] | //form//li[a] | //ul[@style='list-style-type:none;']//li[a] | //ul[@class='attach']//li[a]"`
}

func TestTagParser(t *testing.T) {
	m := reflectMap.NewTagParser("xpath", func(s string) string { return s })
	for _, data := range m.Get(&College{}) {
		t.Log(data)
	}
}
控制台
parser_test.go:31: Data#0(./td[1]//span/text())
parser_test.go:31: Data#1(./td[2]//a/@href)
parser_test.go:31: Data#3(//td//span[a] | //form//li[a] | //ul[@style='list-style-type:none;']//li[a] | //ul[@class='attach']//li[a])
写在最后

写给自己用的,如果真的真的有人用可以直接在 issues 问我咋用🥺🥺

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Addr

func Addr(typ reflect.Type) uintptr

func AddrSlice

func AddrSlice(typ reflect.Type) uintptr

func AddrSlicePtr

func AddrSlicePtr(typ reflect.Type) uintptr

func EFace

func EFace(in any) *eface

func FieldsOf

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

func FunctionsOf

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

func IsEmbeddedMethod

func IsEmbeddedMethod(v reflect.Method) bool

func MethodsOf

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

func Ptr

func Ptr(in any) uintptr

func Slice

func Slice(typ reflect.Type) uintptr

func SlicePtr

func SlicePtr(typ reflect.Type) uintptr

func Type

func Type(typ reflect.Type) uintptr

func ValuesOf

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

func Word

func Word(in any) uintptr

func Zero

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

type Alias func(reflect.Type) uintptr

type Data

type Data[V any] struct {
	Index  int
	V      V
	Fields []Data[V]
}

func (Data[V]) String

func (d Data[V]) String() string

type DataParser

type DataParser[V any] func(field reflect.StructField, elem reflect.Type) (v V, ok bool)

func (DataParser[V]) Do

func (d DataParser[V]) Do(m *Map[[]Data[V]], elem reflect.Type) (data []Data[V])

type ElemParser

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

type FieldParser

type FieldParser[V any] func(m *Map[V], field reflect.StructField, elem reflect.Type, data *V)

type Map

type Map[V any] struct {
	ElemParser  ElemParser[V]
	FieldParser FieldParser[V]

	TypeConverter func(reflect.Type) reflect.Type
	Aliases       []Alias
	// contains filtered or unexported fields
}

func New

func New[V any](p ElemParser[V], aliases ...Alias) *Map[V]

func NewDataParser

func NewDataParser[V any](p DataParser[V], aliases ...Alias) *Map[[]Data[V]]

func NewFieldParser

func NewFieldParser[V any](p FieldParser[V], aliases ...Alias) *Map[V]

func NewTagErrorParser

func NewTagErrorParser[V any](tag string, f func(string) (v V, err error), aliases ...Alias) *Map[[]Data[V]]

func NewTagOKParser

func NewTagOKParser[V any](tag string, f func(string) (v V, ok bool), aliases ...Alias) *Map[[]Data[V]]

func NewTagParser

func NewTagParser[V any](tag string, f func(string) V, aliases ...Alias) *Map[[]Data[V]]

func (*Map[V]) Clear

func (m *Map[V]) Clear()

func (*Map[V]) Get

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

func (*Map[V]) GetType

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

func (*Map[V]) Gets

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

func (*Map[V]) Init

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

func (*Map[V]) MustGetType

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

func (*Map[V]) Ptr

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

func (*Map[V]) SetConverter

func (m *Map[V]) SetConverter(f func(reflect.Type) reflect.Type) *Map[V]

type Value

type Value struct {
	reflect.Value
}

func ValueOf

func ValueOf(i any) Value

func (Value) Call

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

func (Value) CallAny

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

func (Value) CallAnySlice

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

func (Value) CallSlice

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