Is there a JSON Field Coverage Verification Tool

blov · · 390 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey there,</p> <p>I was wondering if anyone knows of a tool that can take a json string and a struct and verify that the struct has all the fields of the json string. It would be greatly helpful for some integration test suites I&#39;m writing.</p> <p>If not, would anyone be interested in such a tool? I&#39;m newish to go but it seems like it could be done relatively simply with something like <a href="https://github.com/buger/jsonparser" rel="nofollow">jsonparser</a> and <a href="https://github.com/fatih/structs" rel="nofollow">structs</a>.</p> <hr/>**评论:**<br/><br/>circuitously: <pre><p>What are you trying to test exactly? That the JSON contains all the data it should? Though it sounds like you&#39;re trying to validate the strict against the JSON, which seems the wrong way around.</p></pre>GoodKn1ght: <pre><p>The use case is I have a library that uses a 3rd party api. The 3rd party api is still a little unstable in that they continually add more data in the json returned. From time to time I check by hand the json returned is covered by the struct but it&#39;s a bit tedious and I usually forget to do so until someone asks about a missing field in the struct. </p> <p>So I want a sanity check that I can run to ensure I have the fields returned by the api endpoint covered in my struct. </p> <p>In a perfect world I would control both endpoints but in this case I do not and this seems like the most logical way to inform me of changes to the json structure</p></pre>avidal: <pre><p>You could potentially write a test that:</p> <ul> <li>Unmarshals a golden file (last known good json response) into a <code>map[string]interface{}</code></li> <li>Create an instance of your struct, marshal it to JSON, unmarshal into <code>map[string]interface{}</code></li> <li>Iterate over the keys in the golden version plus the struct version and ensure the set of keys are equal</li> </ul> <p>Then, extend it by adding a <code>-update</code> flag to your test functions (of sorts) which, before unmarshaling the &#34;golden&#34; file makes an actual HTTP request to your endpoint, saves the response JSON as the new &#34;golden&#34; file, and then proceeds.</p> <p>Effectively, you would periodically run your test suite with the <code>-update</code> flag to pull down an updated version of the API response, and your normal test runs would verify that your struct fields line up with the API responses.</p></pre>AleD93: <pre><p><a href="http://json-schema.org/implementations.html#libraries" rel="nofollow">http://json-schema.org/implementations.html#libraries</a> ?</p></pre>Redundancy_: <pre><p>Check out <a href="https://github.com/mitchellh/mapstructure" rel="nofollow">https://github.com/mitchellh/mapstructure</a> See ErrorUnused or the Metadata</p></pre>GoodKn1ght: <pre><p>Yes! This is exactly what I was looking for, thanks!</p></pre>mcandre: <pre><p>Yes, many json libs offer a way to mark fields as required.</p></pre>

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

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