Skip to content

Commit c13cd4b

Browse files
jackkoenigSethTisue
authored andcommitted
Faster EntityKind deserialization
Split the string at the first colon rather than using regular expressions.
1 parent 76fd4dd commit c13cd4b

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/main/scala/com/lightbend/tools/sculpt/model/ModelJsonProtocol.scala

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,38 @@
22

33
package com.lightbend.tools.sculpt.model
44

5-
import com.lightbend.tools.sculpt.util.RegexInterpolator
65
import spray.json._
76

87
/** JSON serialization/deserialization for the Sculpt model types */
98
object ModelJsonProtocol extends DefaultJsonProtocol {
109

1110
implicit object entityFormat extends JsonFormat[Entity] {
1211
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+
}
2637
}
2738
}
2839

0 commit comments

Comments
 (0)