mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-18 13:04:27 +00:00
Adds implementation flag for swift (#7202)
* Adds implementation flag for swift Forces internal flag when using @_implementationOnly in swift Fixes access type for verifier functions & encoder functions Updates generated code * Addresses PR comments & adds a code gen dir within the swift tests * Adds test case for no-include * Fixes code gen script Removes prefix
This commit is contained in:
@@ -86,6 +86,8 @@ const static FlatCOption options[] = {
|
||||
{ "", "scoped-enums", "",
|
||||
"Use C++11 style scoped and strongly typed enums. Also implies "
|
||||
"--no-prefix." },
|
||||
{ "", "swift-implementation-only", "",
|
||||
"Adds a @_implementationOnly to swift imports" },
|
||||
{ "", "gen-inclues", "",
|
||||
"(deprecated), this is the default behavior. If the original behavior is "
|
||||
"required (no include statements) use --no-includes." },
|
||||
@@ -490,6 +492,8 @@ int FlatCompiler::Compile(int argc, const char **argv) {
|
||||
opts.java_checkerframework = true;
|
||||
} else if (arg == "--gen-generated") {
|
||||
opts.gen_generated = true;
|
||||
} else if (arg == "--swift-implementation-only") {
|
||||
opts.swift_implementation_only = true;
|
||||
} else if (arg == "--gen-json-emit") {
|
||||
opts.gen_json_coders = true;
|
||||
} else if (arg == "--object-prefix") {
|
||||
|
||||
@@ -167,8 +167,12 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_ += "// " + std::string(FlatBuffersGeneratedWarning());
|
||||
code_ += "// swiftlint:disable all";
|
||||
code_ += "// swiftformat:disable all\n";
|
||||
if (parser_.opts.include_dependence_headers || parser_.opts.generate_all)
|
||||
if (parser_.opts.include_dependence_headers || parser_.opts.generate_all) {
|
||||
if (parser_.opts.swift_implementation_only)
|
||||
code_ += "@_implementationOnly \\";
|
||||
|
||||
code_ += "import FlatBuffers\n";
|
||||
}
|
||||
|
||||
// Generate code for all the enum declarations.
|
||||
for (auto it = parser_.enums_.vec.begin(); it != parser_.enums_.vec.end();
|
||||
@@ -212,6 +216,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
// Generates the reader for swift
|
||||
void GenStructReader(const StructDef &struct_def) {
|
||||
const bool is_private_access =
|
||||
parser_.opts.swift_implementation_only ||
|
||||
struct_def.attributes.Lookup("private") != nullptr;
|
||||
code_.SetValue("ACCESS_TYPE", is_private_access ? "internal" : "public");
|
||||
GenComment(struct_def.doc_comment);
|
||||
@@ -286,7 +291,8 @@ class SwiftGenerator : public BaseGenerator {
|
||||
}
|
||||
code_ += "";
|
||||
code_ +=
|
||||
"public static func verify<T>(_ verifier: inout Verifier, at position: "
|
||||
"{{ACCESS_TYPE}} static func verify<T>(_ verifier: inout Verifier, at "
|
||||
"position: "
|
||||
"Int, of type: T.Type) throws where T: Verifiable {";
|
||||
Indent();
|
||||
code_ +=
|
||||
@@ -371,6 +377,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
// Generates the create function for swift
|
||||
void GenStructWriter(const StructDef &struct_def) {
|
||||
const bool is_private_access =
|
||||
parser_.opts.swift_implementation_only ||
|
||||
struct_def.attributes.Lookup("private") != nullptr;
|
||||
code_.SetValue("ACCESS_TYPE", is_private_access ? "internal" : "public");
|
||||
code_.SetValue("STRUCTNAME", namer_.NamespacedType(struct_def));
|
||||
@@ -440,6 +447,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
// Generates the reader for swift
|
||||
void GenTable(const StructDef &struct_def) {
|
||||
const bool is_private_access =
|
||||
parser_.opts.swift_implementation_only ||
|
||||
struct_def.attributes.Lookup("private") != nullptr;
|
||||
code_.SetValue("ACCESS_TYPE", is_private_access ? "internal" : "public");
|
||||
GenObjectHeader(struct_def);
|
||||
@@ -699,7 +707,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
field.value.type.struct_def->fixed) &&
|
||||
(IsVector(field.value.type) || IsArray(field.value.type))) {
|
||||
const auto field_name = namer_.NamespacedType(*vectortype.struct_def);
|
||||
code_ += "public static func " +
|
||||
code_ += "{{ACCESS_TYPE}} static func " +
|
||||
namer_.Method("start_vector_of", field_var) +
|
||||
"(_ size: Int, in builder: inout "
|
||||
"FlatBufferBuilder) {";
|
||||
@@ -1054,7 +1062,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_ += "";
|
||||
if (struct_def.fields.vec.empty() == false) GenerateCodingKeys(struct_def);
|
||||
|
||||
code_ += "public func encode(to encoder: Encoder) throws {";
|
||||
code_ += "{{ACCESS_TYPE}} func encode(to encoder: Encoder) throws {";
|
||||
Indent();
|
||||
if (struct_def.fields.vec.empty() == false) GenerateEncoderBody(struct_def);
|
||||
Outdent();
|
||||
@@ -1066,7 +1074,8 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
void GenerateVerifier(const StructDef &struct_def) {
|
||||
code_ +=
|
||||
"public static func verify<T>(_ verifier: inout Verifier, at position: "
|
||||
"{{ACCESS_TYPE}} static func verify<T>(_ verifier: inout Verifier, at "
|
||||
"position: "
|
||||
"Int, of type: T.Type) throws where T: Verifiable {";
|
||||
Indent();
|
||||
code_ += "var _v = try verifier.visitTable(at: position)";
|
||||
|
||||
Reference in New Issue
Block a user