Golang 编码 xml 不会将空值映射到 nil
Golang encoding xml doesn't map empty value to nil
我想将标签中的空值映射到对象中的空值,例如
我有这个 xml
<Address>
<city>city</city>
<streetNumber></streetNumber>
</Address>
我的类型是
type Address struct{
city string `xml:"city"`
streetNumber *int `xml:"streetNumber"`
}
在这种情况下,streetNumber 是 0,但是当我从 xml 中删除标签 streetNumber 时,值是 nil
是否可以将 streetNumber 标签为空时映射为 nil?
您可能想看看使用自定义封送处理使 streetNumber
成为自己的类型,就像他们在这里使用时间格式所做的那样:
https://www.programming-books.io/essential/go/custom-xml-marshaling-aa8105fe264b4b198647cbc718480ba1
我倾向于 int + Valid bool 组合,但你可以做一个 int 指针。
编辑: 已更新以包含 marshal/unmarshal.
的潜在实施
type Address struct {
City string `xml:"city"`
StreetNumber StreetNumber `xml:"streetNumber"`
}
type StreetNumber struct {
Value int
Valid bool
}
func (s StreetNumber) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// If the address is null/unset/invalid, we serialize an empty string as the value.
v := ""
if s.Valid {
// The address has a value so we convert the integer to string.
v = strconv.Itoa(s.Value)
}
// Encode the string `v` to the XML element.
return e.EncodeElement(v, start)
}
func (s *StreetNumber) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// Assume the value is invalid so we can just shortcut out.
s.Valid = false
s.Value = 0;
var v string
if err := d.DecodeElement(&v, &start); err != nil {
// Forward the error if it failed to decode the raw XML
return err
}
if v == "" {
// The value was empty, but this is just the 3rd null state, not an error.
return nil
}
i, err := strconv.Atoi(v)
if err != nil {
// The element value was not an integer so this is an invalid state -- forward the error.
return err
}
// The street number was a valid integer.
s.Valid = true
s.Value = i
return nil
}
我想将标签中的空值映射到对象中的空值,例如 我有这个 xml
<Address>
<city>city</city>
<streetNumber></streetNumber>
</Address>
我的类型是
type Address struct{
city string `xml:"city"`
streetNumber *int `xml:"streetNumber"`
}
在这种情况下,streetNumber 是 0,但是当我从 xml 中删除标签 streetNumber 时,值是 nil 是否可以将 streetNumber 标签为空时映射为 nil?
您可能想看看使用自定义封送处理使 streetNumber
成为自己的类型,就像他们在这里使用时间格式所做的那样:
https://www.programming-books.io/essential/go/custom-xml-marshaling-aa8105fe264b4b198647cbc718480ba1
我倾向于 int + Valid bool 组合,但你可以做一个 int 指针。
编辑: 已更新以包含 marshal/unmarshal.
的潜在实施type Address struct {
City string `xml:"city"`
StreetNumber StreetNumber `xml:"streetNumber"`
}
type StreetNumber struct {
Value int
Valid bool
}
func (s StreetNumber) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// If the address is null/unset/invalid, we serialize an empty string as the value.
v := ""
if s.Valid {
// The address has a value so we convert the integer to string.
v = strconv.Itoa(s.Value)
}
// Encode the string `v` to the XML element.
return e.EncodeElement(v, start)
}
func (s *StreetNumber) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
// Assume the value is invalid so we can just shortcut out.
s.Valid = false
s.Value = 0;
var v string
if err := d.DecodeElement(&v, &start); err != nil {
// Forward the error if it failed to decode the raw XML
return err
}
if v == "" {
// The value was empty, but this is just the 3rd null state, not an error.
return nil
}
i, err := strconv.Atoi(v)
if err != nil {
// The element value was not an integer so this is an invalid state -- forward the error.
return err
}
// The street number was a valid integer.
s.Valid = true
s.Value = i
return nil
}