Skip to content

Commit

Permalink
- rename ref -> def, lazy-ref -> ref
Browse files Browse the repository at this point in the history
  • Loading branch information
aonyshchuk committed Nov 27, 2020
1 parent c997885 commit e94bbe9
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AsDraft04Spec extends AnyWordSpec {
Field("foo" , `string`),
Field("meta", `object`.Free[Any]()),
Field("bar" , `integer`, required = false),
Field("baz" , `ref`[Boolean]("my-bool", `boolean`))),
Field("baz" , `def`[Boolean]("my-bool", `boolean`))),
Draft04()) shouldEqual obj(
f"$$schema" -> "http://json-schema.org/draft-04/schema#",
"type" -> "object",
Expand Down Expand Up @@ -204,7 +204,7 @@ class AsDraft04Spec extends AnyWordSpec {

"consider Ref if defined" in {
asDraft04(
`ref`[Boolean](
`def`[Boolean](
"my-bool",
`boolean`)) shouldEqual obj(s"$$ref" -> "#/definitions/my-bool")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AsDraft06Spec extends AnyWordSpec {
val o = `object`(
Field("foo", `string`[String]),
Field("bar", `integer`, required = false),
Field("baz", `ref`[Boolean]("my-bool", `boolean`)))
Field("baz", `def`[Boolean]("my-bool", `boolean`)))

AsValue.schema(o, Draft06(id = "http://example.com/foobarbaz.json")) should containJson(obj(
f"$$schema" -> "http://json-schema.org/draft-06/schema#",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AsDraft07Spec extends AnyWordSpec {
val o = `object`(
Field("foo", `string`[String]),
Field("bar", `integer`, required = false),
Field("baz", `ref`[Boolean]("my-bool", `boolean`)))
Field("baz", `def`[Boolean]("my-bool", `boolean`)))

AsValue.schema(o, Draft07(id = "http://example.com/foobarbaz.json")) should containJson(obj(
f"$$schema" -> "http://json-schema.org/draft-07/schema#",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class DefinitionsSpec extends AnyWordSpec {
val userS = Json.schema[User]

userS shouldBe `object`(
Field("id" , `ref`[UserId]("com.github.andyglow.jsonschema.DefinitionsSpec.UserId", `integer`)),
Field("name", `ref`[UserName]("com.github.andyglow.jsonschema.DefinitionsSpec.UserName", `string`)))
Field("id" , `def`[UserId]("com.github.andyglow.jsonschema.DefinitionsSpec.UserId", `integer`)),
Field("name", `def`[UserName]("com.github.andyglow.jsonschema.DefinitionsSpec.UserName", `string`)))

AsValue.schema(userS, Version.Draft07(id = "users")) shouldBe obj(
f"$$schema" -> "http://json-schema.org/draft-07/schema#",
Expand All @@ -51,8 +51,8 @@ class DefinitionsSpec extends AnyWordSpec {
val userS = Json.schema[User]

userS shouldBe `object`(
Field("id" , `ref`[UserId]("user-id", `integer`)),
Field("name", `ref`[UserName]("user-name", `string`)))
Field("id" , `def`[UserId]("user-id", `integer`)),
Field("name", `def`[UserName]("user-name", `string`)))

AsValue.schema(userS, Version.Draft07(id = "users")) shouldBe obj(
f"$$schema" -> "http://json-schema.org/draft-07/schema#",
Expand All @@ -79,8 +79,8 @@ class DefinitionsSpec extends AnyWordSpec {
val userS = Json.schema[User]

userS shouldBe `object`(
Field("id" , `ref`[UserId]("com.github.andyglow.jsonschema.DefinitionsSpec.UserId", `integer`)),
Field("name", `ref`[UserName]("com.github.andyglow.jsonschema.DefinitionsSpec.UserName", `string`)))
Field("id" , `def`[UserId]("com.github.andyglow.jsonschema.DefinitionsSpec.UserId", `integer`)),
Field("name", `def`[UserName]("com.github.andyglow.jsonschema.DefinitionsSpec.UserName", `string`)))

AsValue.schema(userS, Version.Draft07(id = "users")) shouldBe obj(
f"$$schema" -> "http://json-schema.org/draft-07/schema#",
Expand All @@ -107,8 +107,8 @@ class DefinitionsSpec extends AnyWordSpec {
val userS = Json.schema[User]

userS shouldBe `object`(
Field("id" , `ref`[UserId]("user-id", `integer`)),
Field("name", `ref`[UserName]("user-name", `string`)))
Field("id" , `def`[UserId]("user-id", `integer`)),
Field("name", `def`[UserName]("user-name", `string`)))

AsValue.schema(userS, Version.Draft07(id = "users")) shouldBe obj(
f"$$schema" -> "http://json-schema.org/draft-07/schema#",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,32 @@ class RecursiveTypesSpec extends AnyWordSpec {
"product types. inner references root" in {
import case1._

json.Json.schema[SList] shouldEqual `ref`(
json.Json.schema[SList] shouldEqual `def`(
"com.github.andyglow.jsonschema.RecursiveTypesSpec.case1.SList",
`object`(
Field("head", `string`, required = true),
Field("tail", `lazy-ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case1.SList"), required = false)))
Field("tail", `ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case1.SList"), required = false)))
}

"product types. inner references root. specific ref-name" in {
import case1._

json.Json.schema[SList].toDefinition("given-name") shouldEqual `ref`(
json.Json.schema[SList].toDefinition("given-name") shouldEqual `def`(
"given-name",
`object`(
Field("head", `string`, required = true),
Field("tail", `lazy-ref`("given-name"), required = false)))
Field("tail", `ref`("given-name"), required = false)))
}

"sum types. inner references root" in {
import case2._

json.Json.schema[Node] shouldEqual `ref`(
json.Json.schema[Node] shouldEqual `def`(
"com.github.andyglow.jsonschema.RecursiveTypesSpec.case2.Node",
`oneof`.of(
`object`(
Field("tag", `string`, required = true),
Field("children", `array`(`lazy-ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case2.Node")), required = true)),
Field("children", `array`(`ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case2.Node")), required = true)),
`object`(
Field("content", `string`, required = true))))
}
Expand All @@ -78,11 +78,11 @@ class RecursiveTypesSpec extends AnyWordSpec {

json.Json.schema[User] shouldEqual `object`(
Field("id", `string`, required = true),
Field("friends", `ref`(
Field("friends", `def`(
"com.github.andyglow.jsonschema.RecursiveTypesSpec.case3.LList[scala.Predef.String]",
`object`(
Field("head", `string`, required = true),
Field("tail", `lazy-ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case3.LList[scala.Predef.String]"), required = false)))))
Field("tail", `ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case3.LList[scala.Predef.String]"), required = false)))))
}

"inner is explicitly given" in {
Expand All @@ -91,11 +91,11 @@ class RecursiveTypesSpec extends AnyWordSpec {
implicit def llist[T](implicit t: Schema[T]): Schema[LList[T]] = json.Json.schema[LList[T]]
json.Json.schema[User] shouldEqual `object`(
Field("id", `string`, required = true),
Field("friends", `ref`(
Field("friends", `def`(
"com.github.andyglow.jsonschema.RecursiveTypesSpec.case3.LList[scala.Predef.String]",
`object`(
Field("head", `string`, required = true),
Field("tail", `lazy-ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case3.LList[scala.Predef.String]"), required = false)))))
Field("tail", `ref`("com.github.andyglow.jsonschema.RecursiveTypesSpec.case3.LList[scala.Predef.String]"), required = false)))))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class SchemaMacroSpec extends AnyWordSpec {
schema shouldEqual `object`(
Field(
"component",
`ref`[Compo1](
`def`[Compo1](
"com.github.andyglow.jsonschema.SchemaMacroSpec.Compo1",
`string`[Compo1]),
required = true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AsDraft06(val v: Draft06) extends AsValue with AsDraftSupport {

override def buildRef(ref: String): String = s"#$ref"

override def inferDefinition(x: `ref`[_]): (String, obj) = {
override def inferDefinition(x: `def`[_]): (String, obj) = {
val ref = x.sig
ref -> (obj(f"$$id" -> s"#$ref") ++ apply(x.tpe))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AsDraft07(val v: Draft07) extends AsValue with AsDraftSupport {

override def buildRef(ref: String): String = s"#$ref"

override def inferDefinition(x: `ref`[_]): (String, obj) = {
override def inferDefinition(x: `def`[_]): (String, obj) = {
val ref = x.sig
ref -> (obj(f"$$id" -> s"#$ref") ++ apply(x.tpe))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ trait AsDraftSupport {
val (validations, pp) = inferValidations(x)
val specifics = inferSpecifics.lift((pp, x, isRoot)) getOrElse obj()
val base = x match {
case _: `ref`[_] | _: `allof`[_] | _: `oneof`[_] | _: `lazy-ref`[_] => obj()
case `value-class`(x) => obj("type" -> x.jsonType)
case _: `def`[_] | _: `allof`[_] | _: `oneof`[_] | _: `ref`[_] => obj()
case `value-class`(x) => obj("type" -> x.jsonType)
case _ if includeType => obj("type" -> x.jsonType)
case _ => obj()
}
Expand Down Expand Up @@ -104,12 +104,12 @@ trait AsDraftSupport {
obj("not" -> apply(x.tpe, includeType = false, isRoot = false))
}

def mkRef(pp: Option[V.Def[_, _]], x: `ref`[_]): obj = {
def mkRef(pp: Option[V.Def[_, _]], x: `def`[_]): obj = {
val ref = x.sig
obj(f"$$ref" -> buildRef(ref))
}

def mkLazyRef(pp: Option[V.Def[_, _]], x: `lazy-ref`[_]): obj = {
def mkLazyRef(pp: Option[V.Def[_, _]], x: `ref`[_]): obj = {
val ref = x.sig
obj(f"$$ref" -> buildRef(ref))
}
Expand All @@ -128,10 +128,10 @@ trait AsDraftSupport {
case (pp, x: `enum`[_], _) => mkEnum(pp, x)
case (pp, x: `oneof`[_], isRoot) => mkOneOf(pp, x, isRoot)
case (pp, x: `allof`[_], isRoot) => mkAllOf(pp, x, isRoot)
case (pp, x: `not`[_], _) => mkNot(pp, x)
case (pp, x: `ref`[_], _) => mkRef(pp, x)
case (pp, x: `lazy-ref`[_], _) => mkLazyRef(pp, x)
case (pp, x: `value-class`[_, _], _) => mkValueClass(pp, x)
case (pp, x: `not`[_], _) => mkNot(pp, x)
case (pp, x: `def`[_], _) => mkRef(pp, x)
case (pp, x: `ref`[_], _) => mkLazyRef(pp, x)
case (pp, x: `value-class`[_, _], _) => mkValueClass(pp, x)
}

def inferValidations(x: json.Schema[_]): (obj, Option[V.Def[_, _]]) = {
Expand All @@ -148,14 +148,14 @@ trait AsDraftSupport {
(validations, pp)
}

def inferDefinition(x: `ref`[_]): (String, obj) = {
def inferDefinition(x: `def`[_]): (String, obj) = {
val ref = x.sig
ref -> apply(x.tpe, includeType = true, isRoot = false)
}

def inferDefinitions(x: Schema[_]): obj = {
def references(tpe: json.Schema[_]): Seq[`ref`[_]] = tpe match {
case x: `ref`[_] => references(x.tpe) :+ x
def references(tpe: json.Schema[_]): Seq[`def`[_]] = tpe match {
case x: `def`[_] => references(x.tpe) :+ x
case x: `allof`[_] => x.subTypes.toSeq flatMap references
case x: `oneof`[_] => x.subTypes.toSeq flatMap references
case `array`(ref, _) => references(ref)
Expand Down
44 changes: 22 additions & 22 deletions core/src/main/scala/json/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ sealed trait Schema[+T] {
}
def withDescription(x: String): Self = duplicate(description = Some(x))
def withTitle(x: String): Self = duplicate(title = Some(x))
def toDefinition[TT >: T](sig: String): Schema.`ref`[TT] = Schema.`ref`(sig, this)
def toDefinition[TT >: T](sig: String): Schema.`def`[TT] = Schema.`def`(sig, this)
}

object Schema {
Expand Down Expand Up @@ -418,28 +418,28 @@ object Schema {
}

// +------------
// | Ref
// | Def
// +---------------
//
final case class `ref`[T](
final case class `def`[T](
sig: String,
tpe: Schema[_]) extends Schema[T] {
type Self = `ref`[T]
type Self = `def`[T]
override def jsonType: String = s"$$ref"
def mkCopy() = new `ref`[T](sig, tpe)
override def canEqual(that: Any): Boolean = that.isInstanceOf[`ref`[_]]
def mkCopy() = new `def`[T](sig, tpe)
override def canEqual(that: Any): Boolean = that.isInstanceOf[`def`[_]]
override def equals(obj: Any): Boolean = obj match {
case `ref`(s, t) => sig == s && tpe == t && super.equals(obj)
case _ => false
case `def`(s, t) => sig == s && tpe == t && super.equals(obj)
case _ => false
}
override def toString: String = ToString { sb =>
sb append "ref(signature = "
sb append "def(signature = "
sb append sig
sb append ", schema = "
sb append tpe
sb append ")"
}
override def toDefinition[TT >: T](sig: String): `ref`[TT] = {
override def toDefinition[TT >: T](sig: String): `def`[TT] = {
def deepCopy(x: Schema[_]): Schema[_] = {
val y = x match {
case `object`(fields) => new `object`(fields.map { f => f.copy(tpe = deepCopy(f.tpe)) })
Expand All @@ -448,8 +448,8 @@ object Schema {
case `oneof`(ys) => `oneof`(ys map deepCopy)
case `allof`(ys) => `allof`(ys map deepCopy)
case `not`(y) => `not`(deepCopy(y))
case `ref`(s, y) => `ref`(s, deepCopy(y))
case `lazy-ref`(s) if s == sig => `lazy-ref`(sig)
case `def`(s, y) => `def`(s, deepCopy(y))
case `ref`(s) if s == sig => `ref`(sig)
case y => y
}
y withExtraFrom x
Expand All @@ -459,11 +459,11 @@ object Schema {
}
}

final object `ref` {
def apply[T](tpe: Schema[_])(sig: => String): `ref`[T] = tpe match {
case `ref`(originalSig, innerTpe) => `ref`(originalSig, innerTpe)
case `value-class`(innerTpe) => `ref`(sig, innerTpe)
case _ => `ref`(sig, tpe)
final object `def` {
def apply[T](tpe: Schema[_])(sig: => String): `def`[T] = tpe match {
case `def`(originalSig, innerTpe) => `def`(originalSig, innerTpe)
case `value-class`(innerTpe) => `def`(sig, innerTpe)
case _ => `def`(sig, tpe)
}
}

Expand All @@ -486,20 +486,20 @@ object Schema {
sb append tpe
sb append ")"
}
override def toDefinition[TT >: O](sig: String): `ref`[TT] = `ref`[TT](sig, tpe)
override def toDefinition[TT >: O](sig: String): `def`[TT] = `def`[TT](sig, tpe)
}

// +------------
// | LazyRef
// +---------------
// Pseudo member, doesn't have it's own representation in resulted schema
//
final case class `lazy-ref`[T](sig: String) extends Schema[T] {
override type Self = `lazy-ref`[T]
override protected def mkCopy(): `lazy-ref`[T] = `lazy-ref`(sig)
final case class `ref`[T](sig: String) extends Schema[T] {
override type Self = `ref`[T]
override protected def mkCopy(): `ref`[T] = `ref`(sig)
override def jsonType: String = ??? // should never call this. instead calling code should interpret it as `ref`
override def toString: String = ToString { sb =>
sb append "lazy-ref("
sb append "ref("
sb append sig
sb append ")"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class Macroses(val c: blackbox.Context) extends UContext
if (ctx contains tpe) {
val sig = signature(tpe)
ctx.onCycle(tpe)
U.LazyRef(tpe, q"$sig")
U.Ref(tpe, q"$sig")
} else {
implicit def _ctx = ctx
implicit def _specFD = specFD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ private[jsonschema] trait SchemaTypes { this: UContext with UCommons =>
final case class AllOf(tpe: Type, memberSchema: Seq[SchemaType], extra: Extra = Extra()) extends SchemaType { type Self = AllOf; def prefix = q"${N.Schema}.`allof`[$tpe](Set(..${memberSchema map { _.tree }}))"; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final case class Not(tpe: Type, schema: SchemaType, extra: Extra = Extra()) extends SchemaType { type Self = Not; def prefix = q"${N.Schema}.`not`[$tpe](${schema.tree})"; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final case class ValueClass(tpe: Type, innerTpe: Type, schema: SchemaType, extra: Extra = Extra()) extends SchemaType { type Self = ValueClass; def prefix = q"${N.Schema}.`value-class`[$tpe, $innerTpe](${schema.tree})"; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final case class Ref(tpe: Type, sig: Tree, schema: SchemaType, extra: Extra = Extra()) extends SchemaType { type Self = Ref; def prefix = q"${N.Schema}.`ref`[$tpe]($sig, ${schema.tree})"; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final case class LazyRef(tpe: Type, sig: Tree, extra: Extra = Extra()) extends SchemaType { type Self = LazyRef; def prefix = q"${N.Schema}.`lazy-ref`[$tpe]($sig)"; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final case class Def(tpe: Type, sig: Tree, schema: SchemaType, extra: Extra = Extra()) extends SchemaType { type Self = Def; def prefix = q"${N.Schema}.`def`[$tpe]($sig, ${schema.tree})"; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final case class Ref(tpe: Type, sig: Tree, extra: Extra = Extra()) extends SchemaType { type Self = Ref; def prefix = q"${N.Schema}.`ref`[$tpe]($sig)"; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final case class `-from-tree-`(tpe: Type, prefix: Tree, extra: Extra = Extra()) extends SchemaType { type Self = `-from-tree-`; def withExtra(x: SchemaType.Extra) = copy(extra = x) }
final object Obj {
sealed trait Field {
Expand Down Expand Up @@ -104,14 +104,14 @@ private[jsonschema] trait SchemaTypes { this: UContext with UCommons =>
import SchemaType._

val out = in match {
case s: Arr => s.copy(elementSchema = transformSchema(s.elementSchema)(pf))
case s: Dict => s.copy(valueSchema = transformSchema(s.valueSchema)(pf))
case s: OneOf => s.copy(memberSchema = s.memberSchema map { transformSchema(_)(pf) })
case s: AllOf => s.copy(memberSchema = s.memberSchema map { transformSchema(_)(pf) })
case s: Not => s.copy(schema = transformSchema(s.schema)(pf))
case s: Ref => s.copy(schema = transformSchema(s.schema)(pf))
case s: ValueClass => s.copy(schema = transformSchema(s.schema)(pf))
case s: Obj => s.copy(fields = s.fields map { f =>
case s: Arr => s.copy(elementSchema = transformSchema(s.elementSchema)(pf))
case s: Dict => s.copy(valueSchema = transformSchema(s.valueSchema)(pf))
case s: OneOf => s.copy(memberSchema = s.memberSchema map { transformSchema(_)(pf) })
case s: AllOf => s.copy(memberSchema = s.memberSchema map { transformSchema(_)(pf) })
case s: Not => s.copy(schema = transformSchema(s.schema)(pf))
case s: Def => s.copy(schema = transformSchema(s.schema)(pf))
case s: ValueClass => s.copy(schema = transformSchema(s.schema)(pf))
case s: Obj => s.copy(fields = s.fields map { f =>
f.mapSchema(transformSchema(_)(pf))
})
case _ => in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private[jsonschema] trait UCommons extends SchemaTypes with ULogging { this: UCo
val toValue = weakTypeOf[ToValue[_]]
val set = weakTypeOf[Set[_]]
val map = weakTypeOf[Map[_, _]]
val lazyRef = typeOf[json.Schema.`lazy-ref`[_]]
val lazyRef = typeOf[json.Schema.`ref`[_]]
val keyPattern = typeOf[KeyPattern[_]]
val schemaC = typeOf[json.Schema[_]].typeConstructor
val predefC = typeOf[json.schema.Predef[_]].typeConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private[jsonschema] trait UImplicits { this: UContext with UCommons with USignat
lookupSchema match {
case NotFound => gen
case FromPredef(x) => U.`-from-tree-`(tpe, x)
case FromSchema(x) => U.`-from-tree-`(tpe, q"""${N.Schema}.`ref`[$tpe]($x)(${signature(tpe)})""")
case FromSchema(x) => U.`-from-tree-`(tpe, q"""${N.Schema}.`def`[$tpe]($x)(${signature(tpe)})""")
}
}
}
Expand Down
Loading

0 comments on commit e94bbe9

Please sign in to comment.