-
Notifications
You must be signed in to change notification settings - Fork 2
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
Support nested objects within attributes when Marshaling #28
base: main
Are you sure you want to change the base?
Conversation
Unmarshaling an attribute that is a struct or struct pointer that is decorated with jsonapi tags has historically worked as expected, but, curiously, marshaling did not and required the use of `json` tags instead, making struct reuse difficult for both input and output object attributes. Now, you can specify a struct or struct pointer as an attribute and it will be marshaled into the correct keys. Note that this implemenation does not yet support slices of structs or struct pointers.
b04ea2e
to
9333e5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works well! I tested the changes using Michael Yocca's nested object that we discussed in Slack.
I noticed that the visitModelNode function is starting to get super long. So a bit of refactoring around the code that you added within that else
block could help with readability.
response.go
Outdated
strAttr, ok := fieldValue.Interface().(string) | ||
if ok { | ||
node.Attributes[args[1]] = strAttr | ||
isStruct := fieldValue.Type().Kind() == reflect.Struct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this new logic (that starts at this line and ends prior to strAttr, ok := fieldValue.Interface().(string)
line) be extracted into a helper function like "handleStructFieldValues"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a refactor commit that breaks up individual attr/relation field handling
Unmarshaling an attribute that an object or array of objects that is decorated with jsonapi tags has historically worked as expected, but, curiously, marshaling did not and required the use of
json
tags instead, making struct reuse difficult for both input and output object attributes.Now, you can specify a struct, struct pointer, slice of structs, or slice of struct pointers as an attribute and it will be marshaled into the correct keys.
https://hashicorp.atlassian.net/browse/TF-23299