From 24f19ca8a8ad92d1ec6b97806fe10403256671f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Koz=C5=82owski?= Date: Tue, 12 Jul 2022 01:37:53 +0200 Subject: [PATCH] Fix age-old timestamp bug --- .../main/scala/playground/QueryCompiler.scala | 7 +++++-- .../scalajs/playground/TimestampPlatform.scala | 16 ++++++++++++++++ .../scalajvm/playground/TimestampPlatform.scala | 6 ++++++ .../playground/smithyql/CompilationTests.scala | 11 ++++------- 4 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 core/src/main/scalajs/playground/TimestampPlatform.scala create mode 100644 core/src/main/scalajvm/playground/TimestampPlatform.scala diff --git a/core/src/main/scala/playground/QueryCompiler.scala b/core/src/main/scala/playground/QueryCompiler.scala index 2adbe1c8..8611f1ff 100644 --- a/core/src/main/scala/playground/QueryCompiler.scala +++ b/core/src/main/scala/playground/QueryCompiler.scala @@ -243,8 +243,11 @@ object QueryCompiler extends SchemaVisitor[PartialCompiler] { // the actual serialization format will be used by the client when we eventually use it in the Runner. val format = TimestampFormat.DATE_TIME - Timestamp - .parse(s.value, format) + TimestampPlatform + .fixupTimestamp( + Timestamp + .parse(s.value, format) + ) .toRightIor( CompilationError( InvalidTimestampFormat(format), diff --git a/core/src/main/scalajs/playground/TimestampPlatform.scala b/core/src/main/scalajs/playground/TimestampPlatform.scala new file mode 100644 index 00000000..b7a5af9f --- /dev/null +++ b/core/src/main/scalajs/playground/TimestampPlatform.scala @@ -0,0 +1,16 @@ +package playground + +import smithy4s.Timestamp +import cats.implicits._ + +// js +object TimestampPlatform { + + // Workaround for smithy4s 0.13.x bug + def fixupTimestamp(opt: Option[Timestamp]): Option[Timestamp] = + opt match { + case Some(s) if Either.catchNonFatal(s.toString()).isRight => Some(s) + case _ => None + } + +} diff --git a/core/src/main/scalajvm/playground/TimestampPlatform.scala b/core/src/main/scalajvm/playground/TimestampPlatform.scala new file mode 100644 index 00000000..8db5e62c --- /dev/null +++ b/core/src/main/scalajvm/playground/TimestampPlatform.scala @@ -0,0 +1,6 @@ +package playground + +// jvm +object TimestampPlatform { + def fixupTimestamp[T](t: T): T = t +} diff --git a/core/src/test/scala/playground/smithyql/CompilationTests.scala b/core/src/test/scala/playground/smithyql/CompilationTests.scala index df1b5a5b..72a69096 100644 --- a/core/src/test/scala/playground/smithyql/CompilationTests.scala +++ b/core/src/test/scala/playground/smithyql/CompilationTests.scala @@ -210,14 +210,11 @@ object CompilationTests extends SimpleIOSuite with Checkers { // todo: timestamp parsing always passes on JS but does something weird. // this is fixed in smithy4s 0.14.0 - if (Platform.isJVM) - assert( - result == Ior.leftNec( - CompilationErrorDetails.InvalidTimestampFormat(TimestampFormat.DATE_TIME) - ) + assert( + result == Ior.leftNec( + CompilationErrorDetails.InvalidTimestampFormat(TimestampFormat.DATE_TIME) ) - else - assert(result.toEither.isRight) + ) } pureTest("uuid - OK") {