-
Notifications
You must be signed in to change notification settings - Fork 0
/
unmarshal.go
240 lines (220 loc) · 6.24 KB
/
unmarshal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package libxml
import (
"errors"
"fmt"
"reflect"
"strconv"
goxml "encoding/xml"
"github.com/jbowtie/gokogiri"
"github.com/jbowtie/gokogiri/xml"
)
// ErrInvalidStructPtr is returned when the v argument of Unmarshal is not a valid pointer to a struct
var ErrInvalidStructPtr = errors.New("Not a valid struct pointer")
var xmlNameType = reflect.TypeOf(goxml.Name{})
// Unmarshal parses the XML-encoded data and stores the result in the value pointed to by v, which must be an
// arbitrary struct, slice, or string. Well-formed data that does not fit into v is discarded.
//
// See https://golang.org/pkg/encoding/xml/#Unmarshal for more information.
func Unmarshal(data string, v interface{}) error {
// Attempt to resolve to a struct pointer
if v == nil {
return ErrInvalidStructPtr
}
if reflect.TypeOf(v).Kind() != reflect.Ptr {
return ErrInvalidStructPtr
}
s := reflect.ValueOf(v).Elem()
if s.NumField() == 0 {
return nil
}
// Parse XML document with libxml2 binding
doc, err := gokogiri.ParseXml([]byte(data))
if err != nil {
return err
}
defer doc.Free()
// Get document root and fill s with values from elements under root
err = fillStructFromNode(doc.Root(), s)
if err != nil {
return err
}
return nil
}
func setValFromString(t reflect.Type, s reflect.Value, str string) error {
switch t.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val, err := strconv.ParseInt(str, 0, 64)
if err != nil {
return err
}
if s.OverflowInt(val) {
return errors.New("Int value too big: " + str)
}
s.SetInt(val)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, err := strconv.ParseUint(str, 0, 64)
if err != nil {
return err
}
if s.OverflowUint(val) {
return errors.New("UInt value too big: " + str)
}
s.SetUint(val)
case reflect.Float32:
val, err := strconv.ParseFloat(str, 32)
if err != nil {
return err
}
s.SetFloat(val)
case reflect.Float64:
val, err := strconv.ParseFloat(str, 64)
if err != nil {
return err
}
s.SetFloat(val)
case reflect.String:
s.SetString(str)
case reflect.Bool:
val, err := strconv.ParseBool(str)
if err != nil {
return err
}
s.SetBool(val)
}
return fmt.Errorf("Type %s (%s) not implemented", t.String(), t.Kind().String())
}
func setStructFieldFromContent(t reflect.Type, s reflect.Value, n xml.Node) error {
switch t.Kind() {
case reflect.Ptr:
sType := t.Elem()
if s.IsNil() {
val := reflect.New(sType)
err := setStructFieldFromContent(sType, val.Elem(), n)
s.Set(val)
return err
}
return setStructFieldFromContent(sType, s.Elem(), n)
case reflect.Interface:
if s.IsNil() {
return nil
}
sType := s.Elem().Type()
val := reflect.New(sType)
err := setStructFieldFromContent(sType, val.Elem(), n)
s.Set(val)
return err
case reflect.Struct:
return fillStructFromNode(n, s)
}
return setValFromString(t, s, n.Content())
}
func setStructFieldFromAttr(t reflect.Type, s reflect.Value, n xml.Node, attrName string) error {
attr := n.Attribute(attrName)
if attr == nil {
return errors.New("No attribute defined")
}
switch t.Kind() {
case reflect.Ptr:
sType := t.Elem()
val := reflect.New(sType)
err := setStructFieldFromAttr(sType, val, n, attrName)
s.Set(val)
return err
}
return setValFromString(t, s, attr.Content())
}
func fillStructFromNode(n xml.Node, s reflect.Value) error {
t := s.Type()
// Loop over struct fields
for i := 0; i < s.NumField(); i++ {
tField := t.Field(i)
sField := s.Field(i)
if !sField.CanSet() && tField.Type.Kind() != reflect.Struct {
continue
}
// Set XMLName field
if tField.Name == "XMLName" && tField.Type == xmlNameType {
sField.Set(reflect.ValueOf(goxml.Name{
Space: n.Namespace(),
Local: n.Name(),
}))
}
// Get XML tag name and namespace from struct definition
tagNS := ""
tagName := ""
isCDATA := false
if xmlTag := parseXMLTag(tField.Tag); xmlTag != nil {
// Handle special struct tag flags
if xmlTag.flagInnerXML {
if tField.Type.Kind() == reflect.String {
sField.SetString(n.String())
} else if tField.Type.Kind() == reflect.Slice && tField.Type.Elem().Kind() == reflect.Uint8 {
sField.SetBytes([]byte(n.String()))
}
continue
} else if xmlTag.flagAttribute {
setStructFieldFromAttr(tField.Type, sField, n, xmlTag.tagName)
continue
} else if xmlTag.flagChardata {
if tField.Type.Kind() == reflect.String {
sField.SetString(n.Content())
} else if tField.Type.Kind() == reflect.Slice && tField.Type.Elem().Kind() == reflect.Uint8 {
sField.SetBytes([]byte(n.Content()))
}
continue
} else if xmlTag.flagCDATA {
isCDATA = true
}
tagName = xmlTag.tagName
tagNS = xmlTag.namespace
} else {
// Fall back to field name if tag name is empty
tagName = tField.Name
}
// Fall back to type name if tag name is empty
if tagName == "" {
if tField.Type.Kind() == reflect.Ptr {
tagName = sField.Elem().Type().String()
} else {
tagName = tField.Type.String()
}
}
// Strip namespace prefix from tag name
tagName = stripTagPrefix(tagName)
// Loop over child elements to find value(s) for current struct field
docf := xml.DocumentFragment{Node: n}
for _, c := range docf.Children() {
if isCDATA && c.NodeType() == xml.XML_CDATA_SECTION_NODE {
if tField.Type.Kind() == reflect.String {
sField.SetString(c.Content())
} else if tField.Type.Kind() == reflect.Slice && tField.Type.Elem().Kind() == reflect.Uint8 {
sField.SetBytes([]byte(c.Content()))
}
break
}
if c.Name() == tagName && (tagNS == "" || c.Namespace() == tagNS) {
if tField.Type.Kind() == reflect.Slice {
// Handle slice fields
elemType := tField.Type.Elem()
isPtr := false
if elemType.Kind() == reflect.Ptr {
isPtr = true
elemType = tField.Type.Elem().Elem()
}
elem := reflect.New(elemType)
if isPtr {
setStructFieldFromContent(elemType, elem.Elem(), c)
sField.Set(reflect.Append(sField, elem))
} else {
setStructFieldFromContent(elemType, elem.Elem(), c)
sField.Set(reflect.Append(sField, elem.Elem()))
}
} else {
// Handle normal fields
setStructFieldFromContent(tField.Type, sField, c)
}
}
}
}
return nil
}