Skip to content

Commit 4d6a7aa

Browse files
authored
Removes Dead code & regenerate code (#7744)
Formats the swift project Update Sample files Update docc documentation Updates swift docs in the website Updates code for Wasm
1 parent e61b003 commit 4d6a7aa

File tree

61 files changed

+277
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+277
-243
lines changed

[email protected]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ let package = Package(
3232
.target(
3333
name: "FlatBuffers",
3434
dependencies: [],
35-
path: "swift/Sources")
35+
path: "swift/Sources"),
3636
])
3737

docs/source/SwiftUsage.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ Now you can access values like this:
7272
In some cases it's necessary to modify values in an existing FlatBuffer in place (without creating a copy). For this reason, scalar fields of a Flatbuffer table or struct can be mutated.
7373

7474
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
75-
let monster = Monster.getRootAsMonster(bb: ByteBuffer(data: data))
75+
var byteBuffer = ByteBuffer(bytes: data)
76+
// Get an accessor to the root object inside the buffer.
77+
let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
78+
// let monster: Monster = getRoot(byteBuffer: &byteBuffer)
7679
7780
if !monster.mutate(hp: 10) {
7881
fatalError("couldn't mutate")

docs/source/Tutorial.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,10 +2472,10 @@ myGame.Monster monster = new myGame.Monster(data);
24722472
<div class="language-swift">
24732473
~~~{.swift}
24742474
// create a ByteBuffer(:) from an [UInt8] or Data()
2475-
let buf = // Get your data
2476-
2475+
var buf = // Get your data
24772476
// Get an accessor to the root object inside the buffer.
2478-
let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))
2477+
let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
2478+
// let monster: Monster = getRoot(byteBuffer: &byteBuffer)
24792479
~~~
24802480
</div>
24812481

