TODO: render/ 相关模块
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package collect
|
||||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
@@ -215,23 +214,33 @@ func StructSliceJson2Maps(s interface{}) []map[string]interface{} {
|
||||
return maps
|
||||
}
|
||||
|
||||
// LoadJsonTag 根据loader的json tag 赋值 给拥有相同json tag
|
||||
func LoadJsonTag(v interface{}, loaded interface{}) {
|
||||
vtype := reflect.TypeOf(v)
|
||||
if vtype.Kind() != reflect.Ptr {
|
||||
panic("v must is a pointer")
|
||||
}
|
||||
vtype = vtype.Elem()
|
||||
|
||||
var vvalue reflect.Value
|
||||
if vtype.Kind() == reflect.Ptr {
|
||||
vtype = vtype.Elem()
|
||||
}
|
||||
vvalue := reflect.ValueOf(v)
|
||||
if vvalue.Kind() == reflect.Ptr {
|
||||
vvalue = vvalue.Elem()
|
||||
vvalue = reflect.ValueOf(v).Elem().Elem()
|
||||
} else {
|
||||
vvalue = reflect.ValueOf(v).Elem()
|
||||
}
|
||||
|
||||
ltype := reflect.TypeOf(loaded)
|
||||
if ltype.Kind() != reflect.Ptr {
|
||||
panic("loaded must is a pointer")
|
||||
}
|
||||
ltype = ltype.Elem()
|
||||
var lvalue reflect.Value
|
||||
if ltype.Kind() == reflect.Ptr {
|
||||
ltype = ltype.Elem()
|
||||
}
|
||||
lvalue := reflect.ValueOf(loaded)
|
||||
if lvalue.Kind() == reflect.Ptr {
|
||||
lvalue = lvalue.Elem()
|
||||
lvalue = reflect.ValueOf(loaded).Elem().Elem()
|
||||
} else {
|
||||
lvalue = reflect.ValueOf(loaded).Elem()
|
||||
}
|
||||
|
||||
for i := 0; i < vtype.NumField(); i++ {
|
||||
@@ -244,26 +253,25 @@ func LoadJsonTag(v interface{}, loaded interface{}) {
|
||||
if ltag, ok := lfield.Tag.Lookup("json"); ok && vtag == ltag {
|
||||
vv := vvalue.Field(i)
|
||||
lv := lvalue.Field(j)
|
||||
log.Println(vv.Kind(), vv.Type().Elem(), lv.Kind())
|
||||
|
||||
if vv.Kind() == reflect.Ptr {
|
||||
|
||||
if lv.Kind() == reflect.Ptr {
|
||||
vv.Set(lv)
|
||||
} else {
|
||||
vv = reflect.New(vv.Type().Elem())
|
||||
log.Println(vv.Type().Kind(), vv.Elem().Kind(), lv, reflect.Indirect(vv))
|
||||
reflect.Indirect(vv.Addr()).Set(lv.Addr())
|
||||
vv = reflect.New(vv.Type().Elem())
|
||||
|
||||
vv.Set(lv.Addr())
|
||||
x := reflect.New(vv.Type().Elem())
|
||||
x.Elem().Set(lv)
|
||||
vv.Set(x)
|
||||
}
|
||||
|
||||
vv.Set(lv)
|
||||
} else {
|
||||
|
||||
if lv.Kind() != reflect.Ptr {
|
||||
vv.Set(lv)
|
||||
} else {
|
||||
vv.Set(lv.Elem())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package collect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -40,3 +42,36 @@ func TestArrayColumnTag(t *testing.T) {
|
||||
log.Printf("%##v", a)
|
||||
log.Println(len(a))
|
||||
}
|
||||
|
||||
func TestCaseMain(t *testing.T) {
|
||||
|
||||
type MyStruct struct {
|
||||
SomeIntPtr *int
|
||||
SomeStringPtr *string
|
||||
}
|
||||
|
||||
var ms MyStruct
|
||||
|
||||
// Set int pointer
|
||||
{
|
||||
var i interface{} = 3 // of type int
|
||||
|
||||
f := reflect.ValueOf(&ms).Elem().FieldByName("SomeIntPtr")
|
||||
x := reflect.New(f.Type().Elem())
|
||||
x.Elem().Set(reflect.ValueOf(i))
|
||||
f.Set(x)
|
||||
}
|
||||
|
||||
// Set string pointer
|
||||
{
|
||||
var i interface{} = "hi" // of type string
|
||||
|
||||
f := reflect.ValueOf(&ms).Elem().Field(1)
|
||||
x := reflect.New(f.Type().Elem())
|
||||
x.Elem().Set(reflect.ValueOf(i))
|
||||
f.Set(x)
|
||||
}
|
||||
|
||||
fmt.Println("ms.SomeIntPtr", *ms.SomeIntPtr)
|
||||
fmt.Println("ms.SomeStringPtr", *ms.SomeStringPtr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user