diff --git a/test/xml_test.go b/test/xml_test.go new file mode 100644 index 0000000..e64a66e --- /dev/null +++ b/test/xml_test.go @@ -0,0 +1,32 @@ +package test + +import ( + "testing" +) + +type Sub struct { + A string `xml:"a"` +} +type Test struct { + A int32 `json:"a" xml:"a"` + B bool `json:"b" xml:"b"` + C string `json:"c" xml:"c"` + D Sub `xml:"sub"` +} + +func TestXml(t *testing.T) { + // x := map[string]interface{}{} + // x["map"] = 1 + // x["ss"] = 2 + // test := Test{A: 1, B: false, C: "hell", D: Sub{A: "aa"}} + // bytes, e := xml.Marshal(&test) + // if nil != e { + // t.Error(e.Error()) + + // } + // bytes, e = xml.Marshal(&x) + // if nil != e { + // t.Error(e.Error()) + + // } +} diff --git a/utils/reflect.go b/utils/reflect.go index 46d17ea..847e884 100644 --- a/utils/reflect.go +++ b/utils/reflect.go @@ -131,6 +131,35 @@ func SameKind(typ1 reflect.Kind,typ2 reflect.Kind) bool{ return false } +func UnmarshalWithTag(value interface{}, maps map[string]interface{},tag string){ + if "" == tag{ + return + } + valueof := reflect.ValueOf(value) + if !valueof.Elem().CanAddr(){ + log.Print("should be addr") + return + } + remap := ReflectTagMap(valueof.Elem().Type()) + _,ok := remap[tag] + if !ok{ + return + } + for k,v := range(maps){ + log.Print(k) + if filedName,ok := remap["json"][k];ok{ + log.Print(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind().String()) + if SameKind(valueof.Elem().FieldByName(filedName).Kind(),reflect.TypeOf(v).Kind()){ + if(valueof.Elem().FieldByName(filedName).CanSet()){ + valueof.Elem().FieldByName(filedName).Set(reflect.ValueOf(v).Convert(valueof.Elem().FieldByName(filedName).Type())) + } + } + } + } +} + + + func UnmarshalJson(value interface{}, maps map[string]interface{}){ valueof := reflect.ValueOf(value) if !valueof.Elem().CanAddr(){