grpc/examples/swift/Greeter/Sources/Model/greeter_generated.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public struct models_HelloReply: FlatBufferObject, Verifiable {
1010
public var __buffer: ByteBuffer! { return _accessor.bb }
1111
private var _accessor: Table
1212

13-
public static func getRootAsHelloReply(bb: ByteBuffer) -> models_HelloReply { return models_HelloReply(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) }
14-
1513
private init(_ t: Table) { _accessor = t }
1614
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
1715

@@ -59,8 +57,6 @@ public struct models_HelloRequest: FlatBufferObject, Verifiable {
5957
public var __buffer: ByteBuffer! { return _accessor.bb }
6058
private var _accessor: Table
6159

62-
public static func getRootAsHelloRequest(bb: ByteBuffer) -> models_HelloRequest { return models_HelloRequest(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) }
63-
6460
private init(_ t: Table) { _accessor = t }
6561
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
6662

grpc/examples/swift/Greeter/Sources/client/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Google Inc. All rights reserved.
2+
* Copyright 2023 Google Inc. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

grpc/examples/swift/Greeter/Sources/server/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Google Inc. All rights reserved.
2+
* Copyright 2023 Google Inc. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

samples/monster_generated.swift

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,51 @@
44

55
import FlatBuffers
66

7-
public enum MyGame_Sample_Color: Int8, Enum {
7+
public enum MyGame_Sample_Color: Int8, Enum, Verifiable {
88
public typealias T = Int8
99
public static var byteSize: Int { return MemoryLayout<Int8>.size }
1010
public var value: Int8 { return self.rawValue }
1111
case red = 0
1212
case green = 1
1313
case blue = 2
1414

15-
1615
public static var max: MyGame_Sample_Color { return .blue }
1716
public static var min: MyGame_Sample_Color { return .red }
1817
}
1918

20-
public enum MyGame_Sample_Equipment: UInt8, Enum {
19+
20+
public enum MyGame_Sample_Equipment: UInt8, UnionEnum {
2121
public typealias T = UInt8
22+
23+
public init?(value: T) {
24+
self.init(rawValue: value)
25+
}
26+
2227
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
2328
public var value: UInt8 { return self.rawValue }
2429
case none_ = 0
2530
case weapon = 1
2631

27-
2832
public static var max: MyGame_Sample_Equipment { return .weapon }
2933
public static var min: MyGame_Sample_Equipment { return .none_ }
3034
}
3135

32-
public struct MyGame_Sample_Vec3: NativeStruct {
36+
37+
public struct MyGame_Sample_Vec3: NativeStruct, Verifiable, FlatbuffersInitializable {
3338

3439
static func validateVersion() { FlatBuffersVersion_23_1_4() }
3540

3641
private var _x: Float32
3742
private var _y: Float32
3843
private var _z: Float32
3944

45+
public init(_ bb: ByteBuffer, o: Int32) {
46+
let _accessor = Struct(bb: bb, position: o)
47+
_x = _accessor.readBuffer(of: Float32.self, at: 0)
48+
_y = _accessor.readBuffer(of: Float32.self, at: 4)
49+
_z = _accessor.readBuffer(of: Float32.self, at: 8)
50+
}
51+
4052
public init(x: Float32, y: Float32, z: Float32) {
4153
_x = x
4254
_y = y
@@ -52,6 +64,10 @@ public struct MyGame_Sample_Vec3: NativeStruct {
5264
public var x: Float32 { _x }
5365
public var y: Float32 { _y }
5466
public var z: Float32 { _z }
67+
68+
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
69+
try verifier.inBuffer(position: position, of: MyGame_Sample_Vec3.self)
70+
}
5571
}
5672

5773
public struct MyGame_Sample_Vec3_Mutable: FlatBufferObject {
@@ -70,14 +86,12 @@ public struct MyGame_Sample_Vec3_Mutable: FlatBufferObject {
7086
@discardableResult public func mutate(z: Float32) -> Bool { return _accessor.mutate(z, index: 8) }
7187
}
7288

73-
public struct MyGame_Sample_Monster: FlatBufferObject {
89+
public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable {
7490

7591
static func validateVersion() { FlatBuffersVersion_23_1_4() }
7692
public var __buffer: ByteBuffer! { return _accessor.bb }
7793
private var _accessor: Table
7894

79-
public static func getRootAsMonster(bb: ByteBuffer) -> MyGame_Sample_Monster { return MyGame_Sample_Monster(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) }
80-
8195
private init(_ t: Table) { _accessor = t }
8296
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
8397

@@ -104,16 +118,19 @@ public struct MyGame_Sample_Monster: FlatBufferObject {
104118
@discardableResult public func mutate(hp: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.hp.v); return _accessor.mutate(hp, index: o) }
105119
public var name: String? { let o = _accessor.offset(VTOFFSET.name.v); return o == 0 ? nil : _accessor.string(at: o) }
106120
public var nameSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.name.v) }
121+
public var hasInventory: Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? false : true }
107122
public var inventoryCount: Int32 { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? 0 : _accessor.vector(count: o) }
108123
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) }
109124
public var inventory: [UInt8] { return _accessor.getVector(at: VTOFFSET.inventory.v) ?? [] }
110125
public func mutate(inventory: UInt8, at index: Int32) -> Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return _accessor.directMutate(inventory, index: _accessor.vector(at: o) + index * 1) }
111126
public var color: MyGame_Sample_Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : MyGame_Sample_Color(rawValue: _accessor.readBuffer(of: Int8.self, at: o)) ?? .blue }
112127
@discardableResult public func mutate(color: MyGame_Sample_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) }
128+
public var hasWeapons: Bool { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? false : true }
113129
public var weaponsCount: Int32 { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? 0 : _accessor.vector(count: o) }
114130
public func weapons(at index: Int32) -> MyGame_Sample_Weapon? { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? nil : MyGame_Sample_Weapon(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
115131
public var equippedType: MyGame_Sample_Equipment { let o = _accessor.offset(VTOFFSET.equippedType.v); return o == 0 ? .none_ : MyGame_Sample_Equipment(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .none_ }
116132
public func equipped<T: FlatbuffersInitializable>(type: T.Type) -> T? { let o = _accessor.offset(VTOFFSET.equipped.v); return o == 0 ? nil : _accessor.union(o) }
133+
public var hasPath: Bool { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? false : true }
117134
public var pathCount: Int32 { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? 0 : _accessor.vector(count: o) }
118135
public func path(at index: Int32) -> MyGame_Sample_Vec3? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : _accessor.directRead(of: MyGame_Sample_Vec3.self, offset: _accessor.vector(at: o) + index * 12) }
119136
public func mutablePath(at index: Int32) -> MyGame_Sample_Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? nil : MyGame_Sample_Vec3_Mutable(_accessor.bb, o: _accessor.vector(at: o) + index * 12) }
@@ -158,16 +175,35 @@ public struct MyGame_Sample_Monster: FlatBufferObject {
158175
MyGame_Sample_Monster.addVectorOf(path: path, &fbb)
159176
return MyGame_Sample_Monster.endMonster(&fbb, start: __start)
160177
}
178+
179+
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
180+
var _v = try verifier.visitTable(at: position)
181+
try _v.visit(field: VTOFFSET.pos.p, fieldName: "pos", required: false, type: MyGame_Sample_Vec3.self)
182+
try _v.visit(field: VTOFFSET.mana.p, fieldName: "mana", required: false, type: Int16.self)
183+
try _v.visit(field: VTOFFSET.hp.p, fieldName: "hp", required: false, type: Int16.self)
184+
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: false, type: ForwardOffset<String>.self)
185+
try _v.visit(field: VTOFFSET.inventory.p, fieldName: "inventory", required: false, type: ForwardOffset<Vector<UInt8, UInt8>>.self)
186+
try _v.visit(field: VTOFFSET.color.p, fieldName: "color", required: false, type: MyGame_Sample_Color.self)
187+
try _v.visit(field: VTOFFSET.weapons.p, fieldName: "weapons", required: false, type: ForwardOffset<Vector<ForwardOffset<MyGame_Sample_Weapon>, MyGame_Sample_Weapon>>.self)
188+
try _v.visit(unionKey: VTOFFSET.equippedType.p, unionField: VTOFFSET.equipped.p, unionKeyName: "equippedType", fieldName: "equipped", required: false, completion: { (verifier, key: MyGame_Sample_Equipment, pos) in
189+
switch key {
190+
case .none_:
191+
break // NOTE - SWIFT doesnt support none
192+
case .weapon:
193+
try ForwardOffset<MyGame_Sample_Weapon>.verify(&verifier, at: pos, of: MyGame_Sample_Weapon.self)
194+
}
195+
})
196+
try _v.visit(field: VTOFFSET.path.p, fieldName: "path", required: false, type: ForwardOffset<Vector<MyGame_Sample_Vec3, MyGame_Sample_Vec3>>.self)
197+
_v.finish()
198+
}
161199
}
162200

