org.coursera.courier.coercers

SingleElementCaseClassCoercer

Related Docs: object SingleElementCaseClassCoercer | package coercers

class SingleElementCaseClassCoercer[T <: Product] extends DirectCoercer[T]

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 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:

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 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)

json { "name": "CreatedAt", "namespace": "org.example.schemas", "type": "typeref", "ref": "org.example.schemas.DateTime", "scala": { "class": "org.example.CreatedAt" } }

Linear Supertypes
DirectCoercer[T], AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. SingleElementCaseClassCoercer
  2. DirectCoercer
  3. AnyRef
  4. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Instance Constructors

  1. new SingleElementCaseClassCoercer(productType: Class[T], coercer: ElementCoercer)

Value Members

  1. final def !=(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  5. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  6. def coerceInput(obj: T): AnyRef

    Definition Classes
    SingleElementCaseClassCoercer → DirectCoercer
  7. def coerceOutput(obj: AnyRef): T

    Definition Classes
    SingleElementCaseClassCoercer → DirectCoercer
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  18. def toString(): String

    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from DirectCoercer[T]

Inherited from AnyRef

Inherited from Any

Ungrouped