blob: b8cf5c1e14dbe52a7ecca7505183aac55b4bbffa [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
Nicolas Nobled446eb82015-03-12 17:22:33 -070059inline grpc::string StripProto(grpc::string filename) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010060 if (!StripSuffix(&filename, ".protodevel")) {
61 StripSuffix(&filename, ".proto");
62 }
63 return filename;
64}
65
Nicolas Nobled446eb82015-03-12 17:22:33 -070066inline grpc::string StringReplace(grpc::string str, const grpc::string &from,
Jan Tattermusch41f9f332015-05-20 08:52:00 -070067 const grpc::string &to, bool replace_all) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010068 size_t pos = 0;
69
Jan Tattermusch41f9f332015-05-20 08:52:00 -070070 do {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010071 pos = str.find(from, pos);
Nicolas Nobled446eb82015-03-12 17:22:33 -070072 if (pos == grpc::string::npos) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010073 break;
74 }
75 str.replace(pos, from.length(), to);
76 pos += to.length();
Jan Tattermusch41f9f332015-05-20 08:52:00 -070077 } while(replace_all);
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010078
79 return str;
80}
81
Jan Tattermusch41f9f332015-05-20 08:52:00 -070082inline grpc::string StringReplace(grpc::string str, const grpc::string &from,
83 const grpc::string &to) {
84 return StringReplace(str, from, to, true);
85}
86
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010087inline std::vector<grpc::string> tokenize(const grpc::string &input,
88 const grpc::string &delimiters) {
89 std::vector<grpc::string> tokens;
90 size_t pos, last_pos = 0;
91
92 for (;;) {
93 bool done = false;
94 pos = input.find_first_of(delimiters, last_pos);
95 if (pos == grpc::string::npos) {
96 done = true;
97 pos = input.length();
98 }
99
100 tokens.push_back(input.substr(last_pos, pos - last_pos));
101 if (done) return tokens;
102
103 last_pos = pos + 1;
104 }
105}
106
murgatroid99ac0002a2015-04-07 12:49:14 -0700107inline grpc::string CapitalizeFirstLetter(grpc::string s) {
murgatroid99d3efd0a2015-04-07 11:40:29 -0700108 if (s.empty()) {
109 return s;
110 }
111 s[0] = ::toupper(s[0]);
112 return s;
113}
114
Jorge Canizales9a065d22015-04-27 00:05:01 -0700115inline grpc::string LowercaseFirstLetter(grpc::string s) {
116 if (s.empty()) {
117 return s;
118 }
119 s[0] = ::tolower(s[0]);
120 return s;
121}
122
murgatroid99d3efd0a2015-04-07 11:40:29 -0700123inline grpc::string LowerUnderscoreToUpperCamel(grpc::string str) {
124 std::vector<grpc::string> tokens = tokenize(str, "_");
125 grpc::string result = "";
126 for (unsigned int i = 0; i < tokens.size(); i++) {
127 result += CapitalizeFirstLetter(tokens[i]);
128 }
129 return result;
130}
131
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700132inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file,
133 bool include_package_path) {
Jorge Canizales5115af52015-08-02 16:09:41 -0700134 std::vector<grpc::string> tokens = tokenize(StripProto(file->name()), "/");
135 grpc::string result = "";
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700136 if (include_package_path) {
137 for (unsigned int i = 0; i < tokens.size() - 1; i++) {
138 result += tokens[i] + "/";
139 }
Jorge Canizales5115af52015-08-02 16:09:41 -0700140 }
141 result += LowerUnderscoreToUpperCamel(tokens.back());
142 return result;
murgatroid99d3efd0a2015-04-07 11:40:29 -0700143}
144
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700145inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file) {
146 return FileNameInUpperCamel(file, true);
147}
148
Jan Tattermuschb5897bf2015-05-07 15:45:37 -0700149enum MethodType {
150 METHODTYPE_NO_STREAMING,
151 METHODTYPE_CLIENT_STREAMING,
152 METHODTYPE_SERVER_STREAMING,
153 METHODTYPE_BIDI_STREAMING
154};
155
156inline MethodType GetMethodType(const grpc::protobuf::MethodDescriptor *method) {
157 if (method->client_streaming()) {
158 if (method->server_streaming()) {
159 return METHODTYPE_BIDI_STREAMING;
160 } else {
161 return METHODTYPE_CLIENT_STREAMING;
162 }
163 } else {
164 if (method->server_streaming()) {
165 return METHODTYPE_SERVER_STREAMING;
166 } else {
167 return METHODTYPE_NO_STREAMING;
168 }
169 }
170}
171
yang-g9efec8e2016-04-14 14:34:55 -0700172inline void Split(const grpc::string &s, char delim,
173 std::vector<grpc::string> *append_to) {
174 std::istringstream iss(s);
175 grpc::string piece;
176 while (std::getline(iss, piece)) {
177 append_to->push_back(piece);
178 }
179}
180
181enum CommentType {
182 COMMENTTYPE_LEADING,
183 COMMENTTYPE_TRAILING,
184 COMMENTTYPE_LEADING_DETACHED
185};
186
187// Get all the comments and append each line to out.
188template <typename DescriptorType>
189inline void GetComment(const DescriptorType *desc, CommentType type,
190 std::vector<grpc::string> *out) {
191 grpc::protobuf::SourceLocation location;
192 if (!desc->GetSourceLocation(&location)) {
193 return;
194 }
195 if (type == COMMENTTYPE_LEADING || type == COMMENTTYPE_TRAILING) {
196 const grpc::string &comments = type == COMMENTTYPE_LEADING
197 ? location.leading_comments
198 : location.trailing_comments;
199 Split(comments, '\n', out);
200 } else if (type == COMMENTTYPE_LEADING_DETACHED) {
yang-gb01e0132016-04-14 16:41:09 -0700201 for (unsigned int i = 0; i < location.leading_detached_comments.size();
202 i++) {
yang-g9efec8e2016-04-14 14:34:55 -0700203 Split(location.leading_detached_comments[i], '\n', out);
204 out->push_back("");
205 }
206 } else {
yang-g25df28e2016-04-20 16:36:12 -0700207 std::cerr << "Unknown comment type " << type << std::endl;
yang-g9efec8e2016-04-14 14:34:55 -0700208 abort();
209 }
210}
211
yang-g2e089412016-04-15 10:46:41 -0700212// For file level leading and detached leading comments, we return comments
213// above syntax line. Return nothing for trailing comments.
214template <>
215inline void GetComment(const grpc::protobuf::FileDescriptor *desc,
216 CommentType type, std::vector<grpc::string> *out) {
217 if (type == COMMENTTYPE_TRAILING) {
218 return;
219 }
220 grpc::protobuf::SourceLocation location;
221 std::vector<int> path;
222 path.push_back(grpc::protobuf::FileDescriptorProto::kSyntaxFieldNumber);
223 if (!desc->GetSourceLocation(path, &location)) {
224 return;
225 }
226 if (type == COMMENTTYPE_LEADING) {
227 Split(location.leading_comments, '\n', out);
228 } else if (type == COMMENTTYPE_LEADING_DETACHED) {
229 for (unsigned int i = 0; i < location.leading_detached_comments.size();
230 i++) {
231 Split(location.leading_detached_comments[i], '\n', out);
232 out->push_back("");
233 }
234 } else {
yang-g25df28e2016-04-20 16:36:12 -0700235 std::cerr << "Unknown comment type " << type << std::endl;
yang-g2e089412016-04-15 10:46:41 -0700236 abort();
237 }
238}
239
yang-g6ba96de2016-04-21 09:58:23 -0700240// Add prefix and newline to each comment line and concatenate them together.
241// Make sure there is a space after the prefix unless the line is empty.
242inline grpc::string GenerateCommentsWithPrefix(
243 const std::vector<grpc::string> &in, const grpc::string &prefix) {
yang-g25df28e2016-04-20 16:36:12 -0700244 std::ostringstream oss;
245 for (const grpc::string &elem : in) {
246 if (elem.empty()) {
yang-g6ba96de2016-04-21 09:58:23 -0700247 oss << prefix << "\n";
yang-g25df28e2016-04-20 16:36:12 -0700248 } else if (elem[0] == ' ') {
yang-g6ba96de2016-04-21 09:58:23 -0700249 oss << prefix << elem << "\n";
yang-g25df28e2016-04-20 16:36:12 -0700250 } else {
yang-g6ba96de2016-04-21 09:58:23 -0700251 oss << prefix << " " << elem << "\n";
yang-g25df28e2016-04-20 16:36:12 -0700252 }
253 }
254 return oss.str();
255}
256
yang-g25df28e2016-04-20 16:36:12 -0700257// Get leading or trailing comments in a string. Comment lines start with "// ".
258// Leading detached comments are put in in front of leading comments.
259template <typename DescriptorType>
260inline grpc::string GetComments(const DescriptorType *desc, bool leading) {
261 std::vector<grpc::string> out;
262 if (leading) {
263 grpc_generator::GetComment(
264 desc, grpc_generator::COMMENTTYPE_LEADING_DETACHED, &out);
265 std::vector<grpc::string> leading;
266 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_LEADING,
267 &leading);
268 out.insert(out.end(), leading.begin(), leading.end());
269 } else {
270 grpc_generator::GetComment(desc, grpc_generator::COMMENTTYPE_TRAILING,
271 &out);
272 }
yang-g6ba96de2016-04-21 09:58:23 -0700273 return GenerateCommentsWithPrefix(out, "//");
yang-g25df28e2016-04-20 16:36:12 -0700274}
275
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +0100276} // namespace grpc_generator
277
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100278#endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H