-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CropSpec metadata can handle optional non String values #4347
Open
tonytw1
wants to merge
3
commits into
guardian:main
Choose a base branch
from
eelpie:cropspec-metadata-none
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+119
−42
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lib | ||
|
||
import com.gu.mediaservice.lib.formatting.printDateTime | ||
import com.gu.mediaservice.model.{Bounds, Crop, CropSpec, Dimensions, ExportType} | ||
|
||
trait CropSpecMetadata { | ||
|
||
def metadataForCrop(crop: Crop, dimensions: Dimensions): Map[String, String] = { | ||
val CropSpec(sourceUri, Bounds(x, y, w, h), r, t) = crop.specification | ||
val metadata = Map("source" -> sourceUri, | ||
"bounds-x" -> x, | ||
"bounds-y" -> y, | ||
"bounds-width" -> w, | ||
"bounds-height" -> h, | ||
"type" -> t.name, | ||
"author" -> crop.author, | ||
"date" -> crop.date.map(printDateTime), | ||
"width" -> dimensions.width, | ||
"height" -> dimensions.height, | ||
"aspect-ratio" -> r) | ||
|
||
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 | ||
|
||
flattenedMetadata | ||
} | ||
|
||
def cropSpecFromMetadata(userMetadata: Map[String, String]): Option[CropSpec] = { | ||
for { | ||
source <- userMetadata.get("source") | ||
// we've moved to kebab-case as localstack doesn't like `_` | ||
// fallback to reading old values for older crops | ||
// see https://github.com/localstack/localstack/issues/459 | ||
x <- getOrElseOrNone(userMetadata, "bounds-x", "bounds_x").map(_.toInt) | ||
y <- getOrElseOrNone(userMetadata, "bounds-y", "bounds_y").map(_.toInt) | ||
w <- getOrElseOrNone(userMetadata, "bounds-width", "bounds_w").map(_.toInt) | ||
h <- getOrElseOrNone(userMetadata, "bounds-height", "bounds_h").map(_.toInt) | ||
ratio = getOrElseOrNone(userMetadata, "aspect-ratio", "aspect_ratio") | ||
exportType = userMetadata.get("type").map(ExportType.valueOf).getOrElse(ExportType.default) | ||
} yield { | ||
CropSpec(source, Bounds(x, y, w, h), ratio, exportType) | ||
} | ||
} | ||
|
||
private def getOrElseOrNone(theMap: Map[String, String], preferredKey: String, fallbackKey: String): Option[String] = { | ||
// Return the `preferredKey` value in `theMap` or the `fallbackKey` or `None` | ||
theMap.get(preferredKey).orElse(theMap.get(fallbackKey)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package lib | ||
|
||
import com.gu.mediaservice.model._ | ||
import org.joda.time.DateTime | ||
import org.scalatest.funspec.AnyFunSpec | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
class CropSpecMetadataTest extends AnyFunSpec with Matchers with CropSpecMetadata { | ||
|
||
private val cropSpec = CropSpec( | ||
uri = "/test", | ||
bounds = Bounds(1, 2, 3, 4), aspectRatio = Some("16:9"), `type` = ExportType.default | ||
) | ||
private val crop = Crop( | ||
id = Some("123"), | ||
specification = cropSpec, | ||
author = Some("Tony McCrae"), | ||
date = Some(DateTime.now), | ||
master = None, | ||
assets = List.empty, | ||
) | ||
private val dimensions = Dimensions(640, 480) | ||
|
||
describe("metadata for crop spec") { | ||
it("should serialize crop spec to key value pairs") { | ||
val metadata = metadataForCrop(crop, dimensions) | ||
|
||
metadata("width") shouldBe "640" | ||
metadata("height") shouldBe "480" | ||
metadata.get("aspect-ratio") shouldBe Some("16:9") | ||
metadata.get("author") shouldBe Some("Tony McCrae") | ||
} | ||
|
||
it("should handle empty optional fields") { | ||
val withEmptyField = cropSpec.copy(aspectRatio = None) | ||
|
||
val metadata = metadataForCrop(crop.copy(specification = withEmptyField, author = None), dimensions) | ||
|
||
metadata.get("aspect-ratio") shouldBe None | ||
metadata.get("author") shouldBe None | ||
} | ||
|
||
it("should round trip metadata back to crop spec") { | ||
val metadata = metadataForCrop(crop, dimensions) | ||
|
||
val roundTripped = cropSpecFromMetadata(metadata) | ||
|
||
roundTripped shouldBe Some(cropSpec) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.