diff options
author | Liu Liu <i@liuliu.me> | 2020-06-28 02:36:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-28 12:36:55 +0300 |
commit | e810635eaac4cad6e026522843152e2b501c5889 (patch) | |
tree | df80a9ee90e2a485bf737287149e6be9de6913c7 /samples | |
parent | 4995e1527335fbeed80cecc9b15c4acf54309aa8 (diff) | |
download | flatbuffers-e810635eaac4cad6e026522843152e2b501c5889.tar.gz flatbuffers-e810635eaac4cad6e026522843152e2b501c5889.tar.bz2 flatbuffers-e810635eaac4cad6e026522843152e2b501c5889.zip |
[Swift] FlatBuffers createMonster method doesn't treat struct properly (#5992)
* [Swift] FlatBuffers createMonster method doesn't treat struct properly
This PR fixed a issue where a struct is not treated properly when use
create inside.
A example would be the pos inside Monster. The createMonster method
takes an Offset for pos. However, FlatBuffersBuilder.add(struct:)
doesn't really take Offset argument. That means we don't really add a
struct at all for Monster.
It will show up as the pos never set.
This doesn't show up in FlatBuffersMonsterWriterTests.swift because it
implements its own createMonster method, which happens do the dance
properly (i.e. first call create(struct) and then immediately call
add:).
This PR modified the `add(pos:)` interface such that it takes the
UnsafeMutableRawPointer directly, calling `create(struct:)` under the hood.
I can add unit tests once the direction of this PR approved.
* Fix object api pack method codegen.
* Add unit tests that uses Monster.createMonster method to serialize.
* Updated sample_binary.swift
Diffstat (limited to 'samples')
-rw-r--r-- | samples/sample_binary.swift | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/samples/sample_binary.swift b/samples/sample_binary.swift index 3a6dc747..832eb3d5 100644 --- a/samples/sample_binary.swift +++ b/samples/sample_binary.swift @@ -1,5 +1,5 @@ // THIS IS JUST TO SHOW THE CODE, PLEASE DO IMPORT FLATBUFFERS WITH SPM.. -import Flatbuffers +import FlatBuffers typealias Monster = MyGame.Sample.Monster typealias Weapon = MyGame.Sample.Weapon @@ -10,29 +10,28 @@ func main() { let expectedDMG: [Int16] = [3, 5] let expectedNames = ["Sword", "Axe"] - let builder = FlatBufferBuilder(initialSize: 1024) + 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 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 = builder.create(struct: MyGame.Sample.createVec3(x: 1, y: 2, z: 3), type: Vec3.self) - - - let orc = Monster.createMonster(builder, - offsetOfPos: pos, + let pos = MyGame.Sample.createVec3(x: 1, y: 2, z: 3) + + let orc = Monster.createMonster(&builder, + structOfPos: pos, hp: 300, offsetOfName: name, vectorOfInventory: inventoryOffset, @@ -42,8 +41,8 @@ func main() { offsetOfEquipped: axe) builder.finish(offset: orc) - var buf = builder.sizedByteArray - var monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf)) + let buf = builder.sizedByteArray + let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf)) assert(monster.mana == 150) assert(monster.hp == 300) @@ -65,4 +64,4 @@ func main() { assert(equipped?.name == "Axe") assert(equipped?.damage == 5) print("Monster Object is Verified") -}
\ No newline at end of file +} |