Skip to content

Commit 6e9e9df

Browse files
pazderakianlancetaylor
authored andcommitted
encoding/xml: improve package based on the suggestions from metalinter
Existing code in encoding/xml packages contains code which breaks various linter rules (comments, constant and variable naming, variable shadowing, etc). Fixes #21578 Change-Id: Id4bd9a9be6d5728ce88fb6efe33030ef943c078c Reviewed-on: https://go-review.googlesource.com/58210 Reviewed-by: Sam Whited <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Sam Whited <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 77b4beb commit 6e9e9df

File tree

8 files changed

+103
-94
lines changed

8 files changed

+103
-94
lines changed

src/encoding/xml/atom_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ var atomValue = &Feed{
1212
Link: []Link{{Href: "http://example.org/"}},
1313
Updated: ParseTime("2003-12-13T18:30:02Z"),
1414
Author: Person{Name: "John Doe"},
15-
Id: "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6",
15+
ID: "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6",
1616

1717
Entry: []Entry{
1818
{
1919
Title: "Atom-Powered Robots Run Amok",
2020
Link: []Link{{Href: "http://example.org/2003/12/13/atom03"}},
21-
Id: "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a",
21+
ID: "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a",
2222
Updated: ParseTime("2003-12-13T18:30:02Z"),
2323
Summary: NewText("Some text."),
2424
},
2525
},
2626
}
2727

28-
var atomXml = `` +
28+
var atomXML = `` +
2929
`<feed xmlns="http://www.w3.org/2005/Atom" updated="2003-12-13T18:30:02Z">` +
3030
`<title>Example Feed</title>` +
3131
`<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>` +

src/encoding/xml/marshal.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import (
1616
)
1717

1818
const (
19-
// A generic XML header suitable for use with the output of Marshal.
19+
// Header is a generic XML header suitable for use with the output of Marshal.
2020
// This is not automatically added to any output of this package,
2121
// it is provided as a convenience.
22-
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
22+
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
23+
xmlNamespacePrefix = "xml"
2324
)
2425

