Allow map/2 and struct/2 to take subsets of embed fields #4558
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.
Closes #4536
The general idea is to select the fields individually and then combine them in Elixir after they are retrieved from the database. There are more corner cases than I was anticipating. Here are some points for consideration:
json_extract_path
to select the individual fields from the embed assuming this abstraction is meant to work for most databases. If it's not mean to be, then is it better to create a general expression for embed fields and have the adapter convert it?embeds_one
and notembeds_many
because of the way extractions are being done. There is a similar limitation onjson_extract_path
. There is a chance it can be extended toembeds_many
but I am not sure there is a simple operator to extract the same key from every object in a json array. So if we want to keep this we might have to be ok with it only working forembeds_one
map(p, [assoc_field: [:assoc_sub_field])
the:assoc_field
will also be a map. But if it's an embed field it will be a struct. This happens with or without the current PR. I kept the current behaviour but wanted to highlight it in case we want to make these consistent in a different PR. It will probably be a larger change to see why it behaves this way today.