no message

master
DESKTOP-4RNDQIC\29019 2021-02-07 00:48:29 +08:00
parent 62fb8c540c
commit 3a49c9f4c9
2 changed files with 61 additions and 0 deletions

32
test/xml_test.go Normal file
View File

@ -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())
// }
}

View File

@ -131,6 +131,35 @@ func SameKind(typ1 reflect.Kind,typ2 reflect.Kind) bool{
return false 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{}){ func UnmarshalJson(value interface{}, maps map[string]interface{}){
valueof := reflect.ValueOf(value) valueof := reflect.ValueOf(value)
if !valueof.Elem().CanAddr(){ if !valueof.Elem().CanAddr(){