Skip to content
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

Open
ChimeraCoder opened this issue Dec 29, 2014 · 5 comments
Open

Allow named struct types for nested objects? #14

ChimeraCoder opened this issue Dec 29, 2014 · 5 comments

Comments

@ChimeraCoder
Copy link
Owner

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.

@ttacon
Copy link

ttacon commented May 8, 2015

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?

@ChimeraCoder
Copy link
Owner Author

@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.

@darrennoble
Copy link
Contributor

darrennoble commented Oct 26, 2016

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.

@suntong
Copy link

suntong commented Feb 10, 2017

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?

@suntong
Copy link

suntong commented Feb 10, 2017

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants