Skip to content

Instantly share code, notes, and snippets.

@benweidig
Created October 16, 2020 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benweidig/e0e7c9a26f3805e610c4511a0a15b9e3 to your computer and use it in GitHub Desktop.
Save benweidig/e0e7c9a26f3805e610c4511a0a15b9e3 to your computer and use it in GitHub Desktop.
Tapestry Java Time / JSR310 Type Coercer proposal

Java Time API / JSR310 Type Coercers

Date: 2020-10-16
Author: Ben Weidig

Summary

Add JSR310 / Java Time CoercionTuples to Tapestry.

Goals

With Tapestry 5.5.0 elevating the minimum Java version to 1.8, it should provide CoercionTuples for the new Java Time API (JSR310):

  • Add a CoercionTuple for all relevant types
  • Add specific tuples to reduce possible mismatches

Motivation

Type coercion is deeply ingrained in Tapestry. And therefore it should be as mature and complete as possible for its Java version.

The addition of the Java time API is a well thought-out replacement for the previous limited java.util.Date-/java.util.Calendar-based approach. By adding type coercion for the new types we could encourage developers to use them in their code, instead of working around them missing, or needing to provide their own implementation.

Risks and Assumptions

None to minimal risk is assumed.

For Tapestry itself, no risk can be assumed. Existing type coercers won't be changed, only new ones added.

For user projects a minimal risk of duplicate type coercers exist. Due to the implementation of org.apache.tapestry5.ioc.internal.services.TypeCoercerImpl user type coercers will override the newly provided ones.

Alternatives

Instead of automatically including the type coercers we could lock them behind a feature-flag.

Details of Proposed Changes

The Java Time API has well-defined toString() methods, so we don't need explicit Type.class --> String.class coercers.

Additional type coercers are needed for type-downgrade (LocalDateTime.class --> LocalDate.class), due to possible parsing exception between different String-based output formats.

Following type coerces will be added:

* Year --> Integer
* Integer --> Year

* Month --> Integer
* Integer --> Month
* Month --> String (enum)
* String --> Month (enum)

* String --> YearMonth
* YearMonth --> Year
* YearMonth --> Month

* String --> MonthDay (enum)
* MonthDay --> Month (enum)

* DayOfWeek --> Integer
* Integer --> DayOfWeek
* DayOfWeek --> String
* String --> DayOfWeek

* String --> LocalDate
* LocalDate --> YearMonth
* LocalDate --> MonthDay

* LocalTime --> Long
* Long --> LocalTime
* String --> LocalTime

* String --> LocalDateTime
* LocalDateTime --> LocalDate

* String --> OffsetDateTime
* OffsetDateTime --> OffsetTime

* String --> ZoneId
* String --> ZoneOffset

* String --> ZonedDateTime
* ZonedDateTime --> ZoneId

* Instant --> Long
* Long --> Instant

* Duration --> Long
* Long --> Duration

* String --> Period

Testing

All added functionality will be unit tested.

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