Skip to content

Commit

Permalink
add derived extension method on Schema.type. (#528)
Browse files Browse the repository at this point in the history
Allows to use the scala3 `derives` keyword.

Co-authored-by: John A. De Goes <john@degoes.net>
  • Loading branch information
Matthieu Leclercq and jdegoes authored Mar 21, 2023
1 parent daf7656 commit d7ea85b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import Schema._

import zio.schema.annotation.fieldName

extension (s: Schema.type) transparent inline def derived[A] = DeriveSchema.gen[A]

object DeriveSchema {

transparent inline def gen[T]: Schema[T] = ${ deriveSchema[T] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,32 @@ trait VersionSpecificDeriveSchemaSpec extends ZIOSpecDefault {
}

inline def verifyFieldName[F]: FieldNameVerifier[F] = new FieldNameVerifier[F]



class FieldNameVerifier[F] {
inline def apply[S <: String & scala.Singleton](name: S): Boolean =
VerifyFieldNameMacro.verifyFieldName[F, S]
}

def versionSpecificSuite = Spec.labeled("Scala 3 specific tests", Spec.empty)
import SchemaAssertions._

final case class AutoDerives(i: Int) derives Schema

def versionSpecificSuite = Spec.labeled(
"Scala 3 specific tests",
suite("Derivation")(
test("correctly derives case class with `derives` keyword") {
val expected: Schema[AutoDerives] = Schema.CaseClass1(
TypeId.parse("zio.schema.VersionSpecificDeriveSchemaSpec.AutoDerives"),
field0 = Schema.Field(
"i",
Schema.Primitive(StandardType.IntType),
get0 = _.i,
set0 = (a, b: Int) => a.copy(i = b)
),
AutoDerives.apply
)
assert(Schema[AutoDerives])(hasSameSchema(expected))
}
)
)
}

0 comments on commit d7ea85b

Please sign in to comment.