Decoder[A]
を使用して、任意のケース クラスの型 () のインスタンスを派生させたいと考えていますshapeless
。
trait Decoder[A] extends Serializable { self =>
def decode(source: String): Either[Throwable, A]
}
ケースクラスのデフォルト値を考慮しない場合、基本的なアプローチを使用してすべてがうまくいっています。
final implicit def genericDecoder[A, H <: HList](
implicit gen: LabelledGeneric.Aux[A, H],
hDecoder: Lazy[Decoder[H]]
): Decoder[A] = value => hDecoder.value.decode(value).map(gen.from)
final implicit val hnilDecoder: Decoder[HNil] = ???
final implicit def hlistDecoder[K <: Symbol, H, T <: HList](
implicit witness: Witness.Aux[K],
hDecoder: Lazy[Decoder[H]],
tDecoder: Lazy[Decoder[T]]
): Decoder[FieldType[K, H] :: T] = ???
今、デコードできないフィールドのデフォルト値を使用できるようにしたいと考えています。この場合、私はこのアプローチを試しました(追加の抽象化レイヤーを追加します):
trait DecoderWithDefaults[A, B] extends Serializable {
def decode(value: String, defaults: B): Either[Throwable, A]
}
final implicit def genericDecoder[A, H <: HList, HD <: HList](
implicit gen: LabelledGeneric.Aux[A, H],
defaults: Default.AsOptions.Aux[A, HD],
hDecoder: Lazy[DecoderWithDefaults[H, HD]]
): Decoder[A] = value => hDecoder.value.decode(value, defaults()).map(gen.from)
final implicit val hnilDecoder: DecoderWithDefaults[HNil, HNil] = (_, _) => Right(HNil)
final implicit def hlistDecoder[K <: Symbol, H, T <: HList, TD <: HList](
implicit witness: Witness.Aux[K],
hDecoder: Lazy[DecoderWithDefaults[H, Option[H]]],
tDecoder: Lazy[DecoderWithDefaults[T, TD]]
): DecoderWithDefaults[FieldType[K, H] :: T, Option[H] :: TD] = ???
だから、私の質問は、同じことを達成することは可能ですが、追加の抽象化レイヤーを使用せずに (DecoderWithDefaults
この場合のように) ですか? 何かのようなもの:
final implicit def hlistDecoder[K <: Symbol, H, T <: HList](
implicit witness: Witness.Aux[K],
defaultValueHead: Option[H],
hDecoder: Lazy[Decoder[H]],
tDecoder: Lazy[Decoder[T]]
): Decoder[FieldType[K, H] :: T] = ???