Skip to content

Commit

Permalink
feat: 新增选项式参数
Browse files Browse the repository at this point in the history
  • Loading branch information
Drelf2018 committed Nov 3, 2023
1 parent d954130 commit cca5cac
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
64 changes: 54 additions & 10 deletions Reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Fields(typ reflect.Type) Chan.Chan[reflect.StructField] {

type Reflect[V any] struct {
types map[uintptr][]V
Alias func(elem reflect.Type) []uintptr
Parse func(self *Reflect[V], field reflect.StructField, elem reflect.Type) V
}

Expand Down Expand Up @@ -86,12 +87,21 @@ func (r *Reflect[V]) GetType(elem reflect.Type, v *[]V) bool {
return true
}
r.types[ue] = make([]V, 0)
values := make([]V, 0, elem.NumField())
for field := range Fields(elem) {
*v = append(*v, r.Parse(r, field, elem))
values = append(values, r.Parse(r, field, elem))
}
r.types[ue] = values
for _, u := range r.Alias(elem) {
r.types[u] = values
}
return r.Ptr(ue, v)
}

func (r *Reflect[V]) Init(in ...any) {
for _, i := range in {
r.GetType(reflect.TypeOf(i), nil)
}
r.types[ue] = *v
r.types[Addr(elem)] = *v
return true
}

func (r *Reflect[V]) Get(in any) (v []V) {
Expand All @@ -101,17 +111,51 @@ func (r *Reflect[V]) Get(in any) (v []V) {
panic(ErrValue)
}

func New[V any](parse func(self *Reflect[V], field reflect.StructField, elem reflect.Type) V) *Reflect[V] {
return &Reflect[V]{
func Pointer(elem reflect.Type) []uintptr {
return []uintptr{
Addr(elem),
}
}

func Slice(elem reflect.Type) []uintptr {
return []uintptr{
Addr(elem),
Type(reflect.SliceOf(elem)),
Addr(reflect.SliceOf(elem)),
}
}

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

func WithAlias[V any](f func(elem reflect.Type) []uintptr) Option[V] {
return func(r *Reflect[V]) {
r.Alias = f
}
}

func WithSlice[V any](in ...any) Option[V] {
return func(r *Reflect[V]) {
r.Alias = Slice
r.Init(in...)
}
}

func New[V any](parse func(self *Reflect[V], field reflect.StructField, elem reflect.Type) V, options ...Option[V]) (r *Reflect[V]) {
r = &Reflect[V]{
types: make(map[uintptr][]V),
Alias: Pointer,
Parse: parse,
}
for _, op := range options {
op(r)
}
return
}

func NewTag(tag string) *Reflect[string] {
func NewTag(tag string, options ...Option[string]) *Reflect[string] {
return New(func(self *Reflect[string], field reflect.StructField, elem reflect.Type) string {
return field.Tag.Get(tag)
})
}, options...)
}

type Tag struct {
Expand Down Expand Up @@ -141,10 +185,10 @@ func (t Tags) String() string {
return buf.String()
}

func NewTagStruct(tag string) *Reflect[Tag] {
func NewTagStruct(tag string, options ...Option[Tag]) *Reflect[Tag] {
return New(func(self *Reflect[Tag], field reflect.StructField, elem reflect.Type) (t Tag) {
t.Tag = field.Tag.Get(tag)
self.GetType(field.Type, &t.Fields)
return
})
}, options...)
}
4 changes: 2 additions & 2 deletions Reflect/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Struct1 struct {
}

func TestTag(t *testing.T) {
tag := Reflect.NewTagStruct("ref")
v := tag.Get(&Struct1{})
tag := Reflect.NewTagStruct("ref", Reflect.WithSlice[Reflect.Tag](Struct1{}))
v := tag.Get(&[]Struct1{})
for idx, val := range v {
fmt.Printf("#%d: %v\n", idx, val)
}
Expand Down

0 comments on commit cca5cac

Please sign in to comment.