@@ -6,6 +6,13 @@ import shapeless.labelled.FieldType
6
6
/** Automatically derive RLP codecs for case classes. */
7
7
object RLPImplicitDerivations {
8
8
9
+ case class DerivationPolicy (
10
+ // Whether to treat optional fields at the end of the list like
11
+ // they can be omitted from the RLP list, or inserted as a value,
12
+ // as opposed to a list of 0 or 1 items.
13
+ omitTrailingOptionals : Boolean
14
+ )
15
+
9
16
/** Support introspecting on what happened during encoding the tail. */
10
17
case class FieldInfo (isOptional : Boolean )
11
18
@@ -41,14 +48,15 @@ object RLPImplicitDerivations {
41
48
implicit def deriveOptionHListRLPListEncoder [K , H , T <: HList ](implicit
42
49
hEncoder : Lazy [RLPEncoder [H ]],
43
50
tEncoder : Lazy [RLPListEncoder [T ]],
44
- ev : H <:< Option [_]
51
+ ev : H <:< Option [_],
52
+ policy : DerivationPolicy
45
53
): RLPListEncoder [FieldType [K , H ] :: T ] = {
46
54
val hInfo = FieldInfo (isOptional = true )
47
55
// Create an encoder that takes a list of field values.
48
56
RLPListEncoder { case head :: tail =>
49
57
val (tRLP, tInfos) = tEncoder.value.encodeList(tail)
50
58
val htRLP =
51
- if (tInfos.forall(_.isOptional)) {
59
+ if (policy.omitTrailingOptionals && tInfos.forall(_.isOptional)) {
52
60
// This is still a trailing optional field, so we can insert it as a value or omit it.
53
61
hEncoder.value.encode(head) match {
54
62
case RLPList (hRLP) =>
0 commit comments