Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tonytw1
Copy link
Contributor

@tonytw1 tonytw1 commented Oct 7, 2024

What does this change?

CropSpec metadata currently persists Option None as literal string "None" .
This does not look intentional and fails for optional non string values.

ie. The current code round trips CropSpec fields like so:

Some("Hello") -> Some("Hello")
Some(1) -> Some(1)
None -> Some("None")
None[Int] -> Fails with cannot parse "None" as an Int ❌

After this change, the following happens:

Some("Hello") -> Some("Hello")
Some(1) -> Some(1)
None -> None
None[Int] -> None[Int]

  • Pull the serialization method into a new trait for testing.
  • Change the code so that Option None round trips to None. More verbose implementation to spell out the steps.

This change is backwards compatible but makes no attempt to clean the existing data.
ie. optional None Strings which have already been persisted as "None" will continue to be read as Some("None").

This issue was noticed when trying to use an optional non String value in the CropSpec (ie. an Int field).

How should a reviewer test this change?

How can success be measured?

Who should look at this?

Tested? Documented?

  • locally by committer
  • locally by Guardian reviewer
  • on the Guardian's TEST environment
  • relevant documentation added or amended (if needed)

@tonytw1 tonytw1 marked this pull request as ready for review October 7, 2024 13:27
@tonytw1 tonytw1 requested review from a team as code owners October 7, 2024 13:27
@tonytw1 tonytw1 changed the title CropSpec metadata persists Option None as literal string "None" CropSpec metadata can handle optional non String values Oct 8, 2024
@tonytw1 tonytw1 force-pushed the cropspec-metadata-none branch 2 times, most recently from b7562a6 to c393106 Compare November 29, 2024 09:01
@tonytw1
Copy link
Contributor Author

tonytw1 commented Nov 29, 2024

Rebased.

… to handle None inputs to metadata differently!

Not sure if this is intentional.
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.
Copy link
Member

@andrew-nowak andrew-nowak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great! I've got just one suggestion to simplify slightly

Comment on lines +22 to +30
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
val flattenedMetadata = metadata.collect {
case (key, Some(value)) => key -> value.toString
case (key, value) if value != None => key -> value.toString
}
flattenedMetadata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants