more yaml unmarshalling, this time with multiple target structs for a map

polaris · · 553 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>WARNING: this request may make no sense at all, also my golang syntax may have some errors</p> <p>OK, let&#39;s say I have yaml like this:</p> <pre><code>one: Type: circle Radius: 4 two: Type: square Length: 6 three: Type: square Length: 2 </code></pre> <p>And I would like to end up with a Map[string]Shape, Where Shape is an interface that two classes implement: Square and Circle.</p> <p>Something like this:</p> <pre><code>type Shape interface { Render() bool } type Shapes Map[string]Shape type Circle struct { Radius int `yaml:&#34;Radius&#34;` } func (shape Circle) Render() bool { return true } type Square struct { Length int `yaml:&#34;Length&#34;` } func (shape Square) Render() bool { return true } </code></pre> <p>And I want an unmarshalled result of something like this:</p> <pre><code>Shapes { &#34;one&#34;: Shape( &amp;Circle( Radius:4 ) ) &#34;two&#34;: Shape( &amp;Square( Length:6 ) ) &#34;three&#34;: Shape( &amp;Square( Length:2 ) ) } </code></pre> <p>It occurs to me that I should be able to use an interim handling struct/class, for a marshall method on the map. The interim struct can catch the Type, and then marshall the yaml for just the one map element into the particular class, convert for the interface, and then attach the appropriate struct to the map.</p> <p>I will use comments to iterate some of the approaches I was thinking of.</p> <p>[edit: like 9 or 10 edits just to fix some golang, and get the formatting right]</p> <hr/>**评论:**<br/><br/>jaxxed: <pre><p>It seems that using an interim struct for the purposes of the parsing makes sense.</p> <pre><code>struct YamlShape { Type string `yaml:&#34;Type&#34;` } </code></pre> <p>Initially each element of the Yaml map, can be unmarshalled first into the YamlShape, and then the type can be used create the proper Shape object.</p> <p>I know how to overload the UnMarshall() to get map[string[YamlShapes, but then I don&#39;t know how to get access to the initial Yaml for the Shape? </p></pre>mattyw83: <pre><p>You seem to be in a similar situation to this post I did a while back (in json but the ideas are similar) <a href="http://mattyjwilliams.blogspot.co.uk/2013/01/using-go-to-unmarshal-json-lists-with.html" rel="nofollow">http://mattyjwilliams.blogspot.co.uk/2013/01/using-go-to-unmarshal-json-lists-with.html</a></p> <p>I&#39;d try to start of with very little magic: Just turn the yaml into a map of maps and then iterate over the maps building the shapes as you</p></pre>jaxxed: <pre><p>that seems to make sense. Just unmarshall into interfaces, then read the interface attributes for type.</p> <p>I&#39;ll give it a shot, probably using your first example.</p></pre>mattyw83: <pre><p>Let me know how it goes, happy to take a look at the code</p></pre>

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

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