Skip to content

Commit 32089f0

Browse files
authored
Merge pull request #8 from nbio/go1.21
Go 1.21 support
2 parents 3196bed + eefa6e7 commit 32089f0

File tree

11 files changed

+62
-87
lines changed

11 files changed

+62
-87
lines changed

.github/workflows/go.yaml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,21 @@ jobs:
1818
submodules: recursive
1919

2020
- name: Set up Go
21-
uses: actions/setup-go@v4
21+
uses: actions/setup-go@v4.1.0
2222
with:
23-
go-version: ^1
23+
go-version-file: go.mod
24+
check-latest: true
2425

2526
- name: Vet Go code
26-
run: go vet ./encoding/xml
27-
working-directory: vendor/go/src
27+
run: go vet ./...
2828

2929
- name: Test Go code
30-
run: go test -v -race ./encoding/xml
31-
working-directory: vendor/go/src
30+
run: go test -v -race ./...
3231

3332
- name: Test Go without cgo
3433
env:
3534
CGO_ENABLED: 0
36-
run: go test -v ./encoding/xml
37-
working-directory: vendor/go/src
35+
run: go test -v ./...
3836

3937
- name: Verify repo is unchanged
4038
run: git diff --exit-code HEAD

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ https://play.golang.org/p/-6Ee8tcLl2L
3535
import "github.com/nbio/xml"
3636
```
3737

38-
## Development
38+
## Notes
3939

40-
To ease keeping this code in sync with a fork of Go, this repository contains a `go.mod` file in `vendor/go/src` that declares itself as the `std` package. This package must be tested from that directory:
40+
Because this package tracks the current [master] branch of the Go repository, only the latest stable release of Go is guaranteed to be supported. The following commits are not present in this fork to permit use under the current stable release:
4141

42-
```shell
43-
cd vendor/go/src && go test -v ./encoding/xml
44-
```
42+
- [encoding/xml: use reflect.TypeFor for known types](https://github.com/golang/go/commit/db25bc19e5221c7df2caed3b1daeda673ec757d9)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/nbio/xml
2+
3+
go 1.21.1

marshal.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
)
1818

1919
const (
20-
// Header is a generic XML header suitable for use with the output of Marshal.
20+
// Header is a generic XML header suitable for use with the output of [Marshal].
2121
// This is not automatically added to any output of this package,
2222
// it is provided as a convenience.
2323
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
@@ -34,7 +34,7 @@ const (
3434
//
3535
// The name for the XML elements is taken from, in order of preference:
3636
// - the tag on the XMLName field, if the data is a struct
37-
// - the value of the XMLName field of type Name
37+
// - the value of the XMLName field of type [Name]
3838
// - the tag of the struct field used to obtain the data
3939
// - the name of the struct field used to obtain the data
4040
// - the name of the marshaled type
@@ -62,9 +62,9 @@ const (
6262
// string of length zero.
6363
// - an anonymous struct field is handled as if the fields of its
6464
// value were part of the outer struct.
65-
// - a field implementing Marshaler is written by calling its MarshalXML
65+
// - a field implementing [Marshaler] is written by calling its MarshalXML
6666
// method.
67-
// - a field implementing encoding.TextMarshaler is written by encoding the
67+
// - a field implementing [encoding.TextMarshaler] is written by encoding the
6868
// result of its MarshalText method as text.
6969
//
7070
// If a field uses a tag "a>b>c", then the element c will be nested inside
@@ -74,7 +74,7 @@ const (
7474
// If the XML name for a struct field is defined by both the field tag and the
7575
// struct's XMLName field, the names must match.
7676
//
77-
// See MarshalIndent for an example.
77+
// See [MarshalIndent] for an example.
7878
//
7979
// Marshal will return an error if asked to marshal a channel, function, or map.
8080
func Marshal(v any) ([]byte, error) {
@@ -96,7 +96,7 @@ func Marshal(v any) ([]byte, error) {
9696
// By convention, arrays or slices are typically encoded as a sequence
9797
// of elements, one per entry.
9898
// Using start as the element tag is not required, but doing so
99-
// will enable Unmarshal to match the XML elements to the correct
99+
// will enable [Unmarshal] to match the XML elements to the correct
100100
// struct field.
101101
// One common implementation strategy is to construct a separate
102102
// value with a layout corresponding to the desired XML and then
@@ -114,17 +114,17 @@ type Marshaler interface {
114114
//
115115
// MarshalXMLAttr returns an XML attribute with the encoded value of the receiver.
116116
// Using name as the attribute name is not required, but doing so
117-
// will enable Unmarshal to match the attribute to the correct
117+
// will enable [Unmarshal] to match the attribute to the correct
118118
// struct field.
119-
// If MarshalXMLAttr returns the zero attribute Attr{}, no attribute
119+
// If MarshalXMLAttr returns the zero attribute [Attr]{}, no attribute
120120
// will be generated in the output.
121121
// MarshalXMLAttr is used only for struct fields with the
122122
// "attr" option in the field tag.
123123
type MarshalerAttr interface {
124124
MarshalXMLAttr(name Name) (Attr, error)
125125
}
126126

127-
// MarshalIndent works like Marshal, but each XML element begins on a new
127+
// MarshalIndent works like [Marshal], but each XML element begins on a new
128128
// indented line that starts with prefix and is followed by one or more
129129
// copies of indent according to the nesting depth.
130130
func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
@@ -162,10 +162,10 @@ func (enc *Encoder) Indent(prefix, indent string) {
162162

163163
// Encode writes the XML encoding of v to the stream.
164164
//
165-
// See the documentation for Marshal for details about the conversion
165+
// See the documentation for [Marshal] for details about the conversion
166166
// of Go values to XML.
167167
//
168-
// Encode calls Flush before returning.
168+
// Encode calls [Encoder.Flush] before returning.
169169
func (enc *Encoder) Encode(v any) error {
170170
err := enc.p.marshalValue(reflect.ValueOf(v), nil, nil)
171171
if err != nil {
@@ -177,10 +177,10 @@ func (enc *Encoder) Encode(v any) error {
177177
// EncodeElement writes the XML encoding of v to the stream,
178178
// using start as the outermost tag in the encoding.
179179
//
180-
// See the documentation for Marshal for details about the conversion
180+
// See the documentation for [Marshal] for details about the conversion
181181
// of Go values to XML.
182182
//
183-
// EncodeElement calls Flush before returning.
183+
// EncodeElement calls [Encoder.Flush] before returning.
184184
func (enc *Encoder) EncodeElement(v any, start StartElement) error {
185185
err := enc.p.marshalValue(reflect.ValueOf(v), nil, &start)
186186
if err != nil {
@@ -196,16 +196,16 @@ var (
196196
)
197197

198198
// EncodeToken writes the given XML token to the stream.
199-
// It returns an error if StartElement and EndElement tokens are not properly matched.
199+
// It returns an error if [StartElement] and [EndElement] tokens are not properly matched.
200200
//
201-
// EncodeToken does not call Flush, because usually it is part of a larger operation
202-
// such as Encode or EncodeElement (or a custom Marshaler's MarshalXML invoked
201+
// EncodeToken does not call [Encoder.Flush], because usually it is part of a larger operation
202+
// such as [Encoder.Encode] or [Encoder.EncodeElement] (or a custom [Marshaler]'s MarshalXML invoked
203203
// during those), and those will call Flush when finished.
204204
// Callers that create an Encoder and then invoke EncodeToken directly, without
205205
// using Encode or EncodeElement, need to call Flush when finished to ensure
206206
// that the XML is written to the underlying writer.
207207
//
208-
// EncodeToken allows writing a ProcInst with Target set to "xml" only as the first token
208+
// EncodeToken allows writing a [ProcInst] with Target set to "xml" only as the first token
209209
// in the stream.
210210
func (enc *Encoder) EncodeToken(t Token) error {
211211

@@ -303,7 +303,7 @@ func isValidDirective(dir Directive) bool {
303303
}
304304

305305
// Flush flushes any buffered XML to the underlying writer.
306-
// See the EncodeToken documentation for details about when it is necessary.
306+
// See the [Encoder.EncodeToken] documentation for details about when it is necessary.
307307
func (enc *Encoder) Flush() error {
308308
return enc.p.w.Flush()
309309
}
@@ -586,10 +586,10 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
586586
}
587587
}
588588

589-
// If an xmlname was found, namespace must be overridden.
589+
// If an empty name was found, namespace is overridden with an empty space
590590
if tinfo.xmlname != nil && start.Name.Space == "" &&
591+
tinfo.xmlname.xmlns == "" && tinfo.xmlname.name == "" &&
591592
len(p.elements) != 0 && p.elements[len(p.elements)-1].xmlns != "" {
592-
// Add attr xmlns="" to override the outer tag namespace
593593
start.Attr = append(start.Attr, Attr{Name{Space: "", Local: xmlnsPrefix}, ""})
594594
}
595595

@@ -916,7 +916,7 @@ func (p *printer) marshalSimple(typ reflect.Type, val reflect.Value) (string, []
916916
// [...]byte
917917
var bytes []byte
918918
if val.CanAddr() {
919-
bytes = val.Slice(0, val.Len()).Bytes()
919+
bytes = val.Bytes()
920920
} else {
921921
bytes = make([]byte, val.Len())
922922
reflect.Copy(reflect.ValueOf(bytes), val)
@@ -1225,7 +1225,7 @@ func (s *parentStack) push(parents []string) error {
12251225
return nil
12261226
}
12271227

1228-
// UnsupportedTypeError is returned when Marshal encounters a type
1228+
// UnsupportedTypeError is returned when [Marshal] encounters a type
12291229
// that cannot be converted into XML.
12301230
type UnsupportedTypeError struct {
12311231
Type reflect.Type

read.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
// an XML element is an order-dependent collection of anonymous
2020
// values, while a data structure is an order-independent collection
2121
// of named values.
22-
// See package json for a textual representation more suitable
22+
// See [encoding/json] for a textual representation more suitable
2323
// to data structures.
2424

2525
// Unmarshal parses the XML-encoded data and stores the result in
@@ -100,7 +100,7 @@ import (
100100
// If Unmarshal encounters a field type that implements the Unmarshaler
101101
// interface, Unmarshal calls its UnmarshalXML method to produce the value from
102102
// the XML element. Otherwise, if the value implements
103-
// encoding.TextUnmarshaler, Unmarshal calls that value's UnmarshalText method.
103+
// [encoding.TextUnmarshaler], Unmarshal calls that value's UnmarshalText method.
104104
//
105105
// Unmarshal maps an XML element to a string or []byte by saving the
106106
// concatenation of that element's character data in the string or
@@ -109,7 +109,7 @@ import (
109109
// Unmarshal maps an attribute value to a string or []byte by saving
110110
// the value in the string or slice.
111111
//
112-
// Unmarshal maps an attribute value to an Attr by saving the attribute,
112+
// Unmarshal maps an attribute value to an [Attr] by saving the attribute,
113113
// including its name, in the Attr.
114114
//
115115
// Unmarshal maps an XML element or attribute value to a slice by
@@ -138,16 +138,16 @@ func Unmarshal(data []byte, v any) error {
138138
return NewDecoder(bytes.NewReader(data)).Decode(v)
139139
}
140140

141-
// Decode works like Unmarshal, except it reads the decoder
141+
// Decode works like [Unmarshal], except it reads the decoder
142142
// stream to find the start element.
143143
func (d *Decoder) Decode(v any) error {
144144
return d.DecodeElement(v, nil)
145145
}
146146

147-
// DecodeElement works like Unmarshal except that it takes
147+
// DecodeElement works like [Unmarshal] except that it takes
148148
// a pointer to the start XML element to decode into v.
149149
// It is useful when a client reads some raw XML tokens itself
150-
// but also wants to defer to Unmarshal for some elements.
150+
// but also wants to defer to [Unmarshal] for some elements.
151151
func (d *Decoder) DecodeElement(v any, start *StartElement) error {
152152
val := reflect.ValueOf(v)
153153
if val.Kind() != reflect.Pointer {
@@ -188,7 +188,7 @@ type Unmarshaler interface {
188188
// an XML attribute description of themselves.
189189
//
190190
// UnmarshalXMLAttr decodes a single XML attribute.
191-
// If it returns an error, the outer call to Unmarshal stops and
191+
// If it returns an error, the outer call to [Unmarshal] stops and
192192
// returns that error.
193193
// UnmarshalXMLAttr is used only for struct fields with the
194194
// "attr" option in the field tag.

typeinfo.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,6 @@ func lookupXMLName(typ reflect.Type) (xmlname *fieldInfo) {
255255
return nil
256256
}
257257

258-
func min(a, b int) int {
259-
if a <= b {
260-
return a
261-
}
262-
return b
263-
}
264-
265258
// addFieldInfo adds finfo to tinfo.fields if there are no
266259
// conflicts, or if conflicts arise from previous fields that were
267260
// obtained from deeper embedded structures than finfo. In the latter

vendor/go/src/encoding/xml

Lines changed: 0 additions & 1 deletion
This file was deleted.

vendor/go/src/go.mod

Lines changed: 0 additions & 13 deletions
This file was deleted.

vendor/go/src/go.sum

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)