Skip to content

Commit d3d0907

Browse files
authored
Add module that uses snakeyaml-engine, which only handles yaml 1.2 (#303)
closes #300 closes #301 closes #302
1 parent 9df6d5c commit d3d0907

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1206
-63
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ jobs:
2020
build:
2121
name: Build and Test
2222
strategy:
23+
fail-fast: false
2324
matrix:
2425
os: [ubuntu-latest]
2526
scala: [2.12.15, 2.13.8, 3.2.0]

README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
[![Codecov status](https://codecov.io/gh/circe/circe-yaml/branch/master/graph/badge.svg)](https://codecov.io/gh/circe/circe-yaml)
55
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.circe/circe-yaml_2.12/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.circe/circe-yaml_2.12)
66

7-
This is a small library which translates [SnakeYAML](https://bitbucket.org/snakeyaml/snakeyaml)'s AST into
8-
[circe](https://github.com/circe/circe)'s AST. It enables parsing [YAML](https://yaml.org) 1.1 documents into circe's
9-
`Json` AST.
7+
This is a small library for parsing [YAML](https://yaml.org) into [circe](https://github.com/circe/circe)'s `Json` AST.
8+
* For parsing YAML 1.1 it uses [SnakeYAML](https://bitbucket.org/snakeyaml/snakeyaml).
9+
* For parsing YAML 1.2 it uses [snakeyaml-engine](https://bitbucket.org/snakeyaml/snakeyaml-engine).
1010

1111
## Why?
1212

@@ -22,14 +22,19 @@ the ADT marshalling. You can also use circe's `Encoder` to obtain a `Json`, and
2222

2323
The artifact is hosted by Sonatype, and release versions are synced to Maven Central:
2424

25+
For YAML 1.1
2526
```scala
26-
libraryDependencies += "io.circe" %% "circe-yaml" % "0.14.1"
27+
libraryDependencies += "io.circe" %% "circe-yaml" % "0.14.2"
28+
```
29+
or for YAML 1.2
30+
```scala
31+
libraryDependencies += "io.circe" %% "circe-yaml-v12" % "0.14.2"
2732
```
2833

2934
Snapshot versions are available by adding the Sonatype Snapshots resolver:
3035

3136
```scala
32-
resolvers += Resolver.sonatypeRepo("snapshots")
37+
resolvers ++= Resolver.sonatypeOssRepos("snapshots")
3338
```
3439

3540
### Parsing

build.sbt

Lines changed: 86 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ val Versions = new {
1919
val scalaTest = "3.2.14"
2020
val scalaTestPlus = "3.2.11.0"
2121
val snakeYaml = "1.33"
22+
val snakeYamlEngine = "2.5"
2223
val previousCirceYamls = Set("0.14.0", "0.14.1", "0.14.2")
2324
}
2425

@@ -28,44 +29,92 @@ ThisBuild / crossScalaVersions := Seq("2.12.15", "2.13.8", "3.2.0")
2829

2930
val root = project
3031
.in(file("."))
31-
.enablePlugins(GhpagesPlugin)
32+
// .settings(commonSettings)
3233
.settings(
33-
name := "circe-yaml",
34-
description := "Library for converting between SnakeYAML's AST and circe's AST",
35-
scalacOptions ++= compilerOptions,
36-
scalacOptions ++= {
37-
CrossVersion.partialVersion(scalaVersion.value) match {
38-
case Some((2, v)) if v <= 12 =>
39-
Seq(
40-
"-Xfuture",
41-
"-Yno-adapted-args",
42-
"-Ywarn-unused-import"
43-
)
44-
case _ =>
45-
Seq(
46-
"-Ywarn-unused:imports"
47-
)
48-
}
49-
},
50-
Compile / console / scalacOptions ~= {
51-
_.filterNot(Set("-Ywarn-unused-import", "-Ywarn-unused:imports"))
52-
},
53-
Test / console / scalacOptions ~= {
54-
_.filterNot(Set("-Ywarn-unused-import", "-Ywarn-unused:imports"))
55-
},
56-
libraryDependencies ++= Seq(
57-
"io.circe" %% "circe-core" % Versions.circe,
58-
"io.circe" %% "circe-jawn" % Versions.circe % Test,
59-
"org.yaml" % "snakeyaml" % Versions.snakeYaml,
60-
"io.circe" %% "circe-testing" % Versions.circe % Test,
61-
"org.typelevel" %% "discipline-core" % Versions.discipline % Test,
62-
"org.scalacheck" %% "scalacheck" % Versions.scalaCheck % Test,
63-
"org.scalatest" %% "scalatest" % Versions.scalaTest % Test,
64-
"org.scalatestplus" %% "scalacheck-1-15" % Versions.scalaTestPlus % Test
65-
),
66-
mimaPreviousArtifacts := Versions.previousCirceYamls.map("io.circe" %% "circe-yaml" % _)
34+
name := "circe-yaml-root",
35+
publish / skip := true
36+
)
37+
.aggregate(
38+
`circe-yaml-common`,
39+
`circe-yaml`,
40+
`circe-yaml-v12`
6741
)
68-
.settings(publishSettings ++ docSettings)
42+
// .enablePlugins(GhpagesPlugin)
43+
44+
lazy val `circe-yaml-common` =
45+
project
46+
.in(file("circe-yaml-common"))
47+
.settings(commonSettings)
48+
.settings(
49+
description := "Library for converting between SnakeYAML's AST (YAML 1.1) and circe's AST",
50+
libraryDependencies ++= Seq(
51+
"io.circe" %% "circe-core" % Versions.circe
52+
)
53+
)
54+
.enablePlugins(GhpagesPlugin)
55+
56+
lazy val `circe-yaml` =
57+
project
58+
.in(file("circe-yaml"))
59+
.settings(commonSettings)
60+
.dependsOn(`circe-yaml-common`)
61+
.settings(
62+
description := "Library for converting between SnakeYAML's AST (YAML 1.1) and circe's AST",
63+
libraryDependencies ++= Seq(
64+
"org.yaml" % "snakeyaml" % Versions.snakeYaml,
65+
"io.circe" %% "circe-jawn" % Versions.circe % Test,
66+
"io.circe" %% "circe-testing" % Versions.circe % Test,
67+
"org.typelevel" %% "discipline-core" % Versions.discipline % Test,
68+
"org.scalacheck" %% "scalacheck" % Versions.scalaCheck % Test,
69+
"org.scalatest" %% "scalatest" % Versions.scalaTest % Test,
70+
"org.scalatestplus" %% "scalacheck-1-15" % Versions.scalaTestPlus % Test
71+
),
72+
mimaPreviousArtifacts := Versions.previousCirceYamls.map("io.circe" %% "circe-yaml" % _)
73+
)
74+
.enablePlugins(GhpagesPlugin)
75+
76+
lazy val `circe-yaml-v12` =
77+
project
78+
.in(file("circe-yaml-v12"))
79+
.settings(commonSettings)
80+
.dependsOn(`circe-yaml-common`)
81+
.settings(
82+
description := "Library for converting between snakeyaml-engine's AST (YAML 1.2) and circe's AST",
83+
libraryDependencies ++= Seq(
84+
"io.circe" %% "circe-jawn" % Versions.circe % Test,
85+
"org.snakeyaml" % "snakeyaml-engine" % Versions.snakeYamlEngine,
86+
"io.circe" %% "circe-testing" % Versions.circe % Test,
87+
"org.typelevel" %% "discipline-core" % Versions.discipline % Test,
88+
"org.scalacheck" %% "scalacheck" % Versions.scalaCheck % Test,
89+
"org.scalatest" %% "scalatest" % Versions.scalaTest % Test,
90+
"org.scalatestplus" %% "scalacheck-1-15" % Versions.scalaTestPlus % Test
91+
)
92+
)
93+
.enablePlugins(GhpagesPlugin)
94+
95+
lazy val commonSettings = List(
96+
scalacOptions ++= compilerOptions,
97+
scalacOptions ++= {
98+
CrossVersion.partialVersion(scalaVersion.value) match {
99+
case Some((2, v)) if v <= 12 =>
100+
Seq(
101+
"-Xfuture",
102+
"-Yno-adapted-args",
103+
"-Ywarn-unused-import"
104+
)
105+
case _ =>
106+
Seq(
107+
"-Ywarn-unused:imports"
108+
)
109+
}
110+
},
111+
Compile / console / scalacOptions ~= {
112+
_.filterNot(Set("-Ywarn-unused-import", "-Ywarn-unused:imports"))
113+
},
114+
Test / console / scalacOptions ~= {
115+
_.filterNot(Set("-Ywarn-unused-import", "-Ywarn-unused:imports"))
116+
}
117+
) ++ publishSettings ++ docSettings
69118

70119
lazy val docSettings = Seq(
71120
autoAPIMappings := true,
@@ -116,6 +165,7 @@ lazy val publishSettings = Seq(
116165
ThisBuild / githubWorkflowJavaVersions := Seq("adopt@1.8")
117166
// No auto-publish atm. Remove this line to generate publish stage
118167
ThisBuild / githubWorkflowPublishTargetBranches := Seq.empty
168+
ThisBuild / githubWorkflowBuildMatrixFailFast := Some(false)
119169
ThisBuild / githubWorkflowBuild := Seq(
120170
WorkflowStep.Sbt(
121171
List("clean", "coverage", "test", "coverageReport", "scalastyle", "scalafmtCheckAll"),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.circe.yaml.common
2+
3+
import cats.data.ValidatedNel
4+
import io.circe.{ Decoder, Error, Json, ParsingFailure }
5+
import java.io.Reader
6+
7+
trait Parser extends io.circe.Parser {
8+
9+
/**
10+
* Parse YAML from the given [[Reader]], returning either [[ParsingFailure]] or [[Json]]
11+
*
12+
* @param yaml
13+
* @return
14+
*/
15+
def parse(yaml: Reader): Either[ParsingFailure, Json]
16+
def parseDocuments(yaml: Reader): Stream[Either[ParsingFailure, Json]]
17+
def parseDocuments(yaml: String): Stream[Either[ParsingFailure, Json]]
18+
def decode[A: Decoder](input: Reader): Either[Error, A]
19+
def decodeAccumulating[A: Decoder](input: Reader): ValidatedNel[Error, A]
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.circe.yaml.common
2+
3+
import io.circe.Json
4+
5+
trait Printer {
6+
def pretty(json: Json): String
7+
}
8+
9+
object Printer {
10+
11+
sealed trait FlowStyle
12+
object FlowStyle {
13+
case object Flow extends FlowStyle
14+
case object Block extends FlowStyle
15+
}
16+
17+
sealed trait LineBreak
18+
object LineBreak {
19+
case object Unix extends LineBreak
20+
case object Windows extends LineBreak
21+
case object Mac extends LineBreak
22+
}
23+
24+
sealed trait StringStyle
25+
object StringStyle {
26+
case object Plain extends StringStyle
27+
case object DoubleQuoted extends StringStyle
28+
case object SingleQuoted extends StringStyle
29+
case object Literal extends StringStyle
30+
case object Folded extends StringStyle
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.circe.yaml.v12
2+
3+
import io.circe.yaml.common
4+
import org.snakeyaml.engine.v2.api.LoadSettings
5+
6+
object Parser {
7+
final case class Config(
8+
allowDuplicateKeys: Boolean = false,
9+
allowRecursiveKeys: Boolean = false,
10+
bufferSize: Int = 1024,
11+
label: String = "reader",
12+
maxAliasesForCollections: Int = 50,
13+
parseComments: Boolean = false,
14+
useMarks: Boolean = true
15+
)
16+
17+
def make(config: Config = Config()): common.Parser = {
18+
import config._
19+
new ParserImpl(
20+
LoadSettings.builder
21+
.setAllowDuplicateKeys(allowDuplicateKeys)
22+
.setAllowRecursiveKeys(allowRecursiveKeys)
23+
.setBufferSize(bufferSize)
24+
.setLabel(label)
25+
.setMaxAliasesForCollections(maxAliasesForCollections)
26+
.setParseComments(parseComments)
27+
.setUseMarks(useMarks)
28+
.build
29+
)
30+
}
31+
32+
lazy val default: common.Parser = make()
33+
}

0 commit comments

Comments
 (0)