@@ -17,7 +17,7 @@ import (
1717)
1818
1919const (
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.
8080func 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.
123123type 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.
130130func 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.
169169func (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.
184184func (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.
210210func (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.
307307func (enc * Encoder ) Flush () error {
308308 return enc .p .w .Flush ()
309309}
@@ -455,9 +455,9 @@ func (p *printer) writePrefixAttr(prefix, uri string) {
455455}
456456
457457var (
458- marshalerType = reflect .TypeOf (( * Marshaler )( nil )). Elem ()
459- marshalerAttrType = reflect .TypeOf (( * MarshalerAttr )( nil )). Elem ()
460- textMarshalerType = reflect .TypeOf (( * encoding .TextMarshaler )( nil )). Elem ()
458+ marshalerType = reflect .TypeFor [ Marshaler ] ()
459+ marshalerAttrType = reflect .TypeFor [ MarshalerAttr ] ()
460+ textMarshalerType = reflect .TypeFor [ encoding.TextMarshaler ] ()
461461)
462462
463463// marshalValue writes one or more XML elements representing val.
@@ -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.
12301230type UnsupportedTypeError struct {
12311231 Type reflect.Type
0 commit comments