-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-codecs.scala
executable file
·39 lines (31 loc) · 1.12 KB
/
generate-codecs.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
32
33
34
35
36
37
38
39
#!/bin/sh
exec scala "$0" "$@"
!#
def generateJsonCodecTupleN(n: Int): String = {
val types: String = (1 to n).map(i => s"T$i").mkString(", ")
val typesWithConstraints: String =
(1 to n).map(i => s"T$i: JsonCodec").mkString(", ")
val toJsonLines: String = (1 to n).map {
i => s""" "_$i" -> json.toJson[T$i](t._$i)"""
}.mkString(",\n")
val fromJsonLines: String = (1 to n).map {
i => s""" _$i <- (j ~> "_$i").flatMap(json.fromJson[T$i])"""
}.mkString("\n")
val components: String = (1 to n).map(i => s"_$i").mkString(", ")
s"""/**
| * Implements [[JsonCodec]] for `Tuple$n` whenever the component types
| * have instances of [[JsonCodec]] in implicit scope.
| */
|implicit def jsonCodecTuple$n[$typesWithConstraints]:
|JsonCodec[($types)] = new JsonCodec[($types)] {
| import JsonImplicits._
| def toJson(t: ($types)): Json = Json.obj(
|$toJsonLines
| )
| def fromJson(j: Json): Option[($types)] = for {
|$fromJsonLines
| } yield ($components)
|}
|""".stripMargin
}
(2 to 7).foreach { i => println(generateJsonCodecTupleN(i)) }