summarylogtreecommitdiffstats
path: root/0002-fix-compatibility-with-protobuf.patch
blob: 6b296ddefd91e12d2767f72d6b84e93b3fbdf011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
From 89a812a19b87b9aed0a5eb63d93dd78ff9ebd50f Mon Sep 17 00:00:00 2001
From: kXuan <kxuanobj@gmail.com>
Date: Mon, 8 Sep 2025 10:34:18 +0800
Subject: [PATCH 2/2] fix compatibility with protobuf

protobuf has migrate to use std::string_view. This patch fix the compatibility
issue.

Signed-off-by: kXuan <kxuanobj@gmail.com>
---
 .../net/grpc/web/generator/grpc_generator.cc  | 83 ++++++++++---------
 1 file changed, 44 insertions(+), 39 deletions(-)

diff --git a/javascript/net/grpc/web/generator/grpc_generator.cc b/javascript/net/grpc/web/generator/grpc_generator.cc
index ac44360..f08f193 100644
--- a/javascript/net/grpc/web/generator/grpc_generator.cc
+++ b/javascript/net/grpc/web/generator/grpc_generator.cc
@@ -51,6 +51,7 @@ namespace web {
 namespace {
 
 using std::string;
+using std::string_view;
 
 enum Mode {
   OP = 0,       // first party google3 one platform services
@@ -132,7 +133,8 @@ std::string GetSerializeMethodReturnType(std::map<string, string> vars) {
   return "!Uint8Array";
 }
 
-string LowercaseFirstLetter(string s) {
+string LowercaseFirstLetter(const string_view &sv) {
+  string s{sv};
   if (s.empty()) {
     return s;
   }
@@ -140,7 +142,8 @@ string LowercaseFirstLetter(string s) {
   return s;
 }
 
-string Lowercase(string s) {
+string Lowercase(const string_view &sv) {
+  string s{sv};
   if (s.empty()) {
     return s;
   }
@@ -151,7 +154,8 @@ string Lowercase(string s) {
   return s;
 }
 
-string UppercaseFirstLetter(string s) {
+string UppercaseFirstLetter(const string_view &sv) {
+  string s{sv};
   if (s.empty()) {
     return s;
   }
@@ -159,7 +163,8 @@ string UppercaseFirstLetter(string s) {
   return s;
 }
 
-string Uppercase(string s) {
+string Uppercase(const string_view &sv) {
+  string s{sv};
   if (s.empty()) {
     return s;
   }
@@ -173,35 +178,35 @@ string Uppercase(string s) {
 // The following 5 functions were copied from
 // google/protobuf/src/google/protobuf/stubs/strutil.h
 
-inline bool HasPrefixString(const string& str, const string& prefix) {
+inline bool HasPrefixString(const string_view& str, const string_view& prefix) {
   return str.size() >= prefix.size() &&
          str.compare(0, prefix.size(), prefix) == 0;
 }
 
 // Strips the given prefix from the string, as well as the remaining leading dot
 // if it exists.
-inline string StripPrefixString(const string& str, const string& prefix) {
+inline string StripPrefixString(const string_view& str, const string_view& prefix) {
   if (!HasPrefixString(str, prefix)) {
-    return str;
+    return string(str);
   }
 
-  string remaining_str = str.substr(prefix.size());
+  string remaining_str = string(str.substr(prefix.size()));
   if (!remaining_str.empty() && remaining_str[0] == '.') {
     remaining_str = remaining_str.substr(1);
   }
   return remaining_str;
 }
 
-inline bool HasSuffixString(const string& str, const string& suffix) {
+inline bool HasSuffixString(const string_view& str, const string_view& suffix) {
   return str.size() >= suffix.size() &&
          str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
 }
 
-inline string StripSuffixString(const string& str, const string& suffix) {
+inline string StripSuffixString(const string_view& str, const string_view& suffix) {
   if (HasSuffixString(str, suffix)) {
-    return str.substr(0, str.size() - suffix.size());
+    return string(str.substr(0, str.size() - suffix.size()));
   } else {
-    return str;
+    return string(str);
   }
 }
 
@@ -217,7 +222,7 @@ void ReplaceCharacters(string* s, const char* remove, char replacewith) {
 // The following function was copied from
 // google/protobuf/src/google/protobuf/compiler/cpp/cpp_helpers.cc
 
-string StripProto(const string& filename) {
+string StripProto(const string_view& filename) {
   if (HasSuffixString(filename, ".protodevel")) {
     return StripSuffixString(filename, ".protodevel");
   } else {
@@ -236,7 +241,7 @@ char ToLowerASCII(char c) {
   }
 }
 
-std::vector<string> ParseLowerUnderscore(const string& input) {
+std::vector<string> ParseLowerUnderscore(const string_view& input) {
   std::vector<string> words;
   string running = "";
   for (size_t i = 0; i < input.size(); i++) {
@@ -269,7 +274,7 @@ string ToUpperCamel(const std::vector<string>& words) {
 
 // Returns the alias we assign to the module of the given .proto filename
 // when importing.
-string ModuleAlias(const string& filename) {
+string ModuleAlias(const string_view& filename) {
   // This scheme could technically cause problems if a file includes any 2 of:
   //   foo/bar_baz.proto
   //   foo_bar_baz.proto
@@ -291,7 +296,7 @@ string JSMessageType(const Descriptor* desc, const FileDescriptor* file) {
     // messages.
     return class_name;
   }
-  return ModuleAlias(desc->file()->name()) + "." + class_name;
+  return ModuleAlias(string(desc->file()->name())) + "." + class_name;
 }
 
 string JSMessageType(const Descriptor* desc) {
@@ -336,7 +341,7 @@ string JSElementType(const FieldDescriptor* desc, const FileDescriptor* file) {
         return StripPrefixString(desc->enum_type()->full_name(),
                                  desc->enum_type()->file()->package());
       }
-      return ModuleAlias(desc->enum_type()->file()->name()) + "." +
+      return ModuleAlias(string(desc->enum_type()->file()->name())) + "." +
              StripPrefixString(desc->enum_type()->full_name(),
                                desc->enum_type()->file()->package());
 
@@ -433,7 +438,7 @@ string GetNestedMessageName(const Descriptor* descriptor) {
 
 // Given a filename like foo/bar/baz.proto, returns the root directory
 // path ../../
-string GetRootPath(const string& from_filename, const string& to_filename) {
+string GetRootPath(const string_view& from_filename, const string_view& to_filename) {
   if (HasPrefixString(to_filename, "google/protobuf")) {
     // Well-known types (.proto files in the google/protobuf directory) are
     // assumed to come from the 'google-protobuf' npm package.  We may want to
@@ -494,8 +499,8 @@ std::map<string, const Descriptor*> GetAllMessages(const FileDescriptor* file) {
     const ServiceDescriptor* service = file->service(s);
     for (int m = 0; m < service->method_count(); ++m) {
       const MethodDescriptor* method = service->method(m);
-      messages[method->input_type()->full_name()] = method->input_type();
-      messages[method->output_type()->full_name()] = method->output_type();
+      messages[string(method->input_type()->full_name())] = method->input_type();
+      messages[string(method->output_type()->full_name())] = method->output_type();
     }
   }
 
@@ -513,14 +518,14 @@ void PrintCommonJsMessagesDeps(Printer* printer, const FileDescriptor* file) {
   std::map<string, string> vars;
 
   for (int i = 0; i < file->dependency_count(); i++) {
-    const string& name = file->dependency(i)->name();
+    const string_view& name = file->dependency(i)->name();
     vars["alias"] = ModuleAlias(name);
     vars["dep_filename"] = GetRootPath(file->name(), name) + StripProto(name);
     // we need to give each cross-file import an alias
     printer->Print(vars, "\nvar $alias$ = require('$dep_filename$_pb.js')\n");
   }
 
-  const string& package = file->package();
+  const string& package = string(file->package());
   vars["package_name"] = package;
 
   if (!package.empty()) {
@@ -556,7 +561,7 @@ void PrintES6Imports(Printer* printer, const FileDescriptor* file) {
 
   std::set<string> imports;
   for (const auto& entry : GetAllMessages(file)) {
-    const string& proto_filename = entry.second->file()->name();
+    const string_view& proto_filename = entry.second->file()->name();
     string dep_filename = GetRootPath(file->name(), proto_filename) + StripProto(proto_filename);
     if (imports.find(dep_filename) != imports.end()) {
       continue;
@@ -810,7 +815,7 @@ void PrintProtoDtsOneofCase(Printer* printer, const OneofDescriptor* desc) {
 
 void PrintProtoDtsMessage(Printer* printer, const Descriptor* desc,
                           const FileDescriptor* file) {
-  const string& class_name = desc->name();
+  const string& class_name = string(desc->name());
   std::map<string, string> vars;
   vars["class_name"] = class_name;
 
@@ -936,7 +941,7 @@ void PrintProtoDtsFile(Printer* printer, const FileDescriptor* file) {
   printer->Print("import * as jspb from 'google-protobuf'\n\n");
 
   for (int i = 0; i < file->dependency_count(); i++) {
-    const string& proto_filename = file->dependency(i)->name();
+    const string_view& proto_filename = file->dependency(i)->name();
     // We need to give each cross-file import an alias.
     printer->Print("import * as $alias$ from '$dep_filename$_pb'; // proto import: \"$proto_filename$\"\n",
                    "alias", ModuleAlias(proto_filename),
@@ -1254,15 +1259,15 @@ void PrintMultipleFilesMode(const FileDescriptor* file, string file_name,
 
       vars["method_name"] = method->name();
       vars["in"] = method->input_type()->full_name();
-      vars["in_type"] = "proto." + method->input_type()->full_name();
+      vars["in_type"] = "proto." + string(method->input_type()->full_name());
       vars["out"] = method->output_type()->full_name();
-      vars["out_type"] = "proto." + method->output_type()->full_name();
+      vars["out_type"] = "proto." + string(method->output_type()->full_name());
       vars["method_type"] = method->server_streaming()
                                 ? "grpc.web.MethodType.SERVER_STREAMING"
                                 : "grpc.web.MethodType.UNARY";
 
       PrintMethodDescriptorFile(&printer, vars);
-      method_descriptors[service->name() + "." + method->name()] =
+      method_descriptors[string(service->name()) + "." + string(method->name())] =
           "proto." + vars["package_dot"] + vars["class_name"] + "." +
           vars["method_name"] + "MethodDescriptor";
     }
@@ -1335,9 +1340,9 @@ void PrintMultipleFilesMode(const FileDescriptor* file, string file_name,
       vars["in"] = input_type->full_name();
       vars["out"] = output_type->full_name();
       vars["method_descriptor"] =
-          method_descriptors[service->name() + "." + method->name()];
-      vars["in_type"] = "proto." + input_type->full_name();
-      vars["out_type"] = "proto." + output_type->full_name();
+          method_descriptors[string(service->name()) + "." + string(method->name())];
+      vars["in_type"] = "proto." + string(input_type->full_name());
+      vars["out_type"] = "proto." + string(output_type->full_name());
 
       // Client streaming is not supported yet
       if (!method->client_streaming()) {
@@ -1364,7 +1369,7 @@ void PrintClosureES6Imports(Printer* printer, const FileDescriptor* file,
   for (int i = 0; i < file->service_count(); ++i) {
     const ServiceDescriptor* service = file->service(i);
 
-    string service_namespace = "proto." + package_dot + service->name();
+    string service_namespace = "proto." + package_dot + string(service->name());
     printer->Print(
         "import $service_name$Client_import from 'goog:$namespace$';\n",
         "service_name", service->name(), "namespace",
@@ -1379,7 +1384,7 @@ void PrintClosureES6Imports(Printer* printer, const FileDescriptor* file,
 }
 
 void PrintGrpcWebClosureES6File(Printer* printer, const FileDescriptor* file) {
-  string package_dot = file->package().empty() ? "" : file->package() + ".";
+  string package_dot = file->package().empty() ? "" : string(file->package()) + ".";
 
   printer->Print(
       "/**\n"
@@ -1400,7 +1405,7 @@ void PrintGrpcWebClosureES6File(Printer* printer, const FileDescriptor* file) {
   for (int i = 0; i < file->service_count(); ++i) {
     const ServiceDescriptor* service = file->service(i);
 
-    string service_namespace = "proto." + package_dot + service->name();
+    string service_namespace = "proto." + package_dot + string(service->name());
     printer->Print("export const $name$Client = $name$Client_import;\n", "name",
                    service->name());
     printer->Print(
@@ -1539,7 +1544,7 @@ class GrpcCodeGenerator : public CodeGenerator {
 
     std::map<string, string> vars;
     std::map<string, string> method_descriptors;
-    string package = file->package();
+    string package = string(file->package());
     vars["package"] = package;
     vars["package_dot"] = package.empty() ? "" : package + '.';
     vars["promise"] = "Promise";
@@ -1582,7 +1587,7 @@ class GrpcCodeGenerator : public CodeGenerator {
     vars["protoc_version"] = GetProtocVersion(context);
     vars["source_file"]    = file->name();
 
-    string file_name = generator_options.OutputFile(file->name());
+    string file_name = generator_options.OutputFile(string(file->name()));
     if (generator_options.multiple_files() &&
         ImportStyle::CLOSURE == generator_options.import_style()) {
       PrintMultipleFilesMode(file, file_name, context, vars);
@@ -1661,7 +1666,7 @@ class GrpcCodeGenerator : public CodeGenerator {
         vars["in"] = input_type->full_name();
         vars["out"] = output_type->full_name();
         vars["method_descriptor"] =
-            "methodDescriptor_" + service->name() + "_" + method->name();
+            "methodDescriptor_" + string(service->name()) + "_" + string(method->name());
 
         // Cross-file ref in CommonJS needs to use the module alias instead
         // of the global name.
@@ -1670,14 +1675,14 @@ class GrpcCodeGenerator : public CodeGenerator {
           vars["in_type"] = ModuleAlias(input_type->file()->name()) +
                             GetNestedMessageName(input_type);
         } else {
-          vars["in_type"] = "proto." + input_type->full_name();
+          vars["in_type"] = "proto." + string(input_type->full_name());
         }
         if (ImportStyle::COMMONJS == generator_options.import_style() &&
             output_type->file() != file) {
           vars["out_type"] = ModuleAlias(output_type->file()->name()) +
                              GetNestedMessageName(output_type);
         } else {
-          vars["out_type"] = "proto." + output_type->full_name();
+          vars["out_type"] = "proto." + string(output_type->full_name());
         }
 
         // Client streaming is not supported yet
-- 
2.49.0