Skip to content

Commit

Permalink
Crosscompile for JS and Native with scala-yaml as the backend (#431)
Browse files Browse the repository at this point in the history
* Crosscompile for JS and Native with scala-yaml as the backend

This is rudimentary support for YAML on JS and Native. We could add much more tests etc., and maybe there are some bugs for edge cases, but this should be fine for the beginning.

* tlVersionIntroduced 0.15.2

* tlVersionIntroduced 0.15.2

* Drop 2.12 in circe-yaml-scalayaml

* headerCreate

* Test/headerCreate

* scalafixAll

* import org.virtuslab.yaml.NodeOps

* fix tests

* circe-yaml-common tlVersionIntroduced 0.15.2

* Bring back 2.12

* scala-native 0.4.17

* Explicit crossScalaVersions

* Scala.js 1.16.0

* Drop 2.12

* Simplify, rely on scala-yaml more

* scala-yaml 0.1.0

* Update circe

* fix

* fix

* fix

* fix
  • Loading branch information
sideeffffect authored Jul 11, 2024
1 parent 12d2b56 commit 1c3ad13
Show file tree
Hide file tree
Showing 11 changed files with 594 additions and 23 deletions.
60 changes: 50 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
scala: [2.12, 2.13, 3]
scala: [2.13, 3]
java: [temurin@11, temurin@17]
project: [rootJVM]
project: [rootJS, rootJVM, rootNative]
exclude:
- scala: 2.12
java: temurin@17
- scala: 3
java: temurin@17
- project: rootJS
java: temurin@17
- project: rootNative
java: temurin@17
runs-on: ${{ matrix.os }}
timeout-minutes: 60
steps:
Expand Down Expand Up @@ -82,6 +84,14 @@ jobs:
if: matrix.java == 'temurin@11' && matrix.os == 'ubuntu-latest'
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' 'scalafixAll --check'

- name: scalaJSLink
if: matrix.project == 'rootJS'
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' Test/scalaJSLinkerResult

- name: nativeLink
if: matrix.project == 'rootNative'
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' Test/nativeLink

- name: Test
run: sbt 'project ${{ matrix.project }}' '++ ${{ matrix.scala }}' test

Expand All @@ -95,11 +105,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p circe-yaml-v12/target circe-yaml-common/target circe-yaml/target project/target
run: mkdir -p circe-yaml-v12/target circe-yaml-common/jvm/target circe-yaml-common/native/target circe-yaml-common/js/target circe-yaml/target circe-yaml-scalayaml/jvm/target circe-yaml-scalayaml/js/target circe-yaml-scalayaml/native/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar circe-yaml-v12/target circe-yaml-common/target circe-yaml/target project/target
run: tar cf targets.tar circe-yaml-v12/target circe-yaml-common/jvm/target circe-yaml-common/native/target circe-yaml-common/js/target circe-yaml/target circe-yaml-scalayaml/jvm/target circe-yaml-scalayaml/js/target circe-yaml-scalayaml/native/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down Expand Up @@ -149,12 +159,12 @@ jobs:
if: matrix.java == 'temurin@17' && steps.setup-java-temurin-17.outputs.cache-hit == 'false'
run: sbt +update

- name: Download target directories (2.12, rootJVM)
- name: Download target directories (2.13, rootJS)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-2.12-rootJVM
name: target-${{ matrix.os }}-${{ matrix.java }}-2.13-rootJS

- name: Inflate target directories (2.12, rootJVM)
- name: Inflate target directories (2.13, rootJS)
run: |
tar xf targets.tar
rm targets.tar
Expand All @@ -169,6 +179,26 @@ jobs:
tar xf targets.tar
rm targets.tar
- name: Download target directories (2.13, rootNative)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-2.13-rootNative

- name: Inflate target directories (2.13, rootNative)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (3, rootJS)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3-rootJS

- name: Inflate target directories (3, rootJS)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (3, rootJVM)
uses: actions/download-artifact@v4
with:
Expand All @@ -179,6 +209,16 @@ jobs:
tar xf targets.tar
rm targets.tar
- name: Download target directories (3, rootNative)
uses: actions/download-artifact@v4
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3-rootNative

- name: Inflate target directories (3, rootNative)
run: |
tar xf targets.tar
rm targets.tar
- name: Import signing key
if: env.PGP_SECRET != '' && env.PGP_PASSPHRASE == ''
env:
Expand Down Expand Up @@ -246,5 +286,5 @@ jobs:
- name: Submit Dependencies
uses: scalacenter/sbt-dependency-submission@v2
with:
modules-ignore: rootjs_2.12 rootjs_2.13 rootjs_3 rootjvm_2.12 rootjvm_2.13 rootjvm_3 rootnative_2.12 rootnative_2.13 rootnative_3
modules-ignore: rootjs_2.13 rootjs_3 rootjvm_2.13 rootjvm_3 rootnative_2.13 rootnative_3
configs-ignore: test scala-tool scala-doc-tool test-internal
37 changes: 24 additions & 13 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
ThisBuild / tlBaseVersion := "0.15"
ThisBuild / circeRootOfCodeCoverage := None
ThisBuild / startYear := Some(2016)
ThisBuild / scalafixScalaBinaryVersion := "2.12"
ThisBuild / tlFatalWarnings := false //TODO: ... fix this someday
ThisBuild / githubWorkflowBuildMatrixFailFast := Some(false)

val Versions = new {
val circe = "0.14.7"
val circe = "0.14.9"
val discipline = "1.7.0"
val scalaCheck = "1.18.0"
val scalaTest = "3.2.19"
val scalaTest = "3.2.18"
val scalaTestPlus = "3.2.18.0"
val snakeYaml = "2.2"
val snakeYamlEngine = "2.7"
val previousCirceYamls = Set("0.14.0", "0.14.1", "0.14.2")

val scala212 = "2.12.19"
val scala213 = "2.13.14"
val scala3 = "3.3.3"

val scalaVersions = Seq(scala212, scala213, scala3)
val scalaVersions = Seq(scala213, scala3)
}

ThisBuild / scalaVersion := Versions.scala213
Expand All @@ -28,22 +26,23 @@ ThisBuild / crossScalaVersions := Versions.scalaVersions
val root = tlCrossRootProject.aggregate(
`circe-yaml-common`,
`circe-yaml`,
`circe-yaml-v12`
`circe-yaml-v12`,
`circe-yaml-scalayaml`
)

lazy val `circe-yaml-common` = project
lazy val `circe-yaml-common` = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.in(file("circe-yaml-common"))
.settings(
description := "Library for converting between SnakeYAML's AST (YAML 2.0) and circe's AST",
libraryDependencies ++= Seq(
"io.circe" %% "circe-core" % Versions.circe
"io.circe" %%% "circe-core" % Versions.circe
),
tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "0.14.3").toMap
tlVersionIntroduced := List("2.13", "3").map(_ -> "0.15.3").toMap
)

