diff options
author | mustiikhalil <mustii@mmk.one> | 2020-11-16 19:52:38 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-16 19:52:38 +0300 |
commit | 537212afee20e7bbf47888ca92ec13bb986e0a4f (patch) | |
tree | 4231ea687458c2efde72bcd1a8f20eaf74fbbd85 /samples | |
parent | 6764f25d9195f60f2dab6ac2c9f92f6936280597 (diff) | |
download | flatbuffers-537212afee20e7bbf47888ca92ec13bb986e0a4f.tar.gz flatbuffers-537212afee20e7bbf47888ca92ec13bb986e0a4f.tar.bz2 flatbuffers-537212afee20e7bbf47888ca92ec13bb986e0a4f.zip |
[Swift] Adds a format file and reformats the swift project (#6250)
* Formats files & adds licence
* Revert arrays
* Keeps array indentation as is
* Adds licence to code formatter
* Updates code generators
Diffstat (limited to 'samples')
-rw-r--r-- | samples/sample_binary.swift | 124 |
1 files changed, 70 insertions, 54 deletions
diff --git a/samples/sample_binary.swift b/samples/sample_binary.swift index 832eb3d5..46ab99be 100644 --- a/samples/sample_binary.swift +++ b/samples/sample_binary.swift @@ -1,4 +1,19 @@ - // THIS IS JUST TO SHOW THE CODE, PLEASE DO IMPORT FLATBUFFERS WITH SPM.. +/* + * Copyright 2020 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. + */ + import FlatBuffers typealias Monster = MyGame.Sample.Monster @@ -7,61 +22,62 @@ typealias Color = MyGame.Sample.Color typealias Vec3 = MyGame.Sample.Vec3 func main() { - let expectedDMG: [Int16] = [3, 5] - let expectedNames = ["Sword", "Axe"] + let expectedDMG: [Int16] = [3, 5] + let expectedNames = ["Sword", "Axe"] + + var builder = FlatBufferBuilder(initialSize: 1024) + let weapon1Name = builder.create(string: expectedNames[0]) + let weapon2Name = builder.create(string: expectedNames[1]) + + let weapon1Start = Weapon.startWeapon(&builder) + Weapon.add(name: weapon1Name, &builder) + Weapon.add(damage: expectedDMG[0], &builder) + let sword = Weapon.endWeapon(&builder, start: weapon1Start) + let weapon2Start = Weapon.startWeapon(&builder) + Weapon.add(name: weapon2Name, &builder) + Weapon.add(damage: expectedDMG[1], &builder) + let axe = Weapon.endWeapon(&builder, start: weapon2Start) + + let name = builder.create(string: "Orc") + let inventory: [Byte] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] + let inventoryOffset = builder.createVector(inventory) + + let weaponsOffset = builder.createVector(ofOffsets: [sword, axe]) + let pos = MyGame.Sample.createVec3(x: 1, y: 2, z: 3) - var builder = FlatBufferBuilder(initialSize: 1024) - let weapon1Name = builder.create(string: expectedNames[0]) - let weapon2Name = builder.create(string: expectedNames[1]) - - let weapon1Start = Weapon.startWeapon(&builder) - Weapon.add(name: weapon1Name, &builder) - Weapon.add(damage: expectedDMG[0], &builder) - let sword = Weapon.endWeapon(&builder, start: weapon1Start) - let weapon2Start = Weapon.startWeapon(&builder) - Weapon.add(name: weapon2Name, &builder) - Weapon.add(damage: expectedDMG[1], &builder) - let axe = Weapon.endWeapon(&builder, start: weapon2Start) - - let name = builder.create(string: "Orc") - let inventory: [Byte] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] - let inventoryOffset = builder.createVector(inventory) + let orc = Monster.createMonster( + &builder, + structOfPos: pos, + hp: 300, + offsetOfName: name, + vectorOfInventory: inventoryOffset, + color: .red, + vectorOfWeapons: weaponsOffset, + equippedType: .weapon, + offsetOfEquipped: axe) + builder.finish(offset: orc) - let weaponsOffset = builder.createVector(ofOffsets: [sword, axe]) - let pos = MyGame.Sample.createVec3(x: 1, y: 2, z: 3) + let buf = builder.sizedByteArray + let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf)) - let orc = Monster.createMonster(&builder, - structOfPos: pos, - hp: 300, - offsetOfName: name, - vectorOfInventory: inventoryOffset, - color: .red, - vectorOfWeapons: weaponsOffset, - equippedType: .weapon, - offsetOfEquipped: axe) - builder.finish(offset: orc) - - let buf = builder.sizedByteArray - let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf)) + assert(monster.mana == 150) + assert(monster.hp == 300) + assert(monster.name == "Orc") + assert(monster.color == MyGame.Sample.Color.red) + assert(monster.pos != nil) + for i in 0..<monster.inventoryCount { + assert(i == monster.inventory(at: i)) + } - assert(monster.mana == 150) - assert(monster.hp == 300) - assert(monster.name == "Orc") - assert(monster.color == MyGame.Sample.Color.red) - assert(monster.pos != nil) - for i in 0..<monster.inventoryCount { - assert(i == monster.inventory(at: i)) - } - - for i in 0..<monster.weaponsCount { - let weap = monster.weapons(at: i) - let index = Int(i) - assert(weap?.damage == expectedDMG[index]) - assert(weap?.name == expectedNames[index]) - } - assert(monster.equippedType == .weapon) - let equipped = monster.equipped(type: Weapon.self) - assert(equipped?.name == "Axe") - assert(equipped?.damage == 5) - print("Monster Object is Verified") + for i in 0..<monster.weaponsCount { + let weap = monster.weapons(at: i) + let index = Int(i) + assert(weap?.damage == expectedDMG[index]) + assert(weap?.name == expectedNames[index]) + } + assert(monster.equippedType == .weapon) + let equipped = monster.equipped(type: Weapon.self) + assert(equipped?.name == "Axe") + assert(equipped?.damage == 5) + print("Monster Object is Verified") } |