read.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xml
  5. import (
  6. "bytes"
  7. "encoding"
  8. "errors"
  9. "fmt"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. )
  14. // BUG(rsc): Mapping between XML elements and data structures is inherently flawed:
  15. // an XML element is an order-dependent collection of anonymous
  16. // values, while a data structure is an order-independent collection
  17. // of named values.
  18. // See package json for a textual representation more suitable
  19. // to data structures.
  20. // Unmarshal parses the XML-encoded data and stores the result in
  21. // the value pointed to by v, which must be an arbitrary struct,
  22. // slice, or string. Well-formed data that does not fit into v is
  23. // discarded.
  24. //
  25. // Because Unmarshal uses the reflect package, it can only assign
  26. // to exported (upper case) fields. Unmarshal uses a case-sensitive
  27. // comparison to match XML element names to tag values and struct
  28. // field names.
  29. //
  30. // Unmarshal maps an XML element to a struct using the following rules.
  31. // In the rules, the tag of a field refers to the value associated with the
  32. // key 'xml' in the struct field's tag (see the example above).
  33. //
  34. // * If the struct has a field of type []byte or string with tag
  35. // ",innerxml", Unmarshal accumulates the raw XML nested inside the
  36. // element in that field. The rest of the rules still apply.
  37. //
  38. // * If the struct has a field named XMLName of type Name,
  39. // Unmarshal records the element name in that field.
  40. //
  41. // * If the XMLName field has an associated tag of the form
  42. // "name" or "namespace-URL name", the XML element must have
  43. // the given name (and, optionally, name space) or else Unmarshal
  44. // returns an error.
  45. //
  46. // * If the XML element has an attribute whose name matches a
  47. // struct field name with an associated tag containing ",attr" or
  48. // the explicit name in a struct field tag of the form "name,attr",
  49. // Unmarshal records the attribute value in that field.
  50. //
  51. // * If the XML element has an attribute not handled by the previous
  52. // rule and the struct has a field with an associated tag containing
  53. // ",any,attr", Unmarshal records the attribute value in the first
  54. // such field.
  55. //
  56. // * If the XML element contains character data, that data is
  57. // accumulated in the first struct field that has tag ",chardata".
  58. // The struct field may have type []byte or string.
  59. // If there is no such field, the character data is discarded.
  60. //
  61. // * If the XML element contains comments, they are accumulated in
  62. // the first struct field that has tag ",comment". The struct
  63. // field may have type []byte or string. If there is no such
  64. // field, the comments are discarded.
  65. //
  66. // * If the XML element contains a sub-element whose name matches
  67. // the prefix of a tag formatted as "a" or "a>b>c", unmarshal
  68. // will descend into the XML structure looking for elements with the
  69. // given names, and will map the innermost elements to that struct
  70. // field. A tag starting with ">" is equivalent to one starting
  71. // with the field name followed by ">".
  72. //
  73. // * If the XML element contains a sub-element whose name matches
  74. // a struct field's XMLName tag and the struct field has no
  75. // explicit name tag as per the previous rule, unmarshal maps
  76. // the sub-element to that struct field.
  77. //
  78. // * If the XML element contains a sub-element whose name matches a
  79. // field without any mode flags (",attr", ",chardata", etc), Unmarshal
  80. // maps the sub-element to that struct field.
  81. //
  82. // * If the XML element contains a sub-element that hasn't matched any
  83. // of the above rules and the struct has a field with tag ",any",
  84. // unmarshal maps the sub-element to that struct field.
  85. //
  86. // * An anonymous struct field is handled as if the fields of its
  87. // value were part of the outer struct.
  88. //
  89. // * A struct field with tag "-" is never unmarshaled into.
  90. //
  91. // If Unmarshal encounters a field type that implements the Unmarshaler
  92. // interface, Unmarshal calls its UnmarshalXML method to produce the value from
  93. // the XML element. Otherwise, if the value implements
  94. // encoding.TextUnmarshaler, Unmarshal calls that value's UnmarshalText method.
  95. //
  96. // Unmarshal maps an XML element to a string or []byte by saving the
  97. // concatenation of that element's character data in the string or
  98. // []byte. The saved []byte is never nil.
  99. //
  100. // Unmarshal maps an attribute value to a string or []byte by saving
  101. // the value in the string or slice.
  102. //
  103. // Unmarshal maps an attribute value to an Attr by saving the attribute,
  104. // including its name, in the Attr.
  105. //
  106. // Unmarshal maps an XML element or attribute value to a slice by
  107. // extending the length of the slice and mapping the element or attribute
  108. // to the newly created value.
  109. //
  110. // Unmarshal maps an XML element or attribute value to a bool by
  111. // setting it to the boolean value represented by the string. Whitespace
  112. // is trimmed and ignored.
  113. //
  114. // Unmarshal maps an XML element or attribute value to an integer or
  115. // floating-point field by setting the field to the result of
  116. // interpreting the string value in decimal. There is no check for
  117. // overflow. Whitespace is trimmed and ignored.
  118. //
  119. // Unmarshal maps an XML element to a Name by recording the element
  120. // name.
  121. //
  122. // Unmarshal maps an XML element to a pointer by setting the pointer
  123. // to a freshly allocated value and then mapping the element to that value.
  124. //
  125. // A missing element or empty attribute value will be unmarshaled as a zero value.
  126. // If the field is a slice, a zero value will be appended to the field. Otherwise, the
  127. // field will be set to its zero value.
  128. func Unmarshal(data []byte, v interface{}) error {
  129. return NewDecoder(bytes.NewReader(data)).Decode(v)
  130. }
  131. // Decode works like Unmarshal, except it reads the decoder
  132. // stream to find the start element.
  133. func (d *Decoder) Decode(v interface{}) error {
  134. return d.DecodeElement(v, nil)
  135. }
  136. // DecodeElement works like Unmarshal except that it takes
  137. // a pointer to the start XML element to decode into v.
  138. // It is useful when a client reads some raw XML tokens itself
  139. // but also wants to defer to Unmarshal for some elements.
  140. func (d *Decoder) DecodeElement(v interface{}, start *StartElement) error {
  141. val := reflect.ValueOf(v)
  142. if val.Kind() != reflect.Ptr {
  143. return errors.New("non-pointer passed to Unmarshal")
  144. }
  145. return d.unmarshal(val.Elem(), start)
  146. }
  147. // An UnmarshalError represents an error in the unmarshaling process.
  148. type UnmarshalError string
  149. func (e UnmarshalError) Error() string { return string(e) }
  150. // Unmarshaler is the interface implemented by objects that can unmarshal
  151. // an XML element description of themselves.
  152. //
  153. // UnmarshalXML decodes a single XML element
  154. // beginning with the given start element.
  155. // If it returns an error, the outer call to Unmarshal stops and
  156. // returns that error.
  157. // UnmarshalXML must consume exactly one XML element.
  158. // One common implementation strategy is to unmarshal into
  159. // a separate value with a layout matching the expected XML
  160. // using d.DecodeElement, and then to copy the data from
  161. // that value into the receiver.
  162. // Another common strategy is to use d.Token to process the
  163. // XML object one token at a time.
  164. // UnmarshalXML may not use d.RawToken.
  165. type Unmarshaler interface {
  166. UnmarshalXML(d *Decoder, start StartElement) error
  167. }
  168. // UnmarshalerAttr is the interface implemented by objects that can unmarshal
  169. // an XML attribute description of themselves.
  170. //
  171. // UnmarshalXMLAttr decodes a single XML attribute.
  172. // If it returns an error, the outer call to Unmarshal stops and
  173. // returns that error.
  174. // UnmarshalXMLAttr is used only for struct fields with the
  175. // "attr" option in the field tag.
  176. type UnmarshalerAttr interface {
  177. UnmarshalXMLAttr(attr Attr) error
  178. }
  179. // receiverType returns the receiver type to use in an expression like "%s.MethodName".
  180. func receiverType(val interface{}) string {
  181. t := reflect.TypeOf(val)
  182. if t.Name() != "" {
  183. return t.String()
  184. }
  185. return "(" + t.String() + ")"
  186. }
  187. // unmarshalInterface unmarshals a single XML element into val.
  188. // start is the opening tag of the element.
  189. func (d *Decoder) unmarshalInterface(val Unmarshaler, start *StartElement) error {
  190. // Record that decoder must stop at end tag corresponding to start.
  191. d.pushEOF()
  192. d.unmarshalDepth++
  193. err := val.UnmarshalXML(d, *start)
  194. d.unmarshalDepth--
  195. if err != nil {
  196. d.popEOF()
  197. return err
  198. }
  199. if !d.popEOF() {
  200. return fmt.Errorf("xml: %s.UnmarshalXML did not consume entire <%s> element", receiverType(val), start.Name.Local)
  201. }
  202. return nil
  203. }
  204. // unmarshalTextInterface unmarshals a single XML element into val.
  205. // The chardata contained in the element (but not its children)
  206. // is passed to the text unmarshaler.
  207. func (d *Decoder) unmarshalTextInterface(val encoding.TextUnmarshaler) error {
  208. var buf []byte
  209. depth := 1
  210. for depth > 0 {
  211. t, err := d.Token()
  212. if err != nil {
  213. return err
  214. }
  215. switch t := t.(type) {
  216. case CharData:
  217. if depth == 1 {
  218. buf = append(buf, t...)
  219. }
  220. case StartElement:
  221. depth++
  222. case EndElement:
  223. depth--
  224. }
  225. }
  226. return val.UnmarshalText(buf)
  227. }
  228. // unmarshalAttr unmarshals a single XML attribute into val.
  229. func (d *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
  230. if val.Kind() == reflect.Ptr {
  231. if val.IsNil() {
  232. val.Set(reflect.New(val.Type().Elem()))
  233. }
  234. val = val.Elem()
  235. }
  236. if val.CanInterface() && val.Type().Implements(unmarshalerAttrType) {
  237. // This is an unmarshaler with a non-pointer receiver,
  238. // so it's likely to be incorrect, but we do what we're told.
  239. return val.Interface().(UnmarshalerAttr).UnmarshalXMLAttr(attr)
  240. }
  241. if val.CanAddr() {
  242. pv := val.Addr()
  243. if pv.CanInterface() && pv.Type().Implements(unmarshalerAttrType) {
  244. return pv.Interface().(UnmarshalerAttr).UnmarshalXMLAttr(attr)
  245. }
  246. }
  247. // Not an UnmarshalerAttr; try encoding.TextUnmarshaler.
  248. if val.CanInterface() && val.Type().Implements(textUnmarshalerType) {
  249. // This is an unmarshaler with a non-pointer receiver,
  250. // so it's likely to be incorrect, but we do what we're told.
  251. return val.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(attr.Value))
  252. }
  253. if val.CanAddr() {
  254. pv := val.Addr()
  255. if pv.CanInterface() && pv.Type().Implements(textUnmarshalerType) {
  256. return pv.Interface().(encoding.TextUnmarshaler).UnmarshalText([]byte(attr.Value))
  257. }
  258. }
  259. if val.Type().Kind() == reflect.Slice && val.Type().Elem().Kind() != reflect.Uint8 {
  260. // Slice of element values.
  261. // Grow slice.
  262. n := val.Len()
  263. val.Set(reflect.Append(val, reflect.Zero(val.Type().Elem())))
  264. // Recur to read element into slice.
  265. if err := d.unmarshalAttr(val.Index(n), attr); err != nil {
  266. val.SetLen(n)
  267. return err
  268. }
  269. return nil
  270. }
  271. if val.Type() == attrType {
  272. val.Set(reflect.ValueOf(attr))
  273. return nil
  274. }
  275. return copyValue(val, []byte(attr.Value))
  276. }
  277. var (
  278. attrType = reflect.TypeOf(Attr{})
  279. unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
  280. unmarshalerAttrType = reflect.TypeOf((*UnmarshalerAttr)(nil)).Elem()
  281. textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
  282. )
  283. // Find reflect.Type for an element's type attribute.
  284. func (p *Decoder) typeForElement(val reflect.Value, start *StartElement) reflect.Type {
  285. t := ""
  286. for _, a := range start.Attr {
  287. if a.Name == xmlSchemaInstance || a.Name == xsiType {
  288. t = a.Value
  289. break
  290. }
  291. }
  292. if t == "" {
  293. // No type attribute; fall back to looking up type by interface name.
  294. t = val.Type().Name()
  295. }
  296. // Maybe the type is a basic xsd:* type.
  297. typ := stringToType(t)
  298. if typ != nil {
  299. return typ
  300. }
  301. // Maybe the type is a custom type.
  302. if p.TypeFunc != nil {
  303. if typ, ok := p.TypeFunc(t); ok {
  304. return typ
  305. }
  306. }
  307. return nil
  308. }
  309. // Unmarshal a single XML element into val.
  310. func (d *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
  311. // Find start element if we need it.
  312. if start == nil {
  313. for {
  314. tok, err := d.Token()
  315. if err != nil {
  316. return err
  317. }
  318. if t, ok := tok.(StartElement); ok {
  319. start = &t
  320. break
  321. }
  322. }
  323. }
  324. // Try to figure out type for empty interface values.
  325. if val.Kind() == reflect.Interface && val.IsNil() {
  326. typ := d.typeForElement(val, start)
  327. if typ != nil {
  328. pval := reflect.New(typ).Elem()
  329. err := d.unmarshal(pval, start)
  330. if err != nil {
  331. return err
  332. }
  333. for i := 0; i < 2; i++ {
  334. if typ.Implements(val.Type()) {
  335. val.Set(pval)
  336. return nil
  337. }
  338. typ = reflect.PtrTo(typ)
  339. pval = pval.Addr()
  340. }
  341. val.Set(pval)
  342. return nil
  343. }
  344. }
  345. // Load value from interface, but only if the result will be
  346. // usefully addressable.
  347. if val.Kind() == reflect.Interface && !val.IsNil() {
  348. e := val.Elem()
  349. if e.Kind() == reflect.Ptr && !e.IsNil() {
  350. val = e
  351. }
  352. }
  353. if val.Kind() == reflect.Ptr {
  354. if val.IsNil() {
  355. val.Set(reflect.New(val.Type().Elem()))
  356. }
  357. val = val.Elem()
  358. }
  359. if val.CanInterface() && val.Type().Implements(unmarshalerType) {
  360. // This is an unmarshaler with a non-pointer receiver,
  361. // so it's likely to be incorrect, but we do what we're told.
  362. return d.unmarshalInterface(val.Interface().(Unmarshaler), start)
  363. }
  364. if val.CanAddr() {
  365. pv := val.Addr()
  366. if pv.CanInterface() && pv.Type().Implements(unmarshalerType) {
  367. return d.unmarshalInterface(pv.Interface().(Unmarshaler), start)
  368. }
  369. }
  370. if val.CanInterface() && val.Type().Implements(textUnmarshalerType) {
  371. return d.unmarshalTextInterface(val.Interface().(encoding.TextUnmarshaler))
  372. }
  373. if val.CanAddr() {
  374. pv := val.Addr()
  375. if pv.CanInterface() && pv.Type().Implements(textUnmarshalerType) {
  376. return d.unmarshalTextInterface(pv.Interface().(encoding.TextUnmarshaler))
  377. }
  378. }
  379. var (
  380. data []byte
  381. saveData reflect.Value
  382. comment []byte
  383. saveComment reflect.Value
  384. saveXML reflect.Value
  385. saveXMLIndex int
  386. saveXMLData []byte
  387. saveAny reflect.Value
  388. sv reflect.Value
  389. tinfo *typeInfo
  390. err error
  391. )
  392. switch v := val; v.Kind() {
  393. default:
  394. return errors.New("unknown type " + v.Type().String())
  395. case reflect.Interface:
  396. // TODO: For now, simply ignore the field. In the near
  397. // future we may choose to unmarshal the start
  398. // element on it, if not nil.
  399. return d.Skip()
  400. case reflect.Slice:
  401. typ := v.Type()
  402. if typ.Elem().Kind() == reflect.Uint8 {
  403. // []byte
  404. saveData = v
  405. break
  406. }
  407. // Slice of element values.
  408. // Grow slice.
  409. n := v.Len()
  410. v.Set(reflect.Append(val, reflect.Zero(v.Type().Elem())))
  411. // Recur to read element into slice.
  412. if err := d.unmarshal(v.Index(n), start); err != nil {
  413. v.SetLen(n)
  414. return err
  415. }
  416. return nil
  417. case reflect.Bool, reflect.Float32, reflect.Float64, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.String:
  418. saveData = v
  419. case reflect.Struct:
  420. typ := v.Type()
  421. if typ == nameType {
  422. v.Set(reflect.ValueOf(start.Name))
  423. break
  424. }
  425. sv = v
  426. tinfo, err = getTypeInfo(typ)
  427. if err != nil {
  428. return err
  429. }
  430. // Validate and assign element name.
  431. if tinfo.xmlname != nil {
  432. finfo := tinfo.xmlname
  433. if finfo.name != "" && finfo.name != start.Name.Local {
  434. return UnmarshalError("expected element type <" + finfo.name + "> but have <" + start.Name.Local + ">")
  435. }
  436. if finfo.xmlns != "" && finfo.xmlns != start.Name.Space {
  437. e := "expected element <" + finfo.name + "> in name space " + finfo.xmlns + " but have "
  438. if start.Name.Space == "" {
  439. e += "no name space"
  440. } else {
  441. e += start.Name.Space
  442. }
  443. return UnmarshalError(e)
  444. }
  445. fv := finfo.value(sv, initNilPointers)
  446. if _, ok := fv.Interface().(Name); ok {
  447. fv.Set(reflect.ValueOf(start.Name))
  448. }
  449. }
  450. // Assign attributes.
  451. for _, a := range start.Attr {
  452. handled := false
  453. any := -1
  454. for i := range tinfo.fields {
  455. finfo := &tinfo.fields[i]
  456. switch finfo.flags & fMode {
  457. case fAttr:
  458. strv := finfo.value(sv, initNilPointers)
  459. if a.Name.Local == finfo.name && (finfo.xmlns == "" || finfo.xmlns == a.Name.Space) {
  460. needTypeAttr := (finfo.flags & fTypeAttr) != 0
  461. // HACK: avoid using xsi:type value for a "type" attribute, such as ManagedObjectReference.Type for example.
  462. if needTypeAttr || (a.Name != xmlSchemaInstance && a.Name != xsiType) {
  463. if err := d.unmarshalAttr(strv, a); err != nil {
  464. return err
  465. }
  466. }
  467. handled = true
  468. }
  469. case fAny | fAttr:
  470. if any == -1 {
  471. any = i
  472. }
  473. }
  474. }
  475. if !handled && any >= 0 {
  476. finfo := &tinfo.fields[any]
  477. strv := finfo.value(sv, initNilPointers)
  478. if err := d.unmarshalAttr(strv, a); err != nil {
  479. return err
  480. }
  481. }
  482. }
  483. // Determine whether we need to save character data or comments.
  484. for i := range tinfo.fields {
  485. finfo := &tinfo.fields[i]
  486. switch finfo.flags & fMode {
  487. case fCDATA, fCharData:
  488. if !saveData.IsValid() {
  489. saveData = finfo.value(sv, initNilPointers)
  490. }
  491. case fComment:
  492. if !saveComment.IsValid() {
  493. saveComment = finfo.value(sv, initNilPointers)
  494. }
  495. case fAny, fAny | fElement:
  496. if !saveAny.IsValid() {
  497. saveAny = finfo.value(sv, initNilPointers)
  498. }
  499. case fInnerXML:
  500. if !saveXML.IsValid() {
  501. saveXML = finfo.value(sv, initNilPointers)
  502. if d.saved == nil {
  503. saveXMLIndex = 0
  504. d.saved = new(bytes.Buffer)
  505. } else {
  506. saveXMLIndex = d.savedOffset()
  507. }
  508. }
  509. }
  510. }
  511. }
  512. // Find end element.
  513. // Process sub-elements along the way.
  514. Loop:
  515. for {
  516. var savedOffset int
  517. if saveXML.IsValid() {
  518. savedOffset = d.savedOffset()
  519. }
  520. tok, err := d.Token()
  521. if err != nil {
  522. return err
  523. }
  524. switch t := tok.(type) {
  525. case StartElement:
  526. consumed := false
  527. if sv.IsValid() {
  528. consumed, err = d.unmarshalPath(tinfo, sv, nil, &t)
  529. if err != nil {
  530. return err
  531. }
  532. if !consumed && saveAny.IsValid() {
  533. consumed = true
  534. if err := d.unmarshal(saveAny, &t); err != nil {
  535. return err
  536. }
  537. }
  538. }
  539. if !consumed {
  540. if err := d.Skip(); err != nil {
  541. return err
  542. }
  543. }
  544. case EndElement:
  545. if saveXML.IsValid() {
  546. saveXMLData = d.saved.Bytes()[saveXMLIndex:savedOffset]
  547. if saveXMLIndex == 0 {
  548. d.saved = nil
  549. }
  550. }
  551. break Loop
  552. case CharData:
  553. if saveData.IsValid() {
  554. data = append(data, t...)
  555. }
  556. case Comment:
  557. if saveComment.IsValid() {
  558. comment = append(comment, t...)
  559. }
  560. }
  561. }
  562. if saveData.IsValid() && saveData.CanInterface() && saveData.Type().Implements(textUnmarshalerType) {
  563. if err := saveData.Interface().(encoding.TextUnmarshaler).UnmarshalText(data); err != nil {
  564. return err
  565. }
  566. saveData = reflect.Value{}
  567. }
  568. if saveData.IsValid() && saveData.CanAddr() {
  569. pv := saveData.Addr()
  570. if pv.CanInterface() && pv.Type().Implements(textUnmarshalerType) {
  571. if err := pv.Interface().(encoding.TextUnmarshaler).UnmarshalText(data); err != nil {
  572. return err
  573. }
  574. saveData = reflect.Value{}
  575. }
  576. }
  577. if err := copyValue(saveData, data); err != nil {
  578. return err
  579. }
  580. switch t := saveComment; t.Kind() {
  581. case reflect.String:
  582. t.SetString(string(comment))
  583. case reflect.Slice:
  584. t.Set(reflect.ValueOf(comment))
  585. }
  586. switch t := saveXML; t.Kind() {
  587. case reflect.String:
  588. t.SetString(string(saveXMLData))
  589. case reflect.Slice:
  590. if t.Type().Elem().Kind() == reflect.Uint8 {
  591. t.Set(reflect.ValueOf(saveXMLData))
  592. }
  593. }
  594. return nil
  595. }
  596. func copyValue(dst reflect.Value, src []byte) (err error) {
  597. dst0 := dst
  598. if dst.Kind() == reflect.Ptr {
  599. if dst.IsNil() {
  600. dst.Set(reflect.New(dst.Type().Elem()))
  601. }
  602. dst = dst.Elem()
  603. }
  604. // Save accumulated data.
  605. switch dst.Kind() {
  606. case reflect.Invalid:
  607. // Probably a comment.
  608. default:
  609. return errors.New("cannot unmarshal into " + dst0.Type().String())
  610. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  611. if len(src) == 0 {
  612. dst.SetInt(0)
  613. return nil
  614. }
  615. itmp, err := strconv.ParseInt(strings.TrimSpace(string(src)), 10, dst.Type().Bits())
  616. if err != nil {
  617. return err
  618. }
  619. dst.SetInt(itmp)
  620. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  621. var utmp uint64
  622. if len(src) > 0 && src[0] == '-' {
  623. // Negative value for unsigned field.
  624. // Assume it was serialized following two's complement.
  625. itmp, err := strconv.ParseInt(string(src), 10, dst.Type().Bits())
  626. if err != nil {
  627. return err
  628. }
  629. // Reinterpret value based on type width.
  630. switch dst.Type().Bits() {
  631. case 8:
  632. utmp = uint64(uint8(itmp))
  633. case 16:
  634. utmp = uint64(uint16(itmp))
  635. case 32:
  636. utmp = uint64(uint32(itmp))
  637. case 64:
  638. utmp = uint64(uint64(itmp))
  639. }
  640. } else {
  641. if len(src) == 0 {
  642. dst.SetUint(0)
  643. return nil
  644. }
  645. utmp, err = strconv.ParseUint(strings.TrimSpace(string(src)), 10, dst.Type().Bits())
  646. if err != nil {
  647. return err
  648. }
  649. }
  650. dst.SetUint(utmp)
  651. case reflect.Float32, reflect.Float64:
  652. if len(src) == 0 {
  653. dst.SetFloat(0)
  654. return nil
  655. }
  656. ftmp, err := strconv.ParseFloat(strings.TrimSpace(string(src)), dst.Type().Bits())
  657. if err != nil {
  658. return err
  659. }
  660. dst.SetFloat(ftmp)
  661. case reflect.Bool:
  662. if len(src) == 0 {
  663. dst.SetBool(false)
  664. return nil
  665. }
  666. value, err := strconv.ParseBool(strings.TrimSpace(string(src)))
  667. if err != nil {
  668. return err
  669. }
  670. dst.SetBool(value)
  671. case reflect.String:
  672. dst.SetString(string(src))
  673. case reflect.Slice:
  674. if len(src) == 0 {
  675. // non-nil to flag presence
  676. src = []byte{}
  677. }
  678. dst.SetBytes(src)
  679. }
  680. return nil
  681. }
  682. // unmarshalPath walks down an XML structure looking for wanted
  683. // paths, and calls unmarshal on them.
  684. // The consumed result tells whether XML elements have been consumed
  685. // from the Decoder until start's matching end element, or if it's
  686. // still untouched because start is uninteresting for sv's fields.
  687. func (d *Decoder) unmarshalPath(tinfo *typeInfo, sv reflect.Value, parents []string, start *StartElement) (consumed bool, err error) {
  688. recurse := false
  689. Loop:
  690. for i := range tinfo.fields {
  691. finfo := &tinfo.fields[i]
  692. if finfo.flags&fElement == 0 || len(finfo.parents) < len(parents) || finfo.xmlns != "" && finfo.xmlns != start.Name.Space {
  693. continue
  694. }
  695. for j := range parents {
  696. if parents[j] != finfo.parents[j] {
  697. continue Loop
  698. }
  699. }
  700. if len(finfo.parents) == len(parents) && finfo.name == start.Name.Local {
  701. // It's a perfect match, unmarshal the field.
  702. return true, d.unmarshal(finfo.value(sv, initNilPointers), start)
  703. }
  704. if len(finfo.parents) > len(parents) && finfo.parents[len(parents)] == start.Name.Local {
  705. // It's a prefix for the field. Break and recurse
  706. // since it's not ok for one field path to be itself
  707. // the prefix for another field path.
  708. recurse = true
  709. // We can reuse the same slice as long as we
  710. // don't try to append to it.
  711. parents = finfo.parents[:len(parents)+1]
  712. break
  713. }
  714. }
  715. if !recurse {
  716. // We have no business with this element.
  717. return false, nil
  718. }
  719. // The element is not a perfect match for any field, but one
  720. // or more fields have the path to this element as a parent
  721. // prefix. Recurse and attempt to match these.
  722. for {
  723. var tok Token
  724. tok, err = d.Token()
  725. if err != nil {
  726. return true, err
  727. }
  728. switch t := tok.(type) {
  729. case StartElement:
  730. consumed2, err := d.unmarshalPath(tinfo, sv, parents, &t)
  731. if err != nil {
  732. return true, err
  733. }
  734. if !consumed2 {
  735. if err := d.Skip(); err != nil {
  736. return true, err
  737. }
  738. }
  739. case EndElement:
  740. return true, nil
  741. }
  742. }
  743. }
  744. // Skip reads tokens until it has consumed the end element
  745. // matching the most recent start element already consumed.
  746. // It recurs if it encounters a start element, so it can be used to
  747. // skip nested structures.
  748. // It returns nil if it finds an end element matching the start
  749. // element; otherwise it returns an error describing the problem.
  750. func (d *Decoder) Skip() error {
  751. for {
  752. tok, err := d.Token()
  753. if err != nil {
  754. return err
  755. }
  756. switch tok.(type) {
  757. case StartElement:
  758. if err := d.Skip(); err != nil {
  759. return err
  760. }
  761. case EndElement:
  762. return nil
  763. }
  764. }
  765. }