lazy val `circe-yaml` = project
.in(file("circe-yaml"))
.dependsOn(`circe-yaml-common`)
.dependsOn(`circe-yaml-common`.jvm)
.settings(
description := "Library for converting between SnakeYAML's AST (YAML 2.0) and circe's AST",
libraryDependencies ++= Seq(
Expand All @@ -54,12 +53,13 @@ lazy val `circe-yaml` = project
"org.scalacheck" %% "scalacheck" % Versions.scalaCheck % Test,
"org.scalatest" %% "scalatest" % Versions.scalaTest % Test,
"org.scalatestplus" %% "scalacheck-1-17" % Versions.scalaTestPlus % Test
)
),
tlVersionIntroduced := List("2.13", "3").map(_ -> "0.14.3").toMap
)

lazy val `circe-yaml-v12` = project
.in(file("circe-yaml-v12"))
.dependsOn(`circe-yaml-common`)
.dependsOn(`circe-yaml-common`.jvm)
.settings(
description := "Library for converting between snakeyaml-engine's AST (YAML 2.0) and circe's AST",
libraryDependencies ++= Seq(
Expand All @@ -71,7 +71,18 @@ lazy val `circe-yaml-v12` = project
"org.scalatest" %% "scalatest" % Versions.scalaTest % Test,
"org.scalatestplus" %% "scalacheck-1-17" % Versions.scalaTestPlus % Test
),
tlVersionIntroduced := List("2.12", "2.13", "3").map(_ -> "0.14.3").toMap
tlVersionIntroduced := List("2.13", "3").map(_ -> "0.14.3").toMap
)

lazy val `circe-yaml-scalayaml` = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.dependsOn(`circe-yaml-common`)
.settings(
description := "Library for converting between scala-yaml AST and circe's AST",
libraryDependencies ++= Seq(
"org.virtuslab" %%% "scala-yaml" % "0.1.0",
"org.scalatest" %%% "scalatest" % Versions.scalaTest % Test
),
tlVersionIntroduced := List("2.13", "3").map(_ -> "0.15.3").toMap
)

