From 3a49c9f4c9133553baaa63153db684b76a0ff689 Mon Sep 17 00:00:00 2001 From: "DESKTOP-4RNDQIC\\29019" <290198252@qq.com> Date: Sun, 7 Feb 2021 00:48:29 +0800 Subject: [PATCH] no message --- test/xml_test.go | 32 ++++++++++++++++++++++++++++++++ utils/reflect.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/xml_test.go 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(){