summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormustiikhalil <mustii@mmk.one>2020-06-08 20:08:16 +0300
committerGitHub <noreply@github.com>2020-06-08 10:08:16 -0700
commit108e981dbe2d90e5fbcdd102338149cc9ce438e9 (patch)
tree3042c45381bfb7100093e65d0328f7038cf5e35b
parent94a78e385319b0c5971a03af9d077bd090948b12 (diff)
downloadflatbuffers-108e981dbe2d90e5fbcdd102338149cc9ce438e9.tar.gz
flatbuffers-108e981dbe2d90e5fbcdd102338149cc9ce438e9.tar.bz2
flatbuffers-108e981dbe2d90e5fbcdd102338149cc9ce438e9.zip
Required is now implemented in swift (#5952)
-rw-r--r--src/idl_gen_swift.cpp60
-rw-r--r--swift/FlatBuffers.podspec2
-rw-r--r--tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift65
-rw-r--r--tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/union_vector_generated.swift20
4 files changed, 103 insertions, 44 deletions
diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp
index 4404a415..6245b8e2 100644
--- a/src/idl_gen_swift.cpp
+++ b/src/idl_gen_swift.cpp
@@ -367,8 +367,16 @@ class SwiftGenerator : public BaseGenerator {
"inout " +
ObjectAPIName("{{STRUCTNAME}}") + "?) -> Offset<UOffset> {";
Indent();
- code_ += "guard let obj = obj else { return Offset<UOffset>() }";
+ code_ += "guard var obj = obj else { return Offset<UOffset>() }";
+ code_ += "return pack(&builder, obj: &obj)";
+ Outdent();
+ code_ += "}";
code_ += "";
+ code_ +=
+ "public static func pack(_ builder: inout FlatBufferBuilder, obj: "
+ "inout " +
+ ObjectAPIName("{{STRUCTNAME}}") + ") -> Offset<UOffset> {";
+ Indent();
}
void GenerateObjectAPIStructExtension(const StructDef &struct_def) {
@@ -581,10 +589,13 @@ class SwiftGenerator : public BaseGenerator {
return;
}
+ std::string is_required = field.required ? "!" : "?";
+ auto required_reader = field.required ? "return " : const_string;
+
if (IsStruct(field.value.type) && field.value.type.struct_def->fixed) {
code_.SetValue("VALUETYPE", GenType(field.value.type));
code_.SetValue("CONSTANT", "nil");
- code_ += GenReaderMainBody("?") + GenOffset() + const_string +
+ code_ += GenReaderMainBody(is_required) + GenOffset() + required_reader +
GenConstructor("o + {{ACCESS}}.postion");
return;
}
@@ -592,18 +603,19 @@ class SwiftGenerator : public BaseGenerator {
case BASE_TYPE_STRUCT:
code_.SetValue("VALUETYPE", GenType(field.value.type));
code_.SetValue("CONSTANT", "nil");
- code_ += GenReaderMainBody("?") + GenOffset() + const_string +
+ code_ += GenReaderMainBody(is_required) + GenOffset() +
+ required_reader +
GenConstructor(GenIndirect("o + {{ACCESS}}.postion"));
break;
case BASE_TYPE_STRING:
code_.SetValue("VALUETYPE", GenType(field.value.type));
code_.SetValue("CONSTANT", "nil");
- code_ += GenReaderMainBody("?") + GenOffset() + const_string +
- "{{ACCESS}}.string(at: o) }";
- code_ +=
- "public var {{VALUENAME}}SegmentArray: [UInt8]? { return "
- "{{ACCESS}}.getVector(at: {{TABLEOFFSET}}.{{OFFSET}}.v) }";
+ code_ += GenReaderMainBody(is_required) + GenOffset() +
+ required_reader + "{{ACCESS}}.string(at: o) }";
+ code_ += "public var {{VALUENAME}}SegmentArray: [UInt8]" + is_required +
+ " { return "
+ "{{ACCESS}}.getVector(at: {{TABLEOFFSET}}.{{OFFSET}}.v) }";
break;
case BASE_TYPE_ARRAY: FLATBUFFERS_FALLTHROUGH(); // fall thru
@@ -614,8 +626,9 @@ class SwiftGenerator : public BaseGenerator {
code_.SetValue("CONSTANT", "nil");
code_ +=
"public func {{VALUENAME}}<T: FlatBufferObject>(type: "
- "T.Type) -> T? { " +
- GenOffset() + const_string + "{{ACCESS}}.union(o) }";
+ "T.Type) -> T" +
+ is_required + " { " + GenOffset() + required_reader +
+ "{{ACCESS}}.union(o) }";
break;
default: FLATBUFFERS_ASSERT(0);
}
@@ -771,8 +784,8 @@ class SwiftGenerator : public BaseGenerator {
code_ += "case {{KEY}} = {{VALUE}}";
}
code_ += "\n";
- AddMinOrMaxEnumValue(enum_def.MaxValue()->name, "max");
- AddMinOrMaxEnumValue(enum_def.MinValue()->name, "min");
+ AddMinOrMaxEnumValue(Name(*enum_def.MaxValue()), "max");
+ AddMinOrMaxEnumValue(Name(*enum_def.MinValue()), "min");
Outdent();
code_ += "}\n";
if (parser_.opts.generate_object_based_api && enum_def.is_union) {
@@ -871,7 +884,13 @@ class SwiftGenerator : public BaseGenerator {
case BASE_TYPE_STRING: {
unpack_body.push_back("{{STRUCTNAME}}." + body + "__" + name +
builder);
- BuildingOptionalObjects(name, "String", "builder.create(string: s)");
+ if (field.required) {
+ code_ +=
+ "let __" + name + " = builder.create(string: obj." + name + ")";
+ } else {
+ BuildingOptionalObjects(name, "String",
+ "builder.create(string: s)");
+ }
break;
}
case BASE_TYPE_UTYPE: break;
@@ -986,6 +1005,7 @@ class SwiftGenerator : public BaseGenerator {
auto type = GenType(field.value.type);
code_.SetValue("VALUENAME", name);
code_.SetValue("VALUETYPE", type);
+ std::string is_required = field.required ? "" : "?";
switch (field.value.type.base_type) {
case BASE_TYPE_STRUCT: {
@@ -994,10 +1014,13 @@ class SwiftGenerator : public BaseGenerator {
buffer_constructor.push_back("var __" + name + " = _t." + name);
auto optional =
(field.value.type.struct_def && field.value.type.struct_def->fixed);
- std::string question_mark = (optional && is_fixed ? "" : "?");
+ std::string question_mark =
+ (field.required || (optional && is_fixed) ? "" : "?");
+
code_ += "var {{VALUENAME}}: {{VALUETYPE}}" + question_mark;
buffer_constructor.push_back("" + name + " = __" + name +
- question_mark + ".unpack()");
+ (field.required ? "!" : question_mark) +
+ ".unpack()");
base_constructor.push_back("" + name + " = " + type + "()");
break;
}
@@ -1008,8 +1031,9 @@ class SwiftGenerator : public BaseGenerator {
break;
}
case BASE_TYPE_STRING: {
- code_ += "var {{VALUENAME}}: String?";
+ code_ += "var {{VALUENAME}}: String" + is_required;
buffer_constructor.push_back(name + " = _t." + name);
+ if (field.required) base_constructor.push_back(name + " = \"\"");
break;
}
case BASE_TYPE_UTYPE: break;
@@ -1163,9 +1187,7 @@ class SwiftGenerator : public BaseGenerator {
void AddMinOrMaxEnumValue(const std::string &str, const std::string &type) {
auto current_value = str;
- std::transform(current_value.begin(), current_value.end(),
- current_value.begin(), LowerCase);
- code_.SetValue(type, EscapeKeyword(MakeCamel(current_value, false)));
+ code_.SetValue(type, current_value);
code_ += "public static var " + type + ": {{ENUM_NAME}} { return .{{" +
type + "}} }";
}
diff --git a/swift/FlatBuffers.podspec b/swift/FlatBuffers.podspec
index 0114d3c1..65ec025e 100644
--- a/swift/FlatBuffers.podspec
+++ b/swift/FlatBuffers.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'FlatBuffers'
- s.version = '0.4.0'
+ s.version = '0.5.1'
s.summary = 'FlatBuffers: Memory Efficient Serialization Library'
s.description = "FlatBuffers is a cross platform serialization library architected for
diff --git a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift
index 7efe7b91..7f3a8c4d 100644
--- a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift
+++ b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/monster_test_generated.swift
@@ -164,8 +164,11 @@ public struct Test: Readable {
return TestT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TestT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TestT) -> Offset<UOffset> {
return builder.create(struct: createTest(a: obj.a, b: obj.b), type: Test.self)
}
}
@@ -212,8 +215,11 @@ public struct Vec3: Readable {
return Vec3T(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout Vec3T?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout Vec3T) -> Offset<UOffset> {
return builder.create(struct: createVec3(x: obj.x, y: obj.y, z: obj.z, test1: obj.test1, test2: obj.test2, test3a: obj.test3.a, test3b: obj.test3.b), type: Vec3.self)
}
}
@@ -267,8 +273,11 @@ public struct Ability: Readable {
return AbilityT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout AbilityT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout AbilityT) -> Offset<UOffset> {
return builder.create(struct: createAbility(id: obj.id, distance: obj.distance), type: Ability.self)
}
}
@@ -343,8 +352,11 @@ public struct InParentNamespace: FlatBufferObject {
return InParentNamespaceT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout InParentNamespaceT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout InParentNamespaceT) -> Offset<UOffset> {
let __root = InParentNamespace.startInParentNamespace(&builder)
return InParentNamespace.endInParentNamespace(&builder, start: __root)
}
@@ -382,8 +394,11 @@ public struct Monster: FlatBufferObject {
return MonsterT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MonsterT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MonsterT) -> Offset<UOffset> {
let __root = Monster.startMonster(&builder)
return Monster.endMonster(&builder, start: __root)
}
@@ -442,8 +457,11 @@ public struct TestSimpleTableWithEnum: FlatBufferObject {
return TestSimpleTableWithEnumT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TestSimpleTableWithEnumT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TestSimpleTableWithEnumT) -> Offset<UOffset> {
let __root = TestSimpleTableWithEnum.startTestSimpleTableWithEnum(&builder)
TestSimpleTableWithEnum.add(color: obj.color, &builder)
return TestSimpleTableWithEnum.endTestSimpleTableWithEnum(&builder, start: __root)
@@ -510,8 +528,11 @@ public struct Stat: FlatBufferObject {
return StatT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout StatT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout StatT) -> Offset<UOffset> {
let __id: Offset<String>
if let s = obj.id {
__id = builder.create(string: s)
@@ -604,8 +625,11 @@ public struct Referrable: FlatBufferObject {
return ReferrableT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout ReferrableT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout ReferrableT) -> Offset<UOffset> {
let __root = Referrable.startReferrable(&builder)
Referrable.add(id: obj.id, &builder)
return Referrable.endReferrable(&builder, start: __root)
@@ -696,8 +720,8 @@ public struct Monster: FlatBufferObject {
@discardableResult public func mutate(mana: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.mana.v); return _accessor.mutate(mana, index: o) }
public var hp: Int16 { let o = _accessor.offset(VTOFFSET.hp.v); return o == 0 ? 100 : _accessor.readBuffer(of: Int16.self, at: o) }
@discardableResult public func mutate(hp: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.hp.v); return _accessor.mutate(hp, index: o) }
- public var name: String? { let o = _accessor.offset(VTOFFSET.name.v); return o == 0 ? nil : _accessor.string(at: o) }
- public var nameSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.name.v) }
+ public var name: String! { let o = _accessor.offset(VTOFFSET.name.v); return _accessor.string(at: o) }
+ public var nameSegmentArray: [UInt8]! { return _accessor.getVector(at: VTOFFSET.name.v) }
public var inventoryCount: Int32 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func inventory(at index: Int32) -> UInt8 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1) }
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
@@ -981,15 +1005,12 @@ public struct Monster: FlatBufferObject {
return MonsterT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MonsterT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
-
- let __name: Offset<String>
- if let s = obj.name {
- __name = builder.create(string: s)
- } else {
- __name = Offset<String>()
- }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MonsterT) -> Offset<UOffset> {
+ let __name = builder.create(string: obj.name)
let __inventory = builder.createVector(obj.inventory)
let __test = obj.test?.pack(builder: &builder) ?? Offset()
var __test4__: [UnsafeMutableRawPointer] = []
@@ -1108,7 +1129,7 @@ public class MonsterT: NativeTable {
var pos: MyGame.Example.Vec3T?
var mana: Int16
var hp: Int16
- var name: String?
+ var name: String
var inventory: [UInt8]
var color: MyGame.Example.Color
var test: Any_Union?
@@ -1300,6 +1321,7 @@ public class MonsterT: NativeTable {
pos = MyGame.Example.Vec3T()
mana = 150
hp = 100
+ name = ""
inventory = []
color = .blue
test4 = []
@@ -1446,8 +1468,11 @@ public struct TypeAliases: FlatBufferObject {
return TypeAliasesT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TypeAliasesT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TypeAliasesT) -> Offset<UOffset> {
let __v8 = builder.createVector(obj.v8)
let __vf64 = builder.createVector(obj.vf64)
let __root = TypeAliases.startTypeAliases(&builder)
diff --git a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/union_vector_generated.swift b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/union_vector_generated.swift
index a4444fd8..3710d7af 100644
--- a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/union_vector_generated.swift
+++ b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/union_vector_generated.swift
@@ -62,8 +62,11 @@ public struct Rapunzel: Readable {
return RapunzelT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout RapunzelT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout RapunzelT) -> Offset<UOffset> {
return builder.create(struct: createRapunzel(hairLength: obj.hairLength), type: Rapunzel.self)
}
}
@@ -99,8 +102,11 @@ public struct BookReader: Readable {
return BookReaderT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout BookReaderT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout BookReaderT) -> Offset<UOffset> {
return builder.create(struct: createBookReader(booksRead: obj.booksRead), type: BookReader.self)
}
}
@@ -167,8 +173,11 @@ public struct Attacker: FlatBufferObject {
return AttackerT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout AttackerT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout AttackerT) -> Offset<UOffset> {
let __root = Attacker.startAttacker(&builder)
Attacker.add(swordAttackDamage: obj.swordAttackDamage, &builder)
return Attacker.endAttacker(&builder, start: __root)
@@ -239,8 +248,11 @@ public struct Movie: FlatBufferObject {
return MovieT(&self)
}
public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MovieT?) -> Offset<UOffset> {
- guard let obj = obj else { return Offset<UOffset>() }
+ guard var obj = obj else { return Offset<UOffset>() }
+ return pack(&builder, obj: &obj)
+ }
+ public static func pack(_ builder: inout FlatBufferBuilder, obj: inout MovieT) -> Offset<UOffset> {
let __mainCharacter = obj.mainCharacter?.pack(builder: &builder) ?? Offset()
var __characters__: [Offset<UOffset>] = []
for i in obj.characters {