Modifying struct XML mappings for unmarshalling at runtime

polaris · · 461 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m grabbing some XML data from a series of very similar, but not identical APIs and then unmarshalling it to golang structs. I basically want to make one struct to hold all the different types of responses. In order to do that, I think I need to modify the xml mappings at runtime. </p> <p>If I were to make separate types for each type of response it would look like:</p> <pre><code>type Response struct { Products []Product `xml:&#34;GetRedProducts&gt;Product&#34;` } type Response2 struct { Products []Product `xml:&#34;GetBlueProducts&gt;Product&#34;` } </code></pre> <p>This is a big pain. All the rest of the code is essentially the same. It&#39;s only the XML mapping that needs to change between different types.</p> <p>I want to do something like:</p> <pre><code>type Response struct { Products []Product Type: &#34;BlueProducts&#34; } </code></pre> <p>And then somehow infer the xml mapping at runtime from the Type. Is this a reasonable thing to be doing at all? Could someone show me how this works? Thanks :)</p> <hr/>**评论:**<br/><br/>tgulacsi: <pre><p>You can write your own unmarshaler using xml.Decoder, or use go1.8&#39;s new property of allowing assignment between same structs with different tags.</p></pre>theholeeric: <pre><p>I did not really want to write my own unmarshaller, but structs in 1.8 sound worth pursuing. TY</p></pre>TUSF: <pre><p>If you only want a single type for both APIs, you could give the type both fields, and during runtime check if one is empty?</p> <pre><code>type Response struct { RedProducts []Product `xml:&#34;GetRedProducts&gt;Product&#34;` BlueProducts []Product `xml:&#34;GetBlueProducts&gt;Product&#34;` } </code></pre> <p>This way you can infer the XML mapping by checking if <code>len(resp.RedProducts)</code> is 0 or not. Honestly, I think the Unmarshaler should have some kind of syntax to deal with multiple fields, but that&#39;s unfortunately not the case (Not as of 1.7.5, and neither does 1.8rc3).</p></pre>jerf: <pre><p>A general point about the decoders in the encoding library: they are quick, convenient decoders for when they work. However, it is simply unavoidable that sometimes you will have to write custom code. That&#39;s not really about Go per se, its just that the format&#39;s are too powerful to be convenient mapped anywhere, so a convenient mapping <strong>must</strong> also oversimplify, and combined with fact people do crazy things with these formats, you sometimes will have to do manual work. </p> <p>This is especially true when you get things like keys in JSON that have different JSON types, depending on the mod of the emitter. Or an array of heterogeneous types. Or tag soup in XML.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

461 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传