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
- Grouped
- Alphabetic
- By Inheritance
- CoproductCodecBuilder
- AnyRef
- Any
- by toTransformSyntax
- by ValueEnrichedWithTuplingSupport
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
- Public
- All
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
+(other: String): String
- Implicit
- 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
-
def
->[B](y: B): (CoproductCodecBuilder[C, L, R], B)
- Implicit
- 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()
-
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
-
val
a: CoproductCodecBuilder[C, L, R]
- Implicit
- 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
-
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 someL <: HList
to/from anF[CC]
for some case classCC
, where the types in the case class are aligned with the types inL
- an
F[C]
for someC <: Coproduct
to/from anF[SC]
for some sealed classSC
, where the component types in the coproduct are the leaf subtypes of the sealed class.
- Implicit
- 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
- an
-
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( ... ) @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
. -
def
ensuring(cond: (CoproductCodecBuilder[C, L, R]) ⇒ Boolean, msg: ⇒ Any): CoproductCodecBuilder[C, L, R]
- Implicit
- 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
-
def
ensuring(cond: (CoproductCodecBuilder[C, L, R]) ⇒ Boolean): CoproductCodecBuilder[C, L, R]
- Implicit
- 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
-
def
ensuring(cond: Boolean, msg: ⇒ Any): CoproductCodecBuilder[C, L, R]
- Implicit
- 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
-
def
ensuring(cond: Boolean): CoproductCodecBuilder[C, L, R]
- Implicit
- 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
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): 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
exmapc[B](f: (R) ⇒ Attempt[B])(g: (B) ⇒ Attempt[R]): CoproductCodecBuilder[C, L, B]
Curried version of
exmap
.Curried version of
exmap
.- Implicit
- 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
-
def
finalize(): Unit
- Attributes
- protected[lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
formatted(fmtstr: String): String
- Implicit
- 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()
-
def
framing(ct: CodecTransformation): CoproductCodecBuilder[C, L, R]
Applies the specified codec transformation to all component codecs.
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
narrow[B](f: (R) ⇒ Attempt[B], g: (B) ⇒ R): CoproductCodecBuilder[C, L, B]
Transforms using two functions,
A => Attempt[B]
andB => A
.Transforms using two functions,
A => Attempt[B]
andB => A
.The supplied functions form an injection from
B
toA
. Hence, this method converts from a larger to a smaller type. Hence, the namenarrow
.- Implicit
- 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
-
def
narrowc[B](f: (R) ⇒ Attempt[B])(g: (B) ⇒ R): CoproductCodecBuilder[C, L, B]
Curried version of
narrow
.Curried version of
narrow
.- Implicit
- 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
-
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()
-
val
self: CoproductCodecBuilder[C, L, R]
- Implicit
- 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
-
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( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... ) @native()
-
def
widen[B](f: (R) ⇒ B, g: (B) ⇒ Attempt[R]): CoproductCodecBuilder[C, L, B]
Transforms using two functions,
A => B
andB => Attempt[A]
.Transforms using two functions,
A => B
andB => Attempt[A]
.The supplied functions form an injection from
A
toB
. Hence, this method converts from a smaller to a larger type. Hence, the namewiden
.- Implicit
- 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
-
def
widenOpt[B](f: (R) ⇒ B, g: (B) ⇒ Option[R]): CoproductCodecBuilder[C, L, B]
Transforms using two functions,
A => B
andB => Option[A]
.Transforms using two functions,
A => B
andB => Option[A]
.Particularly useful when combined with case class apply/unapply. E.g.,
widenOpt(fa, Foo.apply, Foo.unapply)
.- Implicit
- 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
-
def
widenOptc[B](f: (R) ⇒ B)(g: (B) ⇒ Option[R]): CoproductCodecBuilder[C, L, B]
Curried version of
widenOpt
.Curried version of
widenOpt
.- Implicit
- 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
-
def
widenc[B](f: (R) ⇒ B)(g: (B) ⇒ Attempt[R]): CoproductCodecBuilder[C, L, B]
Curried version of
widen
.Curried version of
widen
.- Implicit
- 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
-
def
xmap[B](f: (R) ⇒ B, g: (B) ⇒ R): CoproductCodecBuilder[C, L, B]
Transforms using the isomorphism described by two functions,
A => B
andB => A
.Transforms using the isomorphism described by two functions,
A => B
andB => A
.- Implicit
- 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
-
def
xmapc[B](f: (R) ⇒ B)(g: (B) ⇒ R): CoproductCodecBuilder[C, L, B]
Curried version of
xmap
.Curried version of
xmap
.- Implicit
- 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
-
def
~[B](b: B): (CoproductCodecBuilder[C, L, R], B)
- Implicit
- 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
-
def
→[B](y: B): (CoproductCodecBuilder[C, L, R], B)
- Implicit
- 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
-
def
exmap[B](f: (R) ⇒ Attempt[B], g: (B) ⇒ Attempt[R]): CoproductCodecBuilder[C, L, B]
Transforms using two functions,
A => Attempt[B]
andB => Attempt[A]
.Transforms using two functions,
A => Attempt[B]
andB => Attempt[A]
.- Implicit
- 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