Unmarshal a single XML element into val.
(val reflect.Value, start *StartElement, depth int)
| 318 | |
| 319 | // Unmarshal a single XML element into val. |
| 320 | func (d *Decoder) unmarshal(val reflect.Value, start *StartElement, depth int) error { |
| 321 | if depth >= maxUnmarshalDepth || runtime.GOARCH == "wasm" && depth >= maxUnmarshalDepthWasm { |
| 322 | return errUnmarshalDepth |
| 323 | } |
| 324 | // Find start element if we need it. |
| 325 | if start == nil { |
| 326 | for { |
| 327 | tok, err := d.Token() |
| 328 | if err != nil { |
| 329 | return err |
| 330 | } |
| 331 | if t, ok := tok.(StartElement); ok { |
| 332 | start = &t |
| 333 | break |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Load value from interface, but only if the result will be |
| 339 | // usefully addressable. |
| 340 | if val.Kind() == reflect.Interface && !val.IsNil() { |
| 341 | e := val.Elem() |
| 342 | if e.Kind() == reflect.Pointer && !e.IsNil() { |
| 343 | val = e |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | if val.Kind() == reflect.Pointer { |
| 348 | if val.IsNil() { |
| 349 | val.Set(reflect.New(val.Type().Elem())) |
| 350 | } |
| 351 | val = val.Elem() |
| 352 | } |
| 353 | |
| 354 | if val.CanInterface() && val.Type().Implements(unmarshalerType) { |
| 355 | // This is an unmarshaler with a non-pointer receiver, |
| 356 | // so it's likely to be incorrect, but we do what we're told. |
| 357 | return d.unmarshalInterface(val.Interface().(Unmarshaler), start) |
| 358 | } |
| 359 | |
| 360 | if val.CanAddr() { |
| 361 | pv := val.Addr() |
| 362 | if pv.CanInterface() && pv.Type().Implements(unmarshalerType) { |
| 363 | return d.unmarshalInterface(pv.Interface().(Unmarshaler), start) |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | if val.CanInterface() && val.Type().Implements(textUnmarshalerType) { |
| 368 | return d.unmarshalTextInterface(val.Interface().(encoding.TextUnmarshaler)) |
| 369 | } |
| 370 | |
| 371 | if val.CanAddr() { |
| 372 | pv := val.Addr() |
| 373 | if pv.CanInterface() && pv.Type().Implements(textUnmarshalerType) { |
| 374 | return d.unmarshalTextInterface(pv.Interface().(encoding.TextUnmarshaler)) |
| 375 | } |
| 376 | } |
| 377 |
no test coverage detected