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

Generate Typescript definition for top level Enums #404

Merged
merged 6 commits into from
Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions javascript/net/grpc/web/grpc_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,30 @@ std::map<string, const Descriptor*> GetAllMessages(const FileDescriptor* file) {
return message_types;
}

void RegisterEnum(const EnumDescriptor* desc, std::map<string, const EnumDescriptor*>& enum_types) {
if (enum_types.count(desc->full_name()) != 0) {
return;
}

enum_types[desc->full_name()] = desc;
}

std::map<string, const EnumDescriptor*> GetAllTopLevelEnums(std::map<string, const Descriptor*> messages) {
std::map<string, const EnumDescriptor*> enum_types;
for (std::map<string, const Descriptor*>::iterator it = messages.begin();
it != messages.end(); it++) {
for (int i = 0; i < it->second->field_count(); i++) {
const FieldDescriptor* field = it->second->field(i);
if (field->type() != FieldDescriptor::Type::TYPE_ENUM || field->enum_type()->containing_type()) {
continue;
}

RegisterEnum(field->enum_type(), enum_types);
}
}
return enum_types;
}

void PrintMessagesDeps(Printer* printer, const FileDescriptor* file) {
std::map<string, const Descriptor*> messages = GetAllMessages(file);
std::map<string, string> vars;
Expand Down Expand Up @@ -675,6 +699,12 @@ void PrintProtoDtsFile(Printer *printer, const FileDescriptor *file)
it != messages.end(); it++) {
PrintProtoDtsMessage(printer, it->second, "");
}

std::map<string, const EnumDescriptor *> enums = GetAllTopLevelEnums(messages);
for (std::map<string, const EnumDescriptor *>::iterator it = enums.begin();
it != enums.end(); it++) {
PrintProtoDtsEnum(printer, it->second);
}
}

void PrintFileHeader(Printer* printer, const std::map<string, string>& vars) {
Expand Down
3 changes: 2 additions & 1 deletion packages/grpc-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"google-protobuf": "^3.6.1",
"mocha": "^5.2.0",
"mock-xmlhttprequest": "^2.0.0",
"require-self": "^0.2.1"
"require-self": "^0.2.1",
"typescript": "^3.2.1"
}
}
6 changes: 6 additions & 0 deletions packages/grpc-web/test/generated_code_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,4 +374,10 @@ describe('grpc-web generated code (commonjs+dts)', function() {
assert.equal(true, fs.existsSync(protoGenCodePath));
assert.equal(true, fs.existsSync(protoDtsGenCodePath));
});

it('should compile', function() {
execSync(genCodeCmd);
execSync(`tsc ${genDtsCodePath}`);
execSync(`tsc ${protoDtsGenCodePath}`);
})
});
19 changes: 19 additions & 0 deletions packages/grpc-web/test/protos/echo.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,27 @@ message ServerStreamingEchoResponse {
string message = 1;
}

enum Status {
UNKNOWN = 0;
SUCCESS = 1;
}

message EchoStatusRequest {
Status status = 1;
}

message EchoStatusResponse {
enum InternalStatus {
UNKNOWN = 0;
SUCCESS = 1;
}
InternalStatus status = 1;
}


service EchoService {
rpc Echo(EchoRequest) returns (EchoResponse);
rpc ServerStreamingEcho(ServerStreamingEchoRequest)
returns (stream ServerStreamingEchoResponse);
rpc EchoStatus(EchoStatusRequest) returns (EchoStatusResponse);
}