scodec.codecs

CoproductCodecBuilder

Related Docs: object CoproductCodecBuilder | package codecs

final class CoproductCodecBuilder[C <: Coproduct, L <: HList, R] extends AnyRef

Supports building a coproduct codec.

A coproduct codec is built by:

To specify the discriminator, call either discriminatedByIndex(intCodec) or discriminatedBy(codec) followed by one of the methods on NeedDiscriminators. The former uses the type index as the discriminator value.

For example:

(int32 :+: bool(8) :+: variableSizeBytes(uint8, ascii)).discriminatedByIndex(uint8)

The first 8 bits of the resulting binary contains the discriminator value due to usage of the uint8 codec as the discriminator codec. A discriminator value of 0 causes the remaining bits to be encoded/decoded with int32. Similarly, a value of 1 causes the remaining bits to be encoded/decoded with bool(8) and a value of 2 causes the remaining bits to be encoded/decoded as a sized ASCII string.

Alternatively, discriminator values can be explicitly specified using discriminatedBy(codec).using(Sized(...)).

For example:

(int32 :+: bool(8) :+: variableSizeBytes(uint8, ascii)).discriminatedBy(fixedSizeBytes(1, ascii)).using(Sized("i", "b", "s"))

In this example, integers are associated with the discriminator i, booleans with b, and strings with s. The discriminator is encoded with fixedSizeBytes(1, ascii).

The methods which generate a Codec return a Codec[R] instead of a Codec[C]. Typically, C =:= R but the xmap and exmap methods allow transformations between C and R to be deferred until after the codec is built.

C

coproduct type

L

hlist type that has a codec for each type in the coproduct C

R

resulting codec type

Source
CoproductCodec.scala
Linear Supertypes
Type Hierarchy Learn more about scaladoc diagrams
Ordering
  1. Grouped
  2. Alphabetic
  3. By inheritance
Inherited
  1. CoproductCodecBuilder
  2. AnyRef
  3. Any
Implicitly
  1. by toTransformSyntax
  2. by ValueEnrichedWithTuplingSupport
  3. by any2stringadd
  4. by StringFormat
  5. by Ensuring
  6. by ArrowAssoc
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Type Members

  1. final class NeedDiscriminators[A] extends AnyRef

    Assists in creating a coproduct codec, after the coproduct type and discriminator type have been fixed.

