Assists in creating a coproduct codec, after the coproduct type and discriminator type have been fixed.
Adds a codec to the head of this coproduct codec.
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:
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
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.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
.
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.
Supports creation of a coproduct codec that uses an arbitrary discriminator.
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
.
Creates a builder that applies the specified transformations to any codecs generated by the returned builder.
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
.
Supports TransformSyntax.
Supports TransformSyntax.
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
.
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)
.
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
.
Transforms using two functions, A => Attempt[B]
and B => Attempt[A]
.
Transforms using two functions, A => Attempt[B]
and B => Attempt[A]
.
(coproductCodecBuilder: TransformSyntax[[a]CoproductCodecBuilder[C, L, a], R]).exmap(f, g)
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)
.
(Since version 1.7.0) Use widenOpt instead
Supports building a coproduct codec.
A coproduct codec is built by:
:+:
operatorchoice
combinator to create a codec that encodes no discriminator and hence, decodes by trying each codec in succession and using the first successful resultTo specify the discriminator, call either
discriminatedByIndex(intCodec)
ordiscriminatedBy(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 withint32
. Similarly, a value of 1 causes the remaining bits to be encoded/decoded withbool(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:
In this example, integers are associated with the discriminator
i
, booleans withb
, and strings withs
. The discriminator is encoded withfixedSizeBytes(1, ascii)
.The methods which generate a
Codec
return aCodec[R]
instead of aCodec[C]
. Typically,C =:= R
but thexmap
andexmap
methods allow transformations betweenC
andR
to be deferred until after the codec is built.coproduct type
hlist type that has a codec for each type in the coproduct
C
resulting codec type