final class CoproductCodecBuilder[C <: Coproduct, L <: HList, R] extends AnyRef
Supports building a coproduct codec.
A coproduct codec is built by:
- specifying a codec for each member of the coproduct, separated by the
:+:
operator - specifying the discriminator codec and mapping between discriminator values and coproduct members
- alternatively, instead of specifying a discriminator codec, using the
choice
combinator to create a codec that encodes no discriminator and hence, decodes by trying each codec in succession and using the first successful result
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
- Alphabetic
- By Inheritance
- CoproductCodecBuilder
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- final class NeedDiscriminators[A] extends AnyRef
Assists in creating a coproduct codec, after the coproduct type and discriminator type have been fixed.
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def ##: Int
- Definition Classes
- AnyRef → Any
- def :+:[A](left: Codec[A]): CoproductCodecBuilder[:+:[A, C], ::[Codec[A], L], :+:[A, C]]
Adds a codec to the head of this coproduct codec.
- final def ==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def auto[A](implicit discriminated: Discriminated[R, A], auto: CoproductBuilderAutoDiscriminators[R, C, A]): Codec[R]
Automatically generates a
Codec[R]
given an implicitDiscriminated[R, A]
and an implicitDiscriminator[R, X, A]
for eachX
that is a member of the coproduct type that representsR
. - 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.
- def clone(): AnyRef
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.CloneNotSupportedException]) @native()
- def discriminatedBy[A](discriminatorCodec: Codec[A]): NeedDiscriminators[A]
Supports creation of a coproduct codec that uses an arbitrary discriminator.
- 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 using0
fora
,1
forb
, and2
forc
. - final def eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- def equals(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef → Any
- 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.
- def finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.Throwable])
- def framing(ct: CodecTransformation): CoproductCodecBuilder[C, L, R]
Applies the specified codec transformation to all component codecs.
- final def getClass(): Class[_ <: AnyRef]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- def hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- final def ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
- final def notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
- final def synchronized[T0](arg0: => T0): T0
- Definition Classes
- AnyRef
- def toString(): String
- Definition Classes
- AnyRef → Any
- final def wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException])
- final def wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws(classOf[java.lang.InterruptedException]) @native()