Skip to content

Nested structs not being Unmarshaled #49

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

Closed
iderdik opened this issue Aug 31, 2016 · 6 comments
Closed

Nested structs not being Unmarshaled #49

iderdik opened this issue Aug 31, 2016 · 6 comments

Comments

@iderdik
Copy link

iderdik commented Aug 31, 2016

Given a struct that contains another struct:

type Address struct {
    City string `jsonapi:"attr,city" json:"city"`
    State string `jsonapi:"attr,state" json:"state"`
}

type AdditionalInfo struct {
    HomeAddress     *Address `jsonapi:"attr,homeaddress" json:"homeaddress"`
        Phone                  *string     `jsonapi:"attr,phone" json:"phone"`
}

When calling UnmarshalPayload with this json:

{
    "homeaddress": {
        "city": "brooklyn",
        "state": "NY"
    },
    "phone": "555-555-5555"
}

I get an error data is not a jsonapi representation of AdditionalInfo. Are nested structs not supported?

@aren55555
Copy link
Contributor

aren55555 commented Aug 31, 2016

Your payload is not valid JSON API; even if it was (see below) I don't believe the nested struct is supported.

{
    "data": {
        "type": "info",
        "attributes": {
            "phone": "555-555-5555",
            "homeaddress": {
                "city": "brooklyn",
                "state": "NY"
            }
        }
    }
}

If you want, please submit a PR adding support for this feature. Please ensure the implementation follow the JSON API spec's Attributes guidelines:

Attributes may contain any valid JSON value.

Complex data structures involving JSON objects and arrays are allowed as attribute values. However, any object that constitutes or is contained in an attribute MUST NOT contain a relationships or links member, as those members are reserved by this specification for future use.

@iderdik
Copy link
Author

iderdik commented Sep 1, 2016

Sorry about that - my actual json was valid jsonAPI, I had just extracted the relevant part of the attributes key.

In any case, I seem to have gotten the functionality I need by declaring the type of HomeAddress to be a map[string]interface{} and handling any conversions between map and struct elsewhere. It's obviously not ideal but it's all I have right now.

I'll leave the feature request open because I still think it would be valuable to have nested struct handling. Perhaps someone else reading this might be able to pick it up and work on it.

@bensullivan
Copy link

+1 for this feature please

@shwoodard
Copy link
Contributor

Dup of #21

@iderdik
Copy link
Author

iderdik commented Sep 22, 2016

#21 is more about embedded anonymous structs. This issue is about structs as a field in a struct.

@msabramo
Copy link
Contributor

See #99 and mrowdy#3, which fixes pointers to struct fields.

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

No branches or pull requests

5 participants