mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-03 12:21:23 +00:00
bulk code format fix (#8707)
This commit is contained in:
@@ -35,22 +35,22 @@ namespace flatbuffers {
|
||||
namespace python {
|
||||
namespace grpc {
|
||||
namespace {
|
||||
bool ClientStreaming(const RPCCall *method) {
|
||||
const Value *val = method->attributes.Lookup("streaming");
|
||||
bool ClientStreaming(const RPCCall* method) {
|
||||
const Value* val = method->attributes.Lookup("streaming");
|
||||
return val != nullptr &&
|
||||
(val->constant == "client" || val->constant == "bidi");
|
||||
}
|
||||
|
||||
bool ServerStreaming(const RPCCall *method) {
|
||||
const Value *val = method->attributes.Lookup("streaming");
|
||||
bool ServerStreaming(const RPCCall* method) {
|
||||
const Value* val = method->attributes.Lookup("streaming");
|
||||
return val != nullptr &&
|
||||
(val->constant == "server" || val->constant == "bidi");
|
||||
}
|
||||
|
||||
void FormatImports(std::stringstream &ss, const Imports &imports) {
|
||||
void FormatImports(std::stringstream& ss, const Imports& imports) {
|
||||
std::set<std::string> modules;
|
||||
std::map<std::string, std::set<std::string>> names_by_module;
|
||||
for (const Import &import : imports.imports) {
|
||||
for (const Import& import : imports.imports) {
|
||||
if (import.IsLocal()) continue; // skip all local imports
|
||||
if (import.name == "") {
|
||||
modules.insert(import.module);
|
||||
@@ -59,14 +59,14 @@ void FormatImports(std::stringstream &ss, const Imports &imports) {
|
||||
}
|
||||
}
|
||||
|
||||
for (const std::string &module : modules) {
|
||||
for (const std::string& module : modules) {
|
||||
ss << "import " << module << '\n';
|
||||
}
|
||||
ss << '\n';
|
||||
for (const auto &import : names_by_module) {
|
||||
for (const auto& import : names_by_module) {
|
||||
ss << "from " << import.first << " import ";
|
||||
size_t i = 0;
|
||||
for (const std::string &name : import.second) {
|
||||
for (const std::string& name : import.second) {
|
||||
if (i > 0) ss << ", ";
|
||||
ss << name;
|
||||
++i;
|
||||
@@ -76,8 +76,8 @@ void FormatImports(std::stringstream &ss, const Imports &imports) {
|
||||
ss << "\n\n";
|
||||
}
|
||||
|
||||
bool SaveStub(const std::string &filename, const Imports &imports,
|
||||
const std::string &content) {
|
||||
bool SaveStub(const std::string& filename, const Imports& imports,
|
||||
const std::string& content) {
|
||||
std::stringstream ss;
|
||||
ss << "# Generated by the gRPC FlatBuffers compiler. DO NOT EDIT!\n"
|
||||
<< '\n'
|
||||
@@ -90,8 +90,8 @@ bool SaveStub(const std::string &filename, const Imports &imports,
|
||||
return flatbuffers::SaveFile(filename.c_str(), ss.str(), false);
|
||||
}
|
||||
|
||||
bool SaveService(const std::string &filename, const Imports &imports,
|
||||
const std::string &content) {
|
||||
bool SaveService(const std::string& filename, const Imports& imports,
|
||||
const std::string& content) {
|
||||
std::stringstream ss;
|
||||
ss << "# Generated by the gRPC FlatBuffers compiler. DO NOT EDIT!\n" << '\n';
|
||||
FormatImports(ss, imports);
|
||||
@@ -103,32 +103,33 @@ bool SaveService(const std::string &filename, const Imports &imports,
|
||||
|
||||
class BaseGenerator {
|
||||
protected:
|
||||
BaseGenerator(const Parser &parser, const Namer::Config &config,
|
||||
const std::string &path, const Version &version)
|
||||
: parser_{ parser },
|
||||
namer_{ WithFlagOptions(config, parser.opts, path), Keywords(version) },
|
||||
version_{ version },
|
||||
BaseGenerator(const Parser& parser, const Namer::Config& config,
|
||||
const std::string& path, const Version& version)
|
||||
: parser_{parser},
|
||||
namer_{WithFlagOptions(config, parser.opts, path), Keywords(version)},
|
||||
version_{version},
|
||||
path_(path) {}
|
||||
|
||||
protected:
|
||||
std::string ModuleForFile(const std::string &file) const {
|
||||
std::string ModuleForFile(const std::string& file) const {
|
||||
std::string module = parser_.opts.include_prefix + StripExtension(file) +
|
||||
parser_.opts.filename_suffix;
|
||||
std::replace(module.begin(), module.end(), '/', '.');
|
||||
return module;
|
||||
}
|
||||
|
||||
template<typename T> std::string ModuleFor(const T *def) const {
|
||||
template <typename T>
|
||||
std::string ModuleFor(const T* def) const {
|
||||
if (parser_.opts.one_file) return ModuleForFile(def->file);
|
||||
return namer_.NamespacedType(*def);
|
||||
}
|
||||
|
||||
std::string NamespaceDir(const Parser &parser, const std::string &path,
|
||||
const Namespace &ns, const bool dasherize) {
|
||||
std::string NamespaceDir(const Parser& parser, const std::string& path,
|
||||
const Namespace& ns, const bool dasherize) {
|
||||
EnsureDirExists(path);
|
||||
if (parser.opts.one_file) return path;
|
||||
std::string namespace_dir = path; // Either empty or ends in separator.
|
||||
auto &namespaces = ns.components;
|
||||
auto& namespaces = ns.components;
|
||||
for (auto it = namespaces.begin(); it != namespaces.end(); ++it) {
|
||||
namespace_dir +=
|
||||
!dasherize ? *it : ConvertCase(*it, Case::kDasher, Case::kUpperCamel);
|
||||
@@ -138,32 +139,32 @@ class BaseGenerator {
|
||||
return namespace_dir;
|
||||
}
|
||||
|
||||
std::string NamespaceDir(const Namespace &ns, const bool dasherize) {
|
||||
std::string NamespaceDir(const Namespace& ns, const bool dasherize) {
|
||||
return NamespaceDir(parser_, path_, ns, dasherize);
|
||||
}
|
||||
|
||||
const Parser &parser_;
|
||||
const Parser& parser_;
|
||||
const IdlNamer namer_;
|
||||
const Version version_;
|
||||
const std::string &path_;
|
||||
const std::string& path_;
|
||||
};
|
||||
|
||||
class StubGenerator : public BaseGenerator {
|
||||
public:
|
||||
StubGenerator(const Parser &parser, const std::string &path,
|
||||
const Version &version)
|
||||
StubGenerator(const Parser& parser, const std::string& path,
|
||||
const Version& version)
|
||||
: BaseGenerator(parser, kStubConfig, path, version) {}
|
||||
|
||||
bool Generate() {
|
||||
Imports imports;
|
||||
std::stringstream stub;
|
||||
std::string ns_name{};
|
||||
for (const ServiceDef *service : parser_.services_.vec) {
|
||||
for (const ServiceDef* service : parser_.services_.vec) {
|
||||
Generate(stub, service, &imports);
|
||||
ns_name = NamespaceDir(*service->defined_namespace, false);
|
||||
}
|
||||
|
||||
std::string sanitized_suffix{ parser_.opts.grpc_filename_suffix };
|
||||
std::string sanitized_suffix{parser_.opts.grpc_filename_suffix};
|
||||
std::replace(sanitized_suffix.begin(), sanitized_suffix.end(), '.', '_');
|
||||
std::string filename =
|
||||
ns_name + kPathSeparator +
|
||||
@@ -174,14 +175,14 @@ class StubGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
private:
|
||||
void Generate(std::stringstream &ss, const ServiceDef *service,
|
||||
Imports *imports) {
|
||||
void Generate(std::stringstream& ss, const ServiceDef* service,
|
||||
Imports* imports) {
|
||||
imports->Import("grpc");
|
||||
|
||||
ss << "class " << service->name << "Stub(object):\n"
|
||||
<< " def __init__(self, channel: grpc.Channel) -> None: ...\n";
|
||||
|
||||
for (const RPCCall *method : service->calls.vec) {
|
||||
for (const RPCCall* method : service->calls.vec) {
|
||||
std::string request = "bytes";
|
||||
std::string response = "bytes";
|
||||
|
||||
@@ -213,7 +214,7 @@ class StubGenerator : public BaseGenerator {
|
||||
ss << "\n\n";
|
||||
ss << "class " << service->name << "Servicer(object):\n";
|
||||
|
||||
for (const RPCCall *method : service->calls.vec) {
|
||||
for (const RPCCall* method : service->calls.vec) {
|
||||
std::string request = "bytes";
|
||||
std::string response = "bytes";
|
||||
|
||||
@@ -252,8 +253,8 @@ class StubGenerator : public BaseGenerator {
|
||||
|
||||
class ServiceGenerator : public BaseGenerator {
|
||||
public:
|
||||
ServiceGenerator(const Parser &parser, const std::string &path,
|
||||
const Version &version)
|
||||
ServiceGenerator(const Parser& parser, const std::string& path,
|
||||
const Version& version)
|
||||
: BaseGenerator(parser, kConfig, path, version) {}
|
||||
|
||||
bool Generate() {
|
||||
@@ -274,14 +275,14 @@ class ServiceGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
std::string ns_name{};
|
||||
for (const ServiceDef *service : parser_.services_.vec) {
|
||||
for (const ServiceDef* service : parser_.services_.vec) {
|
||||
GenerateStub(ss, service, &imports);
|
||||
GenerateServicer(ss, service, &imports);
|
||||
GenerateRegister(ss, service, &imports);
|
||||
ns_name = NamespaceDir(*service->defined_namespace, false);
|
||||
}
|
||||
|
||||
std::string sanitized_suffix{ parser_.opts.grpc_filename_suffix };
|
||||
std::string sanitized_suffix{parser_.opts.grpc_filename_suffix};
|
||||
std::replace(sanitized_suffix.begin(), sanitized_suffix.end(), '.', '_');
|
||||
std::string filename =
|
||||
ns_name + kPathSeparator +
|
||||
@@ -292,8 +293,8 @@ class ServiceGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
private:
|
||||
void GenerateStub(std::stringstream &ss, const ServiceDef *service,
|
||||
Imports *imports) {
|
||||
void GenerateStub(std::stringstream& ss, const ServiceDef* service,
|
||||
Imports* imports) {
|
||||
ss << "class " << service->name << "Stub";
|
||||
if (version_.major != 3) ss << "(object)";
|
||||
ss << ":\n"
|
||||
@@ -307,7 +308,7 @@ class ServiceGenerator : public BaseGenerator {
|
||||
<< " '''\n"
|
||||
<< '\n';
|
||||
|
||||
for (const RPCCall *method : service->calls.vec) {
|
||||
for (const RPCCall* method : service->calls.vec) {
|
||||
std::string response = namer_.Type(*method->response);
|
||||
|
||||
imports->Import(ModuleFor(method->response), response);
|
||||
@@ -330,8 +331,8 @@ class ServiceGenerator : public BaseGenerator {
|
||||
ss << '\n';
|
||||
}
|
||||
|
||||
void GenerateServicer(std::stringstream &ss, const ServiceDef *service,
|
||||
Imports *imports) {
|
||||
void GenerateServicer(std::stringstream& ss, const ServiceDef* service,
|
||||
Imports* imports) {
|
||||
imports->Import("grpc");
|
||||
|
||||
ss << "class " << service->name << "Servicer";
|
||||
@@ -340,7 +341,7 @@ class ServiceGenerator : public BaseGenerator {
|
||||
<< " '''Interface exported by the server.'''\n"
|
||||
<< '\n';
|
||||
|
||||
for (const RPCCall *method : service->calls.vec) {
|
||||
for (const RPCCall* method : service->calls.vec) {
|
||||
const std::string request_param =
|
||||
ClientStreaming(method) ? "request_iterator" : "request";
|
||||
ss << " def " << method->name << "(self, " << request_param
|
||||
@@ -354,15 +355,15 @@ class ServiceGenerator : public BaseGenerator {
|
||||
ss << '\n';
|
||||
}
|
||||
|
||||
void GenerateRegister(std::stringstream &ss, const ServiceDef *service,
|
||||
Imports *imports) {
|
||||
void GenerateRegister(std::stringstream& ss, const ServiceDef* service,
|
||||
Imports* imports) {
|
||||
imports->Import("grpc");
|
||||
|
||||
ss << "def add_" << service->name
|
||||
<< "Servicer_to_server(servicer, server):\n"
|
||||
<< " rpc_method_handlers = {\n";
|
||||
|
||||
for (const RPCCall *method : service->calls.vec) {
|
||||
for (const RPCCall* method : service->calls.vec) {
|
||||
std::string request = namer_.Type(*method->request);
|
||||
|
||||
imports->Import(ModuleFor(method->request), request);
|
||||
@@ -393,15 +394,15 @@ class ServiceGenerator : public BaseGenerator {
|
||||
};
|
||||
} // namespace
|
||||
|
||||
bool Generate(const Parser &parser, const std::string &path,
|
||||
const Version &version) {
|
||||
ServiceGenerator generator{ parser, path, version };
|
||||
bool Generate(const Parser& parser, const std::string& path,
|
||||
const Version& version) {
|
||||
ServiceGenerator generator{parser, path, version};
|
||||
return generator.Generate();
|
||||
}
|
||||
|
||||
bool GenerateStub(const Parser &parser, const std::string &path,
|
||||
const Version &version) {
|
||||
StubGenerator generator{ parser, path, version };
|
||||
bool GenerateStub(const Parser& parser, const std::string& path,
|
||||
const Version& version) {
|
||||
StubGenerator generator{parser, path, version};
|
||||
return generator.Generate();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user