Skip to content

Commit

Permalink
feat: Value
Browse files Browse the repository at this point in the history
  • Loading branch information
Drelf2018 committed Dec 16, 2023
1 parent c17932f commit b0a6a8c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Reflect/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"reflect"
"runtime"
"unsafe"

"github.com/Drelf2018/TypeGo/Chan"
Expand Down Expand Up @@ -64,7 +65,8 @@ func FieldOf(elem reflect.Type) []reflect.StructField {
elem = elem.Elem()
}
if elem.Kind() != reflect.Struct {
panic(ErrValue{elem})
// panic(ErrValue{elem})
return nil
}
l := elem.NumField()
r := make([]reflect.StructField, 0, l)
Expand All @@ -76,7 +78,8 @@ func FieldOf(elem reflect.Type) []reflect.StructField {

func MethodOf(value reflect.Value) map[reflect.Method]reflect.Value {
if value.Kind() != reflect.Struct && (value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Struct) {
panic(ErrValue{value})
// panic(ErrValue{value})
return nil
}
elem := value.Type()
r := make(map[reflect.Method]reflect.Value)
Expand All @@ -88,7 +91,8 @@ func MethodOf(value reflect.Value) map[reflect.Method]reflect.Value {

func MethodFuncOf(value reflect.Value) map[string]any {
if value.Kind() != reflect.Struct && (value.Kind() != reflect.Ptr || value.Elem().Kind() != reflect.Struct) {
panic(ErrValue{value})
// panic(ErrValue{value})
return nil
}
elem := value.Type()
r := make(map[string]any)
Expand All @@ -98,6 +102,12 @@ func MethodFuncOf(value reflect.Value) map[string]any {
return r
}

func IsEmbeddedMethod(v reflect.Method) bool {
f := runtime.FuncForPC(v.Func.Pointer())
file, _ := f.FileLine(f.Entry())
return file == "<autogenerated>"
}

type Reflect[V any] struct {
types map[uintptr][]V
Alias func(elem reflect.Type) []uintptr
Expand Down
31 changes: 31 additions & 0 deletions Reflect/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Reflect

import "reflect"

type Value struct {
reflect.Value
}

func (v Value) Call(in ...reflect.Value) []reflect.Value {
return v.Value.Call(in)
}

func (v Value) CallSlice(in ...reflect.Value) []reflect.Value {
return v.Value.CallSlice(in)
}

func (v Value) CallAny(in ...any) []reflect.Value {
return v.Value.Call(ValuesOf(in...))
}

func (v Value) CallAnySlice(in ...any) []reflect.Value {
return v.Value.CallSlice(ValuesOf(in...))
}

func ValuesOf(in ...any) []reflect.Value {
val := make([]reflect.Value, 0, len(in))
for _, i := range in {
val = append(val, reflect.ValueOf(i))
}
return val
}

0 comments on commit b0a6a8c

Please sign in to comment.