Added rich wrapper for JsField with specialised field getters #64
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.
When writing a custom
JsonFormat
the currently proposed way of using pattern matching/extractors onJsObject.getFields
is elegant and concise especially when all the fields are required.However we often meet the situation where, even though the fields in the domain object are required, we can provide default values for some of these fields, thus making some part of the incoming json optional. For example, a
User
class might require a language and timezone parameters but we we can use the system defaults if the incoming json does not specify these fields.I experimented with multiple options that would make easier the writing of
JsonFormat
s that allow optional fields and I think the best one is to have a rich wrapper forJsObject
that offers specialised getters. If these getters returnEither[String,T]
a for comprehension can be used on theirRight
projection.This also has the added advantage of not polluting the base implementation of
JsObject
and can be used only when facing the optional fields case.The whole thing is demonstrated in the
RichJsObjectSpec
.