Skip to content

Commit

Permalink
encoding/xml: use reflect.TypeFor for known types
Browse files Browse the repository at this point in the history
For #60088

Change-Id: Ib2589b994d304cca1f2e2081639959d80818ac7f
Reviewed-on: https://go-review.googlesource.com/c/go/+/514639
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Joseph Tsai <joetsai@digital-static.net>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Aug 1, 2023
1 parent 29253f4 commit db25bc1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/encoding/xml/marshal.go
Expand Up @@ -415,9 +415,9 @@ func (p *printer) popPrefix() {
}

var (
marshalerType = reflect.TypeOf((*Marshaler)(nil)).Elem()
marshalerAttrType = reflect.TypeOf((*MarshalerAttr)(nil)).Elem()
textMarshalerType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
marshalerType = reflect.TypeFor[Marshaler]()
marshalerAttrType = reflect.TypeFor[MarshalerAttr]()
textMarshalerType = reflect.TypeFor[encoding.TextMarshaler]()
)

// marshalValue writes one or more XML elements representing val.
Expand Down
8 changes: 4 additions & 4 deletions src/encoding/xml/read.go
Expand Up @@ -304,10 +304,10 @@ func (d *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
}

var (
attrType = reflect.TypeOf(Attr{})
unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
unmarshalerAttrType = reflect.TypeOf((*UnmarshalerAttr)(nil)).Elem()
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
attrType = reflect.TypeFor[Attr]()
unmarshalerType = reflect.TypeFor[Unmarshaler]()
unmarshalerAttrType = reflect.TypeFor[UnmarshalerAttr]()
textUnmarshalerType = reflect.TypeFor[encoding.TextUnmarshaler]()
)

const (
Expand Down
8 changes: 4 additions & 4 deletions src/encoding/xml/read_test.go
Expand Up @@ -326,10 +326,10 @@ type BadPathEmbeddedB struct {
var badPathTests = []struct {
v, e any
}{
{&BadPathTestA{}, &TagPathError{reflect.TypeOf(BadPathTestA{}), "First", "items>item1", "Second", "items"}},
{&BadPathTestB{}, &TagPathError{reflect.TypeOf(BadPathTestB{}), "First", "items>item1", "Second", "items>item1>value"}},
{&BadPathTestC{}, &TagPathError{reflect.TypeOf(BadPathTestC{}), "First", "", "Second", "First"}},
{&BadPathTestD{}, &TagPathError{reflect.TypeOf(BadPathTestD{}), "First", "", "Second", "First"}},
{&BadPathTestA{}, &TagPathError{reflect.TypeFor[BadPathTestA](), "First", "items>item1", "Second", "items"}},
{&BadPathTestB{}, &TagPathError{reflect.TypeFor[BadPathTestB](), "First", "items>item1", "Second", "items>item1>value"}},
{&BadPathTestC{}, &TagPathError{reflect.TypeFor[BadPathTestC](), "First", "", "Second", "First"}},
{&BadPathTestD{}, &TagPathError{reflect.TypeFor[BadPathTestD](), "First", "", "Second", "First"}},
}

func TestUnmarshalBadPaths(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/xml/typeinfo.go
Expand Up @@ -46,7 +46,7 @@ const (

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

var nameType = reflect.TypeOf(Name{})
var nameType = reflect.TypeFor[Name]()

// getTypeInfo returns the typeInfo structure with details necessary
// for marshaling and unmarshaling typ.
Expand Down

0 comments on commit db25bc1

Please sign in to comment.