blob: 53391bc41ab10b670c8f74c9a335685dc0ec8778 [file] [log] [blame]
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +01001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H
35#define GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010036
yang-g25df28e2016-04-20 16:36:12 -070037#include <iostream>
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010038#include <map>
yang-g9efec8e2016-04-14 14:34:55 -070039#include <sstream>
40#include <string>
41#include <vector>
Nicolas Nobled446eb82015-03-12 17:22:33 -070042
43#include "src/compiler/config.h"
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010044
45namespace grpc_generator {
46
Nicolas Nobled446eb82015-03-12 17:22:33 -070047inline bool StripSuffix(grpc::string *filename, const grpc::string &suffix) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010048 if (filename->length() >= suffix.length()) {
49 size_t suffix_pos = filename->length() - suffix.length();
Nicolas Nobled446eb82015-03-12 17:22:33 -070050 if (filename->compare(suffix_pos, grpc::string::npos, suffix) == 0) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010051 filename->resize(filename->size() - suffix.size());
52 return true;
53 }
54 }
55
56 return false;
57}
58
murgatroid99d2ee81f2016-02-26 11:10:33 -080059inline bool StripPrefix(grpc::string *name, const grpc::string &prefix) {
60 if (name->length() >= prefix.length()) {
61 if (name->substr(0, prefix.size()) == prefix) {
62 *name = name->substr(prefix.size());
63 return true;
64 }
65 }
66 return false;
67}
68
Nicolas Nobled446eb82015-03-12 17:22:33 -070069inline grpc::string StripProto(grpc::string filename) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010070 if (!StripSuffix(&filename, ".protodevel")) {
71 StripSuffix(&filename, ".proto");
72 }
73 return filename;
74}
75
Nicolas Nobled446eb82015-03-12 17:22:33 -070076inline grpc::string StringReplace(grpc::string str, const grpc::string &from,
Jan Tattermusch41f9f332015-05-20 08:52:00 -070077 const grpc::string &to, bool replace_all) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010078 size_t pos = 0;
79
Jan Tattermusch41f9f332015-05-20 08:52:00 -070080 do {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010081 pos = str.find(from, pos);
Nicolas Nobled446eb82015-03-12 17:22:33 -070082 if (pos == grpc::string::npos) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010083 break;
84 }
85 str.replace(pos, from.length(), to);
86 pos += to.length();
Jan Tattermusch41f9f332015-05-20 08:52:00 -070087 } while(replace_all);
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010088
89 return str;
90}
91
Jan Tattermusch41f9f332015-05-20 08:52:00 -070092inline grpc::string StringReplace(grpc::string str, const grpc::string &from,
93 const grpc::string &to) {
94 return StringReplace(str, from, to, true);
95}
96
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010097inline std::vector<grpc::string> tokenize(const grpc::string &input,
98 const grpc::string &delimiters) {
99 std::vector<grpc::string> tokens;
100 size_t pos, last_pos = 0;
101
102 for (;;) {
103 bool done = false;
104 pos = input.find_first_of(delimiters, last_pos);
105 if (pos == grpc::string::npos) {
106 done = true;
107 pos = input.length();
108 }
109
110 tokens.push_back(input.substr(last_pos, pos - last_pos));
111 if (done) return tokens;
112
113 last_pos = pos + 1;
114 }
115}
116
murgatroid99ac0002a2015-04-07 12:49:14 -0700117inline grpc::string CapitalizeFirstLetter(grpc::string s) {
murgatroid99d3efd0a2015-04-07 11:40:29 -0700118 if (s.empty()) {
119 return s;
120 }
121 s[0] = ::toupper(s[0]);
122 return s;
123}
124
Jorge Canizales9a065d22015-04-27 00:05:01 -0700125inline grpc::string LowercaseFirstLetter(grpc::string s) {
126 if (s.empty()) {
127 return s;
128 }
129 s[0] = ::tolower(s[0]);
130 return s;
131}
132
murgatroid99d3efd0a2015-04-07 11:40:29 -0700133inline grpc::string LowerUnderscoreToUpperCamel(grpc::string str) {
134 std::vector<grpc::string> tokens = tokenize(str, "_");
135 grpc::string result = "";
136 for (unsigned int i = 0; i < tokens.size(); i++) {
137 result += CapitalizeFirstLetter(tokens[i]);
138 }
139 return result;
140}
141
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700142inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file,
143 bool include_package_path) {
Jorge Canizales5115af52015-08-02 16:09:41 -0700144 std::vector<grpc::string> tokens = tokenize(StripProto(file->name()), "/");
145 grpc::string result = "";
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700146 if (include_package_path) {
147 for (unsigned int i = 0; i < tokens.size() - 1; i++) {
148 result += tokens[i] + "/";
149 }
Jorge Canizales5115af52015-08-02 16:09:41 -0700150 }
151 result += LowerUnderscoreToUpperCamel(tokens.back());
152 return result;
murgatroid99d3efd0a2015-04-07 11:40:29 -0700153}
154
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700155inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file) {
156 return FileNameInUpperCamel(file, true);
157}
158
Jan Tattermuschb5897bf2015-05-07 15:45:37 -0700159enum MethodType {
160 METHODTYPE_NO_STREAMING,
161 METHODTYPE_CLIENT_STREAMING,
162 METHODTYPE_SERVER_STREAMING,
163 METHODTYPE_BIDI_STREAMING
164};
165
166inline MethodType GetMethodType(const grpc::protobuf::MethodDescriptor *method) {
167 if (method->client_streaming()) {
168 if (method->server_streaming()) {
169 return METHODTYPE_BIDI_STREAMING;
170 } else {
171 return METHODTYPE_CLIENT_STREAMING;
172 }
173 } else {
174 if (method->server_streaming()) {
175 return METHODTYPE_SERVER_STREAMING;
176 } else {
177 return METHODTYPE_NO_STREAMING;
178 }
179 }
180}
181
yang-g9efec8e2016-04-14 14:34:55 -0700182inline void Split(const grpc::string &s, char delim,
183 std::vector<grpc::string> *append_to) {
184 std::istringstream iss(s);
185 grpc::string piece;
186 while (std::getline(iss, piece)) {
187 append_to->push_back(piece);
188 }
189}
190
191enum CommentType {
192 COMMENTTYPE_LEADING,
193 COMMENTTYPE_TRAILING,
194 COMMENTTYPE_LEADING_DETACHED
195};
196
yang-gc3e1f632016-04-21 10:03:21 -0700197// Get all the raw comments and append each line without newline to out.
yang-g9efec8e2016-04-14 14:34:55 -0700198template <typename DescriptorType>
199inline void GetComment(const DescriptorType *desc, CommentType type,
200 std::vector<grpc::string> *out) {
201 grpc::protobuf::SourceLocation location;
202 if (!desc->GetSourceLocation(&location)) {
203 return;
204 }
205 if (type == COMMENTTYPE_LEADING || type == COMMENTTYPE_TRAILING) {
206 const grpc::string &comments = type == COMMENTTYPE_LEADING
207 ? location.leading_comments
208 : location.trailing_comments;
209 Split(comments, '\n', out);
210 } else if (type == COMMENTTYPE_LEADING_DETACHED) {
yang-gb01e0132016-04-14 16:41:09 -0700211 for (unsigned int i = 0; i < location.leading_detached_comments.size();
212 i++) {
yang-g9efec8e2016-04-14 14:34:55 -0700213 Split(location.leading_detached_comments[i], '\n', out);
214 out->push_back("");
215 }
216 } else {
yang-g25df28e2016-04-20 16:36:12 -0700217 std::cerr << "Unknown comment type " << type << std::endl;
yang-g9efec8e2016-04-14 14:34:55 -0700218 abort();
219 }
220}
221
yang-gc3e1f632016-04-21 10:03:21 -0700222// Each raw comment line without newline is appended to out.
yang-g2e089412016-04-15 10:46:41 -0700223// For file level leading and detached leading comments, we return comments
224// above syntax line. Return nothing for trailing comments.
225template <>
226inline void GetComment(const grpc::protobuf::FileDescriptor *desc,
227 CommentType type, std::vector<grpc::string> *out) {
228 if (type == COMMENTTYPE_TRAILING) {
229 return;
230 }
231 grpc::protobuf::SourceLocation location;
232 std::vector<int> path;
233 path.push_back(grpc::protobuf::FileDescriptorProto::kSyntaxFieldNumber);
234 if (!desc->GetSourceLocation(path, &location)) {
235 return;
236 }
237 if (type == COMMENTTYPE_LEADING) {
238 Split(location.leading_comments, '\n', out);
239 } else if (type == COMMENTTYPE_LEADING_DETACHED) {
240 for (unsigned int i = 0; i < location.leading_detached_comments.size();
241 i++) {
242 Split(location.leading_detached_comments[i], '\n', out);
243 out->push_back("");
244 }
245 } else {
yang-g25df28e2016-04-20 16:36:12 -0700246 std::cerr << "Unknown comment type " << type << std::endl;
yang-g2e089412016-04-15 10:46:41 -0700247 abort();
248 }
249}
250
yang-g6ba96de2016-04-21 09:58:23 -0700251// Add prefix and newline to each comment line and concatenate them together.
252// Make sure there is a space after the prefix unless the line is empty.
253inline grpc::string GenerateCommentsWithPrefix(
254 const std::vector<grpc::string> &in, const grpc::string &prefix) {
yang-g25df28e2016-04-20 16:36:12 -0700255 std::ostringstream oss;
256 for (const grpc::string &elem : in) {
257 if (elem.empty()) {
yang-g6ba96de2016-04-21 09:58:23 -0700258 oss << prefix << "\n";
yang-g25df28e2016-04-20 16:36:12 -0700259 } else if (elem[0] == ' ') {
yang-g6ba96de2016-04-21 09:58:23 -0700260 oss << prefix << elem << "\n";
yang-g25df28e2016-04-20 16:36:12 -0700261 } else {
yang-g6ba96de2016-04-21 09:58:23 -0700262 oss << prefix << " " << elem << "\n";
yang-g25df28e2016-04-20 16:36:12 -0700263 }
264 }
265 return oss.str();
266}
267
yang-g25df28e2016-04-20 16:36:12 -0700268template <typename DescriptorType>
murgatroid99210f3c42016-05-20 13:24:59 -0700269inline grpc::string GetPrefixedComments(const DescriptorType *desc,
270 bool leading,
271 const grpc::string &prefix) {
yang-g25df28e2016-04-20 16:36:12 -0700272 std::vector<grpc::string> out;
273 if (leading) {
274 grpc_generator::GetComment(
275 desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &out);
276 std::vector<grpc::string> leading;
277 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING,
278 &leading);
279 out.insert(out.end(), leading.begin(), leading.end());
280 } else {
281 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING,
282 &out);
283 }
murgatroid99210f3c42016-05-20 13:24:59 -0700284 return GenerateCommentsWithPrefix(out, prefix);
yang-g25df28e2016-04-20 16:36:12 -0700285}
286
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +0100287} // namespace grpc_generator
288
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100289#endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H