Skip to content

Commit

Permalink
Kebs 2.0 cross platform (#389)
Browse files Browse the repository at this point in the history
* Circe, instances, play-json, enum x ScalaJS

* tagged-meta, http4s x ScalaJS

* Missing files

* ScalaJS for http4s and stir

* Some compat changes, Scala 3 releases, version bumps and first Scala Native package.

* ScalaNative for enum & opaque.

* Instances Scala Native

* tagged, taggedMeta native

* Fixin deps for circe & play-json.
  • Loading branch information
luksow authored Sep 13, 2024
1 parent f75647a commit e668595
Show file tree
Hide file tree
Showing 35 changed files with 213 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package pl.iterators.kebs.akkahttp.matchers

import akka.http.scaladsl.server.{PathMatcher1, PathMatchers}
import akka.http.scaladsl.server.PathMatcher1
import pl.iterators.kebs.core.enums.{EnumLike, ValueEnumLike, ValueEnumLikeEntry}
import pl.iterators.kebs.core.instances.InstanceConverter
import pl.iterators.kebs.core.macros.ValueClassLike

trait KebsAkkaHttpMatchers extends PathMatchers {
trait KebsAkkaHttpMatchers extends akka.http.scaladsl.server.PathMatchers {
implicit class SegmentIsomorphism[U](segment: PathMatcher1[U]) {
def as[T](implicit rep: ValueClassLike[T, U]): PathMatcher1[T] = segment.map(rep.apply)
def asValueEnum[T <: ValueEnumLikeEntry[U]](implicit e: ValueEnumLike[U, T]): PathMatcher1[T] = segment.map(e.withValue)
Expand Down
216 changes: 127 additions & 89 deletions build.sbt

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pl.iterators.kebs.instances.TimeInstances
import java.time._

class TimeInstancesTests extends AnyFunSuite with Matchers with KebsCirce with TimeInstances {

private def isScalaJS = System.getProperty("java.vm.name") == "Scala.js"
test("No ValueClassLike implicits derived") {

"implicitly[ValueClassLike[DayOfWeek, Int]]" shouldNot typeCheck
Expand Down Expand Up @@ -268,13 +268,15 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with KebsCirce with T
}

test("ZoneId standard format") {
val encoder = implicitly[Encoder[ZoneId]]
val decoder = implicitly[Decoder[ZoneId]]
val value = "Europe/Warsaw"
val obj = ZoneId.of(value)
if (!isScalaJS) {
val encoder = implicitly[Encoder[ZoneId]]
val decoder = implicitly[Decoder[ZoneId]]
val value = "Europe/Warsaw"
val obj = ZoneId.of(value)

encoder(obj) shouldBe Json.fromString(value)
decoder(Json.fromString(value).hcursor) shouldBe Right(obj)
encoder(obj) shouldBe Json.fromString(value)
decoder(Json.fromString(value).hcursor) shouldBe Right(obj)
}
}

test("ZoneId wrong format exception") {
Expand Down Expand Up @@ -302,13 +304,15 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with KebsCirce with T
}

test("ZonedDateTime standard format") {
val encoder = implicitly[Encoder[ZonedDateTime]]
val decoder = implicitly[Decoder[ZonedDateTime]]
val value = "2011-12-03T10:15:30+01:00[Europe/Warsaw]"
val obj = ZonedDateTime.parse(value)

encoder(obj) shouldBe Json.fromString(value)
decoder(Json.fromString(value).hcursor) shouldBe Right(obj)
if (!isScalaJS) {
val encoder = implicitly[Encoder[ZonedDateTime]]
val decoder = implicitly[Decoder[ZonedDateTime]]
val value = "2011-12-03T10:15:30+01:00[Europe/Warsaw]"
val obj = ZonedDateTime.parse(value)

encoder(obj) shouldBe Json.fromString(value)
decoder(Json.fromString(value).hcursor) shouldBe Right(obj)
}
}

test("ZonedDateTime wrong format exception") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import pl.iterators.kebs.instances.UtilInstances
import java.util.{Currency, Locale, UUID}

class UtilInstancesTests extends AnyFunSuite with Matchers with KebsCirce with UtilInstances {

private def isScalaJS = System.getProperty("java.vm.name") == "Scala.js"
test("No ValueClassLike implicits derived") {

"implicitly[ValueClassLike[Currency, String]]" shouldNot typeCheck
Expand All @@ -21,13 +21,15 @@ class UtilInstancesTests extends AnyFunSuite with Matchers with KebsCirce with U
}

test("Currency standard format") {
val encoder = implicitly[Encoder[Currency]]
val decoder = implicitly[Decoder[Currency]]
val value = "PLN"
val obj = Currency.getInstance(value)
if (!isScalaJS) {
val encoder = implicitly[Encoder[Currency]]
val decoder = implicitly[Decoder[Currency]]
val value = "PLN"
val obj = Currency.getInstance(value)

encoder(obj) shouldBe Json.fromString(value)
decoder(Json.fromString(value).hcursor) shouldBe Right(obj)
encoder(obj) shouldBe Json.fromString(value)
decoder(Json.fromString(value).hcursor) shouldBe Right(obj)
}
}

test("Currency wrong format exception") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import InstanceConverter.DecodeErrorException
import java.time._

class TimeInstancesTests extends AnyFunSuite with Matchers with TimeInstances {
private def isScalaJS = System.getProperty("java.vm.name") == "Scala.js"
private def isScalaNative = System.getProperty("java.vm.name") == "Scala Native"

test("DayOfWeek to Int") {
val ico = implicitly[InstanceConverter[DayOfWeek, Int]]
Expand Down Expand Up @@ -219,12 +221,14 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with TimeInstances {
}

test("ZoneId to String") {
val ico = implicitly[InstanceConverter[ZoneId, String]]
val value = "Europe/Warsaw"
val obj = ZoneId.of(value)
if (!isScalaJS && !isScalaNative) {
val ico = implicitly[InstanceConverter[ZoneId, String]]
val value = "Europe/Warsaw"
val obj = ZoneId.of(value)

ico.encode(obj) shouldBe value
ico.decode(value) shouldBe obj
ico.encode(obj) shouldBe value
ico.decode(value) shouldBe obj
}
}

test("ZoneId wrong format exception") {
Expand All @@ -251,12 +255,14 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with TimeInstances {
}

test("ZonedDateTime to String") {
val ico = implicitly[InstanceConverter[ZonedDateTime, String]]
val value = "2011-12-03T10:15:30+01:00[Europe/Warsaw]"
val obj = ZonedDateTime.parse(value)

ico.encode(obj) shouldBe value
ico.decode(value) shouldBe obj
if (!isScalaJS && !isScalaNative) {
val ico = implicitly[InstanceConverter[ZonedDateTime, String]]
val value = "2011-12-03T10:15:30+01:00[Europe/Warsaw]"
val obj = ZonedDateTime.parse(value)

ico.encode(obj) shouldBe value
ico.decode(value) shouldBe obj
}
}

test("ZonedDateTime wrong format exception") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import InstanceConverter.DecodeErrorException
import java.util.{Currency, Locale, UUID}

class UtilInstancesTests extends AnyFunSuite with Matchers with UtilInstances {
private def isScalaJS = System.getProperty("java.vm.name") == "Scala.js"
private def isScalaNative = System.getProperty("java.vm.name") == "Scala Native"

test("Currency to String") {
val ico = implicitly[InstanceConverter[Currency, String]]
val value = "PLN"
val obj = Currency.getInstance(value)

ico.encode(obj) shouldBe value
ico.decode(value) shouldBe obj
if (!isScalaJS && !isScalaNative) {
val ico = implicitly[InstanceConverter[Currency, String]]
val value = "PLN"
val obj = Currency.getInstance(value)

ico.encode(obj) shouldBe value
ico.decode(value) shouldBe obj
}
}

test("Currency wrong format exception") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import play.api.libs.json.{Format, JsError, JsNumber, JsString, JsSuccess}
import java.time._

class TimeInstancesTests extends AnyFunSuite with Matchers with TimeInstances {
private def isScalaJS = System.getProperty("java.vm.name") == "Scala.js"
import pl.iterators.kebs.playjson._
test("No ValueClassLike implicits derived") {

Expand Down Expand Up @@ -255,12 +256,14 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with TimeInstances {
}

test("ZoneId standard format") {
val jf = implicitly[Format[ZoneId]]
val value = "Europe/Warsaw"
val obj = ZoneId.of(value)
if (!isScalaJS) {
val jf = implicitly[Format[ZoneId]]
val value = "Europe/Warsaw"
val obj = ZoneId.of(value)

jf.writes(obj) shouldBe JsString(value)
jf.reads(JsString(value)) shouldBe JsSuccess(obj)
jf.writes(obj) shouldBe JsString(value)
jf.reads(JsString(value)) shouldBe JsSuccess(obj)
}
}

test("ZoneId wrong format exception") {
Expand All @@ -287,12 +290,14 @@ class TimeInstancesTests extends AnyFunSuite with Matchers with TimeInstances {
}

test("ZonedDateTime standard format") {
val jf = implicitly[Format[ZonedDateTime]]
val value = "2011-12-03T10:15:30+01:00[Europe/Warsaw]"
val obj = ZonedDateTime.parse(value)

jf.writes(obj) shouldBe JsString(value)
jf.reads(JsString(value)) shouldBe JsSuccess(obj)
if (!isScalaJS) {
val jf = implicitly[Format[ZonedDateTime]]
val value = "2011-12-03T10:15:30+01:00[Europe/Warsaw]"
val obj = ZonedDateTime.parse(value)

jf.writes(obj) shouldBe JsString(value)
jf.reads(JsString(value)) shouldBe JsSuccess(obj)
}
}

test("ZonedDateTime wrong format exception") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import play.api.libs.json.{Format, JsError, JsString, JsSuccess}
import java.util.{Currency, Locale, UUID}

class UtilInstancesTests extends AnyFunSuite with Matchers with UtilInstances {
private def isScalaJS = System.getProperty("java.vm.name") == "Scala.js"
import pl.iterators.kebs.playjson._
test("No ValueClassLike implicits derived") {

Expand All @@ -21,12 +22,14 @@ class UtilInstancesTests extends AnyFunSuite with Matchers with UtilInstances {
}

test("Currency standard format") {
val jf = implicitly[Format[Currency]]
val value = "PLN"
val obj = Currency.getInstance(value)
if (!isScalaJS) {
val jf = implicitly[Format[Currency]]
val value = "PLN"
val obj = Currency.getInstance(value)

jf.writes(obj) shouldBe JsString(value)
jf.reads(JsString(value)) shouldBe JsSuccess(obj)
jf.writes(obj) shouldBe JsString(value)
jf.reads(JsString(value)) shouldBe JsSuccess(obj)
}
}

test("Currency wrong format exception") {
Expand Down
6 changes: 4 additions & 2 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.7")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.12")

addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.3.2")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.16.0")
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.5.4")
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package pl.iterators.kebs.tagged.slick

import pl.iterators.kebs.tagged._
import _root_.slick.jdbc.JdbcProfile
import scala.reflect.ClassTag

trait KebsTaggedSlickSupport { this: JdbcProfile =>
trait KebsTaggedImplicits {
implicit def taggedColumnType[T, U](implicit baseColumnType: BaseColumnType[T]): BaseColumnType[T @@ U] = {
implicit def taggedColumnType[T, U](implicit baseColumnType: BaseColumnType[T], cls: ClassTag[T @@ U]): ColumnType[T @@ U] = {
MappedColumnType.base[T @@ U, T](identity, _.@@[U])
}
}
Expand Down

0 comments on commit e668595

Please sign in to comment.