@@ -451,23 +451,27 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
451
451
452
452
// Check for marshaler.
453
453
if val .CanInterface () && typ .Implements (marshalerType ) {
454
- return p .marshalInterface (val .Interface ().(Marshaler ), defaultStart (typ , finfo , startTemplate ))
454
+ marshaler , _ := reflect.TypeAssert [Marshaler ](val )
455
+ return p .marshalInterface (marshaler , defaultStart (typ , finfo , startTemplate ))
455
456
}
456
457
if val .CanAddr () {
457
458
pv := val .Addr ()
458
459
if pv .CanInterface () && pv .Type ().Implements (marshalerType ) {
459
- return p .marshalInterface (pv .Interface ().(Marshaler ), defaultStart (pv .Type (), finfo , startTemplate ))
460
+ marshaler , _ := reflect.TypeAssert [Marshaler ](pv )
461
+ return p .marshalInterface (marshaler , defaultStart (pv .Type (), finfo , startTemplate ))
460
462
}
461
463
}
462
464
463
465
// Check for text marshaler.
464
466
if val .CanInterface () && typ .Implements (textMarshalerType ) {
465
- return p .marshalTextInterface (val .Interface ().(encoding.TextMarshaler ), defaultStart (typ , finfo , startTemplate ))
467
+ textMarshaler , _ := reflect.TypeAssert [encoding.TextMarshaler ](val )
468
+ return p .marshalTextInterface (textMarshaler , defaultStart (typ , finfo , startTemplate ))
466
469
}
467
470
if val .CanAddr () {
468
471
pv := val .Addr ()
469
472
if pv .CanInterface () && pv .Type ().Implements (textMarshalerType ) {
470
- return p .marshalTextInterface (pv .Interface ().(encoding.TextMarshaler ), defaultStart (pv .Type (), finfo , startTemplate ))
473
+ textMarshaler , _ := reflect.TypeAssert [encoding.TextMarshaler ](pv )
474
+ return p .marshalTextInterface (textMarshaler , defaultStart (pv .Type (), finfo , startTemplate ))
471
475
}
472
476
}
473
477
@@ -503,7 +507,7 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
503
507
start .Name .Space , start .Name .Local = xmlname .xmlns , xmlname .name
504
508
} else {
505
509
fv := xmlname .value (val , dontInitNilPointers )
506
- if v , ok := fv . Interface ().( Name ); ok && v .Local != "" {
510
+ if v , ok := reflect. TypeAssert [ Name ]( fv ); ok && v .Local != "" {
507
511
start .Name = v
508
512
}
509
513
}
@@ -581,7 +585,8 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
581
585
// marshalAttr marshals an attribute with the given name and value, adding to start.Attr.
582
586
func (p * printer ) marshalAttr (start * StartElement , name Name , val reflect.Value ) error {
583
587
if val .CanInterface () && val .Type ().Implements (marshalerAttrType ) {
584
- attr , err := val .Interface ().(MarshalerAttr ).MarshalXMLAttr (name )
588
+ marshaler , _ := reflect.TypeAssert [MarshalerAttr ](val )
589
+ attr , err := marshaler .MarshalXMLAttr (name )
585
590
if err != nil {
586
591
return err
587
592
}
@@ -594,7 +599,8 @@ func (p *printer) marshalAttr(start *StartElement, name Name, val reflect.Value)
594
599
if val .CanAddr () {
595
600
pv := val .Addr ()
596
601
if pv .CanInterface () && pv .Type ().Implements (marshalerAttrType ) {
597
- attr , err := pv .Interface ().(MarshalerAttr ).MarshalXMLAttr (name )
602
+ marshaler , _ := reflect.TypeAssert [MarshalerAttr ](pv )
603
+ attr , err := marshaler .MarshalXMLAttr (name )
598
604
if err != nil {
599
605
return err
600
606
}
@@ -606,7 +612,8 @@ func (p *printer) marshalAttr(start *StartElement, name Name, val reflect.Value)
606
612
}
607
613
608
614
if val .CanInterface () && val .Type ().Implements (textMarshalerType ) {
609
- text , err := val .Interface ().(encoding.TextMarshaler ).MarshalText ()
615
+ textMarshaler , _ := reflect.TypeAssert [encoding.TextMarshaler ](val )
616
+ text , err := textMarshaler .MarshalText ()
610
617
if err != nil {
611
618
return err
612
619
}
@@ -617,7 +624,8 @@ func (p *printer) marshalAttr(start *StartElement, name Name, val reflect.Value)
617
624
if val .CanAddr () {
618
625
pv := val .Addr ()
619
626
if pv .CanInterface () && pv .Type ().Implements (textMarshalerType ) {
620
- text , err := pv .Interface ().(encoding.TextMarshaler ).MarshalText ()
627
+ textMarshaler , _ := reflect.TypeAssert [encoding.TextMarshaler ](pv )
628
+ text , err := textMarshaler .MarshalText ()
621
629
if err != nil {
622
630
return err
623
631
}
@@ -647,7 +655,8 @@ func (p *printer) marshalAttr(start *StartElement, name Name, val reflect.Value)
647
655
}
648
656
649
657
if val .Type () == attrType {
650
- start .Attr = append (start .Attr , val .Interface ().(Attr ))
658
+ attr , _ := reflect.TypeAssert [Attr ](val )
659
+ start .Attr = append (start .Attr , attr )
651
660
return nil
652
661
}
653
662
@@ -855,7 +864,8 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
855
864
return err
856
865
}
857
866
if vf .CanInterface () && vf .Type ().Implements (textMarshalerType ) {
858
- data , err := vf .Interface ().(encoding.TextMarshaler ).MarshalText ()
867
+ textMarshaler , _ := reflect.TypeAssert [encoding.TextMarshaler ](vf )
868
+ data , err := textMarshaler .MarshalText ()
859
869
if err != nil {
860
870
return err
861
871
}
@@ -867,7 +877,8 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
867
877
if vf .CanAddr () {
868
878
pv := vf .Addr ()
869
879
if pv .CanInterface () && pv .Type ().Implements (textMarshalerType ) {
870
- data , err := pv .Interface ().(encoding.TextMarshaler ).MarshalText ()
880
+ textMarshaler , _ := reflect.TypeAssert [encoding.TextMarshaler ](pv )
881
+ data , err := textMarshaler .MarshalText ()
871
882
if err != nil {
872
883
return err
873
884
}
@@ -902,7 +913,7 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
902
913
return err
903
914
}
904
915
case reflect .Slice :
905
- if elem , ok := vf . Interface ().([ ]byte ); ok {
916
+ if elem , ok := reflect. TypeAssert [[ ]byte ]( vf ); ok {
906
917
if err := emit (p , elem ); err != nil {
907
918
return err
908
919
}
0 commit comments