[Swift] Adds GRPC to Swift (#5758)

* Adds the basic structure for required to add grpc support

Added the message implementation

Updated the code to confirm to the protocol flatbuffersobject

Adds an example for Swift flatbuffers GRPC

Started implementing the swift GRPC code Gen

Generator generates protocols now

Fixing ci issues

Migrated the logic to use grpc_generator::File instead of string

Refactored swift project

Implemented GRPC in swift

Finished implementing GRPC in swift

Fixes issues

Adds contiguousBytes initializer to swift

Adds documentation + fixes buffer nameing in tables + structs

Adds documentation + fixes buffer nameing in tables + structs

Updated tests

* Updated version
This commit is contained in:
mustiikhalil
2020-02-24 20:27:41 +03:00
committed by GitHub
parent cd88e6b2aa
commit 34305c4ce4
26 changed files with 973 additions and 8 deletions

View File

@@ -25,6 +25,7 @@
#include "src/compiler/java_generator.h"
#include "src/compiler/python_generator.h"
#include "src/compiler/python_private_generator.h"
#include "src/compiler/swift_generator.h"
#if defined(_MSC_VER)
# pragma warning(push)
@@ -79,9 +80,7 @@ class FlatBufMethod : public grpc_generator::Method {
return true;
}
std::string get_fb_builder() const {
return "builder";
}
std::string get_fb_builder() const { return "builder"; }
std::string input_type_name() const { return GRPCType(*method_->request); }
@@ -186,7 +185,11 @@ class FlatBufPrinter : public grpc_generator::Printer {
class FlatBufFile : public grpc_generator::File {
public:
enum Language {
kLanguageGo, kLanguageCpp, kLanguageJava, kLanguagePython
kLanguageGo,
kLanguageCpp,
kLanguageJava,
kLanguagePython,
kLanguageSwift
};
FlatBufFile(const Parser &parser, const std::string &file_name,
@@ -235,6 +238,9 @@ class FlatBufFile : public grpc_generator::File {
case kLanguagePython: {
return "";
}
case kLanguageSwift: {
return "";
}
}
return "";
}
@@ -373,7 +379,6 @@ bool GenerateJavaGRPC(const Parser &parser, const std::string &path,
bool GeneratePythonGRPC(const Parser &parser, const std::string & /*path*/,
const std::string &file_name) {
int nservices = 0;
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
++it) {
@@ -403,6 +408,46 @@ bool GeneratePythonGRPC(const Parser &parser, const std::string & /*path*/,
return flatbuffers::SaveFile(grpc_py_filename.c_str(), code, false);
}
class SwiftGRPCGenerator : public flatbuffers::BaseGenerator {
private:
CodeWriter code_;
public:
SwiftGRPCGenerator(const Parser &parser, const std::string &path,
const std::string &filename)
: BaseGenerator(parser, path, filename, "", "" /*Unused*/) {}
bool generate() {
code_.Clear();
code_ += "// Generated GRPC code for FlatBuffers swift!";
code_ += grpc_swift_generator::GenerateHeader();
FlatBufFile file(parser_, file_name_, FlatBufFile::kLanguageSwift);
for (int i = 0; i < file.service_count(); i++) {
auto service = file.service(i);
code_ += grpc_swift_generator::Generate(&file, service.get());
}
const auto final_code = code_.ToString();
const auto filename = GeneratedFileName(path_, file_name_);
return SaveFile(filename.c_str(), final_code, false);
}
static std::string GeneratedFileName(const std::string &path,
const std::string &file_name) {
return path + file_name + ".grpc.swift";
}
};
bool GenerateSwiftGRPC(const Parser &parser, const std::string &path,
const std::string &file_name) {
int nservices = 0;
for (auto it = parser.services_.vec.begin(); it != parser.services_.vec.end();
++it) {
if (!(*it)->generated) nservices++;
}
if (!nservices) return true;
return SwiftGRPCGenerator(parser, path, file_name).generate();
}
} // namespace flatbuffers
#if defined(_MSC_VER)