Coercer for single element ("arity 1") Scala case classes.
Coercer for single element ("arity 1") Scala case classes.
Alleviates the need to hand write coercers for single element case classes. To use, simply
identify the Scala case class to coerce to in a typeref Pegasus schema definition.
The class to coerce to must:
- Be public and extend Product. All case classes extend Product.
- Have a single public constructor.
- Have a single public field/element. That is, Product.productArity must return 1.
- Have an element that is (a) a primitive, or (b) another Pegasus CustomType that has a
registered coercer. Product.productElement(0) must return this element.
- (Case classes *may* extend AnyVal if desired)
A primitive may be:
- Any Scala AnyVal type except Unit
- Any boxed primitive type (e.g. java.lang.Integer)
A few primitives are handled as special cases by this coercer:
- Char is represented as a single char string Pegasus type (JSON string)
- Byte is represented as a single byte bytes Pegasus type (JSON string with avro byte
encoding)
- Short is represented as a int pegasus type (JSON number).
"Chaining" is supported. For example, if the SlugId custom type is defined as shown in
the above example, the Scala case class:
scala
case class ClassId(slug: SlugId)
Can be bound as a custom type that "chains" off of SlugId:
"Chaining" also works with ref types that have custom coercers. For example, if
org.example.DateTime is custom type with a coercerClass to org.joda.time.DataTime, in can
be bound to a custom type using:
scala
case class CreatedAt(time: org.joda.time.DateTime)
Coercer for single element ("arity 1") Scala case classes.
Alleviates the need to hand write coercers for single element case classes. To use, simply identify the Scala case class to coerce to in a typeref Pegasus schema definition.
For example, to coerce to the Scala case class:
scala case class SlugId(slug: String)
Define a Pegasus typeref schema like:
json { "name": "SlugId", "namespace": "org.example.schemas", "type": "typeref", "ref": "string", "scala": { "class": "org.example.SlugId" } }
The class to coerce to must: - Be public and extend
Product
. All case classes extendProduct
. - Have a single public constructor. - Have a single public field/element. That is,Product.productArity
must return 1. - Have an element that is (a) a primitive, or (b) another Pegasus CustomType that has a registered coercer.Product.productElement(0)
must return this element. - (Case classes *may* extendAnyVal
if desired)A primitive may be: - Any Scala
AnyVal
type exceptUnit
- Any boxed primitive type (e.g.java.lang.Integer
)A few primitives are handled as special cases by this coercer: -
Char
is represented as a single charstring
Pegasus type (JSON string) -Byte
is represented as a single bytebytes
Pegasus type (JSON string with avro byte encoding) -Short
is represented as aint
pegasus type (JSON number)."Chaining" is supported. For example, if the
SlugId
custom type is defined as shown in the above example, the Scala case class:scala case class ClassId(slug: SlugId)
Can be bound as a custom type that "chains" off of
SlugId
:json { "name": "ClassId", "namespace": "org.example.schemas", "type": "typeref", "ref": "org.example.schemas.SlugId", "scala": { "class": "org.example.ClassId" } }
"Chaining" also works with ref types that have custom coercers. For example, if
org.example.DateTime
is custom type with acoercerClass
toorg.joda.time.DataTime
, in can be bound to a custom type using:scala case class CreatedAt(time: org.joda.time.DateTime)
json { "name": "CreatedAt", "namespace": "org.example.schemas", "type": "typeref", "ref": "org.example.schemas.DateTime", "scala": { "class": "org.example.CreatedAt" } }