Value Members

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

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

    Definition Classes
    AnyRef → Any
  3. def +(other: String): String

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to any2stringadd[CoproductCodecBuilder[C, L, R]] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ->[B](y: B): (CoproductCodecBuilder[C, L, R], B)

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to ArrowAssoc[CoproductCodecBuilder[C, L, R]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  5. def :+:[A](left: Codec[A]): CoproductCodecBuilder[:+:[A, C], ::[Codec[A], L], :+:[A, C]]

    Adds a codec to the head of this coproduct codec.

  6. final def ==(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  7. val a: CoproductCodecBuilder[C, L, R]

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to ValueEnrichedWithTuplingSupport[CoproductCodecBuilder[C, L, R]] performed by method ValueEnrichedWithTuplingSupport in scodec.codecs.
    Definition Classes
    ValueEnrichedWithTuplingSupport
  8. def as[B](implicit as: Transformer[R, B]): CoproductCodecBuilder[C, L, B]

    Transforms using implicitly available evidence that such a transformation is possible.

    Transforms using implicitly available evidence that such a transformation is possible.

    Typical transformations include converting:

    • an F[L] for some L <: HList to/from an F[CC] for some case class CC, where the types in the case class are aligned with the types in L
    • an F[C] for some C <: Coproduct to/from an F[SC] for some sealed class SC, where the component types in the coproduct are the leaf subtypes of the sealed class.
    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Definition Classes
    TransformSyntax
  9. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  10. def auto[A](implicit discriminated: Discriminated[R, A], auto: CoproductBuilderAutoDiscriminators[R, C, A]): Codec[R]

    Automatically generates a Codec[R] given an implicit Discriminated[R, A] and an implicit Discriminator[R, X, A] for each X that is a member of the coproduct type that represents R.

  11. def choice: Codec[R]

    Creates a coproduct codec that encodes no discriminator.

    Creates a coproduct codec that encodes no discriminator. Rather, decoding is accomplished by trying each codec in order and using the first successful result.

  12. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  13. def discriminatedBy[A](discriminatorCodec: Codec[A]): NeedDiscriminators[A]

    Supports creation of a coproduct codec that uses an arbitrary discriminator.

  14. def discriminatedByIndex(discriminatorCodec: Codec[Int]): Codec[R] with KnownDiscriminatorType[Int]

    Creates the coproduct codec using the specified integer codec as the discriminator codec and using coproduct indices as discriminators.

    Creates the coproduct codec using the specified integer codec as the discriminator codec and using coproduct indices as discriminators.

    For example, (a :+: b :+: c).discriminatedByIndex(uint8) results in using 0 for a, 1 for b, and 2 for c.

  15. def ensuring(cond: (CoproductCodecBuilder[C, L, R]) ⇒ Boolean, msg: ⇒ Any): CoproductCodecBuilder[C, L, R]

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to Ensuring[CoproductCodecBuilder[C, L, R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  16. def ensuring(cond: (CoproductCodecBuilder[C, L, R]) ⇒ Boolean): CoproductCodecBuilder[C, L, R]

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to Ensuring[CoproductCodecBuilder[C, L, R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  17. def ensuring(cond: Boolean, msg: ⇒ Any): CoproductCodecBuilder[C, L, R]

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to Ensuring[CoproductCodecBuilder[C, L, R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  18. def ensuring(cond: Boolean): CoproductCodecBuilder[C, L, R]

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to Ensuring[CoproductCodecBuilder[C, L, R]] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  19. final def eq(arg0: AnyRef): Boolean

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

    Definition Classes
    AnyRef → Any
  21. def exmap[S](rToS: (R) ⇒ Attempt[S], sToR: (S) ⇒ Attempt[R]): CoproductCodecBuilder[C, L, S]

    Creates a builder that applies the specified transformations to any codecs generated by the returned builder.

  22. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  23. def formatted(fmtstr: String): String

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to StringFormat[CoproductCodecBuilder[C, L, R]] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @inline()
  24. final def getClass(): Class[_]

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

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

    Definition Classes
    Any
  27. def narrow[B](f: (R) ⇒ Attempt[B], g: (B) ⇒ R): CoproductCodecBuilder[C, L, B]

    Transforms using two functions, A => Attempt[B] and B => A.

    Transforms using two functions, A => Attempt[B] and B => A.

    The supplied functions form an injection from B to A. Hence, this method converts from a larger to a smaller type. Hence, the name narrow.

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Definition Classes
    TransformSyntax
  28. final def ne(arg0: AnyRef): Boolean

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

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

    Definition Classes
    AnyRef
  31. val self: CoproductCodecBuilder[C, L, R]

    Supports TransformSyntax.

    Supports TransformSyntax.

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Definition Classes
    TransformSyntax
  32. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  33. def toString(): String

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

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

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

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  37. def widen[B](f: (R) ⇒ B, g: (B) ⇒ Attempt[R]): CoproductCodecBuilder[C, L, B]

    Transforms using two functions, A => B and B => Attempt[A].

    Transforms using two functions, A => B and B => Attempt[A].

    The supplied functions form an injection from A to B. Hence, this method converts from a smaller to a larger type. Hence, the name widen.

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Definition Classes
    TransformSyntax
  38. def widenOpt[B](f: (R) ⇒ B, g: (B) ⇒ Option[R]): CoproductCodecBuilder[C, L, B]

    Transforms using two functions, A => B and B => Option[A].

    Transforms using two functions, A => B and B => Option[A].

    Particularly useful when combined with case class apply/unapply. E.g., widenOpt(fa, Foo.apply, Foo.unapply).

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Definition Classes
    TransformSyntax
  39. def xmap[B](f: (R) ⇒ B, g: (B) ⇒ R): CoproductCodecBuilder[C, L, B]

    Transforms using the isomorphism described by two functions, A => B and B => A.

    Transforms using the isomorphism described by two functions, A => B and B => A.

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Definition Classes
    TransformSyntax
  40. def ~[B](b: B): (CoproductCodecBuilder[C, L, R], B)

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to ValueEnrichedWithTuplingSupport[CoproductCodecBuilder[C, L, R]] performed by method ValueEnrichedWithTuplingSupport in scodec.codecs.
    Definition Classes
    ValueEnrichedWithTuplingSupport
  41. def [B](y: B): (CoproductCodecBuilder[C, L, R], B)

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to ArrowAssoc[CoproductCodecBuilder[C, L, R]] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc

Shadowed Implicit Value Members

  1. def exmap[B](f: (R) ⇒ Attempt[B], g: (B) ⇒ Attempt[R]): CoproductCodecBuilder[C, L, B]

    Transforms using two functions, A => Attempt[B] and B => Attempt[A].

    Transforms using two functions, A => Attempt[B] and B => Attempt[A].

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Shadowing
    This implicitly inherited member is shadowed by one or more members in this class.
    To access this member you can use a type ascription:
    (coproductCodecBuilder: TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R]).exmap(f, g)
    Definition Classes
    TransformSyntax

Deprecated Value Members

  1. def pxmap[B](f: (R) ⇒ B, g: (B) ⇒ Option[R]): CoproductCodecBuilder[C, L, B]

    Transforms using two functions, A => B and B => Option[A].

    Transforms using two functions, A => B and B => Option[A].

    Particularly useful when combined with case class apply/unapply. E.g., pxmap(fa, Foo.apply, Foo.unapply).

    Implicit information
    This member is added by an implicit conversion from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R] performed by method toTransformSyntax in scodec.codecs.CoproductCodecBuilder. This conversion will take place only if C is a subclass of Coproduct (C <: Coproduct) and at the same time L is a subclass of HList (L <: HList).
    Definition Classes
    TransformSyntax
    Annotations
    @deprecated
    Deprecated

    (Since version 1.7.0) Use widenOpt instead

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion toTransformSyntax from CoproductCodecBuilder[C, L, R] to TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R]

Inherited by implicit conversion ValueEnrichedWithTuplingSupport from CoproductCodecBuilder[C, L, R] to ValueEnrichedWithTuplingSupport[CoproductCodecBuilder[C, L, R]]

Inherited by implicit conversion any2stringadd from CoproductCodecBuilder[C, L, R] to any2stringadd[CoproductCodecBuilder[C, L, R]]

Inherited by implicit conversion StringFormat from CoproductCodecBuilder[C, L, R] to StringFormat[CoproductCodecBuilder[C, L, R]]

Inherited by implicit conversion Ensuring from CoproductCodecBuilder[C, L, R] to Ensuring[CoproductCodecBuilder[C, L, R]]

Inherited by implicit conversion ArrowAssoc from CoproductCodecBuilder[C, L, R] to ArrowAssoc[CoproductCodecBuilder[C, L, R]]

Combinators

Ungrouped