英文:
omitempty does not work in encoding/xml when using pointers?
问题
我正在消费一个返回XML的REST API,并尝试对XML进行解组,但出现了似乎omitempty
不起作用的问题。这是一个工作的XML文件示例:
<?xml version='1.0' encoding='UTF-8'?>
<customer uri="/api/customers/339/" id="339">
<name>
<first>Firstname</first>
<last>Lastname</last>
</name>
<email>myemail@example.com</email>
<billing>
<address>
<address1>123 Main St.</address1>
<address2></address2>
<city>Nowhere</city>
<state>IA</state>
<country>USA</country>
<zip>12345</zip>
</address>
</billing>
</customer>
这是一个“错误”的记录示例:
<?xml version='1.0' encoding='UTF-8'?>
<customer uri="/api/customers/6848/" id="6848">
<name>
<first>Firstname</first>
<last>Lastname</last>
</name>
<email/>
<billing/>
</customer>
现在我已经设置好了我的结构体,如下所示:
type Customer struct {
ID int `xml:"id,attr"`
Name *Name `xml:"name,omitempty"`
Billing *Billing `xml:"billing,omitempty"`
}
type Billing struct {
Address *Address `xml:"address,omitempty"`
}
type Address struct {
Address1 string `xml:"address1,omitempty"`
Address2 string `xml:"address2,omitempty"`
City string `xml:"city,omitempty"`
State string `xml:"state,omitempty"`
Country string `xml:"country,omitempty"`
Zip string `xml:"zip,omitempty"`
}
type Name struct {
First string `xml:"first"`
Last string `xml:"last"`
}
阅读所有记录时,当XML遵循第一个示例<billing></billing>
的模式时,它可以正常工作,但当遇到类似<billing/>
的记录时,它会抛出以下错误:panic: runtime error: invalid memory address or nil pointer dereference
有人可以帮助我弄清楚发生了什么以及如何解决吗?
英文:
I am consuming a REST API that returns XML and am trying to Unmarshal the XML and am having issues that appear that the omitempty
is not working. Here is an example of a working XML file:
<?xml version='1.0' encoding='UTF-8'?>
<customer uri="/api/customers/339/" id="339">
<name>
<first>Firstname</first>
<last>Lastname</last>
</name>
<email>myemail@example.com</email>
<billing>
<address>
<address1>123 Main St.</address123>
<address2></address2>
<city>Nowhere</city>
<state>IA</state>
<country>USA</country>
<zip>12345</zip>
</address>
</billing>
</customer>
Here is an example of a "bad" record
<?xml version='1.0' encoding='UTF-8'?>
<customer uri="/api/customers/6848/" id="6848">
<name>
<first>Firstname</first>
<last>Lastname</last>
</name>
<email/>
<billing/>
</customer>
Now I have my structs set up like the following:
type Customer struct {
ID int `xml:"id,attr"`
Name *Name `xml:"name,omitempty"`
Billing *Billing `xml:"billing,omitempty"`
}
type Billing struct {
Address *Address `xml:"address,omitempty"`
}
type Address struct {
address_1 string `xml:",omitempty"`
address_2 string `xml:",omitempty"`
city string `xml:",omitempty"`
postal string `xml:",omitempty"`
country string `xml:",omitempty"`
}
type Name struct {
first, last string
}
Reading through all of the records it works when the XML follows the pattern of the first example <billing></billing>
but when it hits a record that has something like <billing/>
it throws the following error: panic: runtime error: invalid memory address or nil pointer dereference
Can someone help me figure out what's going on and how to resolve it?
答案1
得分: 2
你可能误解了omitempty
的含义。它只在数据进行编组(marshalling)时生效。如果你将<billing/>
反编组到一个带有,omitempty
的指针字段上,它仍然会初始化该字段。然后,由于XML元素为空,Billing
本身的字段将不会被设置。实际上,如果你假设customer.Billing != nil
意味着customer.Billing.Address != nil
,你将会遇到观察到的恐慌。
注意:http://play.golang.org/p/dClkfOVLXh
英文:
You're probably misunderstanding what ,omitempty
means. It takes effect when marshalling data, only. If you unmarshal <billing/>
onto a pointer field with ,omitempty
, it will still initialize the field. Then, since the XML element is empty, the fields of Billing
itself won't be set. In practice, if you assume that customer.Billing != nil
means customer.Billing.Address != nil
, you'll get the observed panic.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论