blob: 3ed0500efc5d92f20f83e9eb1b7ce7bfd8f332ce [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
37#include <map>
Nicolas Nobled446eb82015-03-12 17:22:33 -070038
39#include "src/compiler/config.h"
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010040
41namespace grpc_generator {
42
Nicolas Nobled446eb82015-03-12 17:22:33 -070043inline bool StripSuffix(grpc::string *filename, const grpc::string &suffix) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010044 if (filename->length() >= suffix.length()) {
45 size_t suffix_pos = filename->length() - suffix.length();
Nicolas Nobled446eb82015-03-12 17:22:33 -070046 if (filename->compare(suffix_pos, grpc::string::npos, suffix) == 0) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010047 filename->resize(filename->size() - suffix.size());
48 return true;
49 }
50 }
51
52 return false;
53}
54
murgatroid99d2ee81f2016-02-26 11:10:33 -080055inline bool StripPrefix(grpc::string *name, const grpc::string &prefix) {
56 if (name->length() >= prefix.length()) {
57 if (name->substr(0, prefix.size()) == prefix) {
58 *name = name->substr(prefix.size());
59 return true;
60 }
61 }
62 return false;
63}
64
Nicolas Nobled446eb82015-03-12 17:22:33 -070065inline grpc::string StripProto(grpc::string filename) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010066 if (!StripSuffix(&filename, ".protodevel")) {
67 StripSuffix(&filename, ".proto");
68 }
69 return filename;
70}
71
Nicolas Nobled446eb82015-03-12 17:22:33 -070072inline grpc::string StringReplace(grpc::string str, const grpc::string &from,
Jan Tattermusch41f9f332015-05-20 08:52:00 -070073 const grpc::string &to, bool replace_all) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010074 size_t pos = 0;
75
Jan Tattermusch41f9f332015-05-20 08:52:00 -070076 do {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010077 pos = str.find(from, pos);
Nicolas Nobled446eb82015-03-12 17:22:33 -070078 if (pos == grpc::string::npos) {
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010079 break;
80 }
81 str.replace(pos, from.length(), to);
82 pos += to.length();
Jan Tattermusch41f9f332015-05-20 08:52:00 -070083 } while(replace_all);
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +010084
85 return str;
86}
87
Jan Tattermusch41f9f332015-05-20 08:52:00 -070088inline grpc::string StringReplace(grpc::string str, const grpc::string &from,
89 const grpc::string &to) {
90 return StringReplace(str, from, to, true);
91}
92
Nicolas "Pixel" Noble375a82b2015-03-24 02:33:18 +010093inline std::vector<grpc::string> tokenize(const grpc::string &input,
94 const grpc::string &delimiters) {
95 std::vector<grpc::string> tokens;
96 size_t pos, last_pos = 0;
97
98 for (;;) {
99 bool done = false;
100 pos = input.find_first_of(delimiters, last_pos);
101 if (pos == grpc::string::npos) {
102 done = true;
103 pos = input.length();
104 }
105
106 tokens.push_back(input.substr(last_pos, pos - last_pos));
107 if (done) return tokens;
108
109 last_pos = pos + 1;
110 }
111}
112
murgatroid99ac0002a2015-04-07 12:49:14 -0700113inline grpc::string CapitalizeFirstLetter(grpc::string s) {
murgatroid99d3efd0a2015-04-07 11:40:29 -0700114 if (s.empty()) {
115 return s;
116 }
117 s[0] = ::toupper(s[0]);
118 return s;
119}
120
Jorge Canizales9a065d22015-04-27 00:05:01 -0700121inline grpc::string LowercaseFirstLetter(grpc::string s) {
122 if (s.empty()) {
123 return s;
124 }
125 s[0] = ::tolower(s[0]);
126 return s;
127}
128
murgatroid99d3efd0a2015-04-07 11:40:29 -0700129inline grpc::string LowerUnderscoreToUpperCamel(grpc::string str) {
130 std::vector<grpc::string> tokens = tokenize(str, "_");
131 grpc::string result = "";
132 for (unsigned int i = 0; i < tokens.size(); i++) {
133 result += CapitalizeFirstLetter(tokens[i]);
134 }
135 return result;
136}
137
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700138inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file,
139 bool include_package_path) {
Jorge Canizales5115af52015-08-02 16:09:41 -0700140 std::vector<grpc::string> tokens = tokenize(StripProto(file->name()), "/");
141 grpc::string result = "";
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700142 if (include_package_path) {
143 for (unsigned int i = 0; i < tokens.size() - 1; i++) {
144 result += tokens[i] + "/";
145 }
Jorge Canizales5115af52015-08-02 16:09:41 -0700146 }
147 result += LowerUnderscoreToUpperCamel(tokens.back());
148 return result;
murgatroid99d3efd0a2015-04-07 11:40:29 -0700149}
150
Jan Tattermusch205e6c82015-08-14 09:25:06 -0700151inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file) {
152 return FileNameInUpperCamel(file, true);
153}
154
Jan Tattermuschb5897bf2015-05-07 15:45:37 -0700155enum MethodType {
156 METHODTYPE_NO_STREAMING,
157 METHODTYPE_CLIENT_STREAMING,
158 METHODTYPE_SERVER_STREAMING,
159 METHODTYPE_BIDI_STREAMING
160};
161
162inline MethodType GetMethodType(const grpc::protobuf::MethodDescriptor *method) {
163 if (method->client_streaming()) {
164 if (method->server_streaming()) {
165 return METHODTYPE_BIDI_STREAMING;
166 } else {
167 return METHODTYPE_CLIENT_STREAMING;
168 }
169 } else {
170 if (method->server_streaming()) {
171 return METHODTYPE_SERVER_STREAMING;
172 } else {
173 return METHODTYPE_NO_STREAMING;
174 }
175 }
176}
177
Nicolas "Pixel" Noble93fa0982015-02-27 21:50:58 +0100178} // namespace grpc_generator
179
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100180#endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H