target service test
This commit is contained in:
snoop 2017-06-05 19:59:10 +09:00
parent 5e898d1485
commit 3b95f90a1d
2 changed files with 128 additions and 3 deletions

View File

@ -16,7 +16,7 @@ type Target struct {
Kinds string `json:"kinds,omitempty"`
Version string `json:"version,omitempty"`
CreateDate timestamp.Timestamp `json:"createDate,omitempty"`
Member *member.MemberService `json:"member,omitempty"`
Member *member.Member `json:"member,omitempty"`
}
type TargetService struct {
@ -27,6 +27,10 @@ func NewTargetService() *TargetService {
return &TargetService{}
}
func (t *TargetService)GetModel() interface{} {
return &Target{}
}
func (t *TargetService)List(tm *Target) string {
bytes, err := json.Marshal(tm)

View File

@ -6,6 +6,8 @@ import (
"git.loafle.net/overflow/overflow_proxy_service/proxy/member"
"reflect"
"fmt"
)
func TestCreateTarget(t *testing.T) {
@ -18,7 +20,7 @@ func TestCreateTarget(t *testing.T) {
Kinds:"PostgreSQL",
Version:"9.5.0",
VendorName:"PostgreSQL 9.5.0",
Member:&member.MemberService{Id:"1"},
Member:&member.Member{Id:"1"},
}
ts := NewTargetService()
@ -32,7 +34,7 @@ func TestCreateTarget(t *testing.T) {
func TestFindAll(t *testing.T) {
tt := Target{
Member:&member.MemberService{Id:"1"},
Member:&member.Member{Id:"1"},
}
ts := NewTargetService()
@ -41,3 +43,122 @@ func TestFindAll(t *testing.T) {
str := ts.List(&tt)
t.Log(str)
}
type MyInt int
func TestRefloect(t *testing.T) {
f := &Foo{
FirstName: "Drew",
LastName: "Olson",
Age: 30,
}
f.reflect()
}
type Foo struct {
FirstName string `tag_name:"tag 1"`
LastName string `tag_name:"tag 2"`
Age int `tag_name:"tag 3"`
}
func (f *Foo) reflect() {
val := reflect.ValueOf(f).Elem()
for i := 0; i < val.NumField(); i++ {
valueField := val.Field(i)
typeField := val.Type().Field(i)
tag := typeField.Tag
fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n", typeField.Name, valueField.Interface(), tag.Get("tag_name"))
}
for j := 0; j < val.NumMethod(); j++ {
v := val.Method(j)
method := val.Type().Method(j)
fmt.Println(v)
fmt.Println(method)
}
}
type YourT1 struct {}
func (y YourT1) MethodBar() {
//do something
}
type YourT2 struct {}
func (y YourT2) MethodFoo(i int, oo string) {
//do something
}
func Invoke(any interface{}, name string, args... interface{}) {
inputs := make([]reflect.Value, len(args))
for i, _ := range args {
inputs[i] = reflect.ValueOf(args[i])
}
reflect.ValueOf(any).MethodByName(name).Call(inputs)
}
func TestRelfect02(t *testing.T) {
Invoke(YourT2{}, "MethodFoo", 10, "abc")
Invoke(YourT1{}, "MethodBar")
}
type Aaa struct {
a string
}
type Bbb struct {
b int
}
type Handler struct{}
func (h Handler) GET(a Aaa, b Bbb, ptr *Aaa) string {
return "OK" + a.a + " ptr:" + ptr.a
}
func TestReflect03(t *testing.T) {
handler := new(Handler)
objects := make(map[reflect.Type]interface{})
objects[reflect.TypeOf(Aaa{})] = Aaa{"jkljkL"}
objects[reflect.TypeOf(new(Aaa))] = &Aaa{"pointer!"}
objects[reflect.TypeOf(Bbb{})] = Bbb{}
//in := make([]reflect.Value, 0)
method := reflect.ValueOf(handler).MethodByName("GET")
fmt.Println(method)
in := make([]reflect.Value, method.Type().NumIn())
fmt.Println("method type num in:", method.Type().NumIn())
for i := 0; i < method.Type().NumIn(); i++ {
t := method.Type().In(i)
if i == 0 {
intPtr := reflect.New(t)
a := intPtr.Elem().Interface().(Aaa)
a.a = "aaaddsasdf"
fmt.Println(a)
}
fmt.Println(t)
object := objects[t]
fmt.Println(i, "->", object)
in[i] = reflect.ValueOf(object)
}
fmt.Println("method type num out:", method.Type().NumOut())
response := method.Call(in)
fmt.Println(response)
}