163-
public struct MyGame_Sample_Weapon: FlatBufferObject {
201+
public struct MyGame_Sample_Weapon: FlatBufferObject, Verifiable {
164202

165203
static func validateVersion() { FlatBuffersVersion_23_1_4() }
166204
public var __buffer: ByteBuffer! { return _accessor.bb }
167205
private var _accessor: Table
168206

169-
public static func getRootAsWeapon(bb: ByteBuffer) -> MyGame_Sample_Weapon { return MyGame_Sample_Weapon(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) }
170-
171207
private init(_ t: Table) { _accessor = t }
172208
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
173209

@@ -196,5 +232,12 @@ public struct MyGame_Sample_Weapon: FlatBufferObject {
196232
MyGame_Sample_Weapon.add(damage: damage, &fbb)
197233
return MyGame_Sample_Weapon.endWeapon(&fbb, start: __start)
198234
}
235+
236+
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
237+
var _v = try verifier.visitTable(at: position)
238+
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: false, type: ForwardOffset<String>.self)
239+
try _v.visit(field: VTOFFSET.damage.p, fieldName: "damage", required: false, type: Int16.self)
240+
_v.finish()
241+
}
199242
}
200243

samples/sample_binary.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 Google Inc. All rights reserved.
2+
* Copyright 2023 Google Inc. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,8 +56,8 @@ func main() {
5656
equippedOffset: axe)
5757
builder.finish(offset: orc)
5858

59-
let buf = builder.sizedByteArray
60-
let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))
59+
var buf = ByteBuffer(bytes: builder.sizedByteArray)
60+
let monster: Monster = try! getCheckedRoot(byteBuffer: &buffer)
6161

6262
assert(monster.mana == 150)
6363
assert(monster.hp == 300)

scripts/generate_code.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,15 @@ def glob(path, pattern):
470470
cwd=swift_code_gen
471471
)
472472

473+
# Swift Wasm Tests
474+
swift_Wasm_prefix = "swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests"
475+
flatc(
476+
SWIFT_OPTS + BASE_OPTS,
477+
schema="monster_test.fbs",
478+
include="include_test",
479+
prefix=swift_Wasm_prefix,
480+
)
481+
473482
# Nim Tests
474483
NIM_OPTS = BASE_OPTS + ["--nim"]
475484
flatc(NIM_OPTS, schema="monster_test.fbs", include="include_test")

src/idl_gen_swift.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,6 @@ class SwiftGenerator : public BaseGenerator {
483483
"fileId: "
484484
"{{STRUCTNAME}}.id, addPrefix: prefix) }";
485485
}
486-
code_ +=
487-
"{{ACCESS_TYPE}} static func getRootAs{{SHORT_STRUCTNAME}}(bb: "
488-
"ByteBuffer) -> "
489-
"{{STRUCTNAME}} { return {{STRUCTNAME}}(Table(bb: bb, position: "
490-
"Int32(bb.read(def: UOffset.self, position: bb.reader)) + "
491-
"Int32(bb.reader))) }\n";
492486
code_ += "private init(_ t: Table) { {{ACCESS}} = t }";
493487
}
494488
code_ +=

0 commit comments

Comments
 (0)