Skip to content

Commit

Permalink
control, codec (feature): Support Scala Native (#3508)
Browse files Browse the repository at this point in the history
- **control (feature): Scala Native support**
- **Fix codec test for Native**
  • Loading branch information
xerial committed Apr 23, 2024
1 parent 77e7729 commit 0d1682f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 21 deletions.
@@ -0,0 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.codec
import java.time.Instant
import java.util.UUID

import wvlet.airframe.surface.Surface

import scala.util.Try

/**
*/
object Compat:
def messageCodecFinder: MessageCodecFinder = MessageCodecFinder.defaultMessageCodecFinder
def platformSpecificCodecs: Map[Surface, MessageCodec[_]] = Map.empty

def codecOfClass(
cl: Class[?],
codecFactory: MessageCodecFactory = MessageCodecFactory.defaultFactoryForJSON
): Option[MessageCodec[_]] = None

private[codec] def parseInstant(s: String): Option[Instant] =
Try(Instant.parse(s)).toOption

def readUUIDFromBytes(data: Array[Byte]): UUID =
throw new IllegalArgumentException("Reading binary UUID is not supported in Scala Native")
Expand Up @@ -23,7 +23,8 @@ object MapConversionTest extends AirSpec {
case class A(id: Int, name: String, key: UUID)

test("convert Map[String, Any] to object") {
val uuid = UUID.randomUUID()
// UUID.randomUUID() is not available in Scala Native
val uuid = UUID.fromString("7692a9a1-2f73-4e7f-b70a-88e74682654d")
val m = Map("id" -> 10, "name" -> "leo", "key" -> uuid)
val codec = MessageCodec.of[A]
val a = codec.fromMap(m)
Expand Down
Expand Up @@ -473,21 +473,25 @@ object PrimitiveCodecTest extends CodecSpec with PropertyCheck {
Map(1 -> "a", "2" -> "b")
)

val msgpack = codec.toMsgPack(input)

val result = codec.unpackMsgPack(msgpack)
result.get shouldBe Seq(
Seq("a", "b"),
Seq(1L, 2L),
Seq(true, false),
Seq(1L, 2L),
Seq(1.0, 2.0),
Seq(1.0, 2.0),
Seq(1L, 2L),
Seq('a'.toLong, 'b'.toLong),
Seq(1L, "a", true),
Map(1 -> "a", "2" -> "b")
)
test("unpack test") {
if (isScalaNative)
pending("Scala Native crashes with this test")

val msgpack = codec.toMsgPack(input)
val result = codec.unpackMsgPack(msgpack)
result.get shouldBe Seq(
Seq("a", "b"),
Seq(1L, 2L),
Seq(true, false),
Seq(1L, 2L),
Seq(1.0, 2.0),
Seq(1.0, 2.0),
Seq(1L, 2L),
Seq('a'.toLong, 'b'.toLong),
Seq(1L, "a", true),
Map(1 -> "a", "2" -> "b")
)
}
}

test("pack throwable object passed as Any") {
Expand Down
Expand Up @@ -108,8 +108,8 @@ class ScalaStandardCodecTest extends CodecSpec {

// Should generate standard Java stack traces
val stackTrace = ge.getStackTrace
if (!isScalaJS) {
// Stacktrace of Scala.js is not the same with JVM
if (isScalaJVM) {
// Stacktrace of Scala.js and Scala Native is not the same with JVM
val errorLoc = stackTrace.find(x => x.getFileName.contains("ScalaStandardCodecTest"))
errorLoc match {
case Some(x) =>
Expand Down
@@ -0,0 +1,20 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package wvlet.airframe.control

/**
*/
object Compat:
def sleep(millis: Long): Unit =
Thread.sleep(millis)
15 changes: 12 additions & 3 deletions build.sbt
Expand Up @@ -170,6 +170,11 @@ val nativeBuildSettings = Seq[Setting[?]](
scalaVersion := SCALA_3,
crossScalaVersions := List(SCALA_3),
coverageEnabled := false
// nativeConfig ~= {
// _.withSourceLevelDebuggingConfig(_.enableAll) // enable generation of debug informations
// .withOptimize(false) // disable Scala Native optimizer
// .withMode(scalanative.build.Mode.debug) // compile using LLVM without optimizations
// }
)

val noPublish = Seq(
Expand Down Expand Up @@ -272,7 +277,9 @@ lazy val nativeProjects: Seq[ProjectReference] = Seq(
json.native,
msgpack.native,
ulid.native,
rx.native
rx.native,
control.native,
codec.native
)

// Integration test projects
Expand Down Expand Up @@ -530,7 +537,7 @@ lazy val config =
.dependsOn(di.jvm, codec.jvm)

lazy val control =
crossProject(JVMPlatform, JSPlatform)
crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("airframe-control"))
.settings(buildSettings)
Expand All @@ -539,6 +546,7 @@ lazy val control =
description := "A library for controlling program flows and retrying"
)
.jsSettings(jsBuildSettings)
.nativeSettings(nativeBuildSettings)
.dependsOn(log, rx)

lazy val ulid =
Expand Down Expand Up @@ -661,7 +669,7 @@ lazy val msgpack =
.dependsOn(log, json)

lazy val codec =
crossProject(JVMPlatform, JSPlatform)
crossProject(JVMPlatform, JSPlatform, NativePlatform)
.crossType(CrossType.Pure)
.in(file("airframe-codec"))
.settings(buildSettings)
Expand All @@ -681,6 +689,7 @@ lazy val codec =
.jsSettings(
jsBuildSettings
)
.nativeSettings(nativeBuildSettings)
.dependsOn(log, surface, msgpack, metrics, json, control, ulid)

lazy val jdbc =
Expand Down

0 comments on commit 0d1682f

Please sign in to comment.