Skip to content

Commit

Permalink
Merge pull request #68 from yoyofx/dev
Browse files Browse the repository at this point in the history
改进action调用方式和性能,集成reflectx并重构
  • Loading branch information
yoyofx authored Jul 31, 2020
2 parents 085ecd1 + 75603d3 commit 16dd0cf
Show file tree
Hide file tree
Showing 17 changed files with 174 additions and 553 deletions.
4 changes: 2 additions & 2 deletions DependencyInjection/ServiceCollection.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package DependencyInjection

import (
"github.com/yoyofx/yoyogo/Utils/Reflect"
"github.com/yoyofxteam/reflectx"
"strings"
)

Expand All @@ -18,7 +18,7 @@ func NewServiceCollection() *ServiceCollection {
//Scoped
//Transient
func (sc *ServiceCollection) AddServiceDescriptor(sd *ServiceDescriptor) {
typeName, _ := Reflect.GetCtorFuncOutTypeName(sd.Provider)
typeName, _ := reflectx.GetCtorFuncOutTypeName(sd.Provider)
typeName = strings.ToLower(typeName)
index := len(sc.serviceDescriptors)
defIndex, exist := sc.serviceDescriptorMaps[typeName]
Expand Down
186 changes: 137 additions & 49 deletions Test/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,101 @@ import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/yoyofx/yoyogo/Examples/SimpleWeb/contollers"
"github.com/yoyofx/yoyogo/Utils"
"github.com/yoyofx/yoyogo/Utils/Reflect"
"github.com/yoyofx/yoyogo/Examples/SimpleWeb/models"
"github.com/yoyofx/yoyogo/WebFramework/Context"
"github.com/yoyofx/yoyogo/WebFramework/Mvc"
"github.com/yoyofxteam/reflectx"
"reflect"
"strings"
"testing"
)

//
//import (
// "fmt"
// "github.com/stretchr/testify/assert"
// "github.com/yoyofx/yoyogo/Examples/SimpleWeb/contollers"
// "github.com/yoyofx/yoyogo/Utils"
// "github.com/yoyofx/yoyogo/Utils/Reflect"
// "github.com/yoyofx/yoyogo/WebFramework/Context"
// "github.com/yoyofx/yoyogo/WebFramework/Mvc"
// "github.com/yoyofxteam/reflectx"
// "testing"
//)
//
//type Person struct {
// Name string
// Student *Student
//}
//
//type Student struct {
// Name string `json:"name"`
// Age int `json:"age"`
// Grade int `json:"grade"`
//}
//
//func (typeInfo Student) Hello() string {
// return "hello"
//}
//
//func (typeInfo Student) Say(hi string) string {
// return "Hello " + hi
//}
//
//func Test_MethodCallerCall2(t *testing.T) {
// utype := &UserInfo{}
//
// methodInfo, _ := reflectx.GetObjectMethodInfoByName(utype, "Hello")
// results := methodInfo.Invoke(&Context.HttpContext{}, "hello world!")
//
// fmt.Println()
// fmt.Printf("Result: %s", results)
// fmt.Println()
//
// assert.Equal(t, results[0].(string), "hello world!")
//}
//
//func Test_RecCreateStruct(t *testing.T) {
// //yourtype := reflect.TypeOf(Mvc.RequestBody{})
// //dd := Reflect.CreateInstance(yourtype)
// //_ = dd
// typeInfo, _ := Reflect.GetTypeInfo(Mvc.RequestBody{})
// ins := typeInfo.CreateInstance()
// assert.Equal(t, ins != nil, true)
//}
//
//func Test_GetCtorFuncTypeName(t *testing.T) {
// ctorFunc := contollers.NewUserController
// name, _ := Reflect.GetCtorFuncOutTypeName(ctorFunc)
// name = Utils.LowercaseFirst(name)
// assert.Equal(t, name, "userController")
//}
//
//func Test_ReflectStructFields(t *testing.T) {
// student := &Student{
// Name: "json",
// Age: 18,
// Grade: 9,
// }
// p := Person{
// Name: "Json",
// Student: student,
// }
//
// ptype, _ := Reflect.GetTypeInfo(p)
// pf1 := ptype.GetFieldByName("Name")
// assert.Equal(t, pf1.GetValue(), "Json")
// pf2 := ptype.GetFieldByName("Student")
// assert.Equal(t, pf2.GetValue(), student)
// typeInfo, _ := pf2.AsTypeInfo()
//
// typeInfo.GetFieldByName("Grade").SetValue(11)
// assert.Equal(t, student.Grade, 11)
// assert.Equal(t, typeInfo.HasMethods(), true)
// sayRet := typeInfo.GetMethodByName("Say").Invoke("World!")[0].(string)
// assert.Equal(t, sayRet, "Hello World!")
//
//}

type Person struct {
Name string
Student *Student
Expand All @@ -23,64 +111,64 @@ type Student struct {
}

func (typeInfo Student) Hello() string {
fmt.Println("hello ")
return "hello"
}

func (typeInfo Student) Say(hi string) string {
fmt.Println("Hello " + hi)
return "Hello " + hi
}

func Test_MethodCallerCall2(t *testing.T) {
utype := &UserInfo{}

methodInfo, _ := Reflect.GetObjectMethodInfoByName(utype, "Hello")
results := methodInfo.Invoke(&Context.HttpContext{}, "hello world!")

fmt.Println()
fmt.Printf("Result: %s", results)
fmt.Println()

assert.Equal(t, results[0].(string), "hello world!")
}
func Test_GetStructMethodList(t *testing.T) {
userInfo := &UserInfo{}
userMethodList := reflectx.GetObjectMethodInfoList(userInfo)
assert.Equal(t, len(userMethodList), 2)
assert.Equal(t, getMehtodInfoByName(userMethodList, "Hello").Invoke(userInfo, &Context.HttpContext{}, "UserInfo Func Call:Hello,")[0],
"UserInfo Func Call:Hello,")
//---------------------------------------------------------------------------------------------
student := Student{}
studentMethodList := reflectx.GetObjectMethodInfoList(student)
assert.Equal(t, len(studentMethodList), 2)
assert.Equal(t, studentMethodList[0].Name, "Hello")
assert.Equal(t, studentMethodList[1].Name, "Say")

func Test_RecCreateStruct(t *testing.T) {
//yourtype := reflect.TypeOf(Mvc.RequestBody{})
//dd := Reflect.CreateInstance(yourtype)
//_ = dd
typeInfo, _ := Reflect.GetTypeInfo(Mvc.RequestBody{})
ins := typeInfo.CreateInstance()
assert.Equal(t, ins != nil, true)
assert.Equal(t, getMehtodInfoByName(studentMethodList, "Hello").Invoke(student)[0], "hello")
assert.Equal(t, getMehtodInfoByName(studentMethodList, "Say").Invoke(student, "Say: Student")[0], "Hello Say: Student")
}

func Test_GetCtorFuncTypeName(t *testing.T) {
ctorFunc := contollers.NewUserController
name, _ := Reflect.GetCtorFuncOutTypeName(ctorFunc)
name = Utils.LowercaseFirst(name)
assert.Equal(t, name, "userController")
}

func Test_ReflectStructFields(t *testing.T) {
student := &Student{
Name: "json",
Age: 18,
Grade: 9,
}
p := Person{
Name: "Json",
Student: student,
func getMehtodInfoByName(infos []reflectx.MethodInfo, name string) reflectx.MethodInfo {
for _, m := range infos {
if m.Name == name {
return m
}
}
return infos[0]
}

ptype, _ := Reflect.GetTypeInfo(p)
pf1 := ptype.GetFieldByName("Name")
assert.Equal(t, pf1.GetValue(), "Json")
pf2 := ptype.GetFieldByName("Student")
assert.Equal(t, pf2.GetValue(), student)
typeInfo, _ := pf2.AsTypeInfo()
func Test_UserController(t *testing.T) {
controllerCtor := contollers.NewUserController
controllerName, controllerType := reflectx.GetCtorFuncOutTypeName(controllerCtor)
controllerName = strings.ToLower(controllerName)
// Create Controller and Action descriptors

typeInfo.GetFieldByName("Grade").SetValue(11)
assert.Equal(t, student.Grade, 11)
assert.Equal(t, typeInfo.HasMethods(), true)
sayRet := typeInfo.GetMethodByName("Say").Invoke("World!")[0].(string)
assert.Equal(t, sayRet, "Hello World!")
instance := reflect.New(controllerType).Interface()
actionList := reflectx.GetObjectMethodInfoList(instance)
mi := getMehtodInfoByName(actionList, "GetUserName")
_ = mi.Parameters[0].ParameterType.Elem().Name()
rets := mi.Invoke(instance, &Context.HttpContext{}, &contollers.RegisterRequest{
UserName: "he",
Password: "123",
})
assert.Equal(t, len(rets), 1)

instance1 := contollers.NewUserController(models.NewUserAction())
_ = instance1
//actionList = reflectx.GetObjectMethodInfoList(instance)
//_ = actionList
//mi = getMehtodInfoByName(actionList,"GetUserName")
//mi.Invoke(instance,&Context.HttpContext{}, &contollers.RegisterRequest{
// UserName: "he",
// Password: "123",
//})
}
14 changes: 0 additions & 14 deletions Test/structtag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package Test

import (
"fmt"
"github.com/magiconair/properties/assert"
"github.com/yoyofx/yoyogo/Utils/Reflect"
"github.com/yoyofx/yoyogo/WebFramework/Context"
"reflect"
"testing"
Expand Down Expand Up @@ -55,18 +53,6 @@ func reflectCall(ctype interface{}, funcName string, params ...interface{}) inte
return nil
}

func Test_MethodCallerCall(t *testing.T) {
utype := &UserInfo{}
method := Reflect.NewMethodCaller(utype, "Hello")
results := method.Invoke(&Context.HttpContext{}, "hello world!")

fmt.Println()
fmt.Printf("Result: %s", results)
fmt.Println()

assert.Equal(t, results[0].(string), "hello world!")
}

func Test_StructGetFieldTag(t *testing.T) {
user := &UserInfo{"John Doe The Fourth", 20}

Expand Down
35 changes: 0 additions & 35 deletions Utils/Reflect/FieldInfo.go

This file was deleted.

62 changes: 0 additions & 62 deletions Utils/Reflect/MethodCaller.go

This file was deleted.

Loading

0 comments on commit 16dd0cf

Please sign in to comment.