uPickle Writer for HList

276 Views Asked by At

I'm trying to create custom uPickle Writer for shapeless.HList transforming to simple array instead of complex nested object.

But i could not provide enough concrete evidence it could me mapped with my poly

I have this code:

import upickle.Js
import upickle.default._
import shapeless._
import poly._
import shapeless.ops.hlist.{ToTraversable, Mapper}

object writeItem extends Poly1 {
  implicit def item[I](implicit w: Writer[I]) = at[I](w.write)(writer.write)
}

implicit def hListWriter[L <: HList, Mappr <:  Mapper[writeItem.type, L] ]
(implicit mapper: Mappr,
 trav: ToTraversable.Aux[Mappr#Out, List, Js.Value]) = Writer[L] {
  l => Js.Arr(l.map(writeItem).toList[Js.Value]:_*)
}

And compilation error:

could not find implicit value for parameter toTraversableAux: shapeless.ops.hlist.ToTraversable.Aux[mapper.Out,List,upickle.Js.Value]

How could one define function to transform HList to uniform List via map?

1

There are 1 best solutions below

0
On

I found there is already Mapper.Aux for this purpose, resulting methid definition looks like:

  implicit def hListWriter[L <: HList, Out <: HList]
  (implicit mapper: Mapper.Aux[writeItem.type, L, Out],
   trav: ToTraversable.Aux[Out, List, Js.Value]) = Writer[L] {
    l => Js.Arr(l.map(writeItem).toList[Js.Value]:_*)