gojson
gojson copied to clipboard
Allow named struct types for nested objects?
This is particularly relevant now that Go 1.4 provides go generate.
Currently, gojson handles nested objects by creating an anonymous struct. For example:
type Repository struct {
ArchiveURL string `json:"archive_url"`
Owner struct {
AvatarURL string `json:"avatar_url"`
} `json:"owner"`
}
This is useful, but sometimes we might want to specify an Owner struct type, and instead have the following:
type Repository struct {
ArchiveURL string `json:"archive_url"`
Owner Owner `json:"owner"`
}
// owner.go
type Owner struct {
AvatarURL string `json:"avatar_url"`
}
And sometimes we might already have the Owner struct defined, but gojson doesn't know that, and will redefine it (anonymously) every time it is used.
Solving the general case may be difficult, but one easy approach for the latter case (the type has already been defined elsewhere) would be to scan the current directory for struct definitions that match the desired structure, and then use those. So in this example, gojson would output the first definition (with the embedded struct) by default, but would use the Owner type if it finds it in the current directory.
An example of the use case is in this Twitter client library: https://github.com/ChimeraCoder/anaconda/blob/master/tweet.go#L30. The User type is quite large, and it would not only be wasteful but inconvenient to have it defined anonymously within the Tweet struct.
This would definitely be nice to have, particularly when a currently embedded type actually shows up multiple times (in different structs). I was wondering if anyone had started taking a stab at this?
@ttacon Thanks for the feedback! I took a stab at this shortly after I posted this issue, but it's still incomplete (and I haven't looked at it recently). If you're interested in working on this, I'd be happy to push what I have in a separate branch if that'd be helpful.
I'm interested in getting this feature out the door; it's just a question of time.
I included this as an option for part of the PR that was merged in. It doesn't give the structs "pretty" names. If you name the struct Foo the sub structs it creates will be named Foo_sub1, Foo_sub2, etc. It will also reuse the same struct if it finds 2 structs with the same layout. For example:
type Repository struct {
Owner Repository_sub1 `json:"owner"`
Contributors []Repository_sub1 `json:"contributors"`
ArchiveURL string `json:"archive_url"`
}
type Repository_sub1 {
UserName string `json:"user_name"`
DisplayName string `json:"display_name"`
AvitarURL string `json:"avitar_url"`
}
The flag for this is -subStruct which defaults to false, but if set true will generate sub structures.
I definitely need to have such feature, for the very reason explained above. Moreover, instead of creating struct Foo as Foo_sub1, Foo_sub2, etc, please add another option to create just one Foo struct, merging all differences from Foo_sub1, Foo_sub2, etc as one. Thanks.
Oh, sorry, re-reading darrennoble's comment again, I think my above comment was wrong (but my idea is right). So, you've already know that Owner corresponding to Repository_sub1 of json:"owner", why can't you use Owner instead of Repository_sub1?
@ChimeraCoder, hope you find some time working on it...
I've prepared a small test sample for your convinient:
https://www.jsoncache.com/json/foo/public/v1/SmartyStreetsAPI https://www.jsoncache.com/json/foo/public/v1/SmartyStreetsAPI?format=pretty