From 26aa8adc30a84d983d020e34b488ac22a31cb544 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sat, 1 Apr 2017 13:21:15 -0700 Subject: Add yaml parser and docker executor --- .../src/main/scala/io/crashbox/ci/yaml/Yaml.scala | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 crashboxd/src/main/scala/io/crashbox/ci/yaml/Yaml.scala (limited to 'crashboxd/src/main/scala/io/crashbox/ci/yaml/Yaml.scala') diff --git a/crashboxd/src/main/scala/io/crashbox/ci/yaml/Yaml.scala b/crashboxd/src/main/scala/io/crashbox/ci/yaml/Yaml.scala new file mode 100644 index 0000000..0370c76 --- /dev/null +++ b/crashboxd/src/main/scala/io/crashbox/ci/yaml/Yaml.scala @@ -0,0 +1,36 @@ +package io.crashbox.ci +package yaml + +import java.util.{List => JList, Map => JMap} + +import scala.collection.JavaConverters._ + +import org.yaml.snakeyaml.{DumperOptions, Yaml => SYaml} +import org.yaml.snakeyaml.constructor.Constructor +import org.yaml.snakeyaml.representer.Representer +import org.yaml.snakeyaml.resolver.Resolver + +object Yaml { + + private def toYaml(yml: Any): YamlValue = yml match { + case m: JMap[_, _] => + YamlMap(m.asScala.toMap.map { case (k, v) => k.toString -> toYaml(v) }) + case l: JList[_] => YamlSeq(l.asScala.toList.map(toYaml(_))) + case s: String => YamlString(s) + case other => throw new YamlFormatException("Unknown YAML type: " + other) + } + + /** Strict parsing */ + def parse(data: String): YamlValue = { + val resolver = new Resolver { + override def addImplicitResolvers: Unit = {} + } + val yml = new SYaml(new Constructor(), + new Representer(), + new DumperOptions(), + resolver) + val node = yml.load(data) + toYaml(node) + } + +} -- cgit v1.2.3