ThisBuild / developers := List(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2016 circe
*
* 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 io.circe.yaml.scalayaml

import cats.data.ValidatedNel
import cats.syntax.all._
import io.circe
import io.circe.Decoder
import io.circe.Json
import io.circe.ParsingFailure
import org.virtuslab.yaml._

import java.io.Reader
import scala.collection.mutable

object Parser extends io.circe.yaml.common.Parser {

private def readerToString(yaml: Reader): String = {
val buffer = new Array[Char](4 * 1024)
val builder = new mutable.StringBuilder(4 * 1024)
var readBytes = -1
while ({ readBytes = yaml.read(buffer); readBytes } > 0)
builder.appendAll(buffer, 0, readBytes)
builder.result()
}

override def parse(yaml: Reader): Either[ParsingFailure, Json] = {
val string = readerToString(yaml)
parse(string)
}

override def parseDocuments(yaml: Reader): Stream[Either[ParsingFailure, Json]] = {
val string = readerToString(yaml)
val parsed = parse(string)
Stream(parsed)
}

override def parseDocuments(yaml: String): Stream[Either[ParsingFailure, Json]] = {
val parsed = parse(yaml)
Stream(parsed)
}

override def decode[A: Decoder](input: Reader): Either[circe.Error, A] = {
val string = readerToString(input)
val parsed = parse(string)
parsed.flatMap(_.as[A])
}

override def decodeAccumulating[A: Decoder](input: Reader): ValidatedNel[circe.Error, A] =
decode(input).toValidatedNel

override def parse(input: String): Either[ParsingFailure, Json] = {
val node = input.asNode
node match {
case Right(node) => nodeToJson(node)
case Left(error) => Left(errorToFailure(error))
}
}

private def scalarNodeToJson(node: Node.ScalarNode): Either[ParsingFailure, Json] = {
val parsed = YamlDecoder.forAny.construct(node).left.map(errorToFailure)
parsed.flatMap {
case null | None => Json.Null.asRight
case value: String => Json.fromString(value).asRight
case value: Int => Json.fromInt(value).asRight
case double: Double =>
Json.fromDouble(double).toRight(ParsingFailure(s"${node.value} cannot be represented as a JSON number.", null))
case value: Boolean => Json.fromBoolean(value).asRight
case value: Long => Json.fromLong(value).asRight
case float: Float =>
Json.fromFloat(float).toRight(ParsingFailure(s"${node.value} cannot be represented as a JSON number.", null))
case value: BigDecimal => Json.fromBigDecimal(value).asRight
case value: BigInt => Json.fromBigInt(value).asRight
case value: Byte => Json.fromInt(value.toInt).asRight
case value: Short => Json.fromInt(value.toInt).asRight
case value => ParsingFailure(s"Can't map ${value.getClass.getSimpleName} (${node.value}) to JSON.", null).asLeft
}
}

private def nodeToJson(node: Node): Either[ParsingFailure, Json] = node match {
case node: Node.ScalarNode =>
scalarNodeToJson(node)
case Node.SequenceNode(nodes, _) =>
val values = nodes.traverse(nodeToJson)
values.map(Json.fromValues)
case Node.MappingNode(mappings, _) =>
val fields = mappings.toList.traverse {
case (Node.ScalarNode(key, _), value) =>
nodeToJson(value).map(key -> _)
case (node, _) =>
Left(ParsingFailure(s"Unexpected ${node.getClass.getSimpleName} type, expected ScalarNode.", null))
}
fields.map(Json.fromFields)
}

private def errorToFailure(error: YamlError): ParsingFailure =
ParsingFailure(s"Parsing failed: ${error.msg}", error)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2016 circe
*
* 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 io.circe.yaml.scalayaml

import io.circe.Json
import org.virtuslab.yaml.Node
import org.virtuslab.yaml.NodeOps

object Printer extends io.circe.yaml.common.Printer {

override def pretty(json: Json): String = {
val node = jsonToNode(json)
node.asYaml
}

private def jsonToNode(json: Json): Node = json match {
case Json.JArray(value) =>
Node.SequenceNode(value.map(jsonToNode): _*)
case Json.JObject(value) =>
Node.MappingNode(value.toMap.map { case (key, value) => (Node.ScalarNode(key): Node) -> jsonToNode(value) })
case json =>
Node.ScalarNode(json.toString)
}
}
Loading

0 comments on commit 1c3ad13

Please sign in to comment.