Skip to content

Commit

Permalink
encoding/xml: minor changes
Browse files Browse the repository at this point in the history
Changes suggested by Nigel Tao in https://go-review.googlesource.com/#/c/11635
after that had been submitted.

Change-Id: I7b28e1c8488c8565399a8017453dc7ff1fd215e8
Reviewed-on: https://go-review.googlesource.com/11832
Reviewed-by: Nigel Tao <nigeltao@golang.org>
  • Loading branch information
rogpeppe committed Jul 1, 2015
1 parent 1cbbd7f commit 5ae822b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/encoding/xml/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,8 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
// 3. type name
var start StartElement

// explicitNS records whether the element's name
// space has been explicitly set (for example an
// and XMLName field).
// explicitNS records whether the element's name space has been
// explicitly set (for example an XMLName field).
explicitNS := false

if startTemplate != nil {
Expand Down Expand Up @@ -623,11 +622,11 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
if finfo.flags&fAttr == 0 {
continue
}
attr, add, err := p.fieldAttr(finfo, val)
attr, err := p.fieldAttr(finfo, val)
if err != nil {
return err
}
if !add {
if attr.Name.Local == "" {
continue
}
start.Attr = append(start.Attr, attr)
Expand Down Expand Up @@ -671,62 +670,63 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo, startTemplat
return p.cachedWriteError()
}

// fieldAttr returns the attribute of the given field and
// whether it should actually be added as an attribute;
// val holds the value containing the field.
func (p *printer) fieldAttr(finfo *fieldInfo, val reflect.Value) (Attr, bool, error) {
// fieldAttr returns the attribute of the given field.
// If the returned attribute has an empty Name.Local,
// it should not be used.
// The given value holds the value containing the field.
func (p *printer) fieldAttr(finfo *fieldInfo, val reflect.Value) (Attr, error) {
fv := finfo.value(val)
name := Name{Space: finfo.xmlns, Local: finfo.name}
if finfo.flags&fOmitEmpty != 0 && isEmptyValue(fv) {
return Attr{}, false, nil
return Attr{}, nil
}
if fv.Kind() == reflect.Interface && fv.IsNil() {
return Attr{}, false, nil
return Attr{}, nil
}
if fv.CanInterface() && fv.Type().Implements(marshalerAttrType) {
attr, err := fv.Interface().(MarshalerAttr).MarshalXMLAttr(name)
return attr, attr.Name.Local != "", err
return attr, err
}
if fv.CanAddr() {
pv := fv.Addr()
if pv.CanInterface() && pv.Type().Implements(marshalerAttrType) {
attr, err := pv.Interface().(MarshalerAttr).MarshalXMLAttr(name)
return attr, attr.Name.Local != "", err
return attr, err
}
}
if fv.CanInterface() && fv.Type().Implements(textMarshalerType) {
text, err := fv.Interface().(encoding.TextMarshaler).MarshalText()
if err != nil {
return Attr{}, false, err
return Attr{}, err
}
return Attr{name, string(text)}, true, nil
return Attr{name, string(text)}, nil
}
if fv.CanAddr() {
pv := fv.Addr()
if pv.CanInterface() && pv.Type().Implements(textMarshalerType) {
text, err := pv.Interface().(encoding.TextMarshaler).MarshalText()
if err != nil {
return Attr{}, false, err
return Attr{}, err
}
return Attr{name, string(text)}, true, nil
return Attr{name, string(text)}, nil
}
}
// Dereference or skip nil pointer, interface values.
switch fv.Kind() {
case reflect.Ptr, reflect.Interface:
if fv.IsNil() {
return Attr{}, false, nil
return Attr{}, nil
}
fv = fv.Elem()
}
s, b, err := p.marshalSimple(fv.Type(), fv)
if err != nil {
return Attr{}, false, err
return Attr{}, err
}
if b != nil {
s = string(b)
}
return Attr{name, s}, true, nil
return Attr{name, s}, nil
}

// defaultStart returns the default start element to use,
Expand Down

0 comments on commit 5ae822b

Please sign in to comment.