2526
// Marshal returns the XML encoding of v.
@@ -320,7 +321,7 @@ func (p *printer) createAttrPrefix(url string) string {
320321
// (The "http://www.w3.org/2000/xmlns/" name space is also predefined as "xmlns",
321322
// but users should not be trying to use that one directly - that's our job.)
322323
if url == xmlURL {
323-
return "xml"
324+
return xmlNamespacePrefix
324325
}
325326

326327
// Need to define a new name space.
@@ -1011,7 +1012,7 @@ func (s *parentStack) push(parents []string) error {
10111012
return nil
10121013
}
10131014

1014-
// A MarshalXMLError is returned when Marshal encounters a type
1015+
// UnsupportedTypeError is returned when Marshal encounters a type
10151016
// that cannot be converted into XML.
10161017
type UnsupportedTypeError struct {
10171018
Type reflect.Type

src/encoding/xml/marshal_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ var marshalTests = []struct {
646646
{Value: &Universe{Visible: 9.3e13}, ExpectXML: `<universe>9.3e+13</universe>`},
647647
{Value: &Particle{HasMass: true}, ExpectXML: `<particle>true</particle>`},
648648
{Value: &Departure{When: ParseTime("2013-01-09T00:15:00-09:00")}, ExpectXML: `<departure>2013-01-09T00:15:00-09:00</departure>`},
649-
{Value: atomValue, ExpectXML: atomXml},
649+
{Value: atomValue, ExpectXML: atomXML},
650650
{
651651
Value: &Ship{
652652
Name: "Heart of Gold",
@@ -1910,7 +1910,7 @@ func BenchmarkMarshal(b *testing.B) {
19101910

19111911
func BenchmarkUnmarshal(b *testing.B) {
19121912
b.ReportAllocs()
1913-
xml := []byte(atomXml)
1913+
xml := []byte(atomXML)
19141914
b.RunParallel(func(pb *testing.PB) {
19151915
for pb.Next() {
19161916
Unmarshal(xml, &Feed{})

src/encoding/xml/read.go

+36-36
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,19 @@ func receiverType(val interface{}) string {
192192

193193
// unmarshalInterface unmarshals a single XML element into val.
194194
// start is the opening tag of the element.
195-
func (p *Decoder) unmarshalInterface(val Unmarshaler, start *StartElement) error {
195+
func (d *Decoder) unmarshalInterface(val Unmarshaler, start *StartElement) error {
196196
// Record that decoder must stop at end tag corresponding to start.
197-
p.pushEOF()
197+
d.pushEOF()
198198

199-
p.unmarshalDepth++
200-
err := val.UnmarshalXML(p, *start)
201-
p.unmarshalDepth--
199+
d.unmarshalDepth++
200+
err := val.UnmarshalXML(d, *start)
201+
d.unmarshalDepth--
202202
if err != nil {
203-
p.popEOF()
203+
d.popEOF()
204204
return err
205205
}
206206

207-
if !p.popEOF() {
207+
if !d.popEOF() {
208208
return fmt.Errorf("xml: %s.UnmarshalXML did not consume entire <%s> element", receiverType(val), start.Name.Local)
209209
}
210210

@@ -214,11 +214,11 @@ func (p *Decoder) unmarshalInterface(val Unmarshaler, start *StartElement) error
214214
// unmarshalTextInterface unmarshals a single XML element into val.
215215
// The chardata contained in the element (but not its children)
216216
// is passed to the text unmarshaler.
217-
func (p *Decoder) unmarshalTextInterface(val encoding.TextUnmarshaler) error {
217+
func (d *Decoder) unmarshalTextInterface(val encoding.TextUnmarshaler) error {
218218
var buf []byte
219219
depth := 1
220220
for depth > 0 {
221-
t, err := p.Token()
221+
t, err := d.Token()
222222
if err != nil {
223223
return err
224224
}
@@ -237,7 +237,7 @@ func (p *Decoder) unmarshalTextInterface(val encoding.TextUnmarshaler) error {
237237
}
238238

239239
// unmarshalAttr unmarshals a single XML attribute into val.
240-
func (p *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
240+
func (d *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
241241
if val.Kind() == reflect.Ptr {
242242
if val.IsNil() {
243243
val.Set(reflect.New(val.Type().Elem()))
@@ -276,7 +276,7 @@ func (p *Decoder) unmarshalAttr(val reflect.Value, attr Attr) error {
276276
val.Set(reflect.Append(val, reflect.Zero(val.Type().Elem())))
277277

278278
// Recur to read element into slice.
279-
if err := p.unmarshalAttr(val.Index(n), attr); err != nil {
279+
if err := d.unmarshalAttr(val.Index(n), attr); err != nil {
280280
val.SetLen(n)
281281
return err
282282
}
@@ -299,11 +299,11 @@ var (
299299
)
300300

301301
// Unmarshal a single XML element into val.
302-
func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
302+
func (d *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
303303
// Find start element if we need it.
304304
if start == nil {
305305
for {
306-
tok, err := p.Token()
306+
tok, err := d.Token()
307307
if err != nil {
308308
return err
309309
}
@@ -333,24 +333,24 @@ func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
333333
if val.CanInterface() && val.Type().Implements(unmarshalerType) {
334334
// This is an unmarshaler with a non-pointer receiver,
335335
// so it's likely to be incorrect, but we do what we're told.
336-
return p.unmarshalInterface(val.Interface().(Unmarshaler), start)
336+
return d.unmarshalInterface(val.Interface().(Unmarshaler), start)
337337
}
338338

339339
if val.CanAddr() {
340340
pv := val.Addr()
341341
if pv.CanInterface() && pv.Type().Implements(unmarshalerType) {
342-
return p.unmarshalInterface(pv.Interface().(Unmarshaler), start)
342+
return d.unmarshalInterface(pv.Interface().(Unmarshaler), start)
343343
}
344344
}
345345

346346
if val.CanInterface() && val.Type().Implements(textUnmarshalerType) {
347-
return p.unmarshalTextInterface(val.Interface().(encoding.TextUnmarshaler))
347+
return d.unmarshalTextInterface(val.Interface().(encoding.TextUnmarshaler))
348348
}
349349

350350
if val.CanAddr() {
351351
pv := val.Addr()
352352
if pv.CanInterface() && pv.Type().Implements(textUnmarshalerType) {
353-
return p.unmarshalTextInterface(pv.Interface().(encoding.TextUnmarshaler))
353+
return d.unmarshalTextInterface(pv.Interface().(encoding.TextUnmarshaler))
354354
}
355355
}
356356

@@ -376,7 +376,7 @@ func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
376376
// TODO: For now, simply ignore the field. In the near
377377
// future we may choose to unmarshal the start
378378
// element on it, if not nil.
379-
return p.Skip()
379+
return d.Skip()
380380

381381
case reflect.Slice:
382382
typ := v.Type()
@@ -392,7 +392,7 @@ func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
392392
v.Set(reflect.Append(val, reflect.Zero(v.Type().Elem())))
393393

394394
// Recur to read element into slice.
395-
if err := p.unmarshal(v.Index(n), start); err != nil {
395+
if err := d.unmarshal(v.Index(n), start); err != nil {
396396
v.SetLen(n)
397397
return err
398398
}
@@ -445,7 +445,7 @@ func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
445445
case fAttr:
446446
strv := finfo.value(sv)
447447
if a.Name.Local == finfo.name && (finfo.xmlns == "" || finfo.xmlns == a.Name.Space) {
448-
if err := p.unmarshalAttr(strv, a); err != nil {
448+
if err := d.unmarshalAttr(strv, a); err != nil {
449449
return err
450450
}
451451
handled = true
@@ -460,7 +460,7 @@ func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
460460
if !handled && any >= 0 {
461461
finfo := &tinfo.fields[any]
462462
strv := finfo.value(sv)
463-
if err := p.unmarshalAttr(strv, a); err != nil {
463+
if err := d.unmarshalAttr(strv, a); err != nil {
464464
return err
465465
}
466466
}
@@ -488,11 +488,11 @@ func (p *Decoder) unmarshal(val reflect.Value, start *StartElement) error {
488488
case fInnerXml:
489489
if !saveXML.IsValid() {
490490
saveXML = finfo.value(sv)
491-
if p.saved == nil {
491+
if d.saved == nil {
492492
saveXMLIndex = 0
493-
p.saved = new(bytes.Buffer)
493+
d.saved = new(bytes.Buffer)
494494
} else {
495-
saveXMLIndex = p.savedOffset()
495+
saveXMLIndex = d.savedOffset()
496496
}
497497
}
498498
}
@@ -505,38 +505,38 @@ Loop:
505505
for {
506506
var savedOffset int
507507
if saveXML.IsValid() {
508-
savedOffset = p.savedOffset()
508+
savedOffset = d.savedOffset()
509509
}
510-
tok, err := p.Token()
510+
tok, err := d.Token()
511511
if err != nil {
512512
return err
513513
}
514514
switch t := tok.(type) {
515515
case StartElement:
516516
consumed := false
517517
if sv.IsValid() {
518-
consumed, err = p.unmarshalPath(tinfo, sv, nil, &t)
518+
consumed, err = d.unmarshalPath(tinfo, sv, nil, &t)
519519
if err != nil {
520520
return err
521521
}
522522
if !consumed && saveAny.IsValid() {
523523
consumed = true
524-
if err := p.unmarshal(saveAny, &t); err != nil {
524+
if err := d.unmarshal(saveAny, &t); err != nil {
525525
return err
526526
}
527527
}
528528
}
529529
if !consumed {
530-
if err := p.Skip(); err != nil {
530+
if err := d.Skip(); err != nil {
531531
return err
532532
}
533533
}
534534

535535
case EndElement:
536536
if saveXML.IsValid() {
537-
saveXMLData = p.saved.Bytes()[saveXMLIndex:savedOffset]
537+
saveXMLData = d.saved.Bytes()[saveXMLIndex:savedOffset]
538538
if saveXMLIndex == 0 {
539-
p.saved = nil
539+
d.saved = nil
540540
}
541541
}
542542
break Loop
@@ -666,7 +666,7 @@ func copyValue(dst reflect.Value, src []byte) (err error) {
666666
// The consumed result tells whether XML elements have been consumed
667667
// from the Decoder until start's matching end element, or if it's
668668
// still untouched because start is uninteresting for sv's fields.
669-
func (p *Decoder) unmarshalPath(tinfo *typeInfo, sv reflect.Value, parents []string, start *StartElement) (consumed bool, err error) {
669+
func (d *Decoder) unmarshalPath(tinfo *typeInfo, sv reflect.Value, parents []string, start *StartElement) (consumed bool, err error) {
670670
recurse := false
671671
Loop:
672672
for i := range tinfo.fields {
@@ -681,7 +681,7 @@ Loop:
681681
}
682682
if len(finfo.parents) == len(parents) && finfo.name == start.Name.Local {
683683
// It's a perfect match, unmarshal the field.
684-
return true, p.unmarshal(finfo.value(sv), start)
684+
return true, d.unmarshal(finfo.value(sv), start)
685685
}
686686
if len(finfo.parents) > len(parents) && finfo.parents[len(parents)] == start.Name.Local {
687687
// It's a prefix for the field. Break and recurse
@@ -704,18 +704,18 @@ Loop:
704704
// prefix. Recurse and attempt to match these.
705705
for {
706706
var tok Token
707-
tok, err = p.Token()
707+
tok, err = d.Token()
708708
if err != nil {
709709
return true, err
710710
}
711711
switch t := tok.(type) {
712712
case StartElement:
713-
consumed2, err := p.unmarshalPath(tinfo, sv, parents, &t)
713+
consumed2, err := d.unmarshalPath(tinfo, sv, parents, &t)
714714
if err != nil {
715715
return true, err
716716
}
717717
if !consumed2 {
718-
if err := p.Skip(); err != nil {
718+
if err := d.Skip(); err != nil {
719719
return true, err
720720
}
721721
}

src/encoding/xml/read_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ not being used from outside intra_region_diff.py.
8383
type Feed struct {
8484
XMLName Name `xml:"http://www.w3.org/2005/Atom feed"`
8585
Title string `xml:"title"`
86-
Id string `xml:"id"`
86+
ID string `xml:"id"`
8787
Link []Link `xml:"link"`
8888
Updated time.Time `xml:"updated,attr"`
8989
Author Person `xml:"author"`
@@ -92,7 +92,7 @@ type Feed struct {
9292

9393
type Entry struct {
9494
Title string `xml:"title"`
95-
Id string `xml:"id"`
95+
ID string `xml:"id"`
9696
Link []Link `xml:"link"`
9797
Updated time.Time `xml:"updated"`
9898
Author Person `xml:"author"`
@@ -123,7 +123,7 @@ var atomFeed = Feed{
123123
{Rel: "alternate", Href: "http://codereview.appspot.com/"},
124124
{Rel: "self", Href: "http://codereview.appspot.com/rss/mine/rsc"},
125125
},
126-
Id: "http://codereview.appspot.com/",
126+
ID: "http://codereview.appspot.com/",
127127
Updated: ParseTime("2009-10-04T01:35:58+00:00"),
128128
Author: Person{
129129
Name: "rietveld<>",
@@ -140,7 +140,7 @@ var atomFeed = Feed{
140140
Name: "email-address-removed",
141141
InnerXML: "<name>email-address-removed</name>",
142142
},
143-
Id: "urn:md5:134d9179c41f806be79b3a5f7877d19a",
143+
ID: "urn:md5:134d9179c41f806be79b3a5f7877d19a",
144144
Summary: Text{
145145
Type: "html",
146146
Body: `
@@ -187,7 +187,7 @@ the top of feeds.py marked NOTE(rsc).
187187
Name: "email-address-removed",
188188
InnerXML: "<name>email-address-removed</name>",
189189
},
190-
Id: "urn:md5:0a2a4f19bb815101f0ba2904aed7c35a",
190+
ID: "urn:md5:0a2a4f19bb815101f0ba2904aed7c35a",
191191
Summary: Text{
192192
Type: "html",
193193
Body: `

0 commit comments

Comments
 (0)