Skip to content

map inside struct unmarshalling #184

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
kamilkoduo opened this issue Dec 18, 2019 · 2 comments
Closed

map inside struct unmarshalling #184

kamilkoduo opened this issue Dec 18, 2019 · 2 comments

Comments

@kamilkoduo
Copy link

Please, help me with some workaround

Adresses: #53

A valid JSON API payload:

{
    "data": {
        "type": "cart_item",
        "id": "ci_id",
        "attributes": {
            "count": 1,
            "offer_id": "1",
            "offer_price": 400,
            "offer_title": {
                "en": "x",
                "ru": "f"
            }
        }
    }
}

is not being able to be unmarshalled into the type

type CartItem struct {
	CartItemID string            `jsonapi:"primary,cart_item"`
	OfferID    string            `jsonapi:"attr,offer_id"`
	OfferPrice float64           `jsonapi:"attr,offer_price"`
	OfferTitle map[string]string `jsonapi:"attr,offer_title"`
	Count      uint              `jsonapi:"attr,count"`
}

reporting errors:

data is not a jsonapi representation of '*api.CartItem'

One may respond that maps, as well as nested structures, are not supported by this library, but still, at least some workaround is needed.

@bogatyr285
Copy link

Greetings.
Your issue is a duplication of #53.
The solution is using map[string]interface{} for the representation of 'map' data type.
This happens because under the hood library parses just built-in types like strings, int, pointers, etc. and there is no reason for writing unmarshalling for each data type if a user could use map[string]interface with type assertion.

@kamilkoduo
Copy link
Author

@bogatyr285
Thank you for the response. Your suggestion works, so I am closing the issue

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

2 participants