<p dir="ltr">Ahh, I didn't realize it was a conscious choice.</p>
<p dir="ltr">I guess I can see both sides. An explicit tag makes it clear what you intend the name to be, though at the same time I find it confusing, because I expect tags only to be defined for field that change the name. Also, you can typo the tag, but you can't typo no tag. Whatever, as long as we're doing it with a purpose, that's fine.</p>
<p dir="ltr"> </p>
<div class="gmail_quote">On Mar 31, 2015 8:01 PM, "Tim Penhey" <<a href="mailto:tim.penhey@canonical.com">tim.penhey@canonical.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 01/04/15 08:22, Nate Finch wrote:<br>
> FYI:<br>
><br>
> Serializing a struct to json works without having to specify struct tags:<br>
><br>
> type foo struct {<br>
> Name string<br>
> Age int<br>
> }<br>
><br>
> f := foo{ "Bob", 40 }<br>
><br>
> serializes ("marshals") into<br>
><br>
> {<br>
> "Name" : "Bob",<br>
> "Age" : 40<br>
> }<br>
><br>
> No struct tags needed. The only time you need struct tags is if you<br>
> want to change the json name of one of the fields from being the same as<br>
> the field name. This is most commonly done to lowercase the field names<br>
> when interoperating with an existing json service. Note that only<br>
> exported fields can be marshaled/unmarshaled, which is why you often see<br>
> stuff like this:<br>
><br>
> type foo struct {<br>
> Name string `json:"name"`<br>
> Age int `json:"age"`<br>
> }<br>
><br>
> This lets the json package translate from the lowercase json name to the<br>
> uppercase field name (and vice versa). But when we control both ends of<br>
> serialization (like in the api params package), we don't need to<br>
> lowercase anything (in which case we can leave out the struct tags<br>
> entirely).<br>
><br>
> Struct tags that are the same as the name of field are never needed:<br>
><br>
> type SomeResult struct {<br>
> Error *Error `json:"Error"`<br>
> Somethings []Something `json:"Somethings"`<br>
> }<br>
><br>
> The above is totally unnecessary... those fields will already serialize<br>
> into "Error" and "Somethings". There's a bunch of this in the<br>
> apiserver/params package... you don't need it, so don't do it. It just<br>
> causes confusion, because it looks like you're changing the name from<br>
> the default, even though you're not.<br>
<br>
Actually it was William who originally suggested and started enforcing<br>
explicit over implicit with respect to the serialization directives.<br>
<br>
YAML and JSON serialize differently by default, and being explicit means<br>
you don't have to think about it.<br>
<br>
Generally, for our serialization directives there is a preference for<br>
lower case, and I'd recommend this for all new directives. The above<br>
explicit directives were probably added for the explicit nature, but<br>
upper case so it matches works with the older clients.<br>
<br>
Personally I am strongly in favour of explicit over implicit.<br>
<br>
Tim<br>
<br>
</blockquote></div>