Skip to content

Commit

Permalink
Schema DDL: add Scala 2.12 support (close #288)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzhanunlu authored and chuwy committed Dec 19, 2017
1 parent 3a19f4d commit 0b83d83
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 64 deletions.
22 changes: 17 additions & 5 deletions 0-common/schema-ddl/project/BuildSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,23 @@ object BuildSettings {
organization := "com.snowplowanalytics",
version := "0.6.0",
description := "Set of Abstract Syntax Trees for various DDL and Schema formats",
scalaVersion := "2.10.6",
crossScalaVersions := Seq("2.10.6", "2.11.8"),
scalacOptions := Seq("-deprecation", "-encoding", "utf8",
"-unchecked", "-feature",
"-target:jvm-1.7"),
scalaVersion := "2.12.4",
crossScalaVersions := Seq("2.11.12", "2.12.4"),
scalacOptions := Seq(
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked",
"-Ywarn-unused-import",
"-Ywarn-nullary-unit",
"-Xfatal-warnings",
"-Xlint",
"-language:higherKinds",
"-Ypartial-unification",
"-Xfuture"),
scalacOptions in (Compile, doc) ++= Seq(
"-no-link-warnings" // Suppresses problems with Scaladoc @throws links
),
scalacOptions in Test := Seq("-Yrangepos")
)

Expand Down
26 changes: 18 additions & 8 deletions 0-common/schema-ddl/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,38 @@
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
import sbt._
import Keys._

object Dependencies {

object V {
// Scala
val igluCoreJson4s = "0.2.0"
val scalaz7 = "7.0.8"
val json4s = "3.3.0"
val scalaz7 = "7.0.9"
// Scala (test only)
val specs2 = "2.3.13"
val scalazSpecs2 = "0.2"
val scalaCheck = "1.12.2"
object specs2 {
val _211 = "2.3.13"
val _212 = "4.0.1"
}
val scalaCheck = "1.13.5"
}

object Libraries {
// Scala
val igluCoreJson4s = "com.snowplowanalytics" %% "iglu-core-json4s" % V.igluCoreJson4s
val scalaz7 = "org.scalaz" %% "scalaz-core" % V.scalaz7
val json4sJackson = "org.json4s" %% "json4s-jackson" % V.json4s
// Scala (test only)
val specs2 = "org.specs2" %% "specs2" % V.specs2 % "test"
val scalazSpecs2 = "org.typelevel" %% "scalaz-specs2" % V.scalazSpecs2 % "test"
object specs2 {
val _211 = "org.specs2" %% "specs2-core" % V.specs2._211 % "test"
val _212 = "org.specs2" %% "specs2-core" % V.specs2._212 % "test"
}
val scalaCheck = "org.scalacheck" %% "scalacheck" % V.scalaCheck % "test"
}

def onVersion[A](all: Seq[A] = Seq(), on211: => Seq[A] = Seq(), on212: => Seq[A] = Seq()) =
scalaVersion(v => all ++ (if (v.contains("2.11.")) {
on211
} else {
on212
}))
}
10 changes: 5 additions & 5 deletions 0-common/schema-ddl/project/SchemaDdlBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ object SchemaDdlBuild extends Build {
lazy val project = Project("schema-ddl", file("."))
.settings(buildSettings: _*)
.settings(
libraryDependencies ++= Seq(
libraryDependencies <++= Dependencies.onVersion(
all = Seq(
// Scala
Libraries.igluCoreJson4s,
Libraries.scalaz7,
Libraries.json4sJackson,
// Scala (test only)
Libraries.specs2,
Libraries.scalazSpecs2,
Libraries.scalaCheck
Libraries.scalaCheck),
on211 = Seq(Libraries.specs2._211),
on212 = Seq(Libraries.specs2._212)
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,6 @@ case class Schema(
private[iglu] val allProperties = List(multipleOf, minimum, maximum, maxLength, minLength,
pattern, format, items, additionalItems, minItems, maxItems, properties,
additionalProperties, required, patternProperties, `type`, enum, oneOf)

/**
* Concise representation of Schema object
*/
override def toString: String = {
val props = allProperties.flatten.map { p =>
s"${p.keyName} = ${p.toString}"
}

s"Schema(${props.mkString(", ")})"
}

/**
* Check if this Schema is all-permissive `{}` (doesn't contain any rules)
*
* @return true if Schema is empty
*/
def isEmpty: Boolean =
allProperties.flatten.isEmpty
}

object Schema {
Expand All @@ -100,13 +81,4 @@ object Schema {
*/
def normalize[J: FromSchema](schema: Schema): J =
implicitly[FromSchema[J]].normalize(schema)

/**
* Helper method to transform property's class into class key
*/
private def lowerCase(s: String): String = {
val c = s.toCharArray
c(0) = Character.toLowerCase(c(0))
new String(c).takeWhile(_ != '(')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ import org.json4s.jackson.JsonMethods.parse

// specs2
import org.specs2.Specification
import org.specs2.scalaz.ValidationMatchers

class SchemaFlattenerSpec extends Specification with ValidationMatchers { def is = s2"""
class SchemaFlattenerSpec extends Specification { def is = s2"""
Check FlatSchema
split product types $e1
stringify JSON array $e2
Expand All @@ -53,20 +52,20 @@ class SchemaFlattenerSpec extends Specification with ValidationMatchers { def is

def e2 = {
val jValues = List(JInt(3), JNull, JString("str"), JDecimal(3.3), JDouble(3.13), JString("another_str"))
FlatSchema.stringifyArray(jValues) must beSuccessful("3,null,str,3.3,3.13,another_str")
FlatSchema.stringifyArray(jValues).toEither must beRight("3,null,str,3.3,3.13,another_str")
}

def e3 = {
val jValues = List(JInt(3), JNull, JString("str"), JDecimal(3.3), JDouble(3.13), JString("another_str"), JObject(List(("keyOfFatum", JInt(42)))))
FlatSchema.stringifyArray(jValues) must beFailing
FlatSchema.stringifyArray(jValues).toEither must beLeft
}

def e4 = {
implicit val formats = DefaultFormats
val json: JObject = parse(Source.fromURL(getClass.getResource("/schema_with_required_properties.json")).mkString).asInstanceOf[JObject]
val map = json.extract[Map[String, JValue]]

FlatSchema.getRequiredProperties(map) must beSuccessful(List("anotherRequired", "requiredKey"))
FlatSchema.getRequiredProperties(map).toEither must beRight(List("anotherRequired", "requiredKey"))
// getRequiredProperties reverses values ^^^
}

Expand All @@ -80,7 +79,7 @@ class SchemaFlattenerSpec extends Specification with ValidationMatchers { def is
"root.anotherRequired" -> Map("type" -> "string")
)

FlatSchema.processProperties(root) must beSuccessful(FlatSchema.SubSchema(resultMap, Set()))
FlatSchema.processProperties(root).toEither must beRight(FlatSchema.SubSchema(resultMap, Set()))
}

def e6 = {
Expand All @@ -93,13 +92,13 @@ class SchemaFlattenerSpec extends Specification with ValidationMatchers { def is
"root.anotherRequired" -> Map("type" -> "string")
)

FlatSchema.processProperties(root, requiredAccum = Set("root")) must beSuccessful(FlatSchema.SubSchema(resultMap, Set("root.requiredKey", "root.anotherRequired")))
FlatSchema.processProperties(root, requiredAccum = Set("root")).toEither must beRight(FlatSchema.SubSchema(resultMap, Set("root.requiredKey", "root.anotherRequired")))
}

def e7 = {
val schema = parse("""{"type": "object"}""")

FlatSchema.flattenJsonSchema(schema, splitProduct = true) must beSuccessful.like {
FlatSchema.flattenJsonSchema(schema, splitProduct = true).toEither must beRight.like {
case flatSchema => flatSchema must beEqualTo(FlatSchema(ListMap.empty[String, Map[String, String]]))
}
}
Expand All @@ -122,7 +121,7 @@ class SchemaFlattenerSpec extends Specification with ValidationMatchers { def is
|}
""".stripMargin)

FlatSchema.flattenJsonSchema(json, splitProduct = true) must beSuccessful.like {
FlatSchema.flattenJsonSchema(json, splitProduct = true).toEither must beRight.like {
case flatSchema => flatSchema must beEqualTo(FlatSchema(ListMap("nested.object_without_properties" -> Map("type" -> "string"))))
}
}
Expand All @@ -141,7 +140,7 @@ class SchemaFlattenerSpec extends Specification with ValidationMatchers { def is
|}
""".stripMargin)

FlatSchema.flattenJsonSchema(json, splitProduct = true) must beSuccessful.like {
FlatSchema.flattenJsonSchema(json, splitProduct = true).toEither must beRight.like {
case flatSchema => flatSchema must beEqualTo(FlatSchema(ListMap.empty[String, Map[String, String]]))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import org.json4s.jackson.JsonMethods.parse

// specs2
import org.specs2.Specification
import org.specs2.scalaz.ValidationMatchers


import ArrayProperties._
import json4s.Json4sToSchema._

class ArraySpec extends Specification with ValidationMatchers { def is = s2"""
class ArraySpec extends Specification { def is = s2"""
Check JSON Schema string specification
parse correct minItems $e1
parse incorrect (negative) minItems (DECIDE IF THIS DESIRED) $e2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import org.json4s.jackson.JsonMethods.parse

// specs2
import org.specs2.Specification
import org.specs2.scalaz.ValidationMatchers


import json4s.Json4sToSchema._
import StringProperties._

class CommonSpec extends Specification with ValidationMatchers { def is = s2"""
class CommonSpec extends Specification { def is = s2"""
Check JSON Schema common properties
parse string-typed Schema $e1
parse union-typed Schema $e2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import org.json4s.jackson.JsonMethods.parse

// specs2
import org.specs2.Specification
import org.specs2.scalaz.ValidationMatchers


import ObjectProperties._
import json4s.Json4sToSchema._

class ObjectSpec extends Specification with ValidationMatchers { def is = s2"""
class ObjectSpec extends Specification { def is = s2"""
Check JSON Schema object specification
parse object with empty properties $e1
parse object with one property $e2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import org.json4s.jackson.JsonMethods.parse

// specs2
import org.specs2.Specification
import org.specs2.scalaz.ValidationMatchers


import StringProperties._
import json4s.Json4sToSchema._

class StringSpec extends Specification with ValidationMatchers { def is = s2"""
class StringSpec extends Specification { def is = s2"""
Check JSON Schema string specification
parse correct minLength $e1
parse maxLength with ipv4 format $e2
Expand Down

0 comments on commit 0b83d83

Please sign in to comment.