Support nested objects within attributes when Marshaling#28
Merged
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
uturunku1
suggested changes
Jan 14, 2025
There was a problem hiding this comment.
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.
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"?
Collaborator
Author
There was a problem hiding this comment.
I added a refactor commit that breaks up individual attr/relation field handling
uturunku1
approved these changes
Jan 15, 2025
ghost
approved these changes
Jan 22, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
jsontags 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