Add preserve_nil_values? to map and struct types #2414
Merged
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.
This PR ensures
preserve_nil_values?is taken into account as a valid option forconstraintsinStructtype. It also add the same feature in theMaptype.Doc is still not correct
As discussed with @Torkan in the discord channel, the PR keeps the current behavior by setting the option to
falseby default. That being said, the current doc is not correct (I copied it from the Struct type) because keys withnilvalue are always stored but the docs says the otherwise.Current behavior:
Behavior with
preserve_nil_values?: trueSuggestion
Update the doc to says that, for not breaking change policy, the keys will
nilvalue are stored as is, but are not taken into account when casting back to Elixir.or
Fix the behavior according to the doc, introducing a breaking change in how the value are stored in the database. I'm not sure if this should be considered a breaking change, given that Ash's map or struct values casted from database will not change, only the values stored in the database will change.
How to consider data that have already been stored?
Ultimately, whatever we do, we may be introducing a breaking change. Indeed, people who already use map and struct have data with keys with
nilvalue stored in the database. If those people add thepreserve_nil_values?: trueoption, the data already in the database will now be taken into account when casting to a map or struct potentially overriding another value.Contributor checklist
Leave anything that you believe does not apply unchecked.