Refactor languages to use CodeGenerator interface. (#7797)

* Refactor to use CodeGenerator interface.

- Move code to its own header file to be included in flatc_main.cpp
- Refactor code to use CodeGenerator interface for all languages

* Format all files

* remove lua code generator since it doesn't support bfbs generator

* Update CMakeLists file with new idl_gen_*.cpp and idl_gen_*.h files

* Add idl_gen_swift header file

* Add idl_gen_swift header file and update bazel file

* Remove CodeGenerator interface for idl_gen_text.*. Remove comments and extern declaration

* Reorder header and implementation files in CMakeLists.txt

* Add idl_gen_* header files to implementation files

* Update CMakeLists and remove unused import

Co-authored-by: Derek Bailey <derekbailey@google.com>
This commit is contained in:
Khanh Nguyen
2023-01-25 09:05:48 -08:00
committed by GitHub
parent 802a3a056a
commit 34c821f4ad
35 changed files with 1269 additions and 7 deletions

View File

@@ -168,6 +168,8 @@ set(FlatBuffers_Library_SRCS
set(FlatBuffers_Compiler_SRCS
${FlatBuffers_Library_SRCS}
src/idl_gen_binary.cpp
src/idl_gen_text.cpp
src/idl_gen_cpp.cpp
src/idl_gen_csharp.cpp
src/idl_gen_dart.cpp

View File

@@ -23,7 +23,7 @@
namespace flatbuffers {
// An code generator interface for producing converting flatbuffer schema into
// A code generator interface for producing converting flatbuffer schema into
// code.
class CodeGenerator {
public:

View File

@@ -31,10 +31,6 @@
namespace flatbuffers {
// TODO(derekbailey): It would be better to define these as normal includes and
// not as extern functions. But this can be done at a later time.
extern std::unique_ptr<flatbuffers::CodeGenerator> NewCppCodeGenerator();
extern void LogCompilerWarn(const std::string &warn);
extern void LogCompilerError(const std::string &err);

View File

@@ -68,22 +68,38 @@ cc_library(
"bfbs_gen_nim.h",
"bfbs_namer.h",
"flatc_main.cpp",
"idl_gen_binary.cpp",
"idl_gen_binary.h",
"idl_gen_cpp.cpp",
"idl_gen_cpp.h",
"idl_gen_csharp.cpp",
"idl_gen_csharp.h",
"idl_gen_dart.cpp",
"idl_gen_dart.h",
"idl_gen_go.cpp",
"idl_gen_go.h",
"idl_gen_grpc.cpp",
"idl_gen_java.cpp",
"idl_gen_java.h",
"idl_gen_json_schema.cpp",
"idl_gen_json_schema.h",
"idl_gen_kotlin.cpp",
"idl_gen_kotlin.h",
"idl_gen_lobster.cpp",
"idl_gen_lobster.h",
"idl_gen_lua.cpp",
"idl_gen_lua.h",
"idl_gen_php.cpp",
"idl_gen_php.h",
"idl_gen_python.cpp",
"idl_gen_python.h",
"idl_gen_rust.cpp",
"idl_gen_rust.h",
"idl_gen_swift.cpp",
"idl_gen_swift.h",
"idl_gen_text.cpp",
"idl_gen_ts.cpp",
"idl_gen_ts.h",
"idl_namer.h",
"namer.h",
"util.cpp",

View File

@@ -23,8 +23,20 @@
#include "flatbuffers/code_generator.h"
#include "flatbuffers/flatc.h"
#include "flatbuffers/util.h"
#include "idl_gen_binary.h"
#include "idl_gen_cpp.h"
#include "idl_gen_csharp.h"
#include "idl_gen_dart.h"
#include "idl_gen_go.h"
#include "idl_gen_java.h"
#include "idl_gen_json_schema.h"
#include "idl_gen_kotlin.h"
#include "idl_gen_lobster.h"
#include "idl_gen_php.h"
#include "idl_gen_python.h"
#include "idl_gen_rust.h"
#include "idl_gen_swift.h"
#include "idl_gen_ts.h"
static const char *g_program_name = nullptr;
@@ -162,12 +174,85 @@ int main(int argc, const char *argv[]) {
flatbuffers::FlatCompiler flatc(params);
std::shared_ptr<flatbuffers::CodeGenerator> binary_generator =
flatbuffers::NewBinaryCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> cpp_generator =
flatbuffers::NewCppCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> csharp_generator =
flatbuffers::NewCSharpCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> dart_generator =
flatbuffers::NewDartCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> go_generator =
flatbuffers::NewGoCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> java_generator =
flatbuffers::NewJavaCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> json_schema_generator =
flatbuffers::NewJsonSchemaCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> kotlin_generator =
flatbuffers::NewKotlinCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> lobster_generator =
flatbuffers::NewLobsterCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> php_generator =
flatbuffers::NewPhpCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> python_generator =
flatbuffers::NewPythonCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> rust_generator =
flatbuffers::NewRustCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> swift_generator =
flatbuffers::NewSwiftCodeGenerator();
std::shared_ptr<flatbuffers::CodeGenerator> ts_generator =
flatbuffers::NewTsCodeGenerator();
flatc.RegisterCodeGenerator("--binary", binary_generator);
flatc.RegisterCodeGenerator("-b", binary_generator);
flatc.RegisterCodeGenerator("--cpp", cpp_generator);
flatc.RegisterCodeGenerator("-c", cpp_generator);
flatc.RegisterCodeGenerator("--csharp", csharp_generator);
flatc.RegisterCodeGenerator("-n", csharp_generator);
flatc.RegisterCodeGenerator("--dart", dart_generator);
flatc.RegisterCodeGenerator("-d", dart_generator);
flatc.RegisterCodeGenerator("--go", go_generator);
flatc.RegisterCodeGenerator("-g", go_generator);
flatc.RegisterCodeGenerator("--java", java_generator);
flatc.RegisterCodeGenerator("-j", java_generator);
flatc.RegisterCodeGenerator("--jsonschema", json_schema_generator);
flatc.RegisterCodeGenerator("--kotlin", kotlin_generator);
flatc.RegisterCodeGenerator("--lobster", lobster_generator);
flatc.RegisterCodeGenerator("--php", php_generator);
flatc.RegisterCodeGenerator("--python", python_generator);
flatc.RegisterCodeGenerator("-p", python_generator);
flatc.RegisterCodeGenerator("--rust", rust_generator);
flatc.RegisterCodeGenerator("-r", rust_generator);
flatc.RegisterCodeGenerator("--swift", rust_generator);
flatc.RegisterCodeGenerator("--ts", ts_generator);
flatc.RegisterCodeGenerator("-T", ts_generator);
// Create the FlatC options by parsing the command line arguments.
const flatbuffers::FlatCOptions &options =
flatc.ParseFromCommandLineArguments(argc, argv);

83
src/idl_gen_binary.cpp Normal file
View File

@@ -0,0 +1,83 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_binary.h"
#include <limits>
#include <memory>
#include <string>
#include <unordered_set>
#include "flatbuffers/base.h"
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/flatc.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
namespace flatbuffers {
namespace {
class BinaryCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateBinary(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
// Generate code from the provided `buffer` of given `length`. The buffer is a
// serialized reflection.fbs.
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
output = BinaryMakeRule(parser, path, filename);
return Status::OK;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return false; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kBinary; }
std::string LanguageName() const override { return "binary"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewBinaryCodeGenerator() {
return std::unique_ptr<BinaryCodeGenerator>(new BinaryCodeGenerator());
}
} // namespace flatbuffers

32
src/idl_gen_binary.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_BINARY_H_
#define FLATBUFFERS_IDL_GEN_BINARY_H_
#include <memory>
#include <string>
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Binary code generator.
std::unique_ptr<CodeGenerator> NewBinaryCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_BINARY_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_cpp.h"
#include <limits>
#include <memory>
#include <string>

29
src/idl_gen_cpp.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_CPP_H_
#define FLATBUFFERS_IDL_GEN_CPP_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Cpp code generator.
std::unique_ptr<CodeGenerator> NewCppCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_CPP_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_csharp.h"
#include <unordered_set>
#include "flatbuffers/code_generators.h"
@@ -2256,4 +2258,49 @@ bool GenerateCSharp(const Parser &parser, const std::string &path,
return generator.generate();
}
namespace {
class CSharpCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateCSharp(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
output = CSharpMakeRule(parser, path, filename);
return Status::OK;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kCSharp; }
std::string LanguageName() const override { return "CSharp"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewCSharpCodeGenerator() {
return std::unique_ptr<CSharpCodeGenerator>(new CSharpCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_csharp.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_CSHARP_H_
#define FLATBUFFERS_IDL_GEN_CSHARP_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new CSharp code generator.
std::unique_ptr<CodeGenerator> NewCSharpCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_CSHARP_H_

View File

@@ -15,6 +15,8 @@
*/
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_dart.h"
#include <cassert>
#include <cmath>
@@ -1142,4 +1144,49 @@ std::string DartMakeRule(const Parser &parser, const std::string &path,
return make_rule;
}
namespace {
class DartCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateDart(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
output = DartMakeRule(parser, path, filename);
return Status::OK;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kDart; }
std::string LanguageName() const override { return "Dart"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewDartCodeGenerator() {
return std::unique_ptr<DartCodeGenerator>(new DartCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_dart.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_DART_H_
#define FLATBUFFERS_IDL_GEN_DART_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Dart code generator.
std::unique_ptr<CodeGenerator> NewDartCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_DART_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_go.h"
#include <algorithm>
#include <cmath>
#include <sstream>
@@ -1581,4 +1583,50 @@ bool GenerateGo(const Parser &parser, const std::string &path,
return generator.generate();
}
namespace {
class GoCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateGo(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateGoGRPC(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kGo; }
std::string LanguageName() const override { return "Go"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewGoCodeGenerator() {
return std::unique_ptr<GoCodeGenerator>(new GoCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_go.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_GO_H_
#define FLATBUFFERS_IDL_GEN_GO_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Go code generator.
std::unique_ptr<CodeGenerator> NewGoCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_GO_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_java.h"
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/idl.h"
@@ -2167,4 +2169,47 @@ bool GenerateJava(const Parser &parser, const std::string &path,
return generator.generate();
}
namespace {
class JavaCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateJava(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
output = JavaMakeRule(parser, path, filename);
return Status::OK;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateJavaGRPC(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kJava; }
std::string LanguageName() const override { return "Java"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewJavaCodeGenerator() {
return std::unique_ptr<JavaCodeGenerator>(new JavaCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_java.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_JAVA_H_
#define FLATBUFFERS_IDL_GEN_JAVA_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Java code generator.
std::unique_ptr<CodeGenerator> NewJavaCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_JAVA_H_

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
#include "idl_gen_json_schema.h"
#include <algorithm>
#include <iostream>
#include <limits>
@@ -330,4 +332,55 @@ bool GenerateJsonSchema(const Parser &parser, std::string *json) {
*json = generator.getJson();
return true;
}
namespace {
class JsonSchemaCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateJsonSchema(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override {
return IDLOptions::kJsonSchema;
}
std::string LanguageName() const override { return "JsonSchema"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewJsonSchemaCodeGenerator() {
return std::unique_ptr<JsonSchemaCodeGenerator>(
new JsonSchemaCodeGenerator());
}
} // namespace flatbuffers

32
src/idl_gen_json_schema.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_JSON_SCHEMA_H_
#define FLATBUFFERS_IDL_GEN_JSON_SCHEMA_H_
#include <memory>
#include <string>
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new JsonSchema Code generator.
std::unique_ptr<CodeGenerator> NewJsonSchemaCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_JSON_SCHEMA_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_kotlin.h"
#include <functional>
#include <unordered_set>
@@ -1595,4 +1597,53 @@ bool GenerateKotlin(const Parser &parser, const std::string &path,
kotlin::KotlinGenerator generator(parser, path, file_name);
return generator.generate();
}
namespace {
class KotlinCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateKotlin(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kKotlin; }
std::string LanguageName() const override { return "Kotlin"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewKotlinCodeGenerator() {
return std::unique_ptr<KotlinCodeGenerator>(new KotlinCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_kotlin.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_KOTLIN_H_
#define FLATBUFFERS_IDL_GEN_KOTLIN_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Kotlin code generator.
std::unique_ptr<CodeGenerator> NewKotlinCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_KOTLIN_H_

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
#include "idl_gen_lobster.h"
#include <string>
#include <unordered_set>
@@ -402,4 +404,54 @@ bool GenerateLobster(const Parser &parser, const std::string &path,
return generator.generate();
}
namespace {
class LobsterCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateLobster(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override {
return IDLOptions::kLobster;
}
std::string LanguageName() const override { return "Lobster"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewLobsterCodeGenerator() {
return std::unique_ptr<LobsterCodeGenerator>(new LobsterCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_lobster.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_LOBSTER_H_
#define FLATBUFFERS_IDL_GEN_LOBSTER_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Lobster code generator.
std::unique_ptr<CodeGenerator> NewLobsterCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_LOBSTER_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_lua.h"
#include <string>
#include <unordered_set>
@@ -744,4 +746,52 @@ bool GenerateLua(const Parser &parser, const std::string &path,
return generator.generate();
}
namespace {
class LuaCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateLua(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return true; }
IDLOptions::Language Language() const override { return IDLOptions::kLua; }
std::string LanguageName() const override { return "Lua"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewLuaCodeGenerator() {
return std::unique_ptr<LuaCodeGenerator>(new LuaCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_lua.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_LUA_H_
#define FLATBUFFERS_IDL_GEN_LUA_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Lua code generator.
std::unique_ptr<CodeGenerator> NewLuaCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_LUA_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_php.h"
#include <string>
#include "flatbuffers/code_generators.h"
@@ -942,4 +944,53 @@ bool GeneratePhp(const Parser &parser, const std::string &path,
php::PhpGenerator generator(parser, path, file_name);
return generator.generate();
}
namespace {
class PhpCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GeneratePhp(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kPhp; }
std::string LanguageName() const override { return "Php"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewPhpCodeGenerator() {
return std::unique_ptr<PhpCodeGenerator>(new PhpCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_php.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_PHP_H_
#define FLATBUFFERS_IDL_GEN_PHP_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Php code generator.
std::unique_ptr<CodeGenerator> NewPhpCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_PHP_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_python.h"
#include <cctype>
#include <set>
#include <string>
@@ -1912,4 +1914,50 @@ bool GeneratePython(const Parser &parser, const std::string &path,
return generator.generate();
}
namespace {
class PythonCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GeneratePython(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GeneratePythonGRPC(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kPython; }
std::string LanguageName() const override { return "Python"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewPythonCodeGenerator() {
return std::unique_ptr<PythonCodeGenerator>(new PythonCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_python.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_PYTHON_H_
#define FLATBUFFERS_IDL_GEN_PYTHON_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Python code generator.
std::unique_ptr<CodeGenerator> NewPythonCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_PYTHON_H_

View File

@@ -16,6 +16,8 @@
// independent from idl_parser, since this code is not needed for most clients
#include "idl_gen_rust.h"
#include <cmath>
#include "flatbuffers/code_generators.h"
@@ -3008,6 +3010,51 @@ std::string RustMakeRule(const Parser &parser, const std::string &path,
return make_rule;
}
namespace {
class RustCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateRust(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
output = RustMakeRule(parser, path, filename);
return Status::OK;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
(void)parser;
(void)path;
(void)filename;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kRust; }
std::string LanguageName() const override { return "Rust"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewRustCodeGenerator() {
return std::unique_ptr<RustCodeGenerator>(new RustCodeGenerator());
}
} // namespace flatbuffers
// TODO(rw): Generated code should import other generated files.

29
src/idl_gen_rust.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_RUST_H_
#define FLATBUFFERS_IDL_GEN_RUST_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Rust code generator.
std::unique_ptr<CodeGenerator> NewRustCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_RUST_H_

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
#include "idl_gen_swift.h"
#include <cctype>
#include <unordered_set>
@@ -1902,4 +1904,51 @@ bool GenerateSwift(const Parser &parser, const std::string &path,
swift::SwiftGenerator generator(parser, path, file_name);
return generator.generate();
}
namespace {
class SwiftCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateSwift(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateSwiftGRPC(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
(void)parser;
(void)path;
(void)filename;
(void)output;
return Status::NOT_IMPLEMENTED;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kSwift; }
std::string LanguageName() const override { return "Swift"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewSwiftCodeGenerator() {
return std::unique_ptr<SwiftCodeGenerator>(new SwiftCodeGenerator());
}
} // namespace flatbuffers

29
src/idl_gen_swift.h Normal file
View File

@@ -0,0 +1,29 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_SWIFT_H_
#define FLATBUFFERS_IDL_GEN_SWIFT_H_
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Cpp code generator.
std::unique_ptr<CodeGenerator> NewSwiftCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_SWIFT_H_

View File

@@ -14,6 +14,8 @@
* limitations under the License.
*/
#include "idl_gen_ts.h"
#include <algorithm>
#include <cassert>
#include <cmath>
@@ -23,6 +25,7 @@
#include "flatbuffers/code_generators.h"
#include "flatbuffers/flatbuffers.h"
#include "flatbuffers/flatc.h"
#include "flatbuffers/idl.h"
#include "flatbuffers/util.h"
#include "idl_namer.h"
@@ -2174,4 +2177,47 @@ std::string TSMakeRule(const Parser &parser, const std::string &path,
return make_rule;
}
namespace {
class TsCodeGenerator : public CodeGenerator {
public:
Status GenerateCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateTS(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
Status GenerateCode(const uint8_t *buffer, int64_t length) override {
(void)buffer;
(void)length;
return Status::NOT_IMPLEMENTED;
}
Status GenerateMakeRule(const Parser &parser, const std::string &path,
const std::string &filename,
std::string &output) override {
output = TSMakeRule(parser, path, filename);
return Status::OK;
}
Status GenerateGrpcCode(const Parser &parser, const std::string &path,
const std::string &filename) override {
if (!GenerateTSGRPC(parser, path, filename)) { return Status::ERROR; }
return Status::OK;
}
bool IsSchemaOnly() const override { return true; }
bool SupportsBfbsGeneration() const override { return false; }
IDLOptions::Language Language() const override { return IDLOptions::kTs; }
std::string LanguageName() const override { return "TS"; }
};
} // namespace
std::unique_ptr<CodeGenerator> NewTsCodeGenerator() {
return std::unique_ptr<TsCodeGenerator>(new TsCodeGenerator());
}
} // namespace flatbuffers

32
src/idl_gen_ts.h Normal file
View File

@@ -0,0 +1,32 @@
/*
* Copyright 2023 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FLATBUFFERS_IDL_GEN_TS_H_
#define FLATBUFFERS_IDL_GEN_TS_H_
#include <memory>
#include <string>
#include "flatbuffers/code_generator.h"
namespace flatbuffers {
// Constructs a new Ts code generator.
std::unique_ptr<CodeGenerator> NewTsCodeGenerator();
} // namespace flatbuffers
#endif // FLATBUFFERS_IDL_GEN_TS_H_