mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
[Swift] RFC: Switch Swift namespace from public enum to ordinary concat with _ (#6045)
This PR attempts to switch namespace from public enum back to ordinary concat with _ in Swift. This kept style similar with protobuf, but different from other popular style guide in Swift. This is needed because previously, when we do `public enum`, we don't really know when to declare and when to extend (extension). With namespace implementation in this PR, there is no such ambiguity.
This commit is contained in:
@@ -41,17 +41,14 @@ inline char LowerCase(char c) {
|
||||
|
||||
class SwiftGenerator : public BaseGenerator {
|
||||
private:
|
||||
const Namespace *cur_name_space_;
|
||||
CodeWriter code_;
|
||||
std::unordered_set<std::string> keywords_;
|
||||
std::set<std::string> namespaces_;
|
||||
int namespace_depth;
|
||||
|
||||
public:
|
||||
SwiftGenerator(const Parser &parser, const std::string &path,
|
||||
const std::string &file_name)
|
||||
: BaseGenerator(parser, path, file_name, "", ".", "swift"),
|
||||
cur_name_space_(nullptr) {
|
||||
: BaseGenerator(parser, path, file_name, "", "_", "swift") {
|
||||
namespace_depth = 0;
|
||||
code_.SetPadding(" ");
|
||||
static const char *const keywords[] = {
|
||||
@@ -151,7 +148,6 @@ class SwiftGenerator : public BaseGenerator {
|
||||
++it) {
|
||||
const auto &enum_def = **it;
|
||||
if (!enum_def.generated) {
|
||||
SetNameSpace(enum_def.defined_namespace);
|
||||
GenEnum(enum_def);
|
||||
}
|
||||
}
|
||||
@@ -160,7 +156,6 @@ class SwiftGenerator : public BaseGenerator {
|
||||
it != parser_.structs_.vec.end(); ++it) {
|
||||
const auto &struct_def = **it;
|
||||
if (struct_def.fixed && !struct_def.generated) {
|
||||
SetNameSpace(struct_def.defined_namespace);
|
||||
GenStructReader(struct_def);
|
||||
if (parser_.opts.generate_object_based_api) {
|
||||
GenObjectAPI(struct_def);
|
||||
@@ -172,7 +167,6 @@ class SwiftGenerator : public BaseGenerator {
|
||||
it != parser_.structs_.vec.end(); ++it) {
|
||||
const auto &struct_def = **it;
|
||||
if (struct_def.fixed && !struct_def.generated) {
|
||||
SetNameSpace(struct_def.defined_namespace);
|
||||
GenStructWriter(struct_def);
|
||||
}
|
||||
}
|
||||
@@ -181,7 +175,6 @@ class SwiftGenerator : public BaseGenerator {
|
||||
it != parser_.structs_.vec.end(); ++it) {
|
||||
const auto &struct_def = **it;
|
||||
if (!struct_def.fixed && !struct_def.generated) {
|
||||
SetNameSpace(struct_def.defined_namespace);
|
||||
GenTable(struct_def);
|
||||
if (parser_.opts.generate_object_based_api) {
|
||||
GenObjectAPI(struct_def);
|
||||
@@ -189,8 +182,6 @@ class SwiftGenerator : public BaseGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
if (cur_name_space_) SetNameSpace(nullptr);
|
||||
|
||||
const auto filename = GeneratedFileName(path_, file_name_, parser_.opts);
|
||||
const auto final_code = code_.ToString();
|
||||
return SaveFile(filename.c_str(), final_code, false);
|
||||
@@ -203,9 +194,11 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
// Generates the create function for swift
|
||||
void GenStructWriter(const StructDef &struct_def) {
|
||||
code_.SetValue("STRUCTNAME", Name(struct_def));
|
||||
std::string static_type = this->namespace_depth == 0 ? "" : "static ";
|
||||
code_ += "public " + static_type + "func create{{STRUCTNAME}}(\\";
|
||||
code_.SetValue("STRUCTNAME", NameWrappedInNameSpace(struct_def));
|
||||
code_.SetValue("SHORT_STRUCTNAME", Name(struct_def));
|
||||
code_ += "extension {{STRUCTNAME}} {";
|
||||
Indent();
|
||||
code_ += "public static func create{{SHORT_STRUCTNAME}}(\\";
|
||||
std::string func_header = "";
|
||||
GenerateStructArgs(struct_def, &func_header, "", "");
|
||||
code_ += func_header.substr(0, func_header.size() - 2) + "\\";
|
||||
@@ -221,6 +214,8 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_ += "return memory";
|
||||
Outdent();
|
||||
code_ += "}\n";
|
||||
Outdent();
|
||||
code_ += "}\n";
|
||||
}
|
||||
|
||||
void GenerateStructBody(const StructDef &struct_def,
|
||||
@@ -284,7 +279,8 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
void GenObjectHeader(const StructDef &struct_def) {
|
||||
GenComment(struct_def.doc_comment);
|
||||
code_.SetValue("STRUCTNAME", Name(struct_def));
|
||||
code_.SetValue("SHORT_STRUCTNAME", Name(struct_def));
|
||||
code_.SetValue("STRUCTNAME", NameWrappedInNameSpace(struct_def));
|
||||
code_.SetValue("PROTOCOL",
|
||||
struct_def.fixed ? "Readable" : "FlatBufferObject");
|
||||
code_.SetValue("OBJECTTYPE", struct_def.fixed ? "Struct" : "Table");
|
||||
@@ -308,7 +304,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
"\"{{FILENAME}}\", addPrefix: prefix) }";
|
||||
}
|
||||
code_ +=
|
||||
"public static func getRootAs{{STRUCTNAME}}(bb: ByteBuffer) -> "
|
||||
"public static func getRootAs{{SHORT_STRUCTNAME}}(bb: ByteBuffer) -> "
|
||||
"{{STRUCTNAME}} { return {{STRUCTNAME}}(Table(bb: bb, position: "
|
||||
"Int32(bb.read(def: UOffset.self, position: bb.reader)) + "
|
||||
"Int32(bb.reader))) }\n";
|
||||
@@ -384,7 +380,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
GenerateObjectAPIExtensionHeader();
|
||||
std::string code;
|
||||
GenerateStructArgs(struct_def, &code, "", "", "obj", true);
|
||||
code_ += "return builder.create(struct: create{{STRUCTNAME}}(\\";
|
||||
code_ += "return builder.create(struct: create{{SHORT_STRUCTNAME}}(\\";
|
||||
code_ += code.substr(0, code.size() - 2) + "\\";
|
||||
code_ += "), type: {{STRUCTNAME}}.self)";
|
||||
Outdent();
|
||||
@@ -409,7 +405,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
code_.SetValue("NUMBEROFFIELDS", NumToString(struct_def.fields.vec.size()));
|
||||
code_ +=
|
||||
"public static func start{{STRUCTNAME}}(_ fbb: inout "
|
||||
"public static func start{{SHORT_STRUCTNAME}}(_ fbb: inout "
|
||||
"FlatBufferBuilder) -> "
|
||||
"UOffset { fbb.startTable(with: {{NUMBEROFFIELDS}}) }";
|
||||
|
||||
@@ -424,7 +420,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
GenTableWriterFields(field, &create_func_body, &create_func_header);
|
||||
}
|
||||
code_ +=
|
||||
"public static func end{{STRUCTNAME}}(_ fbb: inout "
|
||||
"public static func end{{SHORT_STRUCTNAME}}(_ fbb: inout "
|
||||
"FlatBufferBuilder, "
|
||||
"start: "
|
||||
"UOffset) -> Offset<UOffset> { let end = Offset<UOffset>(offset: "
|
||||
@@ -439,7 +435,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_ += "; return end }";
|
||||
|
||||
if (should_generate_create) {
|
||||
code_ += "public static func create{{STRUCTNAME}}(";
|
||||
code_ += "public static func create{{SHORT_STRUCTNAME}}(";
|
||||
Indent();
|
||||
code_ += "_ fbb: inout FlatBufferBuilder,";
|
||||
for (auto it = create_func_header.begin(); it < create_func_header.end();
|
||||
@@ -451,12 +447,12 @@ class SwiftGenerator : public BaseGenerator {
|
||||
Outdent();
|
||||
code_ += ") -> Offset<UOffset> {";
|
||||
Indent();
|
||||
code_ += "let __start = {{STRUCTNAME}}.start{{STRUCTNAME}}(&fbb)";
|
||||
code_ += "let __start = {{STRUCTNAME}}.start{{SHORT_STRUCTNAME}}(&fbb)";
|
||||
for (auto it = create_func_body.begin(); it < create_func_body.end();
|
||||
++it) {
|
||||
code_ += *it;
|
||||
}
|
||||
code_ += "return {{STRUCTNAME}}.end{{STRUCTNAME}}(&fbb, start: __start)";
|
||||
code_ += "return {{STRUCTNAME}}.end{{SHORT_STRUCTNAME}}(&fbb, start: __start)";
|
||||
Outdent();
|
||||
code_ += "}";
|
||||
}
|
||||
@@ -464,12 +460,13 @@ class SwiftGenerator : public BaseGenerator {
|
||||
std::string spacing = "";
|
||||
|
||||
if (key_field != nullptr && !struct_def.fixed && struct_def.has_key) {
|
||||
code_.SetValue("VALUENAME", struct_def.name);
|
||||
code_.SetValue("VALUENAME", NameWrappedInNameSpace(struct_def));
|
||||
code_.SetValue("SHORT_VALUENAME", Name(struct_def));
|
||||
code_.SetValue("VOFFSET", NumToString(key_field->value.offset));
|
||||
|
||||
code_ +=
|
||||
"public static func "
|
||||
"sortVectorOf{{VALUENAME}}(offsets:[Offset<UOffset>], "
|
||||
"sortVectorOf{{SHORT_VALUENAME}}(offsets:[Offset<UOffset>], "
|
||||
"_ fbb: inout FlatBufferBuilder) -> Offset<UOffset> {";
|
||||
Indent();
|
||||
code_ += spacing + "var off = offsets";
|
||||
@@ -791,7 +788,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
void GenEnum(const EnumDef &enum_def) {
|
||||
if (enum_def.generated) return;
|
||||
code_.SetValue("ENUM_NAME", Name(enum_def));
|
||||
code_.SetValue("ENUM_NAME", NameWrappedInNameSpace(enum_def));
|
||||
code_.SetValue("BASE_TYPE", GenTypeBasic(enum_def.underlying_type, false));
|
||||
GenComment(enum_def.doc_comment);
|
||||
code_ += "public enum {{ENUM_NAME}}: {{BASE_TYPE}}, Enum { ";
|
||||
@@ -852,7 +849,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
}
|
||||
code_ += "";
|
||||
BuildObjectAPIConstructor(buffer_constructor,
|
||||
"_ _t: inout " + struct_def.name);
|
||||
"_ _t: inout " + NameWrappedInNameSpace(struct_def));
|
||||
BuildObjectAPIConstructor(base_constructor);
|
||||
Outdent();
|
||||
code_ += "}";
|
||||
@@ -902,8 +899,8 @@ class SwiftGenerator : public BaseGenerator {
|
||||
GenerateStructArgs(*field.value.type.struct_def, &code, "", "",
|
||||
"$0", true);
|
||||
code = code.substr(0, code.size() - 2);
|
||||
code_ += "let __" + name + " = obj." + name + ".map { create" +
|
||||
field.value.type.struct_def->name + "(" + code + ") }";
|
||||
code_ += "let __" + name + " = obj." + name + ".map { " + NameWrappedInNameSpace(*field.value.type.struct_def) + ".create" +
|
||||
Name(*field.value.type.struct_def) + "(" + code + ") }";
|
||||
} else {
|
||||
code_ += "let __" + name + " = " + type +
|
||||
".pack(&builder, obj: &obj." + name + ")";
|
||||
@@ -930,11 +927,11 @@ class SwiftGenerator : public BaseGenerator {
|
||||
builder);
|
||||
}
|
||||
}
|
||||
code_ += "let __root = {{STRUCTNAME}}.start{{STRUCTNAME}}(&builder)";
|
||||
code_ += "let __root = {{STRUCTNAME}}.start{{SHORT_STRUCTNAME}}(&builder)";
|
||||
for (auto it = unpack_body.begin(); it < unpack_body.end(); it++)
|
||||
code_ += *it;
|
||||
code_ +=
|
||||
"return {{STRUCTNAME}}.end{{STRUCTNAME}}(&builder, start: "
|
||||
"return {{STRUCTNAME}}.end{{SHORT_STRUCTNAME}}(&builder, start: "
|
||||
"__root)";
|
||||
Outdent();
|
||||
code_ += "}";
|
||||
@@ -981,8 +978,8 @@ class SwiftGenerator : public BaseGenerator {
|
||||
code_ += "for i in obj." + name + " {";
|
||||
Indent();
|
||||
code_ += "guard let _o = i else { continue }";
|
||||
code_ += "__" + name + "__.append(create" +
|
||||
field.value.type.struct_def->name + "(" + code + "))";
|
||||
code_ += "__" + name + "__.append(" + NameWrappedInNameSpace(*field.value.type.struct_def) + ".create" +
|
||||
Name(*field.value.type.struct_def) + "(" + code + "))";
|
||||
Outdent();
|
||||
code_ += "}";
|
||||
code_ += "let __" + name + " = builder.createVector(structs: __" +
|
||||
@@ -1187,7 +1184,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
std::vector<std::string> &buffer_constructor,
|
||||
const std::string &indentation = "",
|
||||
const bool is_vector = false) {
|
||||
auto field_name = Name(ev);
|
||||
auto field_name = NameWrappedInNameSpace(ev);
|
||||
code_.SetValue("VALUETYPE", field_name);
|
||||
code_ += "var {{VALUENAME}}: \\";
|
||||
code_ += is_vector ? "[{{VALUETYPE}}Union?]" : "{{VALUETYPE}}Union?";
|
||||
@@ -1341,25 +1338,26 @@ class SwiftGenerator : public BaseGenerator {
|
||||
}
|
||||
|
||||
std::string GenType(const Type &type,
|
||||
bool should_consider_prefix = false) const {
|
||||
const bool should_consider_suffix = false) const {
|
||||
return IsScalar(type.base_type)
|
||||
? GenTypeBasic(type)
|
||||
: (IsArray(type) ? GenType(type.VectorType())
|
||||
: GenTypePointer(type, should_consider_prefix));
|
||||
: GenTypePointer(type, should_consider_suffix));
|
||||
}
|
||||
|
||||
std::string GenTypePointer(const Type &type,
|
||||
bool should_consider_prefix) const {
|
||||
const bool should_consider_suffix) const {
|
||||
switch (type.base_type) {
|
||||
case BASE_TYPE_STRING: return "String";
|
||||
case BASE_TYPE_VECTOR: return GenType(type.VectorType());
|
||||
case BASE_TYPE_STRUCT: {
|
||||
if (should_consider_prefix) {
|
||||
auto &struct_ = *type.struct_def;
|
||||
auto &struct_ = *type.struct_def;
|
||||
if (should_consider_suffix) {
|
||||
return WrapInNameSpace(struct_.defined_namespace,
|
||||
ObjectAPIName(struct_.name));
|
||||
ObjectAPIName(Name(struct_)));
|
||||
}
|
||||
return WrapInNameSpace(*type.struct_def);
|
||||
return WrapInNameSpace(struct_.defined_namespace,
|
||||
Name(struct_));
|
||||
}
|
||||
case BASE_TYPE_UNION:
|
||||
default: return "FlatBufferObject";
|
||||
@@ -1378,6 +1376,16 @@ class SwiftGenerator : public BaseGenerator {
|
||||
|
||||
void Outdent() { code_.DecrementIdentLevel(); }
|
||||
|
||||
std::string NameWrappedInNameSpace(const EnumDef &enum_def) const {
|
||||
return WrapInNameSpace(enum_def.defined_namespace,
|
||||
Name(enum_def));
|
||||
}
|
||||
|
||||
std::string NameWrappedInNameSpace(const StructDef &struct_def) const {
|
||||
return WrapInNameSpace(struct_def.defined_namespace,
|
||||
Name(struct_def));
|
||||
}
|
||||
|
||||
std::string GenTypeBasic(const Type &type, bool can_override) const {
|
||||
// clang-format off
|
||||
static const char * const swift_type[] = {
|
||||
@@ -1390,8 +1398,7 @@ class SwiftGenerator : public BaseGenerator {
|
||||
// clang-format on
|
||||
if (can_override) {
|
||||
if (type.enum_def)
|
||||
return WrapInNameSpace(type.enum_def->defined_namespace,
|
||||
Name(*type.enum_def));
|
||||
return NameWrappedInNameSpace(*type.enum_def);
|
||||
if (type.base_type == BASE_TYPE_BOOL) return "Bool";
|
||||
}
|
||||
return swift_type[static_cast<int>(type.base_type)];
|
||||
@@ -1413,58 +1420,6 @@ class SwiftGenerator : public BaseGenerator {
|
||||
return EscapeKeyword(MakeCamel(def.name, false));
|
||||
}
|
||||
|
||||
// MARK: - Copied from the cpp implementation, needs revisiting
|
||||
void SetNameSpace(const Namespace *ns) {
|
||||
if (cur_name_space_ == ns) { return; }
|
||||
// Compute the size of the longest common namespace prefix.
|
||||
// If cur_name_space is A::B::C::D and ns is A::B::E::F::G,
|
||||
// the common prefix is A::B:: and we have old_size = 4, new_size = 5
|
||||
// and common_prefix_size = 2
|
||||
size_t old_size = cur_name_space_ ? cur_name_space_->components.size() : 0;
|
||||
size_t new_size = ns ? ns->components.size() : 0;
|
||||
|
||||
size_t common_prefix_size = 0;
|
||||
while (common_prefix_size < old_size && common_prefix_size < new_size &&
|
||||
ns->components[common_prefix_size] ==
|
||||
cur_name_space_->components[common_prefix_size]) {
|
||||
common_prefix_size++;
|
||||
}
|
||||
|
||||
// Close cur_name_space in reverse order to reach the common prefix.
|
||||
// In the previous example, D then C are closed.
|
||||
for (size_t j = old_size; j > common_prefix_size; --j) {
|
||||
if (namespace_depth >= 0) {
|
||||
code_ += "}";
|
||||
namespace_depth -= 1;
|
||||
}
|
||||
mark(cur_name_space_->components[j - 1]);
|
||||
}
|
||||
if (old_size != common_prefix_size) { code_ += ""; }
|
||||
|
||||
// open namespace parts to reach the ns namespace
|
||||
// in the previous example, E, then F, then G are opened
|
||||
bool is_extension = false;
|
||||
for (auto j = common_prefix_size; j < new_size; ++j) {
|
||||
std::string name = ns->components[j];
|
||||
if (namespaces_.find(name) == namespaces_.end()) {
|
||||
code_ += "public enum " + name + " {";
|
||||
namespace_depth += 1;
|
||||
namespaces_.insert(name);
|
||||
} else {
|
||||
if (namespace_depth != 0) {
|
||||
code_ += "}";
|
||||
namespace_depth = 0;
|
||||
}
|
||||
is_extension = true;
|
||||
}
|
||||
}
|
||||
if (is_extension) {
|
||||
code_.SetValue("EXTENSION", FullNamespace(".", *ns));
|
||||
code_ += "extension {{EXTENSION}} {";
|
||||
}
|
||||
if (new_size != common_prefix_size) { code_ += ""; }
|
||||
cur_name_space_ = ns;
|
||||
}
|
||||
};
|
||||
} // namespace swift
|
||||
bool GenerateSwift(const Parser &parser, const std::string &path,
|
||||
|
||||
@@ -2,10 +2,10 @@ import XCTest
|
||||
import Foundation
|
||||
@testable import FlatBuffers
|
||||
|
||||
public typealias Test = MyGame.Example.Test
|
||||
public typealias Monster = MyGame.Example.Monster
|
||||
public typealias Vec3 = MyGame.Example.Vec3
|
||||
public typealias Stat = MyGame.Example.Stat
|
||||
public typealias Test = MyGame_Example_Test
|
||||
public typealias Monster = MyGame_Example_Monster
|
||||
public typealias Vec3 = MyGame_Example_Vec3
|
||||
public typealias Stat = MyGame_Example_Stat
|
||||
|
||||
class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
|
||||
@@ -57,7 +57,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
|
||||
func testCreateMonsterUsingCreateMonsterMethodWithPosX() {
|
||||
var fbb = FlatBufferBuilder(initialSize: 1)
|
||||
let pos = MyGame.Example.createVec3(x: 10, test2: .blue)
|
||||
let pos = MyGame_Example_Vec3.createVec3(x: 10, test2: .blue)
|
||||
let name = fbb.create(string: "Barney")
|
||||
let monster = Monster.createMonster(&fbb, structOfPos: pos, offsetOfName: name)
|
||||
fbb.finish(offset: monster)
|
||||
@@ -68,7 +68,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
|
||||
func testReadMonsterFromUnsafePointerWithoutCopying() {
|
||||
var array: [UInt8] = [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0]
|
||||
let unpacked = array.withUnsafeMutableBytes { (memory) -> MyGame.Example.MonsterT in
|
||||
let unpacked = array.withUnsafeMutableBytes { (memory) -> MyGame_Example_MonsterT in
|
||||
let bytes = ByteBuffer(assumingMemoryBound: memory.baseAddress!, capacity: memory.count)
|
||||
var monster = Monster.getRootAsMonster(bb: bytes)
|
||||
readFlatbufferMonster(monster: &monster)
|
||||
@@ -81,7 +81,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
func readMonster(fb: ByteBuffer) {
|
||||
var monster = Monster.getRootAsMonster(bb: fb)
|
||||
readFlatbufferMonster(monster: &monster)
|
||||
var unpacked: MyGame.Example.MonsterT? = monster.unpack()
|
||||
var unpacked: MyGame_Example_MonsterT? = monster.unpack()
|
||||
readObjectApi(monster: unpacked!)
|
||||
var builder = FlatBufferBuilder()
|
||||
let root = Monster.pack(&builder, obj: &unpacked)
|
||||
@@ -116,13 +116,13 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
let mon1Start = Monster.startMonster(&fbb)
|
||||
Monster.add(name: fred, &fbb)
|
||||
let mon2 = Monster.endMonster(&fbb, start: mon1Start)
|
||||
let test4 = fbb.createVector(structs: [MyGame.Example.createTest(a: 30, b: 40),
|
||||
MyGame.Example.createTest(a: 10, b: 20)],
|
||||
let test4 = fbb.createVector(structs: [MyGame_Example_Test.createTest(a: 30, b: 40),
|
||||
MyGame_Example_Test.createTest(a: 10, b: 20)],
|
||||
type: Test.self)
|
||||
|
||||
let stringTestVector = fbb.createVector(ofOffsets: [test1, test2])
|
||||
|
||||
let posStruct = MyGame.Example.createVec3(x: 1, y: 2, z: 3, test1: 3, test2: .green, test3a: 5, test3b: 6)
|
||||
let posStruct = MyGame_Example_Vec3.createVec3(x: 1, y: 2, z: 3, test1: 3, test2: .green, test3a: 5, test3b: 6)
|
||||
let mStart = Monster.startMonster(&fbb)
|
||||
Monster.add(pos: posStruct, &fbb)
|
||||
Monster.add(hp: 80, &fbb)
|
||||
@@ -180,7 +180,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
XCTAssertTrue(vec?.mutate(test1: 3) ?? false)
|
||||
}
|
||||
|
||||
func readFlatbufferMonster(monster: inout MyGame.Example.Monster) {
|
||||
func readFlatbufferMonster(monster: inout MyGame_Example_Monster) {
|
||||
XCTAssertEqual(monster.hp, 80)
|
||||
XCTAssertEqual(monster.mana, 150)
|
||||
XCTAssertEqual(monster.name, "MyMonster")
|
||||
@@ -233,7 +233,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func readObjectApi(monster: MyGame.Example.MonsterT) {
|
||||
func readObjectApi(monster: MyGame_Example_MonsterT) {
|
||||
XCTAssertEqual(monster.hp, 80)
|
||||
XCTAssertEqual(monster.mana, 150)
|
||||
XCTAssertEqual(monster.name, "MyMonster")
|
||||
@@ -246,7 +246,7 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
|
||||
let test = pos?.test3
|
||||
XCTAssertEqual(test?.a, 5)
|
||||
XCTAssertEqual(test?.b, 6)
|
||||
let monster2 = monster.test?.value as? MyGame.Example.MonsterT
|
||||
let monster2 = monster.test?.value as? MyGame_Example_MonsterT
|
||||
XCTAssertEqual(monster2?.name, "Fred")
|
||||
XCTAssertEqual(monster.mana, 150)
|
||||
monster.mana = 10
|
||||
|
||||
@@ -62,13 +62,13 @@ final class FlatBuffersTests: XCTestCase {
|
||||
|
||||
func testWriteOptionalValues() {
|
||||
var b = FlatBufferBuilder()
|
||||
let root = optional_scalars.ScalarStuff.createScalarStuff(&b,
|
||||
let root = optional_scalars_ScalarStuff.createScalarStuff(&b,
|
||||
justI8: 80,
|
||||
maybeI8: nil,
|
||||
justU8: 100,
|
||||
maybeU8: 10)
|
||||
b.finish(offset: root)
|
||||
let scalarTable = optional_scalars.ScalarStuff.getRootAsScalarStuff(bb: b.sizedBuffer)
|
||||
let scalarTable = optional_scalars_ScalarStuff.getRootAsScalarStuff(bb: b.sizedBuffer)
|
||||
XCTAssertEqual(scalarTable.justI8, 80)
|
||||
XCTAssertNil(scalarTable.maybeI8)
|
||||
XCTAssertEqual(scalarTable.defaultI8, 42)
|
||||
|
||||
@@ -82,9 +82,9 @@ final class FlatBuffersUnionTests: XCTestCase {
|
||||
|
||||
let characterType: [Character] = [.belle, .mulan, .bookfan]
|
||||
let characters = [
|
||||
fb.create(struct: createBookReader(booksRead: 7), type: BookReader.self),
|
||||
fb.create(struct: BookReader.createBookReader(booksRead: 7), type: BookReader.self),
|
||||
attack,
|
||||
fb.create(struct: createBookReader(booksRead: 2), type: BookReader.self),
|
||||
fb.create(struct: BookReader.createBookReader(booksRead: 2), type: BookReader.self),
|
||||
]
|
||||
let types = fb.createVector(characterType)
|
||||
let characterVector = fb.createVector(ofOffsets: characters)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,15 +3,13 @@
|
||||
|
||||
import FlatBuffers
|
||||
|
||||
public enum optional_scalars {
|
||||
|
||||
public struct ScalarStuff: FlatBufferObject {
|
||||
public struct optional_scalars_ScalarStuff: FlatBufferObject {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_1_12_0() }
|
||||
public var __buffer: ByteBuffer! { return _accessor.bb }
|
||||
private var _accessor: Table
|
||||
|
||||
public static func getRootAsScalarStuff(bb: ByteBuffer) -> ScalarStuff { return ScalarStuff(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) }
|
||||
public static func getRootAsScalarStuff(bb: ByteBuffer) -> optional_scalars_ScalarStuff { return optional_scalars_ScalarStuff(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) }
|
||||
|
||||
private init(_ t: Table) { _accessor = t }
|
||||
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
|
||||
@@ -160,46 +158,41 @@ public struct ScalarStuff: FlatBufferObject {
|
||||
maybeBool: Bool? = nil,
|
||||
defaultBool: Bool = true
|
||||
) -> Offset<UOffset> {
|
||||
let __start = ScalarStuff.startScalarStuff(&fbb)
|
||||
ScalarStuff.add(justI8: justI8, &fbb)
|
||||
ScalarStuff.add(maybeI8: maybeI8, &fbb)
|
||||
ScalarStuff.add(defaultI8: defaultI8, &fbb)
|
||||
ScalarStuff.add(justU8: justU8, &fbb)
|
||||
ScalarStuff.add(maybeU8: maybeU8, &fbb)
|
||||
ScalarStuff.add(defaultU8: defaultU8, &fbb)
|
||||
ScalarStuff.add(justI16: justI16, &fbb)
|
||||
ScalarStuff.add(maybeI16: maybeI16, &fbb)
|
||||
ScalarStuff.add(defaultI16: defaultI16, &fbb)
|
||||
ScalarStuff.add(justU16: justU16, &fbb)
|
||||
ScalarStuff.add(maybeU16: maybeU16, &fbb)
|
||||
ScalarStuff.add(defaultU16: defaultU16, &fbb)
|
||||
ScalarStuff.add(justI32: justI32, &fbb)
|
||||
ScalarStuff.add(maybeI32: maybeI32, &fbb)
|
||||
ScalarStuff.add(defaultI32: defaultI32, &fbb)
|
||||
ScalarStuff.add(justU32: justU32, &fbb)
|
||||
ScalarStuff.add(maybeU32: maybeU32, &fbb)
|
||||
ScalarStuff.add(defaultU32: defaultU32, &fbb)
|
||||
ScalarStuff.add(justI64: justI64, &fbb)
|
||||
ScalarStuff.add(maybeI64: maybeI64, &fbb)
|
||||
ScalarStuff.add(defaultI64: defaultI64, &fbb)
|
||||
ScalarStuff.add(justU64: justU64, &fbb)
|
||||
ScalarStuff.add(maybeU64: maybeU64, &fbb)
|
||||
ScalarStuff.add(defaultU64: defaultU64, &fbb)
|
||||
ScalarStuff.add(justF32: justF32, &fbb)
|
||||
ScalarStuff.add(maybeF32: maybeF32, &fbb)
|
||||
ScalarStuff.add(defaultF32: defaultF32, &fbb)
|
||||
ScalarStuff.add(justF64: justF64, &fbb)
|
||||
ScalarStuff.add(maybeF64: maybeF64, &fbb)
|
||||
ScalarStuff.add(defaultF64: defaultF64, &fbb)
|
||||
ScalarStuff.add(justBool: justBool, &fbb)
|
||||
ScalarStuff.add(maybeBool: maybeBool, &fbb)
|
||||
ScalarStuff.add(defaultBool: defaultBool, &fbb)
|
||||
return ScalarStuff.endScalarStuff(&fbb, start: __start)
|
||||
let __start = optional_scalars_ScalarStuff.startScalarStuff(&fbb)
|
||||
optional_scalars_ScalarStuff.add(justI8: justI8, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeI8: maybeI8, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultI8: defaultI8, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justU8: justU8, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeU8: maybeU8, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultU8: defaultU8, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justI16: justI16, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeI16: maybeI16, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultI16: defaultI16, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justU16: justU16, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeU16: maybeU16, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultU16: defaultU16, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justI32: justI32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeI32: maybeI32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultI32: defaultI32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justU32: justU32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeU32: maybeU32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultU32: defaultU32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justI64: justI64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeI64: maybeI64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultI64: defaultI64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justU64: justU64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeU64: maybeU64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultU64: defaultU64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justF32: justF32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeF32: maybeF32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultF32: defaultF32, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justF64: justF64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeF64: maybeF64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultF64: defaultF64, &fbb)
|
||||
optional_scalars_ScalarStuff.add(justBool: justBool, &fbb)
|
||||
optional_scalars_ScalarStuff.add(maybeBool: maybeBool, &fbb)
|
||||
optional_scalars_ScalarStuff.add(defaultBool: defaultBool, &fbb)
|
||||
return optional_scalars_ScalarStuff.endScalarStuff(&fbb, start: __start)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - optional_scalars
|
||||
|
||||
|
||||
|
||||
@@ -125,18 +125,24 @@ public class BookReaderT: NativeTable {
|
||||
}
|
||||
|
||||
}
|
||||
public func createRapunzel(hairLength: Int32 = 0) -> UnsafeMutableRawPointer {
|
||||
let memory = UnsafeMutableRawPointer.allocate(byteCount: Rapunzel.size, alignment: Rapunzel.alignment)
|
||||
memory.initializeMemory(as: UInt8.self, repeating: 0, count: Rapunzel.size)
|
||||
memory.storeBytes(of: hairLength, toByteOffset: 0, as: Int32.self)
|
||||
return memory
|
||||
extension Rapunzel {
|
||||
public static func createRapunzel(hairLength: Int32 = 0) -> UnsafeMutableRawPointer {
|
||||
let memory = UnsafeMutableRawPointer.allocate(byteCount: Rapunzel.size, alignment: Rapunzel.alignment)
|
||||
memory.initializeMemory(as: UInt8.self, repeating: 0, count: Rapunzel.size)
|
||||
memory.storeBytes(of: hairLength, toByteOffset: 0, as: Int32.self)
|
||||
return memory
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public func createBookReader(booksRead: Int32 = 0) -> UnsafeMutableRawPointer {
|
||||
let memory = UnsafeMutableRawPointer.allocate(byteCount: BookReader.size, alignment: BookReader.alignment)
|
||||
memory.initializeMemory(as: UInt8.self, repeating: 0, count: BookReader.size)
|
||||
memory.storeBytes(of: booksRead, toByteOffset: 0, as: Int32.self)
|
||||
return memory
|
||||
extension BookReader {
|
||||
public static func createBookReader(booksRead: Int32 = 0) -> UnsafeMutableRawPointer {
|
||||
let memory = UnsafeMutableRawPointer.allocate(byteCount: BookReader.size, alignment: BookReader.alignment)
|
||||
memory.initializeMemory(as: UInt8.self, repeating: 0, count: BookReader.size)
|
||||
memory.storeBytes(of: booksRead, toByteOffset: 0, as: Int32.self)
|
||||
return memory
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public struct Attacker: FlatBufferObject {
|
||||
|
||||
Reference in New Issue
Block a user