replace framework include of flatbuffers (#7079)

This commit is contained in:
Derek Bailey 2022-02-03 17:40:15 -08:00 committed by GitHub
parent f5664d33fb
commit 4c71c7b023
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,19 +23,18 @@
#include <utility>
#include <vector>
#include <flatbuffers/util.h>
#include "flatbuffers/util.h"
#define to_string flatbuffers::NumToString
// Stringify helpers used solely to cast GRPC_VERSION
#ifndef STR
#define STR(s) #s
# define STR(s) # s
#endif
#ifndef XSTR
#define XSTR(s) STR(s)
# define XSTR(s) STR(s)
#endif
typedef grpc_generator::Printer Printer;
typedef std::map<grpc::string, grpc::string> VARS;
typedef grpc_generator::Service ServiceDescriptor;
@ -46,12 +45,11 @@ typedef grpc_generator::Method MethodDescriptor;
namespace grpc_java_generator {
typedef std::string string;
// Generates imports for the service
void GenerateImports(grpc_generator::File* file,
grpc_generator::Printer* printer, VARS& vars) {
void GenerateImports(grpc_generator::File *file,
grpc_generator::Printer *printer, VARS &vars) {
vars["filename"] = file->filename();
printer->Print(
vars,
"//Generated by flatc compiler (version $flatc_version$)\n");
printer->Print(vars,
"//Generated by flatc compiler (version $flatc_version$)\n");
printer->Print("//If you make any local changes, they will be lost\n");
printer->Print(vars, "//source: $filename$.fbs\n\n");
printer->Print(vars, "package $Package$;\n\n");
@ -65,7 +63,7 @@ void GenerateImports(grpc_generator::File* file,
// Adjust a method name prefix identifier to follow the JavaBean spec:
// - decapitalize the first letter
// - remove embedded underscores & capitalize the following letter
static string MixedLower(const string& word) {
static string MixedLower(const string &word) {
string w;
w += static_cast<string::value_type>(tolower(word[0]));
bool after_underscore = false;
@ -85,7 +83,7 @@ static string MixedLower(const string& word) {
// - An underscore is inserted where a lower case letter is followed by an
// upper case letter.
// - All letters are converted to upper case
static string ToAllUpperCase(const string& word) {
static string ToAllUpperCase(const string &word) {
string w;
for (size_t i = 0; i < word.length(); ++i) {
w += static_cast<string::value_type>(toupper(word[i]));
@ -96,47 +94,47 @@ static string ToAllUpperCase(const string& word) {
return w;
}
static inline string LowerMethodName(const MethodDescriptor* method) {
static inline string LowerMethodName(const MethodDescriptor *method) {
return MixedLower(method->name());
}
static inline string MethodPropertiesFieldName(const MethodDescriptor* method) {
static inline string MethodPropertiesFieldName(const MethodDescriptor *method) {
return "METHOD_" + ToAllUpperCase(method->name());
}
static inline string MethodPropertiesGetterName(
const MethodDescriptor* method) {
const MethodDescriptor *method) {
return MixedLower("get_" + method->name() + "_method");
}
static inline string MethodIdFieldName(const MethodDescriptor* method) {
static inline string MethodIdFieldName(const MethodDescriptor *method) {
return "METHODID_" + ToAllUpperCase(method->name());
}
static inline string JavaClassName(VARS& vars, const string& name) {
static inline string JavaClassName(VARS &vars, const string &name) {
// string name = google::protobuf::compiler::java::ClassName(desc);
return vars["Package"] + name;
}
static inline string ServiceClassName(const string& service_name) {
static inline string ServiceClassName(const string &service_name) {
return service_name + "Grpc";
}
// TODO(nmittler): Remove once protobuf includes javadoc methods in
// distribution.
template <typename ITR>
static void GrpcSplitStringToIteratorUsing(const string& full,
const char* delim, ITR& result) {
template<typename ITR>
static void GrpcSplitStringToIteratorUsing(const string &full,
const char *delim, ITR &result) {
// Optimize the common case where delim is a single character.
if (delim[0] != '\0' && delim[1] == '\0') {
char c = delim[0];
const char* p = full.data();
const char* end = p + full.size();
const char *p = full.data();
const char *end = p + full.size();
while (p != end) {
if (*p == c) {
++p;
} else {
const char* start = p;
const char *start = p;
while (++p != end && *p != c)
;
*result++ = string(start, p - start);
@ -158,13 +156,13 @@ static void GrpcSplitStringToIteratorUsing(const string& full,
}
}
static void GrpcSplitStringUsing(const string& full, const char* delim,
std::vector<string>* result) {
static void GrpcSplitStringUsing(const string &full, const char *delim,
std::vector<string> *result) {
std::back_insert_iterator<std::vector<string>> it(*result);
GrpcSplitStringToIteratorUsing(full, delim, it);
}
static std::vector<string> GrpcSplit(const string& full, const char* delim) {
static std::vector<string> GrpcSplit(const string &full, const char *delim) {
std::vector<string> result;
GrpcSplitStringUsing(full, delim, &result);
return result;
@ -172,7 +170,7 @@ static std::vector<string> GrpcSplit(const string& full, const char* delim) {
// TODO(nmittler): Remove once protobuf includes javadoc methods in
// distribution.
static string GrpcEscapeJavadoc(const string& input) {
static string GrpcEscapeJavadoc(const string &input) {
string result;
result.reserve(input.size() * 2);
@ -219,9 +217,7 @@ static string GrpcEscapeJavadoc(const string& input) {
// Java interprets Unicode escape sequences anywhere!
result.append("&#92;");
break;
default:
result.push_back(c);
break;
default: result.push_back(c); break;
}
prev = c;
@ -230,7 +226,7 @@ static string GrpcEscapeJavadoc(const string& input) {
return result;
}
static std::vector<string> GrpcGetDocLines(const string& comments) {
static std::vector<string> GrpcGetDocLines(const string &comments) {
if (!comments.empty()) {
// TODO(kenton): Ideally we should parse the comment text as Markdown and
// write it back as HTML, but this requires a Markdown parser. For now
@ -241,27 +237,23 @@ static std::vector<string> GrpcGetDocLines(const string& comments) {
string escapedComments = GrpcEscapeJavadoc(comments);
std::vector<string> lines = GrpcSplit(escapedComments, "\n");
while (!lines.empty() && lines.back().empty()) {
lines.pop_back();
}
while (!lines.empty() && lines.back().empty()) { lines.pop_back(); }
return lines;
}
return std::vector<string>();
}
static std::vector<string> GrpcGetDocLinesForDescriptor(
const DescriptorType* descriptor) {
const DescriptorType *descriptor) {
return descriptor->GetAllComments();
// return GrpcGetDocLines(descriptor->GetLeadingComments("///"));
}
static void GrpcWriteDocCommentBody(Printer* printer, VARS& vars,
const std::vector<string>& lines,
static void GrpcWriteDocCommentBody(Printer *printer, VARS &vars,
const std::vector<string> &lines,
bool surroundWithPreTag) {
if (!lines.empty()) {
if (surroundWithPreTag) {
printer->Print(" * <pre>\n");
}
if (surroundWithPreTag) { printer->Print(" * <pre>\n"); }
for (size_t i = 0; i < lines.size(); i++) {
// Most lines should start with a space. Watch out for lines that start
@ -275,73 +267,72 @@ static void GrpcWriteDocCommentBody(Printer* printer, VARS& vars,
}
}
if (surroundWithPreTag) {
printer->Print(" * </pre>\n");
}
if (surroundWithPreTag) { printer->Print(" * </pre>\n"); }
}
}
static void GrpcWriteDocComment(Printer* printer, VARS& vars,
const string& comments) {
static void GrpcWriteDocComment(Printer *printer, VARS &vars,
const string &comments) {
printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLines(comments);
GrpcWriteDocCommentBody(printer, vars, lines, false);
printer->Print(" */\n");
}
static void GrpcWriteServiceDocComment(Printer* printer, VARS& vars,
const ServiceDescriptor* service) {
static void GrpcWriteServiceDocComment(Printer *printer, VARS &vars,
const ServiceDescriptor *service) {
printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLinesForDescriptor(service);
GrpcWriteDocCommentBody(printer, vars, lines, true);
printer->Print(" */\n");
}
void GrpcWriteMethodDocComment(Printer* printer, VARS& vars,
const MethodDescriptor* method) {
void GrpcWriteMethodDocComment(Printer *printer, VARS &vars,
const MethodDescriptor *method) {
printer->Print("/**\n");
std::vector<string> lines = GrpcGetDocLinesForDescriptor(method);
GrpcWriteDocCommentBody(printer, vars, lines, true);
printer->Print(" */\n");
}
//outputs static singleton extractor for type stored in "extr_type" and "extr_type_name" vars
static void PrintTypeExtractor(Printer* p, VARS& vars) {
p->Print(
vars,
"private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
"extractorOf$extr_type_name$;\n"
"private static FlatbuffersUtils.FBExtactor<$extr_type$> "
"getExtractorOf$extr_type_name$() {\n"
" if (extractorOf$extr_type_name$ != null) return "
"extractorOf$extr_type_name$;\n"
" synchronized ($service_class_name$.class) {\n"
" if (extractorOf$extr_type_name$ != null) return "
"extractorOf$extr_type_name$;\n"
" extractorOf$extr_type_name$ = new "
"FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
" public $extr_type$ extract (ByteBuffer buffer) {\n"
" return "
"$extr_type$.getRootAs$extr_type_name$(buffer);\n"
" }\n"
" };\n"
" return extractorOf$extr_type_name$;\n"
" }\n"
"}\n\n");
// outputs static singleton extractor for type stored in "extr_type" and
// "extr_type_name" vars
static void PrintTypeExtractor(Printer *p, VARS &vars) {
p->Print(vars,
"private static volatile FlatbuffersUtils.FBExtactor<$extr_type$> "
"extractorOf$extr_type_name$;\n"
"private static FlatbuffersUtils.FBExtactor<$extr_type$> "
"getExtractorOf$extr_type_name$() {\n"
" if (extractorOf$extr_type_name$ != null) return "
"extractorOf$extr_type_name$;\n"
" synchronized ($service_class_name$.class) {\n"
" if (extractorOf$extr_type_name$ != null) return "
"extractorOf$extr_type_name$;\n"
" extractorOf$extr_type_name$ = new "
"FlatbuffersUtils.FBExtactor<$extr_type$>() {\n"
" public $extr_type$ extract (ByteBuffer buffer) {\n"
" return "
"$extr_type$.getRootAs$extr_type_name$(buffer);\n"
" }\n"
" };\n"
" return extractorOf$extr_type_name$;\n"
" }\n"
"}\n\n");
}
static void PrintMethodFields(Printer* p, VARS& vars,
const ServiceDescriptor* service) {
static void PrintMethodFields(Printer *p, VARS &vars,
const ServiceDescriptor *service) {
p->Print("// Static method descriptors that strictly reflect the proto.\n");
vars["service_name"] = service->name();
//set of names of rpc input- and output- types that were already encountered.
//this is needed to avoid duplicating type extractor since it's possible that
//the same type is used as an input or output type of more than a single RPC method
// set of names of rpc input- and output- types that were already encountered.
// this is needed to avoid duplicating type extractor since it's possible that
// the same type is used as an input or output type of more than a single RPC
// method
std::set<std::string> encounteredTypes;
for (int i = 0; i < service->method_count(); ++i) {
auto method = service->method(i);
vars["arg_in_id"] = to_string(2L * i); //trying to make msvc 10 happy
vars["arg_in_id"] = to_string(2L * i); // trying to make msvc 10 happy
vars["arg_out_id"] = to_string(2L * i + 1);
vars["method_name"] = method->name();
vars["input_type_name"] = method->get_input_type_name();
@ -351,8 +342,10 @@ static void PrintMethodFields(Printer* p, VARS& vars,
vars["method_field_name"] = MethodPropertiesFieldName(method.get());
vars["method_new_field_name"] = MethodPropertiesGetterName(method.get());
vars["method_method_name"] = MethodPropertiesGetterName(method.get());
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
bool client_streaming =
method->ClientStreaming() || method->BidiStreaming();
bool server_streaming =
method->ServerStreaming() || method->BidiStreaming();
if (client_streaming) {
if (server_streaming) {
vars["method_type"] = "BIDI_STREAMING";
@ -392,32 +385,32 @@ static void PrintMethodFields(Printer* p, VARS& vars,
}
p->Print(
vars,
"@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
"1901\")\n"
"public static $MethodDescriptor$<$input_type$,\n"
" $output_type$> $method_method_name$() {\n"
" $MethodDescriptor$<$input_type$, $output_type$> "
"$method_new_field_name$;\n"
" if (($method_new_field_name$ = "
"$service_class_name$.$method_new_field_name$) == null) {\n"
" synchronized ($service_class_name$.class) {\n"
" if (($method_new_field_name$ = "
"$service_class_name$.$method_new_field_name$) == null) {\n"
" $service_class_name$.$method_new_field_name$ = "
"$method_new_field_name$ = \n"
" $MethodDescriptor$.<$input_type$, "
"$output_type$>newBuilder()\n"
" .setType($MethodType$.$method_type$)\n"
" .setFullMethodName(generateFullMethodName(\n"
" \"$Package$$service_name$\", \"$method_name$\"))\n"
" .setSampledToLocalTracing(true)\n"
" .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
" $input_type$.class, "
"getExtractorOf$input_type_name$()))\n"
" .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
" $output_type$.class, "
"getExtractorOf$output_type_name$()))\n");
vars,
"@$ExperimentalApi$(\"https://github.com/grpc/grpc-java/issues/"
"1901\")\n"
"public static $MethodDescriptor$<$input_type$,\n"
" $output_type$> $method_method_name$() {\n"
" $MethodDescriptor$<$input_type$, $output_type$> "
"$method_new_field_name$;\n"
" if (($method_new_field_name$ = "
"$service_class_name$.$method_new_field_name$) == null) {\n"
" synchronized ($service_class_name$.class) {\n"
" if (($method_new_field_name$ = "
"$service_class_name$.$method_new_field_name$) == null) {\n"
" $service_class_name$.$method_new_field_name$ = "
"$method_new_field_name$ = \n"
" $MethodDescriptor$.<$input_type$, "
"$output_type$>newBuilder()\n"
" .setType($MethodType$.$method_type$)\n"
" .setFullMethodName(generateFullMethodName(\n"
" \"$Package$$service_name$\", \"$method_name$\"))\n"
" .setSampledToLocalTracing(true)\n"
" .setRequestMarshaller(FlatbuffersUtils.marshaller(\n"
" $input_type$.class, "
"getExtractorOf$input_type_name$()))\n"
" .setResponseMarshaller(FlatbuffersUtils.marshaller(\n"
" $output_type$.class, "
"getExtractorOf$output_type_name$()))\n");
// vars["proto_method_descriptor_supplier"] = service->name() +
// "MethodDescriptorSupplier";
@ -449,11 +442,11 @@ enum StubType {
enum CallType { ASYNC_CALL = 0, BLOCKING_CALL = 1, FUTURE_CALL = 2 };
static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
const ServiceDescriptor* service);
static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
const ServiceDescriptor *service);
// Prints a client interface or implementation class, or a server interface.
static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
static void PrintStub(Printer *p, VARS &vars, const ServiceDescriptor *service,
StubType type) {
const string service_name = service->name();
vars["service_name"] = service_name;
@ -474,7 +467,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
break;
case BLOCKING_CLIENT_INTERFACE:
interface = true;
FLATBUFFERS_FALLTHROUGH(); // fall thru
FLATBUFFERS_FALLTHROUGH(); // fall thru
case BLOCKING_CLIENT_IMPL:
call_type = BLOCKING_CALL;
stub_name += "BlockingStub";
@ -482,7 +475,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
break;
case FUTURE_CLIENT_INTERFACE:
interface = true;
FLATBUFFERS_FALLTHROUGH(); // fall thru
FLATBUFFERS_FALLTHROUGH(); // fall thru
case FUTURE_CLIENT_IMPL:
call_type = FUTURE_CALL;
stub_name += "FutureStub";
@ -499,9 +492,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
vars["client_name"] = client_name;
// Class head
if (!interface) {
GrpcWriteServiceDocComment(p, vars, service);
}
if (!interface) { GrpcWriteServiceDocComment(p, vars, service); }
if (impl_base) {
p->Print(vars,
"public static abstract class $abstract_name$ implements "
@ -544,8 +535,10 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
vars["lower_method_name"] = LowerMethodName(&*method);
vars["method_method_name"] = MethodPropertiesGetterName(&*method);
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
bool client_streaming =
method->ClientStreaming() || method->BidiStreaming();
bool server_streaming =
method->ServerStreaming() || method->BidiStreaming();
if (call_type == BLOCKING_CALL && client_streaming) {
// Blocking client interface with client streaming is not available
@ -561,9 +554,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
p->Print("\n");
// TODO(nmittler): Replace with WriteMethodDocComment once included by the
// protobuf distro.
if (!interface) {
GrpcWriteMethodDocComment(p, vars, &*method);
}
if (!interface) { GrpcWriteMethodDocComment(p, vars, &*method); }
p->Print("public ");
switch (call_type) {
case BLOCKING_CALL:
@ -628,8 +619,7 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
"responseObserver);\n");
}
break;
default:
break;
default: break;
}
} else if (!interface) {
switch (call_type) {
@ -704,15 +694,15 @@ static void PrintStub(Printer* p, VARS& vars, const ServiceDescriptor* service,
}
static bool CompareMethodClientStreaming(
const std::unique_ptr<const grpc_generator::Method>& method1,
const std::unique_ptr<const grpc_generator::Method>& method2) {
const std::unique_ptr<const grpc_generator::Method> &method1,
const std::unique_ptr<const grpc_generator::Method> &method2) {
return method1->ClientStreaming() < method2->ClientStreaming();
}
// Place all method invocations into a single class to reduce memory footprint
// on Android.
static void PrintMethodHandlerClass(Printer* p, VARS& vars,
const ServiceDescriptor* service) {
static void PrintMethodHandlerClass(Printer *p, VARS &vars,
const ServiceDescriptor *service) {
// Sort method ids based on ClientStreaming() so switch tables are compact.
std::vector<std::unique_ptr<const grpc_generator::Method>> sorted_methods(
service->method_count());
@ -722,7 +712,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
stable_sort(sorted_methods.begin(), sorted_methods.end(),
CompareMethodClientStreaming);
for (size_t i = 0; i < sorted_methods.size(); i++) {
auto& method = sorted_methods[i];
auto &method = sorted_methods[i];
vars["method_id"] = to_string(i);
vars["method_id_name"] = MethodIdFieldName(&*method);
p->Print(vars,
@ -755,9 +745,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
for (int i = 0; i < service->method_count(); ++i) {
auto method = service->method(i);
if (method->ClientStreaming() || method->BidiStreaming()) {
continue;
}
if (method->ClientStreaming() || method->BidiStreaming()) { continue; }
vars["method_id_name"] = MethodIdFieldName(&*method);
vars["lower_method_name"] = LowerMethodName(&*method);
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
@ -789,9 +777,7 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
for (int i = 0; i < service->method_count(); ++i) {
auto method = service->method(i);
if (!(method->ClientStreaming() || method->BidiStreaming())) {
continue;
}
if (!(method->ClientStreaming() || method->BidiStreaming())) { continue; }
vars["method_id_name"] = MethodIdFieldName(&*method);
vars["lower_method_name"] = LowerMethodName(&*method);
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
@ -816,8 +802,8 @@ static void PrintMethodHandlerClass(Printer* p, VARS& vars,
p->Print("}\n\n");
}
static void PrintGetServiceDescriptorMethod(Printer* p, VARS& vars,
const ServiceDescriptor* service) {
static void PrintGetServiceDescriptorMethod(Printer *p, VARS &vars,
const ServiceDescriptor *service) {
vars["service_name"] = service->name();
// vars["proto_base_descriptor_supplier"] = service->name() +
// "BaseDescriptorSupplier"; vars["proto_file_descriptor_supplier"] =
@ -909,8 +895,8 @@ static void PrintGetServiceDescriptorMethod(Printer* p, VARS& vars,
p->Print("}\n");
}
static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
const ServiceDescriptor* service) {
static void PrintBindServiceMethodBody(Printer *p, VARS &vars,
const ServiceDescriptor *service) {
vars["service_name"] = service->name();
p->Indent();
p->Print(vars,
@ -925,8 +911,10 @@ static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
vars["input_type"] = JavaClassName(vars, method->get_input_type_name());
vars["output_type"] = JavaClassName(vars, method->get_output_type_name());
vars["method_id_name"] = MethodIdFieldName(&*method);
bool client_streaming = method->ClientStreaming() || method->BidiStreaming();
bool server_streaming = method->ServerStreaming() || method->BidiStreaming();
bool client_streaming =
method->ClientStreaming() || method->BidiStreaming();
bool server_streaming =
method->ServerStreaming() || method->BidiStreaming();
if (client_streaming) {
if (server_streaming) {
vars["calls_method"] = "asyncBidiStreamingCall";
@ -960,8 +948,8 @@ static void PrintBindServiceMethodBody(Printer* p, VARS& vars,
p->Outdent();
}
static void PrintService(Printer* p, VARS& vars,
const ServiceDescriptor* service,
static void PrintService(Printer *p, VARS &vars,
const ServiceDescriptor *service,
bool disable_version) {
vars["service_name"] = service->name();
vars["service_class_name"] = ServiceClassName(service->name());
@ -1041,7 +1029,7 @@ static void PrintService(Printer* p, VARS& vars,
p->Print("}\n");
}
void PrintStaticImports(Printer* p) {
void PrintStaticImports(Printer *p) {
p->Print(
"import java.nio.ByteBuffer;\n"
"import static "
@ -1074,8 +1062,8 @@ void PrintStaticImports(Printer* p) {
"io.grpc.stub.ServerCalls.asyncUnimplementedUnaryCall;\n\n");
}
void GenerateService(const grpc_generator::Service* service,
grpc_generator::Printer* printer, VARS& vars,
void GenerateService(const grpc_generator::Service *service,
grpc_generator::Printer *printer, VARS &vars,
bool disable_version) {
// All non-generated classes must be referred by fully qualified names to
// avoid collision with generated classes.
@ -1111,8 +1099,8 @@ void GenerateService(const grpc_generator::Service* service,
}
grpc::string GenerateServiceSource(
grpc_generator::File* file, const grpc_generator::Service* service,
grpc_java_generator::Parameters* parameters) {
grpc_generator::File *file, const grpc_generator::Service *service,
grpc_java_generator::Parameters *parameters) {
grpc::string out;
auto printer = file->CreatePrinter(&out);
VARS vars;