Skip to content

Commit 80363dd

Browse files
committed
Minor improvement
1 parent 3d19059 commit 80363dd

File tree

2 files changed

+65
-47
lines changed

2 files changed

+65
-47
lines changed

compiler/src/dlang_plugin.cc

Lines changed: 51 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ using grpc::protobuf::io::StringOutputStream;
3333
using grpc::protobuf::io::ZeroCopyOutputStream;
3434

3535

36-
37-
38-
39-
4036
static void GenerateService(const std::string &module , CodedOutputStream &cos,const ServiceDescriptor *service)
4137
{
4238
/// client
39+
40+
cos.WriteString("/**\n");
41+
cos.WriteString(" *\n");
42+
cos.WriteString(" */\n");
43+
4344
cos.WriteString("class " + service->name() + "Client\n");
4445
cos.WriteString("{\n");
4546
cos.WriteString("\tthis(Channel channel)\n");
@@ -59,34 +60,34 @@ static void GenerateService(const std::string &module , CodedOutputStream &cos,c
5960

6061
if (m->client_streaming() && m->server_streaming())
6162
{
62-
cos.WriteString("\tClientReaderWriter!(" + res + " ," + req + ") " + func + "(){\n");
63-
cos.WriteString("\t\tmixin(CM3!(" + res + " , " + req + " , " + service->name() + "Base.SERVICE));\n");
63+
cos.WriteString("\tClientReaderWriter!(" + res + ", " + req + ") " + func + "(){\n");
64+
cos.WriteString("\t\tmixin(CM3!(" + res + ", " + req + " , " + service->name() + "Base.SERVICE));\n");
6465
cos.WriteString("\t}\n");
6566
}
6667
else if (m->client_streaming())
6768
{
68-
cos.WriteString("\tClientWriter!" + req + " " + func + "( ref " + res + " response ){\n");
69-
cos.WriteString("\t\tmixin(CM2!(" + req + " , " + service->name() + "Base.SERVICE));\n");
69+
cos.WriteString("\tClientWriter!" + req + " " + func + "(ref " + res + " response ){\n");
70+
cos.WriteString("\t\tmixin(CM2!(" + req + ", " + service->name() + "Base.SERVICE));\n");
7071
cos.WriteString("\t}\n");
7172
}
7273
else if (m->server_streaming())
7374
{
7475
cos.WriteString("\tClientReader!" +res+ " " + func + "(" + req + " request ){\n");
75-
cos.WriteString("\t\tmixin(CM1!(" + res + " , " + service->name() + "Base.SERVICE));\n");
76+
cos.WriteString("\t\tmixin(CM1!(" + res + ", " + service->name() + "Base.SERVICE));\n");
7677
cos.WriteString("\t}\n");
7778
}
7879
else {
7980

80-
cos.WriteString("\t"+ res + " " + func + "( " + req + " request)\n");
81+
cos.WriteString("\t"+ res + " " + func + "(" + req + " request)\n");
8182
cos.WriteString("\t{\n");
82-
cos.WriteString("\t\tmixin(CM!("+ res +" , " + service->name() + "Base.SERVICE));\n");
83+
cos.WriteString("\t\tmixin(CM!("+ res +", " + service->name() + "Base.SERVICE));\n");
8384
cos.WriteString("\t}\n");
8485

8586
cos.WriteString("\n");
8687

87-
cos.WriteString("\tvoid " + func + "( " + req + " request , void delegate(Status status , " + res + " response) dele)\n");
88+
cos.WriteString("\tvoid " + func + "(" + req + " request , void delegate(Status status, " + res + " response) dele)\n");
8889
cos.WriteString("\t{\n");
89-
cos.WriteString("\t\tmixin(CMA!(" + res + " , " + service->name() + "Base.SERVICE));\n");
90+
cos.WriteString("\t\tmixin(CMA!(" + res + ", " + service->name() + "Base.SERVICE));\n");
9091
cos.WriteString("\t}\n");
9192

9293
cos.WriteString("\n");
@@ -100,10 +101,15 @@ static void GenerateService(const std::string &module , CodedOutputStream &cos,c
100101
cos.WriteString("\tChannel _channel;\n");
101102

102103
cos.WriteString("}\n");
103-
cos.WriteString("\n");
104+
105+
cos.WriteString("\n\n");
104106

105107

106108
/// service
109+
110+
cos.WriteString("/**\n");
111+
cos.WriteString(" *\n");
112+
cos.WriteString(" */\n");
107113
cos.WriteString("class " + service->name() + "Base: GrpcService\n");
108114
cos.WriteString("{\n");
109115
cos.WriteString("\tenum SERVICE = \"" + module + "." + service->name() + "\";");
@@ -123,25 +129,25 @@ static void GenerateService(const std::string &module , CodedOutputStream &cos,c
123129
auto func = m->name();
124130
if (m->client_streaming() && m->server_streaming())
125131
{
126-
cos.WriteString("\tStatus " + func + "(ServerReaderWriter!(" + req + " , " + res + ")){ return Status.OK; }\n");
132+
cos.WriteString("\tStatus " + func + "(ServerReaderWriter!(" + req + ", " + res + ")){ return Status.OK; }\n");
127133
}
128134
else if (m->client_streaming())
129135
{
130-
cos.WriteString("\tStatus " + func + "(ServerReader!" + req + " , ref " + res + "){ return Status.OK; }\n");
136+
cos.WriteString("\tStatus " + func + "(ServerReader!" + req + ", ref " + res + "){ return Status.OK; }\n");
131137
}
132138
else if (m->server_streaming())
133139
{
134-
cos.WriteString("\tStatus " + func + "(" + req + " , ServerWriter!" + res + "){ return Status.OK; }\n");
140+
cos.WriteString("\tStatus " + func + "(" + req + " req, ServerWriter!" + res + " res){ return Status.OK; }\n");
135141
}
136142
else{
137-
cos.WriteString("\tStatus " + func + "(" + req + " , ref " + res + "){ return Status.OK; }\n");
143+
cos.WriteString("\tStatus " + func + "(" + req + " req, ref " + res + " res){ return Status.OK; }\n");
138144
}
139145
}
140146

141147
cos.WriteString("\n");
142148

143149
/// service's process
144-
cos.WriteString("\tStatus process(string method , GrpcStream stream, ubyte[] complete)\n");
150+
cos.WriteString("\tStatus process(string method, GrpcStream stream, ubyte[] complete)\n");
145151
cos.WriteString("\t{\n");
146152
cos.WriteString("\t\tswitch(method)\n");
147153
cos.WriteString("\t\t{\n");
@@ -153,19 +159,19 @@ static void GenerateService(const std::string &module , CodedOutputStream &cos,c
153159
auto func = m->name();
154160
if (m->client_streaming() && m->server_streaming())
155161
{
156-
cos.WriteString("\t\t\tmixin(SM3!(" + req + " , " + res + " , \"" + func + "\"));\n");
162+
cos.WriteString("\t\t\tmixin(SM3!(" + req + ", " + res + " , \"" + func + "\"));\n");
157163
}
158164
else if (m->client_streaming())
159165
{
160-
cos.WriteString("\t\t\tmixin(SM2!(" + req + " , " + res + " , \"" + func + "\"));\n");
166+
cos.WriteString("\t\t\tmixin(SM2!(" + req + ", " + res + " , \"" + func + "\"));\n");
161167
}
162168
else if (m->server_streaming())
163169
{
164-
cos.WriteString("\t\t\tmixin(SM1!(" + req + " , " + res + " , \"" + func + "\"));\n");
170+
cos.WriteString("\t\t\tmixin(SM1!(" + req + ", " + res + " , \"" + func + "\"));\n");
165171
}
166172
else
167173
{
168-
cos.WriteString("\t\t\tmixin(SM!(" + req + " , " +res+ " , \"" + func + "\"));\n");
174+
cos.WriteString("\t\t\tmixin(SM!(" + req + ", " +res+ " , \"" + func + "\"));\n");
169175
}
170176
}
171177

@@ -195,45 +201,43 @@ class DlangGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
195201
}
196202

197203
/// filename
204+
198205
grpc::string filename = grpc_generator::StripProto(file->name());
199-
CodedOutputStream cos(context->Open(filename + "rpc.d"));
200-
/// module
201-
{
202-
grpc::string module = "module " + file->package() + "." + filename + "rpc;\n";
203-
cos.WriteString(module);
204-
}
206+
grpc::string moduleName = grpc_generator::ProtoBaseName(file->name());
207+
CodedOutputStream cos(context->Open(filename + "Rpc.d"));
205208

206-
cos.WriteString("\n\n");
209+
cos.WriteString("// Generated by the gRPC-dlang plugin.\n\n");
207210

208-
///
209-
{
210-
cos.WriteString("// Generated by the gRPC dlang plugin.\n");
211-
}
211+
/// module
212+
grpc::string module = "module " + file->package() + "." + moduleName + "Rpc;\n";
213+
cos.WriteString(module);
212214

213-
cos.WriteString("\n\n");
215+
cos.WriteString("\n");
214216

215217
/// import fixed file
216-
{
217-
cos.WriteString("import " + file->package() + "." + filename + ";\n");
218-
cos.WriteString("import std.array;\n");
219-
cos.WriteString("import grpc;\n");
220-
cos.WriteString("import google.protobuf;\n");
221-
cos.WriteString("import hunt.logging;\n");
222-
cos.WriteString("import core.thread;\n");
223-
}
224-
225-
cos.WriteString("\n\n");
218+
cos.WriteString("import " + file->package() + "." + moduleName + ";\n");
226219

227220
/// import dep next
228221
for(int i = 0 ; i < file->dependency_count() ; i++ )
229222
{
230223
auto dep = file->dependency(i);
224+
grpc::string moduleName = grpc_generator::ProtoBaseName(dep->name());
225+
231226
if(dep->package() != "")
232-
cos.WriteString("import " + dep->package() + "." + grpc_generator::StripProto(dep->name()) + ";\n");
227+
cos.WriteString("import " + dep->package() + "." + moduleName + ";\n");
233228
else
234-
cos.WriteString("import " + grpc_generator::StripProto(dep->name()) + ";\n");
229+
cos.WriteString("import " + moduleName + ";\n");
235230
}
236231

232+
cos.WriteString("\n");
233+
cos.WriteString("import grpc;\n");
234+
cos.WriteString("import google.protobuf;\n");
235+
cos.WriteString("import hunt.logging;\n\n");
236+
cos.WriteString("import core.thread;\n");
237+
cos.WriteString("import std.array;\n");
238+
cos.WriteString("import std.traits;\n");
239+
240+
237241
cos.WriteString("\n\n");
238242

239243
/// import public dep next

compiler/src/generator_helpers.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ inline grpc::string StripProto(grpc::string filename) {
5858
return filename;
5959
}
6060

61+
inline grpc::string ProtoBaseName(grpc::string filename) {
62+
if (!StripSuffix(&filename, ".protodevel")) {
63+
StripSuffix(&filename, ".proto");
64+
}
65+
66+
#if _WIN32
67+
grpc::string::size_type iPos = filename.find_last_of('\\')+1;
68+
#else
69+
grpc::string::size_type iPos = filename.find_last_of('/')+1;
70+
#endif
71+
filename = filename.substr(iPos, filename.length() - iPos);
72+
return filename;
73+
}
74+
6175
inline grpc::string StringReplace(grpc::string str, const grpc::string& from,
6276
const grpc::string& to, bool replace_all) {
6377
size_t pos = 0;

0 commit comments

Comments
 (0)