mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/xml: add support for the omitempty flag
This also changes the behavior of attribute marshalling so that strings and byte slices are marshalled even if empty. The omitempty flag may be used to obtain the previous behavior. Fixes #2899. R=rsc CC=golang-dev https://golang.org/cl/5645050
This commit is contained in:
parent
63975807c9
commit
0a7ad329e1
4 changed files with 124 additions and 19 deletions
|
|
@ -52,6 +52,10 @@ const (
|
|||
// - a field with tag ",comment" is written as an XML comment, not
|
||||
// subject to the usual marshalling procedure. It must not contain
|
||||
// the "--" string within it.
|
||||
// - a field with a tag including the "omitempty" option is omitted
|
||||
// if the field value is empty. The empty values are false, 0, any
|
||||
// nil pointer or interface value, and any array, slice, map, or
|
||||
// string of length zero.
|
||||
//
|
||||
// If a field uses a tag "a>b>c", then the element c will be nested inside
|
||||
// parent elements a and b. Fields that appear next to each other that name
|
||||
|
|
@ -63,6 +67,8 @@ const (
|
|||
// FirstName string `xml:"person>name>first"`
|
||||
// LastName string `xml:"person>name>last"`
|
||||
// Age int `xml:"person>age"`
|
||||
// Height float `xml:"person>height,omitempty"`
|
||||
// Married bool `xml:"person>married"`
|
||||
// }
|
||||
//
|
||||
// xml.Marshal(&Result{Id: 13, FirstName: "John", LastName: "Doe", Age: 42})
|
||||
|
|
@ -76,6 +82,7 @@ const (
|
|||
// <last>Doe</last>
|
||||
// </name>
|
||||
// <age>42</age>
|
||||
// <married>false</married>
|
||||
// </person>
|
||||
// </result>
|
||||
//
|
||||
|
|
@ -116,6 +123,9 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo) error {
|
|||
if !val.IsValid() {
|
||||
return nil
|
||||
}
|
||||
if finfo != nil && finfo.flags&fOmitEmpty != 0 && isEmptyValue(val) {
|
||||
return nil
|
||||
}
|
||||
|
||||
kind := val.Kind()
|
||||
typ := val.Type()
|
||||
|
|
@ -183,12 +193,8 @@ func (p *printer) marshalValue(val reflect.Value, finfo *fieldInfo) error {
|
|||
continue
|
||||
}
|
||||
fv := val.FieldByIndex(finfo.idx)
|
||||
switch fv.Kind() {
|
||||
case reflect.String, reflect.Array, reflect.Slice:
|
||||
// TODO: Should we really do this once ,omitempty is in?
|
||||
if fv.Len() == 0 {
|
||||
continue
|
||||
}
|
||||
if finfo.flags&fOmitEmpty != 0 && isEmptyValue(fv) {
|
||||
continue
|
||||
}
|
||||
p.WriteByte(' ')
|
||||
p.WriteString(finfo.name)
|
||||
|
|
@ -378,3 +384,21 @@ type UnsupportedTypeError struct {
|
|||
func (e *UnsupportedTypeError) Error() string {
|
||||
return "xml: unsupported type: " + e.Type.String()
|
||||
}
|
||||
|
||||
func isEmptyValue(v reflect.Value) bool {
|
||||
switch v.Kind() {
|
||||
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
|
||||
return v.Len() == 0
|
||||
case reflect.Bool:
|
||||
return !v.Bool()
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
return v.Int() == 0
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
|
||||
return v.Uint() == 0
|
||||
case reflect.Float32, reflect.Float64:
|
||||
return v.Float() == 0
|
||||
case reflect.Interface, reflect.Ptr:
|
||||
return v.IsNil()
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue