|
2 | 2 |
|
3 | 3 | package com.lightbend.tools.sculpt.model |
4 | 4 |
|
5 | | -import com.lightbend.tools.sculpt.util.RegexInterpolator |
6 | 5 | import spray.json._ |
7 | 6 |
|
8 | 7 | /** JSON serialization/deserialization for the Sculpt model types */ |
9 | 8 | object ModelJsonProtocol extends DefaultJsonProtocol { |
10 | 9 |
|
11 | 10 | implicit object entityFormat extends JsonFormat[Entity] { |
12 | 11 | def write(e: Entity) = new JsString(e.kind.prefix + ":" + e.name) |
13 | | - def read(value: JsValue) = value.convertTo[String] match { |
14 | | - case r"ov:(.*)$n" => Entity(n, EntityKind.Module) |
15 | | - case r"def:(.*)$n" => Entity(n, EntityKind.Method) |
16 | | - case r"var:(.*)$n" => Entity(n, EntityKind.Mutable) |
17 | | - case r"mac:(.*)$n" => Entity(n, EntityKind.Macro) |
18 | | - case r"pk:(.*)$n" => Entity(n, EntityKind.Package) |
19 | | - case r"t:(.*)$n" => Entity(n, EntityKind.Term) |
20 | | - case r"tr:(.*)$n" => Entity(n, EntityKind.Trait) |
21 | | - case r"pkt:(.*)$n" => Entity(n, EntityKind.PackageType) |
22 | | - case r"o:(.*)$n" => Entity(n, EntityKind.ModuleClass) |
23 | | - case r"cl:(.*)$n" => Entity(n, EntityKind.Class) |
24 | | - case r"tp:(.*)$n" => Entity(n, EntityKind.Type) |
25 | | - case _ => throw new DeserializationException("'EntityKind:Name' string expected") |
| 12 | + |
| 13 | + private object Kind { |
| 14 | + def unapply(str: String): Option[EntityKind] = str match { |
| 15 | + case "ov" => Some(EntityKind.Module) |
| 16 | + case "def" => Some(EntityKind.Method) |
| 17 | + case "var" => Some(EntityKind.Mutable) |
| 18 | + case "mac" => Some(EntityKind.Macro) |
| 19 | + case "pk" => Some(EntityKind.Package) |
| 20 | + case "t" => Some(EntityKind.Term) |
| 21 | + case "tr" => Some(EntityKind.Trait) |
| 22 | + case "pkt" => Some(EntityKind.PackageType) |
| 23 | + case "o" => Some(EntityKind.ModuleClass) |
| 24 | + case "cl" => Some(EntityKind.Class) |
| 25 | + case "tp" => Some(EntityKind.Type) |
| 26 | + case _ => None |
| 27 | + } |
| 28 | + } |
| 29 | + def read(value: JsValue) = { |
| 30 | + val valueString = value.convertTo[String] |
| 31 | + val idx = valueString.indexOf(':') |
| 32 | + valueString.splitAt(idx) match { |
| 33 | + case (Kind(kind), n) => |
| 34 | + Entity(n.tail, kind) // n includes ':' so take tail |
| 35 | + case _ => throw new DeserializationException("'EntityKind:Name' string expected") |
| 36 | + } |
26 | 37 | } |
27 | 38 | } |
28 | 39 |
|
|
0 commit comments