-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
forProductN.sc
101 lines (88 loc) · 2.81 KB
/
forProductN.sc
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
def generateProductToJson = {
val out = new StringBuilder()
out ++= "package nrktkt.ninny\n"
out ++= "trait ProductToJson {\n"
for (i <- 1 to 22) {
out ++= s"def forProduct${i}[Target, "
val aTypes = (0 until i).map(j => s"A$j").mkString(", ")
out ++= aTypes
out ++= s"]("
out ++= (0 until i).map(j => s"nameA$j: String").mkString(", ")
out ++= s")(f: Target => ("
out ++= aTypes
out ++= "))(implicit "
out ++=
(0 until i)
.map(j => s"a${j}ToJson: ToJson[A$j]")
.mkString(", ")
out ++= "): ToSomeJsonObject[Target] = target => {\n"
out ++= "val ("
out ++= (0 until i).map(j => s"a$j").mkString(", ")
out ++= ") = f(target)\n"
out ++= "obj("
out ++= (0 until i).map(j => s"(nameA$j ~> a$j)").mkString(", ")
out ++= ")\n}\n"
}
out += '}'
out.result()
}
def generateProductFromJson = {
val out = new StringBuilder()
out ++= "package nrktkt.ninny\n"
out ++= "trait ProductFromJson {\n"
for (i <- 1 to 22) {
out ++= s"def forProduct${i}[Target, "
val aTypes = (0 until i).map(j => s"A$j").mkString(", ")
out ++= aTypes
out ++= s"]("
out ++= (0 until i).map(j => s"nameA$j: String").mkString(", ")
out ++= s")(f: ("
out ++= aTypes
out ++= ") => Target)(implicit "
out ++=
(0 until i).map(j => s"a${j}FromJson: FromJson[A$j]").mkString(", ")
out ++= "): FromJson[Target] = json => for {\n"
out ++=
(0 until i)
.map(j => s"a$j <- (json / nameA$j).to[A$j]")
.mkString("\n")
out ++= "}\nyield f("
out ++= (0 until i).map(j => s"a$j").mkString(", ")
out ++= ")\n"
}
out += '}'
out.result()
}
def generateProductToAndFromJson = {
val out = new StringBuilder()
out ++= "package nrktkt.ninny\n"
out ++= "import nrktkt.ninny.ast._\n"
out ++= "trait ProductToAndFromJson {\n"
for (i <- 1 to 22) {
out ++= s"def forProduct${i}[Target, "
val aTypes = (0 until i).map(j => s"A$j").mkString(", ")
out ++= aTypes
out ++= s"]("
out ++= (0 until i).map(j => s"nameA$j: String").mkString(", ")
out ++= s")(fFrom: ("
out ++= aTypes
out ++= ") => Target,\n"
out ++= "fTo: Target => ("
out ++= s"$aTypes))(implicit "
out ++=
(0 until i)
.map(j => s"a${j}ToAndFromJson: ToAndFromJson[A$j]")
.mkString(", ")
out ++= ") = new ToAndFromJson[Target] {\n"
val namesCsv = (0 until i).map(j => s"nameA$j").mkString(", ")
out ++= s"val _toJson: ToSomeJson[Target] = ToJson.forProduct$i($namesCsv)(fTo)\n"
out ++= s"val _fromJson: FromJson[Target] = FromJson.forProduct$i($namesCsv)(fFrom)\n"
out ++= """
def from(json: Option[JsonValue]) = _fromJson.from(json)
def toSome(target: Target) = _toJson.toSome(target)
"""
out ++= "}\n"
}
out += '}'
out.result()
}