Huangyan9188 / gogoprotobuf

Automatically exported from code.google.com/p/gogoprotobuf
Other
0 stars 0 forks source link

anonymous embedded struct should not have a json name #19

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. create embedded struct type like in the example
2. json.Marshal an instance of this type

What is the expected output? What do you see instead?

The expected output should be marshaled as if their inner exported fields were 
fields in the outer struct, as explained in the json documentation. Currently 
the tag `json:"name"` is generated, which produces a nested object in the json 
encoder.

What version of the product are you using? On what operating system?

current master on linux

Original issue reported on code.google.com by unhol...@gmail.com on 24 Jul 2014 at 4:31

GoogleCodeExporter commented 9 years ago
I specifically designed it to work this way, since I wanted the marshaled json 
to be the same whether you are using goprotobuf or gogoprotobuf.
Maybe this is a weak use case.

Maybe you could explain your use case?

I have thought about introducing a gogoprotobuf extension to allow you to 
explicitly rename the json tag, would this help your case?

Original comment by awalterschulze on 25 Jul 2014 at 7:49

GoogleCodeExporter commented 9 years ago
The use case we have is that we emit certain events to an analytics pipeline. 
These events all share some basic dimensions (timestamp, host, service name, 
etc). The analytics pipeline expects a flat json structure and cannot handle 
the nested object for the shared dimensions.

An extension to simply skip the json tag would be very nice.

Original comment by unhol...@gmail.com on 25 Jul 2014 at 8:02

GoogleCodeExporter commented 9 years ago
My idea is to make the extension of type string and allow you to replace name 
in `json:"name"` with anything you want.

I have not investigated the feasibility of this addition, so this is not going 
to happen tomorrow, so I hope that you are ok with implementing a MarshalJSON 
method temporarily?

Original comment by awalterschulze on 25 Jul 2014 at 8:09

GoogleCodeExporter commented 9 years ago
Here is my solution

https://groups.google.com/forum/#!topic/gogoprotobuf/xmFnqAS6MIc

The example test showcases your problem with embed.

Here is the commit

https://code.google.com/p/gogoprotobuf/source/detail?r=8bc2f601db62358fff1a06419
ea19d3e0bf80a73

I hope this helps.

Original comment by awalterschulze on 5 Aug 2014 at 12:03

GoogleCodeExporter commented 9 years ago
awesome, thank you!

Original comment by unhol...@gmail.com on 5 Aug 2014 at 6:12