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

fix: default case class values memoized #1119

Open
wants to merge 4 commits into
base: series/2.x
Choose a base branch
from

Conversation

Andrapyre
Copy link

@Andrapyre Andrapyre commented May 20, 2024

Description

This PR closes #1055 and, in the process, updates zio-json to the Scala Native 0.5.x series (necessary because this requires multiple PRs to Magnolia and Magnolia is already on the 0.5.x series). The solution does the following things:

  1. It provides out-of-the-box support for random generators which are not time-based, as well as support for all classes that extend java.time.temporal.Temporal - all of the following:

    • HijrahDate
    • Instant,
    • JapaneseDate
    • LocalDate
    • LocalDateTime
    • LocalTime
    • MinguoDate
    • OffsetDateTime
    • OffsetTime
    • ThaiBuddhistDate
    • Year
    • YearMonth
    • ZonedDateTime
  2. It introduces a @jsonAlwaysEvaluateDefault annotation to force evaluation of the default parameter every time json is decoded (especially relevant for timestamp values coming from 3rd party libs, e.g. org.joda.time).

The result is that users can implement the following case class and expect the results for each parameter as described in the comments:

final case class DynamicDefaultExample (
    @jsonAlwaysEvaluateDefault
    jodaInstant: joda.time.Instant = joda.time.Instant.now(), //Will always be fresh, due to annotation
    sometimesMemoizedJodaInstant: joda.time.Instant = joda.time.Instant.now(), //Will be flaky. Without @jsonAlwaysEvaluateDefault, this will sometimes be parsed every time; sometimes it will be memoized.
    javaInstant: java.time.Instant = java.time.Instant.now(), //will always be fresh, with or without annotation
    uuid: UUID = UUID.randomUUID(), //will always be fresh, with or without annotation
    random: Double = scala.math.random() //will always be fresh, with or without annotation
)

External dependencies

Several external dependencies need to be closed before this will build in Github Actions:

  1. The core ZIO libraries need to be upgraded to Scala Native 0.5.x. This can be started once upstream dependencies are upgraded. Publish for Scala native 0.5 zio#8786
  2. The scala-java-time lib needs to be upgraded to Scala Native 0.5.x. Scala Native 0.5.x cquiroz/scala-java-time#508
  3. One PR needs to be merged into the Magnolia Scala 2.x series. [scala2] feat: adding on-demand default evaluation softwaremill/magnolia#533
  4. One PR needs to be merged into the Magnolia Scala 3.x series. feat: adding on-demand default evaluation softwaremill/magnolia#534

/claim #1055

@Andrapyre
Copy link
Author

@jdegoes , while this is waiting for upstream dependencies to upgrade to Scala Native 0.5.x, could you review the current implementation? I've tried to preserve the benefits of Magnolia's pre-computation as much as possible, while still offering support for a wide array of default values that need to be computed in real time.

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

Successfully merging this pull request may close these issues.

Default case class values are memoized
1 participant