-
Notifications
You must be signed in to change notification settings - Fork 209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow named struct types for nested objects? #14
Comments
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 |
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 |
@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 |
This is particularly relevant now that Go 1.4 provides
go generate
.Currently, gojson handles nested objects by creating an anonymous struct. For example:
This is useful, but sometimes we might want to specify an
Owner
struct type, and instead have the following: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 theTweet
struct.The text was updated successfully, but these errors were encountered: