Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 1 | /* |
| 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" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H |
| 35 | #define GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 36 | |
| 37 | #include <map> |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 38 | |
| 39 | #include "src/compiler/config.h" |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 40 | |
| 41 | namespace grpc_generator { |
| 42 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 43 | inline bool StripSuffix(grpc::string *filename, const grpc::string &suffix) { |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 44 | if (filename->length() >= suffix.length()) { |
| 45 | size_t suffix_pos = filename->length() - suffix.length(); |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 46 | if (filename->compare(suffix_pos, grpc::string::npos, suffix) == 0) { |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 47 | filename->resize(filename->size() - suffix.size()); |
| 48 | return true; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return false; |
| 53 | } |
| 54 | |
murgatroid99 | d2ee81f | 2016-02-26 11:10:33 -0800 | [diff] [blame^] | 55 | inline 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 Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 65 | inline grpc::string StripProto(grpc::string filename) { |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 66 | if (!StripSuffix(&filename, ".protodevel")) { |
| 67 | StripSuffix(&filename, ".proto"); |
| 68 | } |
| 69 | return filename; |
| 70 | } |
| 71 | |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 72 | inline grpc::string StringReplace(grpc::string str, const grpc::string &from, |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 73 | const grpc::string &to, bool replace_all) { |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 74 | size_t pos = 0; |
| 75 | |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 76 | do { |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 77 | pos = str.find(from, pos); |
Nicolas Noble | d446eb8 | 2015-03-12 17:22:33 -0700 | [diff] [blame] | 78 | if (pos == grpc::string::npos) { |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | str.replace(pos, from.length(), to); |
| 82 | pos += to.length(); |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 83 | } while(replace_all); |
Nicolas "Pixel" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 84 | |
| 85 | return str; |
| 86 | } |
| 87 | |
Jan Tattermusch | 41f9f33 | 2015-05-20 08:52:00 -0700 | [diff] [blame] | 88 | inline 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" Noble | 375a82b | 2015-03-24 02:33:18 +0100 | [diff] [blame] | 93 | inline 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 | |
murgatroid99 | ac0002a | 2015-04-07 12:49:14 -0700 | [diff] [blame] | 113 | inline grpc::string CapitalizeFirstLetter(grpc::string s) { |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 114 | if (s.empty()) { |
| 115 | return s; |
| 116 | } |
| 117 | s[0] = ::toupper(s[0]); |
| 118 | return s; |
| 119 | } |
| 120 | |
Jorge Canizales | 9a065d2 | 2015-04-27 00:05:01 -0700 | [diff] [blame] | 121 | inline 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 | |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 129 | inline 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 Tattermusch | 205e6c8 | 2015-08-14 09:25:06 -0700 | [diff] [blame] | 138 | inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file, |
| 139 | bool include_package_path) { |
Jorge Canizales | 5115af5 | 2015-08-02 16:09:41 -0700 | [diff] [blame] | 140 | std::vector<grpc::string> tokens = tokenize(StripProto(file->name()), "/"); |
| 141 | grpc::string result = ""; |
Jan Tattermusch | 205e6c8 | 2015-08-14 09:25:06 -0700 | [diff] [blame] | 142 | if (include_package_path) { |
| 143 | for (unsigned int i = 0; i < tokens.size() - 1; i++) { |
| 144 | result += tokens[i] + "/"; |
| 145 | } |
Jorge Canizales | 5115af5 | 2015-08-02 16:09:41 -0700 | [diff] [blame] | 146 | } |
| 147 | result += LowerUnderscoreToUpperCamel(tokens.back()); |
| 148 | return result; |
murgatroid99 | d3efd0a | 2015-04-07 11:40:29 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Jan Tattermusch | 205e6c8 | 2015-08-14 09:25:06 -0700 | [diff] [blame] | 151 | inline grpc::string FileNameInUpperCamel(const grpc::protobuf::FileDescriptor *file) { |
| 152 | return FileNameInUpperCamel(file, true); |
| 153 | } |
| 154 | |
Jan Tattermusch | b5897bf | 2015-05-07 15:45:37 -0700 | [diff] [blame] | 155 | enum MethodType { |
| 156 | METHODTYPE_NO_STREAMING, |
| 157 | METHODTYPE_CLIENT_STREAMING, |
| 158 | METHODTYPE_SERVER_STREAMING, |
| 159 | METHODTYPE_BIDI_STREAMING |
| 160 | }; |
| 161 | |
| 162 | inline 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" Noble | 93fa098 | 2015-02-27 21:50:58 +0100 | [diff] [blame] | 178 | } // namespace grpc_generator |
| 179 | |
Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 180 | #endif // GRPC_INTERNAL_COMPILER_GENERATOR_HELPERS_H |