Skip to content

Commit

Permalink
Add missing packed flag in code generator
Browse files Browse the repository at this point in the history
Fix #22
  • Loading branch information
dcarp committed Jun 13, 2019
1 parent 4c62fb7 commit 5f57d7d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
10 changes: 9 additions & 1 deletion protoc_gen_d/protoc-gen-d.d
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,15 @@ class CodeGenerator
import std.conv : to;
import std.range : join;

return [field.number.to!string, wireByField(field).toString].stripRight("").stripRight("Wire.none").join(", ");
static string packedByField(FieldDescriptorProto field)
{
return (field.options && field.options.packed) ? "Yes.packed" : "No.packed";
}

return [field.number.to!string, wireByField(field).toString, packedByField(field)]
.stripRight("No.packed")
.stripRight("Wire.none")
.join(", ");
}

private string fieldInitializer(FieldDescriptorProto field)
Expand Down
5 changes: 5 additions & 0 deletions src/google/protobuf/common.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module google.protobuf.common;

static import std.typecons;

alias No = std.typecons.No;
alias Yes = std.typecons.Yes;

alias bytes = ubyte[];

auto protoDefaultValue(T)()
Expand Down
10 changes: 10 additions & 0 deletions test/generated_code.d
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ class ForeignMessage
@Proto(1) int c = protoDefaultValue!int;
}

class PackedRepeated
{
@Proto(1) int[] defaultPackedInts = protoDefaultValue!(int[]);
@Proto(2, Wire.none, Yes.packed) int[] packedInts = protoDefaultValue!(int[]);
@Proto(3) int[] nonPackedInts = protoDefaultValue!(int[]);
@Proto(4) bool[] defaultPackedBools = protoDefaultValue!(bool[]);
@Proto(5, Wire.none, Yes.packed) bool[] packedBools = protoDefaultValue!(bool[]);
@Proto(6) bool[] nonPackedBools = protoDefaultValue!(bool[]);
}

enum ForeignEnum
{
FOREIGN_FOO = 0,
Expand Down
10 changes: 10 additions & 0 deletions test/generated_code.proto
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ enum ForeignEnum {
FOREIGN_BAR = 1;
FOREIGN_BAZ = 2;
}

message PackedRepeated {
repeated int32 defaultPackedInts = 1;
repeated int32 packedInts = 2 [packed=true];
repeated int32 nonPackedInts = 3 [packed=false];

repeated bool defaultPackedBools = 4;
repeated bool packedBools = 5 [packed=true];
repeated bool nonPackedBools = 6 [packed=false];
}

0 comments on commit 5f57d7d

Please sign in to comment.