-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.scala
31 lines (22 loc) · 854 Bytes
/
common.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package ncreep.common
import io.circe.*
import scala.compiletime.*
inline def tupleToJson(tuple: Tuple): List[JsonObject] =
inline tuple match
case EmptyTuple => Nil
case tup: (h *: t) =>
val encoder = summonInline[Encoder.AsObject[h]]
val json = encoder.encodeObject(tup.head)
json :: tupleToJson(tup.tail)
// a helper for pattern-matching
trait Is[A]
inline def decodeTuple[T <: Tuple]: Decoder[T] =
inline erasedValue[Is[T]] match
case _: Is[EmptyTuple] => Decoder.const(EmptyTuple)
case _: Is[h *: t] =>
val decoder = summonInline[Decoder[h]]
combineDecoders(decoder, decodeTuple[t])
def concatObjects(jsons: List[JsonObject]): Json =
Json.obj(jsons.flatMap(_.toList): _*)
def combineDecoders[H, T <: Tuple](dh: Decoder[H], dt: Decoder[T]): Decoder[H *: T] =
dh.product(dt).map(_ *: _)