Skip to content

Commit

Permalink
Merge pull request #88 from orElse/master
Browse files Browse the repository at this point in the history
Write Support for packed fields
  • Loading branch information
SandroGrzicic committed Feb 9, 2015
2 parents 74a92f1 + cbe0ae0 commit ad1694a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,28 @@ class Generator protected (sourceName: String, importedSymbols: Map[String, Impo
.append("output.write").append(field.fType.name).append("(")
.append(field.number).append(", ").append(field.name.toScalaIdent).append(".get)\n")
case REPEATED =>
out.append(indent2).append("for (_v <- ")
.append(field.name.toScalaIdent).append(") ")
.append("output.write").append(field.fType.name)
out.append("(").append(field.number).append(", _v)\n")
(field.fType.packable, field.options.filter(value => value.key == "packed" && value.value == "true").headOption) match {
case (true, Some(option)) =>
out.append(indent2).append(s"// write field ${field.name} packed \n")
out.append(indent2).append("if (!").append(field.name.toScalaIdent).append(".isEmpty) {\n")
out.append(indent3).append("import com.google.protobuf.CodedOutputStream._\n")
out.append(indent3).append("val dataSize = ").append(field.name.toScalaIdent)
.append(".map(compute").append(field.fType.name).append("SizeNoTag(_)).sum")
.append(" \n")
out.append(indent3).append("output.writeRawVarint32(")
.append((field.number << 3) | WIRETYPE_LENGTH_DELIMITED).append(")").append("\n")
out.append(indent3).append("output.writeRawVarint32(dataSize)").append("\n")
out.append(indent3).append("for (_v <- ")
.append(field.name.toScalaIdent).append(") ")
.append("output.write").append(field.fType.name).append("NoTag")
.append("(_v)\n")
out.append(indent2).append("}\n")
case _ =>
out.append(indent2).append("for (_v <- ")
.append(field.name.toScalaIdent).append(") ")
.append("output.write").append(field.fType.name)
out.append("(").append(field.number).append(", _v)\n")
}
case _ => // "missing combination <local child>"
}
}
Expand All @@ -216,10 +234,24 @@ class Generator protected (sourceName: String, importedSymbols: Map[String, Impo
.append(field.name.toScalaIdent).append(".isDefined) ")
.append("__size += compute").append(field.fType.name).append("Size(")
.append(field.number).append(", ").append(field.name.toScalaIdent).append(".get)\n")
case REPEATED => out.append(indent2).append("for (_v <- ")
.append(field.name.toScalaIdent).append(") ")
.append("__size += compute").append(field.fType.name).append("Size(")
.append(field.number).append(", _v)\n")
case REPEATED =>
// TODO make this nicer currently code is generated 2 times
(field.fType.packable, field.options.filter(value => value.key == "packed" && value.value == "true").headOption) match {
case (true, Some(option)) =>
out.append(indent2).append("if (!").append(field.name.toScalaIdent).append(".isEmpty) {\n")
out.append(indent3).append("val dataSize = ").append(field.name.toScalaIdent)
.append(".map(compute").append(field.fType.name).append("SizeNoTag(_)).sum")
.append(" \n")

val tagSize = CodedOutputStream.computeTagSize(field.number)
out.append(indent3).append(s"__size += $tagSize + computeInt32SizeNoTag(dataSize) + dataSize\n")
out.append(indent2).append("}\n")
case _ =>
out.append(indent2).append("for (_v <- ")
.append(field.name.toScalaIdent).append(") ")
.append("__size += compute").append(field.fType.name).append("Size(")
.append(field.number).append(", _v)\n")
}
case _ => // "missing combination <local child>"
}
}
Expand Down
1 change: 1 addition & 0 deletions scalabuff-compiler/src/test/resources/parsed/packed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
List(PackageStatement(resources.generated), OptionValue(optimize_for,LITE_RUNTIME), Message(PackedTest,MessageBody(List(Field(required,Int32,required_field,1,List(),), Field(optional,Float,optional_field,2,List(),), Field(repeated,Int32,repeated_packed_field,3,List(OptionValue(packed,true)),)),List(),List(),List(),List(),List(),List())))
11 changes: 11 additions & 0 deletions scalabuff-compiler/src/test/resources/proto/packed.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package resources.generated;

option optimize_for = LITE_RUNTIME;

message PackedTest {
required int32 required_field = 1;
optional float optional_field = 2;
repeated int32 repeated_packed_field = 3 [packed=true] ;
// has data size 2
// repeated int32 repeated_packed_field_33 = 33 [packed=true] ;
}
8 changes: 8 additions & 0 deletions scalabuff-compiler/src/test/tests/ScalaBuffTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class ScalaBuffTest extends FunSuite with ShouldMatchers {

val testProtoMulti = "multi_one"

val testProtoPacked = "packed"

test("apply: simple .proto file") {
val settings = ScalaBuff.Settings(generateJsonMethod = true)
val scalaClass: ScalaClass = ScalaBuff(new File(protoDir + testProto + ".proto"))(settings)
Expand Down Expand Up @@ -173,5 +175,11 @@ class ScalaBuffTest extends FunSuite with ShouldMatchers {
}
}

test("apply: packed .proto file") {

val settings = ScalaBuff.Settings(generateJsonMethod = true)
val scalaClass: ScalaClass = ScalaBuff(new File(protoDir + testProtoPacked + ".proto"))(settings)
// TODO matches
// println(scalaClass)
}
}

0 comments on commit ad1694a

Please sign in to comment.