Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hinted deserializer support #82

Merged
merged 2 commits into from Jul 13, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -543,11 +543,25 @@ class Generator protected (sourceName: String, importedSymbols: Map[String, Impo
// generated output
output.append(generatedOutput).append("\n")

val messageNames = tree.collect { case m:Message => m }.map(_.name)
// outer object
output
.append("object ").append(className).append(" {\n")
.append("\tdef registerAllExtensions(registry: com.google.protobuf.ExtensionRegistryLite) {\n")
.append("\t}\n\n")

.append("\tprivate val fromBinaryHintMap = collection.immutable.HashMap[String, Array[Byte] ⇒ com.google.protobuf.GeneratedMessageLite](").append("\n")
.append(messageNames.map { m => "\t\t" + s""" "$m" -> (bytes ⇒ $m.parseFrom(bytes))""" } mkString(",\n"))
.append("\n\t)").append("\n")

.append("\n")
.append("\tdef deserializePayload(payload: Array[Byte], payloadType: String): com.google.protobuf.GeneratedMessageLite = {").append("\n")
.append("\t\tfromBinaryHintMap.get(payloadType) match {").append("\n")
.append("\t\t\tcase Some(f) ⇒ f(payload)").append("\n")
.append("\t\t\tcase None ⇒ throw new IllegalArgumentException(s\"unimplemented deserialization of message payload of type [${payloadType}]\")").append("\n")
.append("\t\t}").append("\n")
.append("\t}").append("\n")

.append("}\n")

ScalaClass(output.mkString, packageName.replace('.', File.separatorChar) + File.separatorChar, className)
Expand Down