Commit a4b2884e authored by mustiikhalil's avatar mustiikhalil Committed by Wouter van Oortmerssen

Added create function for swift (#5707)

Fixed the create functions and updated the test cases in swift

Removes unneeded code

Updated documentation
parent 04d80f25
......@@ -1699,16 +1699,15 @@ can serialize the monster itself:
</div>
<div class="language-swift">
~~~{.swift}
let start = Monster.startMonster(builder)
Monster.add(pos: pos, builder)
Monster.add(hp: 300, builder)
Monster.add(name: name, builder)
Monster.add(inventory: inventoryOffset, builder)
Monster.add(color: .red, builder)
Monster.add(weapons: weaponsOffset, builder)
Monster.add(equippedType: .weapon, builder)
Monster.add(equipped: axe, builder)
var orc = Monster.endMonster(builder, start: start)
let orc = Monster.createMonster(builder,
offsetOfPos: pos,
hp: 300,
offsetOfName: name,
vectorOfInventory: inventoryOffset,
color: .red,
vectorOfWeapons: weaponsOffset,
equippedType: .weapon,
offsetOfEquipped: axe)
~~~
</div>
......@@ -1776,6 +1775,21 @@ a bit more flexibility.
~~~
</div>
<div class="language-swift">
~~~{.swift}
let start = Monster.startMonster(builder)
Monster.add(pos: pos, builder)
Monster.add(hp: 300, builder)
Monster.add(name: name, builder)
Monster.addVectorOf(inventory: inventoryOffset, builder)
Monster.add(color: .red, builder)
Monster.addVectorOf(weapons: weaponsOffset, builder)
Monster.add(equippedType: .weapon, builder)
Monster.add(equipped: axe, builder)
var orc = Monster.endMonster(builder, start: start)
~~~
</div>
Before finishing the serialization, let's take a quick look at FlatBuffer
`union Equipped`. There are two parts to each FlatBuffer `union`. The first, is
a hidden field `_type`, that is generated to hold the type of `table` referred
......
// THIS IS JUST TO SHOW THE CODE, PLEASE DO IMPORT FLATBUFFERS WITH SPM..
import Flatbuffers
typealias Monster = MyGame1.Sample.Monster
typealias Weapon = MyGame1.Sample.Weapon
typealias Color = MyGame1.Sample.Color
typealias Vec3 = MyGame1.Sample.Vec3
typealias Monster = MyGame.Sample.Monster
typealias Weapon = MyGame.Sample.Weapon
typealias Color = MyGame.Sample.Color
typealias Vec3 = MyGame.Sample.Vec3
func main() {
let expectedDMG: [Int16] = [3, 5]
......@@ -30,16 +30,16 @@ func main() {
let weaponsOffset = builder.createVector(ofOffsets: [sword, axe])
let pos = builder.create(struct: MyGame.Sample.createVec3(x: 1, y: 2, z: 3), type: Vec3.self)
let start = Monster.startMonster(builder)
Monster.add(pos: pos, builder)
Monster.add(hp: 300, builder)
Monster.add(name: name, builder)
Monster.add(inventory: inventoryOffset, builder)
Monster.add(color: .red, builder)
Monster.add(weapons: weaponsOffset, builder)
Monster.add(equippedType: .weapon, builder)
Monster.add(equipped: axe, builder)
var orc = Monster.endMonster(builder, start: start)
let orc = Monster.createMonster(builder,
offsetOfPos: pos,
hp: 300,
offsetOfName: name,
vectorOfInventory: inventoryOffset,
color: .red,
vectorOfWeapons: weaponsOffset,
equippedType: .weapon,
offsetOfEquipped: axe)
builder.finish(offset: orc)
var buf = builder.sizedByteArray
......@@ -48,7 +48,7 @@ func main() {
assert(monster.mana == 150)
assert(monster.hp == 300)
assert(monster.name == "Orc")
assert(monster.color == MyGame1.Sample.Color.red)
assert(monster.color == MyGame.Sample.Color.red)
assert(monster.pos != nil)
for i in 0..<monster.inventoryCount {
assert(i == monster.inventory(at: i))
......
......@@ -213,14 +213,16 @@ class SwiftGenerator : public BaseGenerator {
void GenTableWriter(const StructDef &struct_def) {
flatbuffers::FieldDef *key_field = nullptr;
std::vector<std::string> require_fields;
std::string create_func_body;
std::string create_func_header;
auto should_generate_create = struct_def.fields.vec.size() != 0;
code_.SetValue("NUMBEROFFIELDS", NumToString(struct_def.fields.vec.size()));
code_ +=
"\tpublic static func start{{STRUCTNAME}}(_ fbb: FlatBufferBuilder) -> "
"UOffset { fbb.startTable(with: {{NUMBEROFFIELDS}}) }";
std::vector<std::string> require_fields;
for (auto it = struct_def.fields.vec.begin();
it != struct_def.fields.vec.end(); ++it) {
auto &field = **it;
......@@ -230,7 +232,8 @@ class SwiftGenerator : public BaseGenerator {
require_fields.push_back(NumToString(field.value.offset));
GenTableWriterFields(
field, static_cast<int>(it - struct_def.fields.vec.begin()));
field, &create_func_body, &create_func_header,
static_cast<int>(it - struct_def.fields.vec.begin()));
}
code_ +=
"\tpublic static func end{{STRUCTNAME}}(_ fbb: FlatBufferBuilder, "
......@@ -246,6 +249,19 @@ class SwiftGenerator : public BaseGenerator {
}
code_ += "; return end }";
code_ +=
"\tpublic static func create{{STRUCTNAME}}(_ fbb: FlatBufferBuilder\\";
if (should_generate_create)
code_ += ",\n" +
create_func_header.substr(0, create_func_header.size() - 2) +
"\\";
code_ += ") -> Offset<UOffset> {";
code_ += "\t\tlet start = {{STRUCTNAME}}.start{{STRUCTNAME}}(fbb)";
if (should_generate_create)
code_ += create_func_body.substr(0, create_func_body.size() - 1);
code_ += "\t\treturn {{STRUCTNAME}}.end{{STRUCTNAME}}(fbb, start: start)";
code_ += "\t}";
std::string spacing = "\t\t";
if (key_field != nullptr && !struct_def.fixed && struct_def.has_key) {
......@@ -268,8 +284,11 @@ class SwiftGenerator : public BaseGenerator {
}
}
void GenTableWriterFields(const FieldDef &field, const int position) {
void GenTableWriterFields(const FieldDef &field, std::string *create_body,
std::string *create_header, const int position) {
std::string builder_string = ", _ fbb: FlatBufferBuilder) { fbb.add(";
auto &create_func_body = *create_body;
auto &create_func_header = *create_header;
auto name = Name(field);
auto type = GenType(field.value.type);
code_.SetValue("VALUENAME", name);
......@@ -279,30 +298,48 @@ class SwiftGenerator : public BaseGenerator {
std::string check_if_vector =
(field.value.type.base_type == BASE_TYPE_VECTOR ||
field.value.type.base_type == BASE_TYPE_ARRAY)
? "VectorOf"
: "";
code_ +=
"\tpublic static func add" + check_if_vector + "({{VALUENAME}}: \\";
? "VectorOf("
: "(";
std::string body = "add" + check_if_vector + name + ": ";
code_ += "\tpublic static func " + body + "\\";
create_func_body += "\t\t{{STRUCTNAME}}." + body + name + ", fbb)\n";
if (IsScalar(field.value.type.base_type) &&
!IsBool(field.value.type.base_type)) {
auto default_value = IsEnum(field.value.type) ? GenEnumDefaultValue(field)
: field.value.constant;
auto is_enum = IsEnum(field.value.type) ? ".rawValue" : "";
code_ += "{{VALUETYPE}}" + builder_string + "element: {{VALUENAME}}" +
is_enum + ", def: {{CONSTANT}}, at: {{OFFSET}}) }";
create_func_header +=
"\t\t" + name + ": " + type + " = " + default_value + ",\n";
return;
}
if (IsBool(field.value.type.base_type)) {
std::string default_value =
"0" == field.value.constant ? "false" : "true";
code_.SetValue("VALUETYPE", "Bool");
code_.SetValue("CONSTANT",
"0" == field.value.constant ? "false" : "true");
code_.SetValue("CONSTANT", default_value);
code_ += "{{VALUETYPE}}" + builder_string +
"condition: {{VALUENAME}}, def: {{CONSTANT}}, at: {{OFFSET}}) }";
create_func_header +=
"\t\t" + name + ": " + type + " = " + default_value + ",\n";
return;
}
auto offset_type = field.value.type.base_type == BASE_TYPE_STRING
? "Offset<String>"
: "Offset<UOffset>";
auto camel_case_name =
(field.value.type.base_type == BASE_TYPE_VECTOR ||
field.value.type.base_type == BASE_TYPE_ARRAY
? "vectorOf"
: "offsetOf") +
MakeCamel(name, true);
create_func_header += "\t\t" + camel_case_name + " " + name + ": " +
offset_type + " = Offset(),\n";
auto reader_type =
IsStruct(field.value.type) && field.value.type.struct_def->fixed
? "structOffset: {{OFFSET}}) }"
......@@ -444,7 +481,7 @@ class SwiftGenerator : public BaseGenerator {
if (IsEnum(vectortype)) {
code_.SetValue("BASEVALUE", GenTypeBasic(vectortype, false));
code_ += "return o == 0 ? " + GenEnumDefaultValue(field) +
code_ += "return o == 0 ? {{VALUETYPE}}" + GenEnumDefaultValue(field) +
" : {{VALUETYPE}}(rawValue: {{ACCESS}}.directRead(of: "
"{{BASEVALUE}}.self, offset: {{ACCESS}}.vector(at: o) + "
"index * {{SIZE}})) }";
......@@ -634,7 +671,7 @@ class SwiftGenerator : public BaseGenerator {
name = ev.name;
}
std::transform(name.begin(), name.end(), name.begin(), LowerCase);
return "{{VALUETYPE}}." + name;
return "." + name;
}
std::string GenEnumConstructor(const std::string &at) {
......
......@@ -88,11 +88,7 @@ final class FlatBuffersUnionTests: XCTestCase {
]
let types = fb.createVector(characterType)
let characterVector = fb.createVector(ofOffsets: characters)
let movieStart = Movie.startMovie(fb)
Movie.addVectorOf(charactersType: types, fb)
Movie.addVectorOf(characters: characterVector, fb)
let end = Movie.endMovie(fb, start: movieStart)
let end = Movie.createMovie(fb, vectorOfCharactersType: types, vectorOfCharacters: characterVector)
Movie.finish(fb, end: end)
let movie = Movie.getRootAsMovie(bb: fb.buffer)
......
......@@ -56,6 +56,12 @@ public struct Attacker: FlatBufferObject {
public static func startAttacker(_ fbb: FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) }
public static func add(swordAttackDamage: Int32, _ fbb: FlatBufferBuilder) { fbb.add(element: swordAttackDamage, def: 0, at: 0) }
public static func endAttacker(_ fbb: FlatBufferBuilder, start: UOffset) -> Offset<UOffset> { let end = Offset<UOffset>(offset: fbb.endTable(at: start)); return end }
public static func createAttacker(_ fbb: FlatBufferBuilder,
swordAttackDamage: Int32 = 0) -> Offset<UOffset> {
let start = Attacker.startAttacker(fbb)
Attacker.add(swordAttackDamage: swordAttackDamage, fbb)
return Attacker.endAttacker(fbb, start: start)
}
}
public struct Movie: FlatBufferObject {
......@@ -66,7 +72,7 @@ public struct Movie: FlatBufferObject {
private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
public var mainCharacterType: Character { let o = _accessor.offset(4); return o == 0 ? Character.none : Character(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? Character.none }
public var mainCharacterType: Character { let o = _accessor.offset(4); return o == 0 ? .none : Character(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none }
public func mainCharacter<T: FlatBufferObject>(type: T.Type) -> T? { let o = _accessor.offset(6); return o == 0 ? nil : _accessor.union(o) }
public var charactersTypeCount: Int32 { let o = _accessor.offset(8); return o == 0 ? 0 : _accessor.vector(count: o) }
public func charactersType(at index: Int32) -> Character? { let o = _accessor.offset(8); return o == 0 ? Character.none : Character(rawValue: _accessor.directRead(of: UInt8.self, offset: _accessor.vector(at: o) + index * 1)) }
......@@ -78,5 +84,17 @@ public struct Movie: FlatBufferObject {
public static func addVectorOf(charactersType: Offset<UOffset>, _ fbb: FlatBufferBuilder) { fbb.add(offset: charactersType, at: 2) }
public static func addVectorOf(characters: Offset<UOffset>, _ fbb: FlatBufferBuilder) { fbb.add(offset: characters, at: 3) }
public static func endMovie(_ fbb: FlatBufferBuilder, start: UOffset) -> Offset<UOffset> { let end = Offset<UOffset>(offset: fbb.endTable(at: start)); return end }
public static func createMovie(_ fbb: FlatBufferBuilder,
mainCharacterType: Character = .none,
offsetOfMainCharacter mainCharacter: Offset<UOffset> = Offset(),
vectorOfCharactersType charactersType: Offset<UOffset> = Offset(),
vectorOfCharacters characters: Offset<UOffset> = Offset()) -> Offset<UOffset> {
let start = Movie.startMovie(fbb)
Movie.add(mainCharacterType: mainCharacterType, fbb)
Movie.add(mainCharacter: mainCharacter, fbb)
Movie.addVectorOf(charactersType: charactersType, fbb)
Movie.addVectorOf(characters: characters, fbb)
return Movie.endMovie(fbb, start: start)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment