Error raised when an unknown discriminator is encountered when decoding.
Error raised when an unknown discriminator is encountered when decoding.
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for subcaseO
.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
function used during encoding that converts an A
to an Option[R]
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for subcaseO
.
representative type that this case handles
tag value for this case
function used during encoding that converts an A
to an Option[R]
codec that encodes/decodes R
s
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList L
followed by a B
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList L
followed by a B
.
That is, this operator is a codec-level HList
append operation.
Supports creation of a coproduct codec.
Supports creation of a coproduct codec. See scodec.codecs.CoproductCodecBuilder for details.
When called on a Codec[A]
where A
is not a subytpe of HList
, creates a new codec that encodes/decodes an HList
of B :: A :: HNil
.
When called on a Codec[A]
where A
is not a subytpe of HList
, creates a new codec that encodes/decodes an HList
of B :: A :: HNil
.
For example,
uint8 :: utf8
has type Codec[Int :: String :: HNil]
.
uint8 :: utf8
}}}
When called on a Codec[L]
for some L <: HList
, returns a new codec representing Codec[B :: L]
.
When called on a Codec[L]
for some L <: HList
, returns a new codec representing Codec[B :: L]
.
That is, this operator is a codec-level HList
prepend operation.
codec to prepend
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList K
followed by the HList L
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList K
followed by the HList L
.
When called on a Codec[A]
, returns a new codec that encodes/decodes B :: A :: HNil
.
When called on a Codec[A]
, returns a new codec that encodes/decodes B :: A :: HNil
.
HList equivalent of ~>
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
B :: L
but only returns L
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
B :: L
but only returns L
. HList equivalent of ~>
.
Assuming B
is Unit
, creates a Codec[A]
that: encodes the A
followed by a unit;
decodes an A
followed by a unit and discards the decoded unit.
Creates a new codec that encodes/decodes an HList
type of A :: L
given a function A => Codec[L]
.
Creates a new codec that encodes/decodes an HList
type of A :: L
given a function A => Codec[L]
.
This allows later parts of an HList
codec to be dependent on earlier values.
Operator alias for flatPrepend
.
Returns a new codec that encodes/decodes a value of type (A, B)
where the codec of B
is dependent on A
.
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for caseO
.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
function used during encoding that converts an A
to an Option[R]
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for caseO
.
representative type that this case handles
tag value for this case
function used during encoding that converts an A
to an Option[R]
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for subcaseP
.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
partial function from A
to R
used during encoding
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for subcaseP
.
representative type that this case handles
tag value for this case
partial function from A
to R
used during encoding
codec that encodes/decodes R
s
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.Gets this as a Decoder
.
Gets this as a Decoder
.
Gets this as an Encoder
.
Gets this as an Encoder
.
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
function used during encoding that converts an A
to an Option[R]
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value for this case
function used during encoding that converts an A
to an Option[R]
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
partial function from A
to R
used during encoding
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value for this case
partial function from A
to R
used during encoding
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Converts this codec to a new codec that compacts the encoded bit vector before returning it.
Converts this codec to a new codec that fails decoding if there are remaining bits.
Similar to flatZip
except the A
type is not visible in the resulting type -- the binary
effects of the Codec[A]
still occur though.
Similar to flatZip
except the A
type is not visible in the resulting type -- the binary
effects of the Codec[A]
still occur though.
Example usage:
case class Flags(x: Boolean, y: Boolean, z: Boolean) (bool :: bool :: bool :: ignore(5)).consume { flgs => conditional(flgs.x, uint8) :: conditional(flgs.y, uint8) :: conditional(flgs.z, uint8) } { case x :: y :: z :: HNil => Flags(x.isDefined, y.isDefined, z.isDefined) } }
Note that when B
is an HList
, this method is equivalent to using flatPrepend
and
derive
. That is,
a.consume(f)(g) === a.flatPrepend(f).derive[A].from(g)
.
Converts this GenCodec
to a GenCodec[C, B]
using the supplied C => A
.
Attempts to decode a value of type A
from the specified bit vector.
Attempts to decode a value of type A
from the specified bit vector.
bits to decode
error if value could not be decoded or the remaining bits and the decoded value
Converts this to a codec that fails encoding with an error.
Attempts to decode a value of type A
from the specified bit vector and discards the remaining bits.
Attempts to decode a value of type A
from the specified bit vector and discards the remaining bits.
bits to decode
error if value could not be decoded or the decoded value
Supports building a Codec[M]
for some HList M
where M
is the HList
that results in removing
the first A
from L
.
Supports building a Codec[M]
for some HList M
where M
is the HList
that results in removing
the first A
from L
.
Example usage:
case class Flags(x: Boolean, y: Boolean, z: Boolean) val c = (bool :: bool :: bool :: ignore(5)).flatPrepend { flgs => conditional(flgs.x, uint8) :: conditional(flgs.y, uint8) :: conditional(flgs.z, uint8) } c.derive[Flags].from { case x :: y :: z :: HNil => Flags(x.isDefined, y.isDefined, z.isDefined) }
This codec, the Codec[L]
, is used for encoding/decoding. When decoding, the first value of type
A
is removed from the HList
.
When encoding, the returned codec computes an A
value using the supplied
function and inserts the computed A
in to the HList M
, yielding an HList L
. That HList L
is then encoded using the original codec.
This method is called derive
because the value of type A
is derived from the other fields
in the HList L
.
type to remove from L
and derive from the resulting list
Safely lifts this codec to a codec of a subtype.
Safely lifts this codec to a codec of a subtype.
When a supertype of B
that is not a supertype of A
is decoded,
an decoding error is returned.
Assuming A
is Unit
, creates a Codec[B]
that: encodes the unit followed by a B
;
decodes a unit followed by a B
and discards the decoded unit.
Assuming A
is Unit
, creates a Codec[B]
that: encodes the unit followed by a B
;
decodes a unit followed by a B
and discards the decoded unit.
Assuming B
is Unit
, creates a Codec[A]
that: encodes the A
followed by a unit;
decodes an A
followed by a unit and discards the decoded unit.
Assuming B
is Unit
, creates a Codec[A]
that: encodes the A
followed by a unit;
decodes an A
followed by a unit and discards the decoded unit.
Creates a new codec with all unit values filtered out.
Creates a new codec with all unit values filtered out.
Converts this GenCodec
to a GenCodec[C, B]
using the supplied C => Attempt[A]
.
Converts this GenCodec
to a GenCodec[A, C]
using the supplied B => Attempt[C]
.
Attempts to encode the specified value in to a bit vector.
Attempts to encode the specified value in to a bit vector.
error or binary encoding of the value
Converts this to a codec that fails decoding with an error.
Converts this to a codec that fails decoding with an error.
Transforms using two functions, A => Attempt[B]
and B => Attempt[A]
.
Transforms using two functions, A => Attempt[B]
and B => Attempt[A]
.
Curried version of exmap
.
Curried version of exmap
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList L
followed by the value A
, where the latter is encoded/decoded with the codec
returned from applying L
to f
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList L
followed by the value A
, where the latter is encoded/decoded with the codec
returned from applying L
to f
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList L
followed by the HList M
, where the latter is encoded/decoded with the codec
returned from applying L
to f
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that encodes/decodes
the HList L
followed by the HList M
, where the latter is encoded/decoded with the codec
returned from applying L
to f
.
Converts this decoder to a Decoder[B]
using the supplied A => Decoder[B]
.
Converts this decoder to a Decoder[B]
using the supplied A => Decoder[B]
.
Creates a new codec that encodes/decodes an HList
type of A :: L
given a function A => Codec[L]
.
Creates a new codec that encodes/decodes an HList
type of A :: L
given a function A => Codec[L]
.
This allows later parts of an HList
codec to be dependent on earlier values.
Returns a new codec that encodes/decodes a value of type (A, B)
where the codec of B
is dependent on A
.
Returns a new codec that encodes/decodes a value of type (A, B)
where the codec of B
is dependent on A
.
Creates a new codec that encodes/decodes an HList
type of A :: B :: HNil
given a function A => Codec[B]
.
Creates a new codec that encodes/decodes an HList
type of A :: B :: HNil
given a function A => Codec[B]
.
If B
is an HList
type, consider using flatPrepend
instead, which avoids nested HLists
.
This is the direct HList
equivalent of flatZip
.
Converts this codec to an HList
based codec by flattening all left nested pairs.
Converts this codec to an HList
based codec by flattening all left nested pairs.
For example, flattenLeftPairs
on a Codec[(((A, B), C), D)]
results in a
Codec[A :: B :: C :: D :: HNil]
. This is particularly useful when combined
with ~
, ~>
, and <~
.
Replaces the current framing logic with the specified codec transformation.
Replaces the current framing logic with the specified codec transformation.
Every representative codec is wrapped with the framing logic when encoding/decoding.
new framing logic
Converts this generalized codec in to a non-generalized codec assuming A
and B
are the same type.
Converts this generalized codec in to a non-generalized codec assuming A
and B
are the same type.
Lifts this codec in to a codec of a singleton hlist.
Lifts this codec in to a codec of a singleton hlist.
Converts this GenCodec
to a GenCodec[A, C]
using the supplied B => C
.
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
.
Curried version of narrow
.
Curried version of narrow
.
Creates a Codec[(A, B)]
that first encodes/decodes an A
followed by a B
.
Creates a Codec[(A, B)]
that first encodes/decodes an A
followed by a B
.
Converts this GenCodec
to a GenCodec[C, B]
using the supplied partial
function from C
to A
.
Polymorphic function version of xmap
.
Polymorphic function version of xmap
.
When called on a Codec[A]
where A
is not a subytpe of HList
, returns a new codec that's the result of
xmapping with p
and q
, using p
to convert from A
to B
and using q
to convert from
B
to A
.
polymorphic function that converts from A
to B
polymorphic function that converts from B
to A
Polymorphic function version of xmap
.
Polymorphic function version of xmap
.
When called on a Codec[L]
for some L <: HList
, returns a new codec that's the result of
xmapping with p
and q
, using p
to convert from L
to M
and using q
to convert from
M
to L
.
polymorphic function that converts from L
to M
polymorphic function that converts from M
to L
Polymorphic function version of xmap
that uses a single polymorphic function in both directions.
Polymorphic function version of xmap
that uses a single polymorphic function in both directions.
When called on a Codec[A]
where A
is not a subytpe of HList
, returns a new codec that's the result of
xmapping with p
for both forward and reverse directions.
polymorphic function that converts from A
to B
and from B
to A
Polymorphic function version of xmap
that uses a single polymorphic function in both directions.
Polymorphic function version of xmap
that uses a single polymorphic function in both directions.
When called on a Codec[L]
for some L <: HList
, returns a new codec that's the result of
xmapping with p
for both forward and reverse directions.
polymorphic function that converts from L
to M
and from M
to L
When called on a Decoder[C]
where C
is a coproduct containing type A
, converts to a Decoder[Option[A]]
.
When called on a Decoder[C]
where C
is a coproduct containing type A
, converts to a Decoder[Option[A]]
.
When called on a Encoder[C]
where C
is a coproduct containing type A
, converts to an Encoder[A]
.
When called on a Encoder[C]
where C
is a coproduct containing type A
, converts to an Encoder[A]
.
Provides a bound on the size of successfully encoded values.
Provides a bound on the size of successfully encoded values.
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
function used during encoding that converts an A
to an Option[R]
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value for this case
function used during encoding that converts an A
to an Option[R]
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
partial function from A
to R
used during encoding
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
representative type that this case handles
tag value for this case
partial function from A
to R
used during encoding
codec that encodes/decodes R
s
Lifts this codec to a codec of a shapeless field -- allowing it to be used in records and unions.
Lifts this codec to a codec of a shapeless field -- allowing it to be used in records and unions.
Lifts this codec to a codec of a shapeless field -- allowing it to be used in records and unions.
Lifts this codec to a codec of a shapeless field -- allowing it to be used in records and unions. The specified key is pushed in to the context of any errors that are returned from the resulting codec.
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Note: when encoding a value of A
, this combinator compares the runtime class of that value to the
runtime class of the supplied ClassTag[R]
. As such, the *erased* type of A
is used and hence,
this operation is not safe to use with parameterized representation types.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Note: when encoding a value of A
, this combinator compares the runtime class of that value to the
runtime class of the supplied ClassTag[R]
. As such, the *erased* type of A
is used and hence,
this operation is not safe to use with parameterized representation types.
representative type that this case handles
tag value for this case
codec that encodes/decodes R
s
Converts this to a Codec[Unit]
that encodes using the specified zero value and
decodes a unit value when this codec decodes an A
successfully.
Converts this to a Codec[Unit]
that encodes using the specified zero value and
decodes a unit value when this codec decodes an A
successfully.
Safely lifts this codec to a codec of a supertype.
Safely lifts this codec to a codec of a supertype.
When a subtype of B
that is not a subtype of A
is passed to encode,
an encoding error is returned.
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)
.
Curried version of widenOpt
.
Curried version of widenOpt
.
Curried version of widen
.
Curried version of widen
.
Creates a new codec that is functionally equivalent to this codec but pushes the specified context string in to any errors returned from encode or decode.
Creates a new codec that is functionally equivalent to this codec but pushes the specified context string in to any errors returned from encode or decode.
Creates a new codec that is functionally equivalent to this codec but returns the specified string from toString
.
Creates a new codec that is functionally equivalent to this codec but returns the specified string from toString
.
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
.
Curried version of xmap
.
Curried version of xmap
.
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for caseP
.
representative type that this case handles
tag value to use during encoding for this case
function that determines if this case should be used for decoding given a decoded tag
partial function from A
to R
used during encoding
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Returns a new discriminator codec with a new case added for the specified tag.
Returns a new discriminator codec with a new case added for the specified tag.
Operator alias for caseP
.
representative type that this case handles
tag value for this case
partial function from A
to R
used during encoding
function used during decoding that converts an R
to an A
codec that encodes/decodes R
s
Creates a Codec[(A, B)]
that first encodes/decodes an A
followed by a B
.
Creates a Codec[(A, B)]
that first encodes/decodes an A
followed by a B
.
Operator alias for pairedWith.
Assuming A
is Unit
, creates a Codec[B]
that: encodes the unit followed by a B
;
decodes a unit followed by a B
and discards the decoded unit.
Transforms using two functions, A => Attempt[B]
and B => Attempt[A]
.
Transforms using two functions, A => Attempt[B]
and B => Attempt[A]
.
(discriminatorCodec: TransformSyntax[Codec, A]).exmap(f, g)
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
.
(discriminatorCodec: TransformSyntax[Codec, A]).narrow(f, g)
(discriminatorCodec: Tuple2CodecSupport[A]).self
(discriminatorCodec: EnrichedCoproductDecoder[A]).self
(discriminatorCodec: EnrichedCoproductEncoder[Coproduct]).self
(discriminatorCodec: ValueCodecEnrichedWithGenericSupport[A]).self
(discriminatorCodec: ValueCodecEnrichedWithHListSupport[A]).self
(discriminatorCodec: HListCodecEnrichedWithHListSupport[A]).self
Supports TransformSyntax.
Supports TransformSyntax.
(discriminatorCodec: TransformSyntax[Codec, A]).self
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
.
(discriminatorCodec: TransformSyntax[Codec, A]).widen(f, g)
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
.
(discriminatorCodec: TransformSyntax[Codec, A]).xmap(f, g)
(discriminatorCodec: ValueEnrichedWithTuplingSupport[DiscriminatorCodec[A, B]]).~(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)
.
(Since version 1.7.0) Use widenOpt instead
Codec that supports the binary structure
tag ++ value
where thetag
identifies the encoding/decoding of the value.To build an instance of this codec, call discriminated and specify the tag type via the
by
method. Then call one more more of the case combinators on this class.Each of the case combinators provides two forms, one that defines a case with a single tag value and another, overloaded form that defines a case with a tag value to use in encoding and a predicate on tag value to use in decoding.
The most general case combinators are
caseO
andcaseP
(and their operator equivalents,?
and|
). In addition to a tag or tag/predicate pair, thecaseO
combinators are defined by providing a mapping fromA
toOption[R]
, a mapping fromR
toA
, and aCodec[R]
. The case is used for encoding if the mapping fromA
toOption[R]
returns aSome
and it is used for decoding upon matching the tag value. ThecaseP
combinators work the same but take aPartialFunction[A, R]
instead of anA => Option[R]
.If
R
is a subtype ofA
, then the mapping fromR
toA
can be omitted. Hence, thesubcaseO
andsubcaseP
(and the operator equivalents,/
and\
) constrainR
to being a subtype ofA
and do not take aR => A
function.Finally, the least generic case combinators are the
typecase
combinators which add further constraints to thesubcase*
combinators. Specifically, the typecase operators omit theA => Option[R]
orPartialFunction[A, R]
in favor of doing subtype checks. For example, the following codec is aCodec[AnyVal]
that encodes a 0 if passed aBoolean
and a 1 if passed anInt
:Often, the values are size-delimited -- that is, there is a
size
field after thetag
field and beforethe
valuefield. To support this, use the
framingmethod to provide a transformation to each value codec. For example,
framing(new CodecTransformation { def apply[X](c: Codec[X]) = variableSizeBytes(uint8, c) }).
discriminated