Skip to content

Commit db25bc1

Browse files
ianlancetaylorgopherbot
authored andcommitted
encoding/xml: use reflect.TypeFor for known types
For #60088 Change-Id: Ib2589b994d304cca1f2e2081639959d80818ac7f Reviewed-on: https://go-review.googlesource.com/c/go/+/514639 Run-TryBot: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Joseph Tsai <[email protected]>
1 parent 29253f4 commit db25bc1

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

src/encoding/xml/marshal.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ func (p *printer) popPrefix() {
415415
}
416416

417417
var (
418-
marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
419-
marshalerAttrType = reflect.TypeOf((*MarshalerAttr)(nil)).Elem()
420-
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
418+
marshalerType = reflect.TypeFor[Marshaler]()
419+
marshalerAttrType = reflect.TypeFor[MarshalerAttr]()
420+
textMarshalerType = reflect.TypeFor[encoding.TextMarshaler]()
421421
)
422422

423423
// marshalValue writes one or more XML elements representing val.

src/encoding/xml/read.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,10 @@ func (d *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
304304
}
305305

306306
var (
307-
attrType = reflect.TypeOf(Attr{})
308-
unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
309-
unmarshalerAttrType = reflect.TypeOf((*UnmarshalerAttr)(nil)).Elem()
310-
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
307+
attrType = reflect.TypeFor[Attr]()
308+
unmarshalerType = reflect.TypeFor[Unmarshaler]()
309+
unmarshalerAttrType = reflect.TypeFor[UnmarshalerAttr]()
310+
textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
311311
)
312312

313313
const (

src/encoding/xml/read_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,10 @@ type BadPathEmbeddedB struct {
326326
var badPathTests = []struct {
327327
v, e any
328328
}{
329-
{&BadPathTestA{}, &TagPathError{reflect.TypeOf(BadPathTestA{}), "First", "items>item1", "Second", "items"}},
330-
{&BadPathTestB{}, &TagPathError{reflect.TypeOf(BadPathTestB{}), "First", "items>item1", "Second", "items>item1>value"}},
331-
{&BadPathTestC{}, &TagPathError{reflect.TypeOf(BadPathTestC{}), "First", "", "Second", "First"}},
332-
{&BadPathTestD{}, &TagPathError{reflect.TypeOf(BadPathTestD{}), "First", "", "Second", "First"}},
329+
{&BadPathTestA{}, &TagPathError{reflect.TypeFor[BadPathTestA](), "First", "items>item1", "Second", "items"}},
330+
{&BadPathTestB{}, &TagPathError{reflect.TypeFor[BadPathTestB](), "First", "items>item1", "Second", "items>item1>value"}},
331+
{&BadPathTestC{}, &TagPathError{reflect.TypeFor[BadPathTestC](), "First", "", "Second", "First"}},
332+
{&BadPathTestD{}, &TagPathError{reflect.TypeFor[BadPathTestD](), "First", "", "Second", "First"}},
333333
}
334334

335335
func TestUnmarshalBadPaths(t *testing.T) {

src/encoding/xml/typeinfo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const (
4646

4747
var tinfoMap sync.Map // map[reflect.Type]*typeInfo
4848

49-
var nameType = reflect.TypeOf(Name{})
49+
var nameType = reflect.TypeFor[Name]()
5050

5151
// getTypeInfo returns the typeInfo structure with details necessary
5252
// for marshaling and unmarshaling typ.

0 commit comments

Comments
 (0)