Skip to content

Commit

Permalink
make-structs cmd line option (#15)
Browse files Browse the repository at this point in the history
* add plugin option make-structs

* fix example

* Update protoc_gen_d/protoc-gen-d.d

Co-Authored-By: deviator <[email protected]>

* Update protoc_gen_d/protoc-gen-d.d

Co-Authored-By: deviator <[email protected]>

* fix naming
  • Loading branch information
deviator authored and dcarp committed Feb 11, 2019
1 parent e4a9b29 commit 0559ecb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
12 changes: 9 additions & 3 deletions examples/add_person.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import std.string;
import google.protobuf;
import tutorial.addressbook;

T make(T)()
{
static if (is(T == class)) return new T;
else return T.init;
}

/// This function fills in a Person message based on user input.
Person promptForAddress()
{
auto person = new Person;
auto person = make!Person;

write("Enter person ID number: ");
readf("%d", &person.id);
Expand All @@ -23,7 +29,7 @@ Person promptForAddress()

while (true)
{
auto phoneNumber = new Person.PhoneNumber;
auto phoneNumber = make!(Person.PhoneNumber);
write("Enter a phone number (or leave blank to finish): ");
phoneNumber.number = readln!string.strip;
if (phoneNumber.number.empty)
Expand Down Expand Up @@ -57,7 +63,7 @@ int main(string[] args)
return -1;
}

auto addressBook = new AddressBook;
auto addressBook = make!AddressBook;
try
{
auto input = File(args[1], "rb");
Expand Down
4 changes: 2 additions & 2 deletions examples/dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"sourceFiles": ["tutorial/addressbook.d"],
"sourcePaths": ["../src"],
"mainSourceFile": "add_person.d",
"preBuildCommands": ["protoc $$PROTO_PATH --plugin=../build/protoc-gen-d --d_out=. addressbook.proto"]
"preBuildCommands": ["protoc $$PROTO_PATH --plugin=../build/protoc-gen-d --d_opt=message-as-struct --d_out=. addressbook.proto"]
},
{
"name": "list_people",
Expand All @@ -18,7 +18,7 @@
"sourceFiles": ["tutorial/addressbook.d"],
"sourcePaths": ["../src"],
"mainSourceFile": "list_people.d",
"preBuildCommands": ["protoc $$PROTO_PATH --plugin=../build/protoc-gen-d --d_out=. addressbook.proto"]
"preBuildCommands": ["protoc $$PROTO_PATH --plugin=../build/protoc-gen-d --d_opt=message-as-struct --d_out=. addressbook.proto"]
},
]
}
22 changes: 18 additions & 4 deletions protoc_gen_d/protoc-gen-d.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ class CodeGeneratorException : Exception
class CodeGenerator
{
private enum indentSize = 4;
private bool messageAsStruct = false;

CodeGeneratorResponse handle(CodeGeneratorRequest request)
{
import std.algorithm : filter, map;
import std.algorithm : filter, map, canFind, splitter;
import std.array : array;
import std.conv : to;
import std.format : format;

if (request.parameter.splitter(",").canFind("message-as-struct"))
messageAsStruct = true;

if (request.compilerVersion) with (request.compilerVersion)
protocVersion = format!"%d%03d%03d"(major, minor, patch);

Expand Down Expand Up @@ -146,9 +150,18 @@ class CodeGenerator
if (messageType.isMap)
return "";

auto indentation = "%*s".format(indent, "");

auto result = appender!string;
result ~= "\n%*s%sclass %s\n".format(indent, "", indent > 0 ? "static " : "", messageType.name.escapeKeywords);
result ~= "%*s{\n".format(indent, "");
result ~= "\n";
result ~= indentation;
result ~= indent > 0 ? "static " : "";
result ~= messageAsStruct ? "struct" : "class";
result ~= " ";
result ~= messageType.name.escapeKeywords;
result ~= "\n";
result ~= indentation;
result ~= "{\n";

int[] generatedOneofs;
foreach (field; messageType.fields.sort!((a, b) => a.number < b.number))
Expand All @@ -173,7 +186,8 @@ class CodeGenerator
foreach (enumType; messageType.enumTypes)
result ~= generateEnum(enumType, indent + indentSize);

result ~= "%*s}\n".format(indent, "");
result ~= indentation;
result ~= "}\n";

return result.data;
}
Expand Down

0 comments on commit 0559ecb

Please sign in to comment.