playframework / playframework/play-json
updateNullable
Open
Nobody has claimed this yet.
triage
- Dominant language
- Scala
- Stars
- 375
- Forks
- 140
- Avg merge
- 1d 5h
- Merged PRs (30d)
- 28
Description
It would be great if JsPath supported the ability to update a path if it exists. Currently, if you try to define an update on a path that doesn't exist, you'll get an error. Here's a helper that does this:
private implicit class PathUpdateNullable(path: JsPath) {
def updateNullable[A <: JsValue](reads: Reads[A]) =
Reads[JsObject] {
case o: JsObject =>
path.asSingleJson(o) match {
case JsDefined(jsv) =>
val updated = reads.reads(jsv).repath(path)
updated.map { updatedJsv =>
o.deepMerge(JsPath.createObj(path -> updatedJsv))
}
case _ => JsSuccess(o)
}
case _ =>
JsError(JsPath, JsonValidationError("error.expected.jsobject"))
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the JsPath update behavior and compare it with the updateNullable helper shown in the issue. Trace how a missing path is handled and identify the existing JsPath entry points involved. Done means updates succeed when the path exists and leave the object unchanged when it does not.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- scala
- Domain
- backend-api-design
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100