-
-
Notifications
You must be signed in to change notification settings - Fork 180
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
Flatten child struct to the parent struct #218
Comments
If you embed the location type in your struct it will have the behavior you want (it works the same as the encoding/json package). type User struct {
Id string
*location.Location
} |
You're correct that I can embed the struct, but in this case my JSON structure (and struct API) will also change, which is something I can't have it changed. I need my json API and also my struct API to remain the same with the embedded struct representing the location. =( |
Hmm, I see what you're saying. You can kind of work around it with the "aux pattern", defining custom marshalers/unmarshalers for either dynamo or json. Anyway, I'll think about it a bit. I think the stdlib xml package has something like what you proposed. Writing the boilerplate to flatten things is pretty annoying. |
I see. I will think more about your suggestion, the problem is because I would have to implement custom marshalers/unmarshalers for each struct I have (so many haha). At least the boilerplate is smaller and I can make a script to create these functions for me. If you think something else I'm all ears but of course a native approach would be the best scenario. Thanks. |
Hello again, |
Hello.
It's very common to have structs mapped as columns in the owner's table to avoid complexity in small projects and to be able to create indexes. This is also useful for many other use cases where creating another table brings other problems. I think we could have a way to flatten (idk if this is the right word for this) the child struct to the parent's table automatically using struct tags.
Example:
Would generate the following result in DynamoDB:
Currently I'm doing the above approach to be able to receive in my API the structured structured inside the location field (json) but saving it as individual root fields in dynamodb. Too much boilerplate:
Then I call UpdateLocationFields before saving to DynamoDB. I don't like my own approach but nothing I can do if this feature isn't native supported by this library.
I think the
encoding/json
library can't also do this and it will probably be a difficult thing to do, but it doesn't hurt to ask. Or even if there is a better way to do what I need. Thanks.Thanks!
The text was updated successfully, but these errors were encountered: