Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#241, #242 Capture name of product and enum to schema #246

Merged
merged 19 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 32 additions & 40 deletions tests/shared/src/test/scala-2/zio/schema/DynamicValueGen.scala

Large diffs are not rendered by default.

17 changes: 10 additions & 7 deletions tests/shared/src/test/scala-2/zio/schema/OrderingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ object OrderingSpec extends DefaultRunnableSpec {

def genAnyOrderedPairRecord: Gen[Random with Sized, SchemaAndPair[_]] =
for {
name <- Gen.string(Gen.alphaChar).map(TypeId.parse)
schema <- anyStructure(anyTree(1)).map(fields => {
val fieldSet = fields.foldRight[FieldSet](FieldSet.Empty)((field, acc) => field :*: acc)
Schema.GenericRecord(fieldSet)
Schema.GenericRecord(name, fieldSet)
})
pair <- genOrderedPairRecord(schema)
} yield pair
Expand All @@ -222,12 +223,13 @@ object OrderingSpec extends DefaultRunnableSpec {
equalFields <- genEqualFields(fields, 0, diffInd)
orderedFields <- genOrderedField(fields(diffInd))
randomFields <- genRandomFields(fields, diffInd + 1)
name <- Gen.string(Gen.alphaChar).map(TypeId.parse)
} yield {
val allFields = (equalFields :+ orderedFields) ++ randomFields
val xFields = allFields.map { case (label, value, _) => (label, value) }
val yFields = allFields.map { case (label, _, value) => (label, value) }
val x = DynamicValue.Record(ListMap(xFields: _*)).toTypedValue(schema).toOption.get
val y = DynamicValue.Record(ListMap(yFields: _*)).toTypedValue(schema).toOption.get
val x = DynamicValue.Record(name, ListMap(xFields: _*)).toTypedValue(schema).toOption.get
val y = DynamicValue.Record(name, ListMap(yFields: _*)).toTypedValue(schema).toOption.get
(schema, x, y)
}
}
Expand Down Expand Up @@ -285,8 +287,9 @@ object OrderingSpec extends DefaultRunnableSpec {
for {
(label, caseSchema) <- Gen.elements(schema.structure.toList: _*)
(smallCase, largeCase) <- genOrderedDynamicPair(caseSchema)
small = DynamicValue.Enumeration((label, smallCase)).toTypedValue(schema).toOption.get
large = DynamicValue.Enumeration((label, largeCase)).toTypedValue(schema).toOption.get
id <- Gen.string(Gen.alphaChar).map(TypeId.parse)
small = DynamicValue.Enumeration(id, (label, smallCase)).toTypedValue(schema).toOption.get
large = DynamicValue.Enumeration(id, (label, largeCase)).toTypedValue(schema).toOption.get
} yield (schema, small, large)

def genOrderedPairEnumDiffCase[A](schema: Schema.Enum[A]): Gen[Random with Sized, SchemaAndPair[A]] = {
Expand All @@ -301,10 +304,10 @@ object OrderingSpec extends DefaultRunnableSpec {
} yield (schema, small, large)
}

def genElemOfCase[A, B](enumSchema: Schema[A], label: String, caseSchema: Schema[B]): Gen[Random with Sized, A] =
def genElemOfCase[A, B](enumSchema: Schema.Enum[A], label: String, caseSchema: Schema[B]): Gen[Random with Sized, A] =
genFromSchema(caseSchema).map(b => {
val innerValue = caseSchema.toDynamic(b)
val enumValue = DynamicValue.Enumeration((label, innerValue))
val enumValue = DynamicValue.Enumeration(enumSchema.id, (label, innerValue))
enumValue.toTypedValue(enumSchema).toOption.get
})

Expand Down
14 changes: 13 additions & 1 deletion tests/shared/src/test/scala-2/zio/schema/SchemaAstSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ object SchemaAstSpec extends DefaultRunnableSpec {
test("generic") {
val schema =
Schema.record(
TypeId.parse("zio.schema.SchemaAst.Product"),
Chunk(
Schema.Field("a", Schema[String]),
Schema.Field("b", Schema[Int])
): _*
)
val expectedAst =
SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaAst.Product"),
path = NodePath.root,
fields = Chunk(
("a", SchemaAst.Value(StandardType.StringType, NodePath.root / "a")),
Expand All @@ -59,10 +61,12 @@ object SchemaAstSpec extends DefaultRunnableSpec {
val schema = Schema[SchemaGen.Arity2]
val expectedAst =
SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaGen.Arity2"),
path = NodePath.root,
fields = Chunk(
"value1" -> SchemaAst.Value(StandardType.StringType, NodePath.root / "value1"),
"value2" -> SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaGen.Arity1"),
path = NodePath.root / "value2",
fields = Chunk(
"value" -> SchemaAst.Value(StandardType.IntType, NodePath.root / "value2" / "value")
Expand All @@ -78,7 +82,7 @@ object SchemaAstSpec extends DefaultRunnableSpec {
val ast = SchemaAst.fromSchema(schema)

val recursiveRef: Option[SchemaAst] = ast match {
case SchemaAst.Product(_, elements, _) =>
case SchemaAst.Product(_, _, elements, _) =>
elements.find {
case ("r", _) => true
case _ => false
Expand All @@ -97,25 +101,30 @@ object SchemaAstSpec extends DefaultRunnableSpec {
test("generic") {
val schema =
Schema.enumeration[Any, CaseSet.Aux[Any]](
TypeId.Structural,
caseOf[SchemaGen.Arity1, Any]("type1")(_.asInstanceOf[SchemaGen.Arity1]) ++ caseOf[SchemaGen.Arity2, Any](
"type2"
)(_.asInstanceOf[SchemaGen.Arity2])
)
val expectedAst =
SchemaAst.Sum(
TypeId.Structural,
path = NodePath.root,
cases = Chunk(
"type1" -> SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaGen.Arity1"),
path = NodePath.root / "type1",
fields = Chunk(
"value" -> SchemaAst.Value(StandardType.IntType, NodePath.root / "type1" / "value")
)
),
"type2" -> SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaGen.Arity2"),
path = NodePath.root / "type2",
fields = Chunk(
"value1" -> SchemaAst.Value(StandardType.StringType, NodePath.root / "type2" / "value1"),
"value2" -> SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaGen.Arity1"),
path = NodePath.root / "type2" / "value2",
fields = Chunk(
"value" -> SchemaAst
Expand All @@ -131,14 +140,17 @@ object SchemaAstSpec extends DefaultRunnableSpec {
test("sealed trait") {
val schema = Schema[Pet]
val expectedAst = SchemaAst.Sum(
TypeId.parse("zio.schema.SchemaAstSpec.Pet"),
path = NodePath.root,
cases = Chunk(
"Rock" -> SchemaAst.Value(StandardType.UnitType, NodePath.root / "Rock"),
"Dog" -> SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaAstSpec.Dog"),
path = NodePath.root / "Dog",
fields = Chunk("name" -> SchemaAst.Value(StandardType.StringType, NodePath.root / "Dog" / "name"))
),
"Cat" -> SchemaAst.Product(
id = TypeId.parse("zio.schema.SchemaAstSpec.Cat"),
path = NodePath.root / "Cat",
fields = Chunk(
"name" -> SchemaAst.Value(StandardType.StringType, NodePath.root / "Cat" / "name"),
Expand Down
36 changes: 25 additions & 11 deletions tests/shared/src/test/scala-2/zio/schema/SchemaGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import scala.collection.immutable.ListMap

import zio.Chunk
import zio.random.Random
import zio.schema.TypeId
import zio.test.{ Gen, Sized }

object SchemaGen {
Expand Down Expand Up @@ -183,7 +184,10 @@ object SchemaGen {
} yield schema -> value

val anyEnumeration: Gen[Random with Sized, Schema[Any]] =
anyEnumeration(anySchema).map(toCaseSet).map(Schema.enumeration[Any, CaseSet.Aux[Any]](_))
for {
caseSet <- anyEnumeration(anySchema).map(toCaseSet)
id <- Gen.string(Gen.alphaChar).map(TypeId.parse)
} yield Schema.enumeration[Any, CaseSet.Aux[Any]](id, caseSet)

type EnumerationAndGen = (Schema[Any], Gen[Random with Sized, Any])

Expand All @@ -192,9 +196,10 @@ object SchemaGen {
primitiveAndGen <- anyPrimitiveAndGen
structure <- anyEnumeration(primitiveAndGen._1)
primitiveValue <- primitiveAndGen._2
id <- Gen.string(Gen.alphaChar).map(TypeId.parse)
} yield {
val gen = Gen.oneOf(structure.keys.map(Gen.const(_)).toSeq: _*).map(_ => primitiveValue)
Schema.enumeration[Any, CaseSet.Aux[Any]](toCaseSet(structure)) -> gen
Schema.enumeration[Any, CaseSet.Aux[Any]](id, toCaseSet(structure)) -> gen
}

type EnumerationAndValue = (Schema[Any], Any)
Expand All @@ -206,13 +211,17 @@ object SchemaGen {
} yield schema -> value

val anyRecord: Gen[Random with Sized, Schema[ListMap[String, _]]] =
anyStructure(anySchema).map(Schema.record)
for {
name <- Gen.string(Gen.alphaChar).map(TypeId.parse)
fields <- anyStructure(anySchema)
} yield Schema.record(name, fields: _*)

type GenericRecordAndGen = (Schema[ListMap[String, _]], Gen[Random with Sized, ListMap[String, _]])

val anyGenericRecordAndGen: Gen[Random with Sized, GenericRecordAndGen] =
for {
(schema, gen) <- anyPrimitiveAndGen
name <- Gen.string(Gen.alphaChar).map(TypeId.parse)
structure <- anyStructure(schema)
} yield {
val valueGen = Gen
Expand All @@ -224,7 +233,7 @@ object SchemaGen {
}
.map(ListMap.empty ++ _)

Schema.record(structure: _*) -> valueGen
Schema.record(name, structure: _*) -> valueGen
}

type RecordAndValue = (Schema[ListMap[String, _]], ListMap[String, _])
Expand All @@ -240,11 +249,12 @@ object SchemaGen {
(schema1, gen1) <- anyGenericRecordAndGen
(schema2, gen2) <- anyGenericRecordAndGen
(schema3, gen3) <- anyGenericRecordAndGen
name <- Gen.string(Gen.alphaChar).map(TypeId.parse)
keys <- Gen.setOfN(3)(anyLabel).map(_.toSeq)
(key1, value1) <- Gen.const(keys(0)).zip(gen1)
(key2, value2) <- Gen.const(keys(1)).zip(gen2)
(key3, value3) <- Gen.const(keys(2)).zip(gen3)
} yield Schema.record(Schema.Field(key1, schema1), Schema.Field(key2, schema2), Schema.Field(key3, schema3)) -> ListMap(
} yield Schema.record(name, Schema.Field(key1, schema1), Schema.Field(key2, schema2), Schema.Field(key3, schema3)) -> ListMap(
(key1, value1),
(key2, value2),
(key3, value3)
Expand Down Expand Up @@ -483,10 +493,10 @@ object SchemaGen {

val anyCaseClassAndGen: Gen[Random with Sized, CaseClassAndGen[_]] =
anyCaseClassSchema.map {
case s @ Schema.CaseClass1(_, _, _, _) => (s -> anyArity1).asInstanceOf[CaseClassAndGen[Any]]
case s @ Schema.CaseClass2(_, _, _, _, _, _) => (s -> anyArity2).asInstanceOf[CaseClassAndGen[Any]]
case s @ Schema.CaseClass3(_, _, _, _, _, _, _, _) => (s -> anyArity3).asInstanceOf[CaseClassAndGen[Any]]
case s => (s -> anyArity24).asInstanceOf[CaseClassAndGen[Any]]
case s @ Schema.CaseClass1(_, _, _, _, _) => (s -> anyArity1).asInstanceOf[CaseClassAndGen[Any]]
case s @ Schema.CaseClass2(_, _, _, _, _, _, _) => (s -> anyArity2).asInstanceOf[CaseClassAndGen[Any]]
case s @ Schema.CaseClass3(_, _, _, _, _, _, _, _, _) => (s -> anyArity3).asInstanceOf[CaseClassAndGen[Any]]
case s => (s -> anyArity24).asInstanceOf[CaseClassAndGen[Any]]
}

val anyCaseClassAndValue: Gen[Random with Sized, CaseClassAndValue[_]] =
Expand Down Expand Up @@ -517,7 +527,9 @@ object SchemaGen {
anyPrimitive.map(_.optional),
anyPrimitive.zip(anyPrimitive).map { case (l, r) => Schema.either(l, r) },
anyPrimitive.zip(anyPrimitive).map { case (l, r) => Schema.tuple2(l, r) },
anyStructure(anyPrimitive).map(fields => Schema.record(fields: _*)),
anyStructure(anyPrimitive)
.zip(Gen.string(Gen.alphaChar).map(TypeId.parse))
.map(z => Schema.record(z._2, z._1: _*)),
Gen.const(Schema[Json]),
// anyEnumeration(anyPrimitive).map(toCaseSet).map(Schema.enumeration[Any, CaseSet.Aux[Any]](_)),
anyCaseClassSchema,
Expand All @@ -537,7 +549,9 @@ object SchemaGen {
},
anyTree(depth - 1).zip(anyTree(depth - 1)).map { case (l, r) => Schema.either(l, r) },
anyTree(depth - 1).zip(anyTree(depth - 1)).map { case (l, r) => Schema.tuple2(l, r) },
anyStructure(anyTree(depth - 1)).map(fields => Schema.record(fields: _*)),
anyStructure(anyTree(depth - 1))
.zip(Gen.string(Gen.alphaChar).map(TypeId.parse))
.map(z => Schema.record(z._2, z._1: _*)),
Gen.const(Schema[Json])
// anyEnumeration(anyTree(depth - 1)).map(toCaseSet).map(Schema.enumeration[Any, CaseSet.Aux[Any]](_))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ object DefaultValueSpec extends DefaultRunnableSpec {
test("basic") {
val schema: Schema[UserId] =
Schema.CaseClass1(
TypeId.parse("zio.schema.DefaultValueSpec.UserId"),
field = Schema.Field("id", Schema.Primitive(StandardType.StringType)),
UserId.apply,
(uid: UserId) => uid.id
Expand All @@ -158,9 +159,11 @@ object DefaultValueSpec extends DefaultRunnableSpec {
test("recursive") {
val expected: Schema[User] =
Schema.CaseClass3(
TypeId.parse("zio.schema.DefaultValueSpec.User"),
field1 = Schema.Field(
"id",
Schema.CaseClass1(
TypeId.parse("zio.schema.DefaultValueSpec.UserId"),
field = Schema.Field("id", Schema.Primitive(StandardType.StringType)),
UserId.apply,
(uid: UserId) => uid.id
Expand All @@ -187,6 +190,7 @@ object DefaultValueSpec extends DefaultRunnableSpec {
suite("Enumeration")(
test("defaults to first case") {
val schema = Schema.enumeration[Any, CaseSet.Aux[Any]](
TypeId.Structural,
caseOf[Int, Any]("myInt")(_.asInstanceOf[Int]) ++ caseOf[String, Any]("myString")(_.asInstanceOf[String])
)
assert(schema.defaultValue)(isRight(equalTo(0)))
Expand Down Expand Up @@ -229,6 +233,7 @@ object DefaultValueSpec extends DefaultRunnableSpec {

val schema: Schema[Status] =
Schema.Enum3(
TypeId.parse("zio.schema.DefaultValueSpec.Status"),
Schema.Case("Failed", Schema[Failed], (s: Status) => s.asInstanceOf[Failed]),
Schema.Case("Ok", Schema[Ok], (s: Status) => s.asInstanceOf[Ok]),
Schema.Case(
Expand Down
12 changes: 11 additions & 1 deletion tests/shared/src/test/scala/zio/schema/MigrationSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ object MigrationSpec extends DefaultRunnableSpec {
assert(Migration.DeleteNode(NodePath.root / "v2"))(
transformsValueTo(
Nested1(0, "foo"),
DynamicValue.Record(ListMap("v1" -> DynamicValue.Primitive(0, StandardType.IntType)))
DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.Nested1"),
ListMap("v1" -> DynamicValue.Primitive(0, StandardType.IntType))
)
)
)
},
Expand All @@ -124,9 +127,11 @@ object MigrationSpec extends DefaultRunnableSpec {
transformsValueTo(
Outer1("foo", Nested1(0, "bar")),
DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.Outer1"),
ListMap(
"v1" -> DynamicValue.Primitive("foo", StandardType.StringType),
"v2" -> DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.Nested1"),
ListMap(
"v1" -> DynamicValue.Primitive(0, StandardType.IntType)
)
Expand All @@ -141,15 +146,18 @@ object MigrationSpec extends DefaultRunnableSpec {
transformsValueTo(
Recursive1(0, "", Some(Recursive1(1, "", Some(Recursive1(2, "", None))))),
DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.Recursive1"),
ListMap(
"v1" -> DynamicValue.Primitive(0, StandardType.IntType),
"v2" -> DynamicValue.Primitive("", StandardType.StringType),
"r" -> DynamicValue.SomeValue(
DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.Recursive1"),
ListMap(
"v1" -> DynamicValue.Primitive(1, StandardType.IntType),
"r" -> DynamicValue.SomeValue(
DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.Recursive1"),
ListMap(
"v1" -> DynamicValue.Primitive(2, StandardType.IntType),
"r" -> DynamicValue.NoneValue
Expand All @@ -169,6 +177,7 @@ object MigrationSpec extends DefaultRunnableSpec {
transformsValueTo(
OptionalField(0, Some("foo")),
DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.OptionalField"),
ListMap(
"v1" -> DynamicValue.Primitive(0, StandardType.IntType),
"v2" -> DynamicValue.Primitive("foo", StandardType.StringType)
Expand All @@ -185,6 +194,7 @@ object MigrationSpec extends DefaultRunnableSpec {
transformsValueTo(
Nested1(0, "foo"),
DynamicValue.Record(
TypeId.parse("zio.schema.MigrationSpec.Nested1"),
ListMap(
"v1" -> DynamicValue.Primitive(0, StandardType.IntType),
"v2" -> DynamicValue.SomeValue(DynamicValue.Primitive("foo", StandardType.StringType))
Expand Down
5 changes: 3 additions & 2 deletions tests/shared/src/test/scala/zio/schema/SchemaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ object SchemaSpec extends DefaultRunnableSpec {
def schemaUnit: Schema[Unit] = Schema[Unit]
def schemaInt: Schema[Int] = Schema[Int]

def schemaRecord(key: String): Schema[ListMap[String, _]] = Schema.record(Schema.Field(key, schemaUnit))
def schemaRecord(key: String): Schema[ListMap[String, _]] =
Schema.record(TypeId.Structural, Schema.Field(key, schemaUnit))

def schemaEnum(key: String): Schema[Any] =
Schema.enumeration[Any, CaseSet.Aux[Any]](caseOf[Unit, Any](key)(_ => ()))
Schema.enumeration[Any, CaseSet.Aux[Any]](TypeId.Structural, caseOf[Unit, Any](key)(_ => ()))

val f: Unit => Either[String, Int] = _ => Right(0)
val g: Int => Either[String, Unit] = _ => Right(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package zio.schema
import zio.schema

class TestAccessorBuilder extends AccessorBuilder {
override type Lens[S, A] = TestAccessorBuilder.Lens[S, A]
override type Prism[S, A] = TestAccessorBuilder.Prism[S, A]
override type Lens[F, S, A] = TestAccessorBuilder.Lens[F, S, A]
override type Prism[F, S, A] = TestAccessorBuilder.Prism[F, S, A]
override type Traversal[S, A] = TestAccessorBuilder.Traversal[S, A]

override def makeLens[S, A](product: Schema.Record[S], term: schema.Schema.Field[A]): Lens[S, A] =
override def makeLens[F, S, A](product: Schema.Record[S], term: schema.Schema.Field[A]): Lens[F, S, A] =
TestAccessorBuilder.Lens(product, term)

override def makePrism[S, A](sum: Schema.Enum[S], term: schema.Schema.Case[A, S]): Prism[S, A] =
override def makePrism[F, S, A](sum: Schema.Enum[S], term: schema.Schema.Case[A, S]): Prism[F, S, A] =
TestAccessorBuilder.Prism(sum, term)

override def makeTraversal[S, A](collection: Schema.Collection[S, A], element: Schema[A]): Traversal[S, A] =
Expand All @@ -18,9 +18,9 @@ class TestAccessorBuilder extends AccessorBuilder {

object TestAccessorBuilder {

case class Lens[S, A](s: Schema.Record[S], a: Schema.Field[A])
case class Lens[F, S, A](s: Schema.Record[S], a: Schema.Field[A])

case class Prism[S, A](s: Schema.Enum[S], a: Schema.Case[A, S])
case class Prism[F, S, A](s: Schema.Enum[S], a: Schema.Case[A, S])

case class Traversal[S, A](s: Schema.Collection[S, A], a: Schema[A])
}
Loading