From 34c821f4adbc97723f27998b20e1c86fbf4f5937 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen <44149581+Kn99HN@users.noreply.github.com> Date: Wed, 25 Jan 2023 09:05:48 -0800 Subject: [PATCH] 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 --- CMakeLists.txt | 2 + include/flatbuffers/code_generator.h | 2 +- include/flatbuffers/flatc.h | 4 -- src/BUILD.bazel | 16 +++++ src/flatc_main.cpp | 89 +++++++++++++++++++++++++++- src/idl_gen_binary.cpp | 83 ++++++++++++++++++++++++++ src/idl_gen_binary.h | 32 ++++++++++ src/idl_gen_cpp.cpp | 2 + src/idl_gen_cpp.h | 29 +++++++++ src/idl_gen_csharp.cpp | 47 +++++++++++++++ src/idl_gen_csharp.h | 29 +++++++++ src/idl_gen_dart.cpp | 47 +++++++++++++++ src/idl_gen_dart.h | 29 +++++++++ src/idl_gen_go.cpp | 48 +++++++++++++++ src/idl_gen_go.h | 29 +++++++++ src/idl_gen_java.cpp | 45 ++++++++++++++ src/idl_gen_java.h | 29 +++++++++ src/idl_gen_json_schema.cpp | 53 +++++++++++++++++ src/idl_gen_json_schema.h | 32 ++++++++++ src/idl_gen_kotlin.cpp | 51 ++++++++++++++++ src/idl_gen_kotlin.h | 29 +++++++++ src/idl_gen_lobster.cpp | 52 ++++++++++++++++ src/idl_gen_lobster.h | 29 +++++++++ src/idl_gen_lua.cpp | 50 ++++++++++++++++ src/idl_gen_lua.h | 29 +++++++++ src/idl_gen_php.cpp | 51 ++++++++++++++++ src/idl_gen_php.h | 29 +++++++++ src/idl_gen_python.cpp | 48 +++++++++++++++ src/idl_gen_python.h | 29 +++++++++ src/idl_gen_rust.cpp | 47 +++++++++++++++ src/idl_gen_rust.h | 29 +++++++++ src/idl_gen_swift.cpp | 49 +++++++++++++++ src/idl_gen_swift.h | 29 +++++++++ src/idl_gen_ts.cpp | 46 ++++++++++++++ src/idl_gen_ts.h | 32 ++++++++++ 35 files changed, 1269 insertions(+), 7 deletions(-) create mode 100644 src/idl_gen_binary.cpp create mode 100644 src/idl_gen_binary.h create mode 100644 src/idl_gen_cpp.h create mode 100644 src/idl_gen_csharp.h create mode 100644 src/idl_gen_dart.h create mode 100644 src/idl_gen_go.h create mode 100644 src/idl_gen_java.h create mode 100644 src/idl_gen_json_schema.h create mode 100644 src/idl_gen_kotlin.h create mode 100644 src/idl_gen_lobster.h create mode 100644 src/idl_gen_lua.h create mode 100644 src/idl_gen_php.h create mode 100644 src/idl_gen_python.h create mode 100644 src/idl_gen_rust.h create mode 100644 src/idl_gen_swift.h create mode 100644 src/idl_gen_ts.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2310c8351..21f191720 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/flatbuffers/code_generator.h b/include/flatbuffers/code_generator.h index af8292fae..15baf46bd 100644 --- a/include/flatbuffers/code_generator.h +++ b/include/flatbuffers/code_generator.h @@ -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: diff --git a/include/flatbuffers/flatc.h b/include/flatbuffers/flatc.h index 4a85961ca..4fab34cac 100644 --- a/include/flatbuffers/flatc.h +++ b/include/flatbuffers/flatc.h @@ -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 NewCppCodeGenerator(); - extern void LogCompilerWarn(const std::string &warn); extern void LogCompilerError(const std::string &err); diff --git a/src/BUILD.bazel b/src/BUILD.bazel index d1db07fa0..9f77d7f9c 100644 --- a/src/BUILD.bazel +++ b/src/BUILD.bazel @@ -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", diff --git a/src/flatc_main.cpp b/src/flatc_main.cpp index 5092c29fd..a2794a23a 100644 --- a/src/flatc_main.cpp +++ b/src/flatc_main.cpp @@ -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 binary_generator = + flatbuffers::NewBinaryCodeGenerator(); + std::shared_ptr cpp_generator = flatbuffers::NewCppCodeGenerator(); + std::shared_ptr csharp_generator = + flatbuffers::NewCSharpCodeGenerator(); + + std::shared_ptr dart_generator = + flatbuffers::NewDartCodeGenerator(); + + std::shared_ptr go_generator = + flatbuffers::NewGoCodeGenerator(); + + std::shared_ptr java_generator = + flatbuffers::NewJavaCodeGenerator(); + + std::shared_ptr json_schema_generator = + flatbuffers::NewJsonSchemaCodeGenerator(); + + std::shared_ptr kotlin_generator = + flatbuffers::NewKotlinCodeGenerator(); + + std::shared_ptr lobster_generator = + flatbuffers::NewLobsterCodeGenerator(); + + std::shared_ptr php_generator = + flatbuffers::NewPhpCodeGenerator(); + + std::shared_ptr python_generator = + flatbuffers::NewPythonCodeGenerator(); + + std::shared_ptr rust_generator = + flatbuffers::NewRustCodeGenerator(); + + std::shared_ptr swift_generator = + flatbuffers::NewSwiftCodeGenerator(); + + std::shared_ptr 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); diff --git a/src/idl_gen_binary.cpp b/src/idl_gen_binary.cpp new file mode 100644 index 000000000..a4ecd0def --- /dev/null +++ b/src/idl_gen_binary.cpp @@ -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 +#include +#include +#include + +#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 NewBinaryCodeGenerator() { + return std::unique_ptr(new BinaryCodeGenerator()); +} + +} // namespace flatbuffers diff --git a/src/idl_gen_binary.h b/src/idl_gen_binary.h new file mode 100644 index 000000000..a7c93b9d9 --- /dev/null +++ b/src/idl_gen_binary.h @@ -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 +#include + +#include "flatbuffers/code_generator.h" + +namespace flatbuffers { + +// Constructs a new Binary code generator. +std::unique_ptr NewBinaryCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_BINARY_H_ diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index 970709d0e..a1eb6f9ee 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_cpp.h" + #include #include #include diff --git a/src/idl_gen_cpp.h b/src/idl_gen_cpp.h new file mode 100644 index 000000000..fcca063a8 --- /dev/null +++ b/src/idl_gen_cpp.h @@ -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 NewCppCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_CPP_H_ diff --git a/src/idl_gen_csharp.cpp b/src/idl_gen_csharp.cpp index 18db60530..2811727bf 100644 --- a/src/idl_gen_csharp.cpp +++ b/src/idl_gen_csharp.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_csharp.h" + #include #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 NewCSharpCodeGenerator() { + return std::unique_ptr(new CSharpCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_csharp.h b/src/idl_gen_csharp.h new file mode 100644 index 000000000..f5895a936 --- /dev/null +++ b/src/idl_gen_csharp.h @@ -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 NewCSharpCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_CSHARP_H_ diff --git a/src/idl_gen_dart.cpp b/src/idl_gen_dart.cpp index ed144a524..93f18094b 100644 --- a/src/idl_gen_dart.cpp +++ b/src/idl_gen_dart.cpp @@ -15,6 +15,8 @@ */ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_dart.h" + #include #include @@ -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 NewDartCodeGenerator() { + return std::unique_ptr(new DartCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_dart.h b/src/idl_gen_dart.h new file mode 100644 index 000000000..efaa08e39 --- /dev/null +++ b/src/idl_gen_dart.h @@ -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 NewDartCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_DART_H_ diff --git a/src/idl_gen_go.cpp b/src/idl_gen_go.cpp index cec065e25..a293f9df4 100644 --- a/src/idl_gen_go.cpp +++ b/src/idl_gen_go.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_go.h" + #include #include #include @@ -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 NewGoCodeGenerator() { + return std::unique_ptr(new GoCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_go.h b/src/idl_gen_go.h new file mode 100644 index 000000000..d81c1ff65 --- /dev/null +++ b/src/idl_gen_go.h @@ -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 NewGoCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_GO_H_ diff --git a/src/idl_gen_java.cpp b/src/idl_gen_java.cpp index c2c25ab15..66ccc5c9d 100644 --- a/src/idl_gen_java.cpp +++ b/src/idl_gen_java.cpp @@ -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 NewJavaCodeGenerator() { + return std::unique_ptr(new JavaCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_java.h b/src/idl_gen_java.h new file mode 100644 index 000000000..20798a448 --- /dev/null +++ b/src/idl_gen_java.h @@ -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 NewJavaCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_JAVA_H_ diff --git a/src/idl_gen_json_schema.cpp b/src/idl_gen_json_schema.cpp index 796d1e20c..27d6381d2 100644 --- a/src/idl_gen_json_schema.cpp +++ b/src/idl_gen_json_schema.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "idl_gen_json_schema.h" + #include #include #include @@ -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 NewJsonSchemaCodeGenerator() { + return std::unique_ptr( + new JsonSchemaCodeGenerator()); +} } // namespace flatbuffers diff --git a/src/idl_gen_json_schema.h b/src/idl_gen_json_schema.h new file mode 100644 index 000000000..37a7a0956 --- /dev/null +++ b/src/idl_gen_json_schema.h @@ -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 +#include + +#include "flatbuffers/code_generator.h" + +namespace flatbuffers { + +// Constructs a new JsonSchema Code generator. +std::unique_ptr NewJsonSchemaCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_JSON_SCHEMA_H_ diff --git a/src/idl_gen_kotlin.cpp b/src/idl_gen_kotlin.cpp index 7a23e77b5..84d817d9c 100644 --- a/src/idl_gen_kotlin.cpp +++ b/src/idl_gen_kotlin.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_kotlin.h" + #include #include @@ -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 NewKotlinCodeGenerator() { + return std::unique_ptr(new KotlinCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_kotlin.h b/src/idl_gen_kotlin.h new file mode 100644 index 000000000..22d8ff6ca --- /dev/null +++ b/src/idl_gen_kotlin.h @@ -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 NewKotlinCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_KOTLIN_H_ diff --git a/src/idl_gen_lobster.cpp b/src/idl_gen_lobster.cpp index 67830f3bb..6cedceeb6 100644 --- a/src/idl_gen_lobster.cpp +++ b/src/idl_gen_lobster.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "idl_gen_lobster.h" + #include #include @@ -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 NewLobsterCodeGenerator() { + return std::unique_ptr(new LobsterCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_lobster.h b/src/idl_gen_lobster.h new file mode 100644 index 000000000..284303edc --- /dev/null +++ b/src/idl_gen_lobster.h @@ -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 NewLobsterCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_LOBSTER_H_ diff --git a/src/idl_gen_lua.cpp b/src/idl_gen_lua.cpp index 7be00154c..3ce593d7b 100644 --- a/src/idl_gen_lua.cpp +++ b/src/idl_gen_lua.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_lua.h" + #include #include @@ -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 NewLuaCodeGenerator() { + return std::unique_ptr(new LuaCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_lua.h b/src/idl_gen_lua.h new file mode 100644 index 000000000..43974a8c3 --- /dev/null +++ b/src/idl_gen_lua.h @@ -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 NewLuaCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_LUA_H_ diff --git a/src/idl_gen_php.cpp b/src/idl_gen_php.cpp index 5896935e6..ba8c1633f 100644 --- a/src/idl_gen_php.cpp +++ b/src/idl_gen_php.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_php.h" + #include #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 NewPhpCodeGenerator() { + return std::unique_ptr(new PhpCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_php.h b/src/idl_gen_php.h new file mode 100644 index 000000000..8695ec993 --- /dev/null +++ b/src/idl_gen_php.h @@ -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 NewPhpCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_PHP_H_ diff --git a/src/idl_gen_python.cpp b/src/idl_gen_python.cpp index 76d3dfe9b..222c0faa6 100644 --- a/src/idl_gen_python.cpp +++ b/src/idl_gen_python.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_python.h" + #include #include #include @@ -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 NewPythonCodeGenerator() { + return std::unique_ptr(new PythonCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_python.h b/src/idl_gen_python.h new file mode 100644 index 000000000..cd0cf9f4f --- /dev/null +++ b/src/idl_gen_python.h @@ -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 NewPythonCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_PYTHON_H_ diff --git a/src/idl_gen_rust.cpp b/src/idl_gen_rust.cpp index 4ea9122ee..7a5e4a534 100644 --- a/src/idl_gen_rust.cpp +++ b/src/idl_gen_rust.cpp @@ -16,6 +16,8 @@ // independent from idl_parser, since this code is not needed for most clients +#include "idl_gen_rust.h" + #include #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 NewRustCodeGenerator() { + return std::unique_ptr(new RustCodeGenerator()); +} + } // namespace flatbuffers // TODO(rw): Generated code should import other generated files. diff --git a/src/idl_gen_rust.h b/src/idl_gen_rust.h new file mode 100644 index 000000000..ef17ed8eb --- /dev/null +++ b/src/idl_gen_rust.h @@ -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 NewRustCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_RUST_H_ diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index c8a52bcac..ae1de97d8 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "idl_gen_swift.h" + #include #include @@ -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 NewSwiftCodeGenerator() { + return std::unique_ptr(new SwiftCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_swift.h b/src/idl_gen_swift.h new file mode 100644 index 000000000..4fd8977d4 --- /dev/null +++ b/src/idl_gen_swift.h @@ -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 NewSwiftCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_SWIFT_H_ diff --git a/src/idl_gen_ts.cpp b/src/idl_gen_ts.cpp index 322d54f9d..84c7e9c7c 100644 --- a/src/idl_gen_ts.cpp +++ b/src/idl_gen_ts.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include "idl_gen_ts.h" + #include #include #include @@ -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 NewTsCodeGenerator() { + return std::unique_ptr(new TsCodeGenerator()); +} + } // namespace flatbuffers diff --git a/src/idl_gen_ts.h b/src/idl_gen_ts.h new file mode 100644 index 000000000..d2ece2dd9 --- /dev/null +++ b/src/idl_gen_ts.h @@ -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 +#include + +#include "flatbuffers/code_generator.h" + +namespace flatbuffers { + +// Constructs a new Ts code generator. +std::unique_ptr NewTsCodeGenerator(); + +} // namespace flatbuffers + +#endif // FLATBUFFERS_IDL_GEN_TS_H_