service reflect modify

This commit is contained in:
geek 2017-06-07 20:14:28 +09:00
parent c8cfb12d17
commit 7330b79d73
2 changed files with 25 additions and 28 deletions

View File

@ -67,35 +67,34 @@ func TestServiceParam(t *testing.T) {
// Param *Member `json:"param"` // Param *Member `json:"param"`
//} //}
func TestComvertJson(t *testing.T) { func TestExecServices(t *testing.T) {
//res := `{"serviceName":"Member","methodName":"Regist","param":{"email":"geek@loafle.com","password":"qwer5795"}}` //res := `{"serviceName":"Member","methodName":"Regist","param":{"email":"geek@loafle.com","password":"qwer5795"}}`
res := `{"email":"geekloafle@loafle.com","pwSalt":"dssdft","company":"loafle"}`
m := make(map[string]string)
m["model"] = res
InitServices() InitServices()
meb, _ := g_services["Member"] meb, _ := g_services["Member"]
mem := meb.GetModel()
m := make(map[string]string)
m["email"] = "geek@loafle.com"
m["password"] = "qwer5795"
//ss := member.NewMemberService()
convertParam(mem, m)
methodName := reflect.ValueOf(meb).MethodByName("Regist") methodName := reflect.ValueOf(meb).MethodByName("Regist")
in := make([]reflect.Value, methodName.Type().NumIn()) params := make([]reflect.Value, methodName.Type().NumIn())
var NewObj reflect.Value
for i := 0; i < methodName.Type().NumIn(); i++{ for i := 0; i < methodName.Type().NumIn(); i++{
temp := methodName.Type().In(i) temp := methodName.Type().In(i)
in[i] = reflect.ValueOf(mem) switch temp.Kind() {
fmt.Println(temp) case reflect.Ptr:
NewObj = reflect.New(temp.Elem())
convertParam(NewObj.Interface(), m)
params[i] = NewObj
}
} }
va := methodName.Call(in)[0].String() result := methodName.Call(params)[0].String()
fmt.Println(va) fmt.Println(result)
} }
//func TestReflectType(t *testing.T) { //func TestReflectType(t *testing.T) {

View File

@ -7,7 +7,6 @@ import (
"github.com/golang/glog" "github.com/golang/glog"
"encoding/json" "encoding/json"
"reflect" "reflect"
"fmt"
"git.loafle.net/overflow/overflow_proxy_service/proxy/noauthagent" "git.loafle.net/overflow/overflow_proxy_service/proxy/noauthagent"
"git.loafle.net/overflow/overflow_proxy_service/proxy/target" "git.loafle.net/overflow/overflow_proxy_service/proxy/target"
//"git.loafle.net/overflow/overflow_proxy_service/proxy/agent" //"git.loafle.net/overflow/overflow_proxy_service/proxy/agent"
@ -15,11 +14,11 @@ import (
) )
var g_services map[string]Services var g_services map[string]interface{}
func AddServices(name string, s Services) { func AddServices(name string, s Services) {
if g_services == nil { if g_services == nil {
g_services = make(map[string]Services,0) g_services = make(map[string]interface{},0)
} }
g_services[name] = s g_services[name] = s
@ -30,7 +29,7 @@ type Services interface {
} }
func InitServices() { func InitServices() {
g_services = make(map[string]Services,0) g_services = make(map[string]interface{},0)
// proxy services save // proxy services save
//AddServices("Agent", agent.NewAgentService()) //AddServices("Agent", agent.NewAgentService())
@ -51,21 +50,20 @@ func (s *ServiceImpl) ExecServices(c context.Context, in *pb.ServiceInput) (*pb.
return nil, errors.New("Not Exist Service Name") return nil, errors.New("Not Exist Service Name")
} }
glog.Infoln(serviceName.GetModel())
model := serviceName.GetModel()
convertParam(model, in.Param)
pbs := &pb.ServiceOutput{} pbs := &pb.ServiceOutput{}
methodName := reflect.ValueOf(serviceName).MethodByName(in.MethodName) methodName := reflect.ValueOf(serviceName).MethodByName(in.MethodName)
params := make([]reflect.Value, methodName.Type().NumIn()) params := make([]reflect.Value, methodName.Type().NumIn())
var NewObj reflect.Value
for i := 0; i < methodName.Type().NumIn(); i++{ for i := 0; i < methodName.Type().NumIn(); i++{
temp := methodName.Type().In(i) temp := methodName.Type().In(i)
params[i] = reflect.ValueOf(model) switch temp.Kind() {
fmt.Println(temp) case reflect.Ptr:
NewObj = reflect.New(temp.Elem())
convertParam(NewObj, in.Param)
params[i] = NewObj
}
} }
// Todo Call Service Method // Todo Call Service Method