Skip to content

Commit

Permalink
Fixes literal 'None' encoding of some empty Crop fields.
Browse files Browse the repository at this point in the history
Treat all fields the same way then explicitly filter out None fields to protect ourselves from any misunderstandings about how collect treats values of type Option.
  • Loading branch information
tonytw1 committed Nov 29, 2024
1 parent 6b57601 commit c393106
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions cropper/app/lib/CropSpecMetadata.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ trait CropSpecMetadata {
"date" -> crop.date.map(printDateTime),
"width" -> dimensions.width,
"height" -> dimensions.height,
) ++ r.map("aspect-ratio" -> _)
"aspect-ratio" -> r)

val nonEmptyMetadata = metadata.collect {
val nonEmptyMetadata = metadata.filter {
case (_, None) => false
case _ => true
}

val flattenedMetadata = nonEmptyMetadata.collect {
case (key, Some(value)) => key -> value
case (key, value) => key -> value
}.view.mapValues(_.toString).toMap

nonEmptyMetadata
flattenedMetadata
}

def cropSpecFromMetadata(userMetadata: Map[String, String]): Option[CropSpec] = {
Expand Down
2 changes: 1 addition & 1 deletion cropper/test/lib/CropSpecMetadataTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CropSpecMetadataTest extends AnyFunSpec with Matchers with CropSpecMetadat
val metadata = metadataForCrop(crop.copy(specification = withEmptyField, author = None), dimensions)

metadata.get("aspect-ratio") shouldBe None
metadata.get("author") shouldBe Some("None") // TODO this does not look intentional!
metadata.get("author") shouldBe None
}

it("should round trip metadata back to crop spec") {
Expand Down

0 comments on commit c393106

Please sign in to comment.