fatih / structs

Utilities for Go structs

Home Page:http://godoc.org/github.com/fatih/structs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Report fields of anonymous fields

keep94 opened this issue · comments

From the documentation of the encoding/json package:

Anonymous struct fields are usually marshaled as if their inner exported fields were fields in the outer struct, subject to the usual Go visibility rules amended as described in the next paragraph...

For my use case it would be really nice if structs.Map worked the same way including all the inner fields of an anonymous struct field instead of the anonymous struct field itself.

I need to slightly tweak the JSON produced by marshaling a certain struct with an anonymous field. My plan is to use structs.Map() to turn my struct into a map[string]interface{} which would let me easily tweak what I need to before marshaling. Trouble is that structs.Map doesn't seem to treat anonymous fields the same way that the json marshaler treats them.

something like this:

type B struct {}
type A struct {
	B  `json:",flatten"`
}

Hi,

Unfortunately, I'm archiving this project and will no longer maintaining it. For more information, please read my blog post: Taking an indefinite sabbatical from my projects.

Thanks for the feedback and contribution.