github.com/songzhibin97/gkit@v1.2.13/coding/xml/xml.go (about) 1 package xml 2 3 import ( 4 "encoding/xml" 5 "reflect" 6 7 "github.com/songzhibin97/gkit/coding" 8 ) 9 10 const Name = "xml" 11 12 func init() { 13 _ = coding.RegisterCode(code{}) 14 } 15 16 type code struct{} 17 18 func (c code) Marshal(v interface{}) ([]byte, error) { 19 return xml.Marshal(v) 20 } 21 22 func (c code) Unmarshal(data []byte, v interface{}) error { 23 rv := reflect.ValueOf(v) 24 for rv.Kind() == reflect.Ptr { 25 if rv.IsNil() { 26 rv.Set(reflect.New(rv.Type().Elem())) 27 } 28 rv = rv.Elem() 29 } 30 return xml.Unmarshal(data, v) 31 } 32 33 func (c code) Name() string { 34 return Name 35 }