mirror of https://github.com/golang/go.git
encoding/xml: prevent omitempty from omitting non-nil pointers to empty values
There was an inconsistency between the (json encoding + documentation) and the xml encoding implementation. Pointer to an empty value was not being serialized (i.e simply ignored). Which had the effect of making impossible to have a struct with a string field for which we wanted to serialize the value "" Fixes #5452 Change-Id: Id858701801158409be01e962d2cda843424bd22a Reviewed-on: https://go-review.googlesource.com/15684 Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
347259cbae
commit
daa121167b
|
|
@ -760,14 +760,6 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
|
||||||
}
|
}
|
||||||
vf := finfo.value(val)
|
vf := finfo.value(val)
|
||||||
|
|
||||||
// Dereference or skip nil pointer, interface values.
|
|
||||||
switch vf.Kind() {
|
|
||||||
case reflect.Ptr, reflect.Interface:
|
|
||||||
if !vf.IsNil() {
|
|
||||||
vf = vf.Elem()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch finfo.flags & fMode {
|
switch finfo.flags & fMode {
|
||||||
case fCDATA, fCharData:
|
case fCDATA, fCharData:
|
||||||
emit := EscapeText
|
emit := EscapeText
|
||||||
|
|
@ -800,6 +792,16 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Drill into interfaces and pointers.
|
||||||
|
// This can turn into an infinite loop given a cyclic chain,
|
||||||
|
// but it matches the Go 1 behavior.
|
||||||
|
for vf.Kind() == reflect.Interface || vf.Kind() == reflect.Ptr {
|
||||||
|
if vf.IsNil() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
vf = vf.Elem()
|
||||||
|
}
|
||||||
|
|
||||||
var scratch [64]byte
|
var scratch [64]byte
|
||||||
switch vf.Kind() {
|
switch vf.Kind() {
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||||
|
|
|
||||||
|
|
@ -207,6 +207,7 @@ type OmitAttrTest struct {
|
||||||
Bool bool `xml:",attr,omitempty"`
|
Bool bool `xml:",attr,omitempty"`
|
||||||
Str string `xml:",attr,omitempty"`
|
Str string `xml:",attr,omitempty"`
|
||||||
Bytes []byte `xml:",attr,omitempty"`
|
Bytes []byte `xml:",attr,omitempty"`
|
||||||
|
PStr *string `xml:",attr,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type OmitFieldTest struct {
|
type OmitFieldTest struct {
|
||||||
|
|
@ -217,6 +218,7 @@ type OmitFieldTest struct {
|
||||||
Bool bool `xml:",omitempty"`
|
Bool bool `xml:",omitempty"`
|
||||||
Str string `xml:",omitempty"`
|
Str string `xml:",omitempty"`
|
||||||
Bytes []byte `xml:",omitempty"`
|
Bytes []byte `xml:",omitempty"`
|
||||||
|
PStr *string `xml:",omitempty"`
|
||||||
Ptr *PresenceTest `xml:",omitempty"`
|
Ptr *PresenceTest `xml:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -377,6 +379,7 @@ var (
|
||||||
nameAttr = "Sarah"
|
nameAttr = "Sarah"
|
||||||
ageAttr = uint(12)
|
ageAttr = uint(12)
|
||||||
contentsAttr = "lorem ipsum"
|
contentsAttr = "lorem ipsum"
|
||||||
|
empty = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
// Unless explicitly stated as such (or *Plain), all of the
|
// Unless explicitly stated as such (or *Plain), all of the
|
||||||
|
|
@ -835,9 +838,10 @@ var marshalTests = []struct {
|
||||||
Bool: true,
|
Bool: true,
|
||||||
Str: "str",
|
Str: "str",
|
||||||
Bytes: []byte("byt"),
|
Bytes: []byte("byt"),
|
||||||
|
PStr: &empty,
|
||||||
},
|
},
|
||||||
ExpectXML: `<OmitAttrTest Int="8" int="9" Float="23.5" Uint8="255"` +
|
ExpectXML: `<OmitAttrTest Int="8" int="9" Float="23.5" Uint8="255"` +
|
||||||
` Bool="true" Str="str" Bytes="byt"></OmitAttrTest>`,
|
` Bool="true" Str="str" Bytes="byt" PStr=""></OmitAttrTest>`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Value: &OmitAttrTest{},
|
Value: &OmitAttrTest{},
|
||||||
|
|
@ -868,6 +872,7 @@ var marshalTests = []struct {
|
||||||
Bool: true,
|
Bool: true,
|
||||||
Str: "str",
|
Str: "str",
|
||||||
Bytes: []byte("byt"),
|
Bytes: []byte("byt"),
|
||||||
|
PStr: &empty,
|
||||||
Ptr: &PresenceTest{},
|
Ptr: &PresenceTest{},
|
||||||
},
|
},
|
||||||
ExpectXML: `<OmitFieldTest>` +
|
ExpectXML: `<OmitFieldTest>` +
|
||||||
|
|
@ -878,6 +883,7 @@ var marshalTests = []struct {
|
||||||
`<Bool>true</Bool>` +
|
`<Bool>true</Bool>` +
|
||||||
`<Str>str</Str>` +
|
`<Str>str</Str>` +
|
||||||
`<Bytes>byt</Bytes>` +
|
`<Bytes>byt</Bytes>` +
|
||||||
|
`<PStr></PStr>` +
|
||||||
`<Ptr></Ptr>` +
|
`<Ptr></Ptr>` +
|
||||||
`</OmitFieldTest>`,
|
`</OmitFieldTest>`,
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue