-
Notifications
You must be signed in to change notification settings - Fork 22
/
FamilyFormatsSpec.scala
283 lines (237 loc) · 10.2 KB
/
FamilyFormatsSpec.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package fommil.sjs
import org.scalatest._
import spray.json._
import shapeless._
import java.util.UUID
// Example domain models used in the tests. Note that the domain model
// and formatters are defined in sibling packages.
package examples {
sealed trait SimpleTrait
case class Foo(s: String) extends SimpleTrait
case class Bar() extends SimpleTrait
case object Baz extends SimpleTrait
case class Faz(o: Option[String]) extends SimpleTrait
sealed trait SubTrait extends SimpleTrait
case object Fuzz extends SubTrait
sealed trait Spiel
case object Buzz extends Spiel
case class Schpugel(v: String) // I asked my wife to make up a word
case class Smim(v: String) // I should stop asking my wife to make up words
sealed trait StringEnum {
def label: String
}
case class Flooma(label: String) extends StringEnum // to be fair, that was mine
case class Blam(label: String) extends StringEnum
sealed trait Cloda
case class Plooba(thing: String) extends Cloda // *sigh*
object Quack
case class Huey(duck: Quack.type, witch: Option[Quack.type])
case class Dewey(duck: Quack.type, witch: Option[Quack.type])
case class Louie(duck: Quack.type, witch: Option[Quack.type])
// I love monkeys, you got a problem with that?
sealed trait Primates
sealed trait Strepsirrhini extends Primates
sealed trait Haplorhini extends Primates
sealed trait Tarsiiformes extends Haplorhini
case object Tarsiidae extends Tarsiiformes
sealed trait Simiiformes extends Haplorhini
sealed trait Platyrrhini extends Simiiformes
case object Callitrichidae extends Platyrrhini
case object Cebidae extends Platyrrhini
case object Aotidae extends Platyrrhini
case object Pitheciidae extends Platyrrhini
case object Atelidae extends Platyrrhini
sealed trait Catarrhini extends Simiiformes
sealed trait Cercopithecoidea extends Catarrhini
case object Cercopithecidae extends Cercopithecoidea
sealed trait Hominoidea extends Catarrhini
case object Hylobatidae extends Hominoidea
case class Hominidae(id: UUID) extends Hominoidea
}
object ExamplesFormats extends DefaultJsonProtocol with FamilyFormats {
import examples._
// wtf?? why is this needed, why does it even work? Miles??
implicit val symbolFormat = SymbolJsonFormat
///////////////////////////////////////////////
// Example of "explicit implicit" for performance
implicit val SimpleTraitFormat: RootJsonFormat[SimpleTrait] = cachedImplicit
///////////////////////////////////////////////
// user-defined hinting
implicit object SubTraitHint extends FlatCoproductHint[SubTrait]("hint")
implicit object SpielHint extends NestedCoproductHint[Spiel]
///////////////////////////////////////////////
// user-defined field naming rules
implicit object ClodaHint extends FlatCoproductHint[Cloda]("TYPE") {
override def fieldName(orig: String): String = orig.toUpperCase
}
implicit object PloobaHint extends ProductHint[Plooba] {
override def fieldName[K <: Symbol](k: K): String = k.name.toUpperCase
}
///////////////////////////////////////////////
// user-defined /missing value rules
implicit object HueyHint extends ProductHint[Huey] {
override def nulls = AlwaysJsNull
}
implicit object DeweyHint extends ProductHint[Dewey] {
override def nulls = JsNullNotNone
}
implicit object LouieHint extends ProductHint[Louie] {
override def nulls = NeverJsNull
}
implicit object QuackFormat extends JsonFormat[Quack.type] {
// needed something that would serialise to JsNull for testing
def read(j: JsValue): Quack.type = j match {
case JsNull => Quack
case other => deserializationError(s"unexpected $other")
}
def write(q: Quack.type): JsValue = JsNull
}
///////////////////////////////////////////////
// user-defined JsonFormat
implicit object SchpugelFormat extends JsonFormat[Schpugel] {
def read(j: JsValue): Schpugel = j match {
case JsString(v) => Schpugel(v)
case other => deserializationError(s"unexpected $other")
}
def write(s: Schpugel): JsValue = JsString(s.v)
}
///////////////////////////////////////////////
// user-defined RootJsonFormat
implicit object SmimFormat extends RootJsonFormat[Smim] {
def read(j: JsValue): Smim = j match {
case JsObject(els) if els.contains("smim") =>
els("smim") match {
case JsString(v) => Smim(v)
case other => deserializationError(s"unexpected $other")
}
case other => deserializationError(s"unexpected $other")
}
def write(s: Smim): JsValue = JsObject("smim" -> JsString(s.v))
}
///////////////////////////////////////////////
// non-trivial user-defined JsonFormat
implicit def stringEnumFormat[T <: StringEnum](
implicit
g: Generic.Aux[T, String :: HNil],
tpe: Typeable[T]
): JsonFormat[T] = new JsonFormat[T] {
def read(json: JsValue): T = json match {
case JsString(value) => g.from(value :: HNil)
case _ => deserializationError("expected JsString, got " + json)
}
def write(obj: T): JsValue = JsString(obj.label)
}
}
class FamilyFormatsSpec extends FlatSpec with Matchers
with SprayJsonTestSupport {
import examples._
import ExamplesFormats._
"FamilyFormats" should "support case objects" in {
roundtrip(Baz, "{}")
}
it should "support symbols, provided by base spray-json" in {
// any use of Symbol seems to need the hack above. Known caveat.
roundtrip('foo, """"foo"""")
}
it should "support case classes" in {
roundtrip(Foo("foo"), """{"s":"foo"}""")
roundtrip(Bar(), "{}")
}
it should "support optional parameters on case classes" in {
roundtrip(Faz(Some("meh")), """{"o":"meh"}""") // note uses optionFormat, not familyFormat
roundtrip(Faz(None), "{}") // should be omitted, not "null"
}
it should "fail when missing required fields" in {
intercept[DeserializationException] {
"""{}""".parseJson.convertTo[Foo]
}
}
it should "support simple sealed families" in {
roundtrip(Foo("foo"): SimpleTrait, """{"type":"Foo","s":"foo"}""")
roundtrip(Bar(): SimpleTrait, """{"type":"Bar"}""")
roundtrip(Baz: SimpleTrait, """{"type":"Baz"}""")
roundtrip(Fuzz: SimpleTrait, """{"type":"Fuzz"}""")
}
it should "fail when missing required coproduct disambiguators" in {
intercept[DeserializationException] {
"""{"s":"foo"}""".parseJson.convertTo[SimpleTrait]
}
}
it should "support custom coproduct keys" in {
roundtrip(Fuzz: SubTrait, """{"hint":"Fuzz"}""")
roundtrip(Buzz: Spiel, """{"Buzz":{}}""")
}
it should "support custom coproduct field naming rules" in {
roundtrip(Plooba("poo"): Cloda, """{"TYPE":"PLOOBA","THING":"poo"}""")
}
it should "support custom product field naming rules" in {
roundtrip(Plooba("poo"), """{"THING":"poo"}""")
}
it should "support custom missing value rules" in {
roundtrip(Huey(Quack, None), """{"duck":null,"witch":null}""")
roundtrip(Dewey(Quack, None), """{"duck":null}""")
roundtrip(Louie(Quack, None), """{}""")
val json = """{"duck":null,"witch":null}""".parseJson
json.convertTo[Huey] shouldBe Huey(Quack, None)
json.convertTo[Dewey] shouldBe Dewey(Quack, Some(Quack))
json.convertTo[Louie] shouldBe Louie(Quack, None)
}
it should "fail when missing required (null) values" in {
val noduck = """{"witch":null}""".parseJson
val nowitch = """{"duck":null}""".parseJson
intercept[DeserializationException] {
noduck.convertTo[Huey]
}
intercept[DeserializationException] {
noduck.convertTo[Dewey]
}
noduck.convertTo[Louie] shouldBe Louie(Quack, None)
intercept[DeserializationException] {
nowitch.convertTo[Huey]
}
nowitch.convertTo[Dewey] shouldBe Dewey(Quack, None)
nowitch.convertTo[Louie] shouldBe Louie(Quack, None)
}
it should "prefer user customisable JsonFormats" in {
roundtrip(Schpugel("foo"), """"foo"""")
}
it should "prefer user customisable RootJsonFormats" in {
roundtrip(Smim("foo"), """{"smim":"foo"}""")
}
it should "prefer non-trivial user customisable JsonFormats" in {
roundtrip(Flooma("aha"), """"aha"""")
// This might surprise you: the user's custom formatter for the
// classes in this family is a `JsonFormat` (not a
// `RootJsonFormat`), so when we serialise the trait, it is forced
// to fall back to the derived product rule, not the custom one,
// because it expects a `RootJsonFormat`. Funky.
// WORKAROUND: https://github.com/fommil/spray-json-shapeless/issues/5
//roundtrip(Flooma("aha"): StringEnum, """{"type":"Flooma","label":"aha"}""")
}
it should "fail to compile when a member of the family cannot be serialised" in {
// this is an example of when this library can be very
// frustrating. The compiler error when an implicit cannot be
// created is always the least specific type. Here we're missing a
// formatter for UUIDs but the compiler warns about Primates. If
// we narrow it down to Hominidae it also errors... but finding
// these problems is a human driven search game.
shapeless.test.illTyped(
"""roundtrip(Hominidae(UUID.randomUUID): Primates)""",
".*could not find implicit value for evidence parameter of type spray.json.JsonFormat\\[fommil.sjs.examples.Primates\\].*"
)
shapeless.test.illTyped(
"""roundtrip(Hominidae(UUID.randomUUID))""",
".*could not find implicit value for evidence parameter of type spray.json.JsonFormat\\[fommil.sjs.examples.Hominidae\\].*"
)
}
///////////////////////////////////////////////
// non-trivial AST (in separate file)
it should "support an example ADT" in {
import ExampleAst._
roundtrip(SpecialToken: TokenTree, """{"type":"SpecialToken"}""")
val fieldTerm = FieldTerm("thing is ten", DatabaseField("THING"), "10")
roundtrip(fieldTerm: TokenTree, """{"type":"FieldTerm","text":"thing is ten","field":{"column":"THING"},"value":"10"}""")
val and = AndCondition(fieldTerm, fieldTerm, "wibble")
roundtrip(and: TokenTree, """{"type":"AndCondition","left":{"type":"FieldTerm","text":"thing is ten","field":{"column":"THING"},"value":"10"},"right":{"type":"FieldTerm","text":"thing is ten","field":{"column":"THING"},"value":"10"},"text":"wibble"}""")
}
}