marshalValue writes one or more XML elements representing val. If val was obtained from a struct field, finfo must have its details.
(val reflect.Value, finfo *fieldInfo, startTemplate *StartElement)
| 423 | // marshalValue writes one or more XML elements representing val. |
| 424 | // If val was obtained from a struct field, finfo must have its details. |
| 425 | func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplate *StartElement) error { |
| 426 | if startTemplate != nil && startTemplate.Name.Local == "" { |
| 427 | return fmt.Errorf("xml: EncodeElement of StartElement with missing name") |
| 428 | } |
| 429 | |
| 430 | if !val.IsValid() { |
| 431 | return nil |
| 432 | } |
| 433 | if finfo != nil && finfo.flags&fOmitEmpty != 0 && isEmptyValue(val) { |
| 434 | return nil |
| 435 | } |
| 436 | |
| 437 | // Drill into interfaces and pointers. |
| 438 | // This can turn into an infinite loop given a cyclic chain, |
| 439 | // but it matches the Go 1 behavior. |
| 440 | for val.Kind() == reflect.Interface || val.Kind() == reflect.Pointer { |
| 441 | if val.IsNil() { |
| 442 | return nil |
| 443 | } |
| 444 | val = val.Elem() |
| 445 | } |
| 446 | |
| 447 | kind := val.Kind() |
| 448 | typ := val.Type() |
| 449 | |
| 450 | // Check for marshaler. |
| 451 | if val.CanInterface() && typ.Implements(marshalerType) { |
| 452 | return p.marshalInterface(val.Interface().(Marshaler), defaultStart(typ, finfo, startTemplate)) |
| 453 | } |
| 454 | if val.CanAddr() { |
| 455 | pv := val.Addr() |
| 456 | if pv.CanInterface() && pv.Type().Implements(marshalerType) { |
| 457 | return p.marshalInterface(pv.Interface().(Marshaler), defaultStart(pv.Type(), finfo, startTemplate)) |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | // Check for text marshaler. |
| 462 | if val.CanInterface() && typ.Implements(textMarshalerType) { |
| 463 | return p.marshalTextInterface(val.Interface().(encoding.TextMarshaler), defaultStart(typ, finfo, startTemplate)) |
| 464 | } |
| 465 | if val.CanAddr() { |
| 466 | pv := val.Addr() |
| 467 | if pv.CanInterface() && pv.Type().Implements(textMarshalerType) { |
| 468 | return p.marshalTextInterface(pv.Interface().(encoding.TextMarshaler), defaultStart(pv.Type(), finfo, startTemplate)) |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Slices and arrays iterate over the elements. They do not have an enclosing tag. |
| 473 | if (kind == reflect.Slice || kind == reflect.Array) && typ.Elem().Kind() != reflect.Uint8 { |
| 474 | for i, n := 0, val.Len(); i < n; i++ { |
| 475 | if err := p.marshalValue(val.Index(i), finfo, startTemplate); err != nil { |
| 476 | return err |
| 477 | } |
| 478 | } |
| 479 | return nil |
| 480 | } |
| 481 | |
| 482 | tinfo, err := getTypeInfo(typ) |
no test coverage detected