Implements verifier and code gen for swift (#6373)

Updates test cases on linux

Adhere to new protocol naming

Adds fuzzing

Adds documentation

Adds support for string unions

Updated fuzzer generated code
This commit is contained in:
mustiikhalil
2021-05-14 20:59:28 +03:00
committed by GitHub
parent 04b10f5a3a
commit a5175c513a
38 changed files with 2291 additions and 168 deletions

View File

@@ -40,7 +40,7 @@ func run(name: String, runs: Int, action: () -> Void) -> Benchmark {
func createDocument(Benchmarks: [Benchmark]) -> String {
let separator = "-------------------------------------"
var document = "\(separator)\n"
document += "\(String(format: "|\t%@\t\t|\t\t%@\t\t|", "Name", "Scores"))\n"
document += "\(String(format: "|\t%@\t\t |\t\t%@\t\t|", "Name", "Scores"))\n"
document += "\(separator)\n"
for i in Benchmarks {
document += "\(i.description) \n"
@@ -89,7 +89,9 @@ func benchmarkThreeMillionStructs() {
var offsets: [Offset] = []
for _ in 0..<structCount {
fb.startVector(5 * MemoryLayout<AA>.size, elementSize: MemoryLayout<AA>.alignment)
fb.startVector(
5 * MemoryLayout<AA>.size,
elementSize: MemoryLayout<AA>.alignment)
for _ in 0..<5 {
_ = fb.create(struct: AA(a: 2.4, b: 2.4))
}
@@ -119,12 +121,18 @@ struct AA: NativeStruct {
func benchmark(numberOfRuns runs: Int) {
var benchmarks: [Benchmark] = []
let str = (0...99).map { _ -> String in "x" }.joined()
benchmarks.append(run(name: "500_000", runs: runs, action: benchmarkFiveHundredAdds))
benchmarks.append(run(
name: "500_000",
runs: runs,
action: benchmarkFiveHundredAdds))
benchmarks.append(run(name: "10 str", runs: runs, action: create10Strings))
let hundredStr = run(name: "100 str", runs: runs) {
create100Strings(str: str)
}
benchmarks.append(run(name: "3M strc", runs: 1, action: benchmarkThreeMillionStructs))
benchmarks.append(run(
name: "3M strc",
runs: 1,
action: benchmarkThreeMillionStructs))
benchmarks.append(hundredStr)
print(createDocument(Benchmarks: benchmarks))
}

View File

@@ -28,7 +28,9 @@ let package = Package(
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.0.0"),
],
targets: [
.target(name: "SwiftFlatBuffers"),
.target(
name: "SwiftFlatBuffers",
dependencies: ["FlatBuffers"]),
.testTarget(
name: "FlatBuffers.Test.SwiftTests",
dependencies: ["FlatBuffers", "GRPC"]),

View File

@@ -0,0 +1,32 @@
enum Color:ubyte (bit_flags) {
Red = 0, // color Red = (1u << 0)
/// \brief color Green
/// Green is bit_flag with value (1u << 1)
Green,
/// \brief color Blue (1u << 3)
Blue = 3,
}
struct Test { a:short; b:byte; }
struct Vec3 (force_align: 8) {
x:float;
y:float;
z:float;
test1:double;
test2:Color;
test3:Test;
}
/// an example documentation comment: "monster object"
table Monster {
pos:Vec3 (id: 0);
hp:short = 100 (id: 2);
mana:short = 150 (id: 1);
name:string (id: 3, key);
color:Color = Blue (id: 6);
inventory:[ubyte] (id: 5);
testarrayoftables:[Monster] (id: 4);
}
root_type Monster;

View File

@@ -0,0 +1,224 @@
// automatically generated by the FlatBuffers compiler, do not modify
// swiftlint:disable all
// swiftformat:disable all
import FlatBuffers
public enum Color: UInt8, Enum, Verifiable {
public typealias T = UInt8
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
public var value: UInt8 { return self.rawValue }
case red = 1
/// \brief color Green
/// Green is bit_flag with value (1u << 1)
case green = 2
/// \brief color Blue (1u << 3)
case blue = 8
public static var max: Color { return .blue }
public static var min: Color { return .red }
}
public struct Test: NativeStruct, Verifiable {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
private var _a: Int16
private var _b: Int8
private let padding0__: UInt8 = 0
public init(a: Int16, b: Int8) {
_a = a
_b = b
}
public init() {
_a = 0
_b = 0
}
public var a: Int16 { _a }
public var b: Int8 { _b }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: Test.self)
}
}
public struct Test_Mutable: FlatBufferObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Struct
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) }
public var a: Int16 { return _accessor.readBuffer(of: Int16.self, at: 0) }
public var b: Int8 { return _accessor.readBuffer(of: Int8.self, at: 2) }
}
public struct Vec3: NativeStruct, Verifiable {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
private var _x: Float32
private var _y: Float32
private var _z: Float32
private let padding0__: UInt32 = 0
private var _test1: Double
private var _test2: UInt8
private let padding1__: UInt8 = 0
private var _test3: Test
private let padding2__: UInt16 = 0
public init(x: Float32, y: Float32, z: Float32, test1: Double, test2: Color, test3: Test) {
_x = x
_y = y
_z = z
_test1 = test1
_test2 = test2.value
_test3 = test3
}
public init() {
_x = 0.0
_y = 0.0
_z = 0.0
_test1 = 0.0
_test2 = 0
_test3 = Test()
}
public var x: Float32 { _x }
public var y: Float32 { _y }
public var z: Float32 { _z }
public var test1: Double { _test1 }
public var test2: Color { Color(rawValue: _test2)! }
public var test3: Test { _test3 }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: Vec3.self)
}
}
public struct Vec3_Mutable: FlatBufferObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Struct
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) }
public var x: Float32 { return _accessor.readBuffer(of: Float32.self, at: 0) }
public var y: Float32 { return _accessor.readBuffer(of: Float32.self, at: 4) }
public var z: Float32 { return _accessor.readBuffer(of: Float32.self, at: 8) }
public var test1: Double { return _accessor.readBuffer(of: Double.self, at: 16) }
public var test2: Color { return Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: 24)) ?? .red }
public var test3: Test_Mutable { return Test_Mutable(_accessor.bb, o: _accessor.postion + 26) }
}
/// an example documentation comment: "monster object"
public struct Monster: FlatBufferObject, Verifiable {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Table
public static func getRootAsMonster(bb: ByteBuffer) -> Monster { return Monster(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) }
private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }
private enum VTOFFSET: VOffset {
case pos = 4
case mana = 6
case hp = 8
case name = 10
case testarrayoftables = 12
case inventory = 14
case color = 16
var v: Int32 { Int32(self.rawValue) }
var p: VOffset { self.rawValue }
}
public var pos: Vec3? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : _accessor.readBuffer(of: Vec3.self, at: o) }
public var mutablePos: Vec3_Mutable? { let o = _accessor.offset(VTOFFSET.pos.v); return o == 0 ? nil : Vec3_Mutable(_accessor.bb, o: o + _accessor.postion) }
public var mana: Int16 { let o = _accessor.offset(VTOFFSET.mana.v); return o == 0 ? 150 : _accessor.readBuffer(of: Int16.self, at: o) }
public var hp: Int16 { let o = _accessor.offset(VTOFFSET.hp.v); return o == 0 ? 100 : _accessor.readBuffer(of: Int16.self, at: o) }
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 testarrayoftablesCount: Int32 { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? 0 : _accessor.vector(count: o) }
public func testarrayoftables(at index: Int32) -> Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : Monster(_accessor.bb, o: _accessor.indirect(_accessor.vector(at: o) + index * 4)) }
public func testarrayoftablesBy(key: String) -> Monster? { let o = _accessor.offset(VTOFFSET.testarrayoftables.v); return o == 0 ? nil : Monster.lookupByKey(vector: _accessor.vector(at: o), key: key, fbb: _accessor.bb) }
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) ?? [] }
public var color: Color { let o = _accessor.offset(VTOFFSET.color.v); return o == 0 ? .blue : Color(rawValue: _accessor.readBuffer(of: UInt8.self, at: o)) ?? .blue }
public static func startMonster(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 7) }
public static func add(pos: Vec3?, _ fbb: inout FlatBufferBuilder) { guard let pos = pos else { return }; fbb.create(struct: pos, position: VTOFFSET.pos.p) }
public static func add(mana: Int16, _ fbb: inout FlatBufferBuilder) { fbb.add(element: mana, def: 150, at: VTOFFSET.mana.p) }
public static func add(hp: Int16, _ fbb: inout FlatBufferBuilder) { fbb.add(element: hp, def: 100, at: VTOFFSET.hp.p) }
public static func add(name: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: name, at: VTOFFSET.name.p) }
public static func addVectorOf(testarrayoftables: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: testarrayoftables, at: VTOFFSET.testarrayoftables.p) }
public static func addVectorOf(inventory: Offset, _ fbb: inout FlatBufferBuilder) { fbb.add(offset: inventory, at: VTOFFSET.inventory.p) }
public static func add(color: Color, _ fbb: inout FlatBufferBuilder) { fbb.add(element: color.rawValue, def: 8, at: VTOFFSET.color.p) }
public static func endMonster(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); fbb.require(table: end, fields: [10]); return end }
public static func createMonster(
_ fbb: inout FlatBufferBuilder,
pos: Vec3? = nil,
mana: Int16 = 150,
hp: Int16 = 100,
nameOffset name: Offset = Offset(),
testarrayoftablesVectorOffset testarrayoftables: Offset = Offset(),
inventoryVectorOffset inventory: Offset = Offset(),
color: Color = .blue
) -> Offset {
let __start = Monster.startMonster(&fbb)
Monster.add(pos: pos, &fbb)
Monster.add(mana: mana, &fbb)
Monster.add(hp: hp, &fbb)
Monster.add(name: name, &fbb)
Monster.addVectorOf(testarrayoftables: testarrayoftables, &fbb)
Monster.addVectorOf(inventory: inventory, &fbb)
Monster.add(color: color, &fbb)
return Monster.endMonster(&fbb, start: __start)
}
public static func sortVectorOfMonster(offsets:[Offset], _ fbb: inout FlatBufferBuilder) -> Offset {
var off = offsets
off.sort { Table.compare(Table.offset(Int32($1.o), vOffset: 10, fbb: fbb.buffer), Table.offset(Int32($0.o), vOffset: 10, fbb: fbb.buffer), fbb: fbb.buffer) < 0 }
return fbb.createVector(ofOffsets: off)
}
fileprivate static func lookupByKey(vector: Int32, key: String, fbb: ByteBuffer) -> Monster? {
let key = key.utf8.map { $0 }
var span = fbb.read(def: Int32.self, position: Int(vector - 4))
var start: Int32 = 0
while span != 0 {
var middle = span / 2
let tableOffset = Table.indirect(vector + 4 * (start + middle), fbb)
let comp = Table.compare(Table.offset(Int32(fbb.capacity) - tableOffset, vOffset: 10, fbb: fbb), key, fbb: fbb)
if comp > 0 {
span = middle
} else if comp < 0 {
middle += 1
start += middle
span -= middle
} else {
return Monster(fbb, o: tableOffset)
}
}
return nil
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.pos.p, fieldName: "pos", required: false, type: Vec3.self)
try _v.visit(field: VTOFFSET.mana.p, fieldName: "mana", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.hp.p, fieldName: "hp", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: true, type: ForwardOffset<String>.self)
try _v.visit(field: VTOFFSET.testarrayoftables.p, fieldName: "testarrayoftables", required: false, type: ForwardOffset<Vector<ForwardOffset<Monster>, Monster>>.self)
try _v.visit(field: VTOFFSET.inventory.p, fieldName: "inventory", required: false, type: ForwardOffset<Vector<UInt8, UInt8>>.self)
try _v.visit(field: VTOFFSET.color.p, fieldName: "color", required: false, type: Color.self)
_v.finish()
}
}

View File

@@ -14,5 +14,16 @@
* limitations under the License.
*/
import Foundation
print("Flatbuffers")
import FlatBuffers
@_cdecl("LLVMFuzzerTestOneInput")
public func FuzzFlatbuffers(_ start: UnsafeRawPointer, _ count: Int) -> CInt {
let bytes = UnsafeRawBufferPointer(start: start, count: count)
do {
var buffer = ByteBuffer(contiguousBytes: bytes, count: count)
let _: Monster = try getCheckedRoot(byteBuffer: &buffer)
} catch {
}
return 0
}

View File

@@ -9,5 +9,17 @@ fbc --swift --gen-mutable --grpc --gen-object-api -I ${test_dir}/include_test ${
fbc --swift ${test_dir}/optional_scalars.fbs
fbc --swift --gen-object-api ${test_dir}/more_defaults.fbs
cd ${swift_dir}
cd ${swift_dir}/Sources/SwiftFlatBuffers
# create better fuzzing test file
fbc --swift fuzzer.fbs
cd ${swift_dir}
swift build --build-tests
swift test
if [ $(uname -s) != Darwin ]; then
echo fuzzing
swift build -c debug -Xswiftc -sanitize=fuzzer,address -Xswiftc -parse-as-library
swift build -c release -Xswiftc -sanitize=fuzzer,address -Xswiftc -parse-as-library
fi

View File

@@ -26,9 +26,11 @@ typealias Stat = MyGame_Example_Stat
class FlatBuffersMonsterWriterTests: XCTestCase {
func testData() {
// swiftformat:disable all
let data = Data([48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
// swiftformat:enable all
let _data = ByteBuffer(data: data)
readMonster(fb: _data)
readVerifiedMonster(fb: _data)
}
func testReadFromOtherLanguages() {
@@ -36,29 +38,36 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
let url = URL(fileURLWithPath: path, isDirectory: true).appendingPathComponent("monsterdata_test").appendingPathExtension("mon")
guard let data = try? Data(contentsOf: url) else { return }
let _data = ByteBuffer(data: data)
readMonster(fb: _data)
readVerifiedMonster(fb: _data)
}
func testCreateMonster() {
let bytes = createMonster(withPrefix: false)
// swiftformat:disable all
XCTAssertEqual(bytes.sizedByteArray, [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
readMonster(fb: bytes.buffer)
// swiftformat:enable all
let monster = MyGame_Example_Monster.getRootAsMonster(bb: bytes.buffer)
readMonster(monster: monster)
mutateMonster(fb: bytes.buffer)
readMonster(fb: bytes.buffer)
readMonster(monster: monster)
}
func testCreateMonsterResizedBuffer() {
let bytes = createMonster(withPrefix: false)
// swiftformat:disable all
XCTAssertEqual(bytes.sizedByteArray, [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
readMonster(fb: bytes.sizedBuffer)
// swiftformat:enable all
readVerifiedMonster(fb: bytes.sizedBuffer)
}
func testCreateMonsterPrefixed() {
let bytes = createMonster(withPrefix: true)
// swiftformat:disable all
XCTAssertEqual(bytes.sizedByteArray, [44, 1, 0, 0, 44, 0, 0, 0, 77, 79, 78, 83, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
// swiftformat:enable all
let newBuf = FlatBuffersUtils.removeSizePrefix(bb: bytes.buffer)
readMonster(fb: newBuf)
var buffer = bytes.buffer
readMonster(monster: getPrefixedSizeRoot(byteBuffer: &buffer))
}
func testCreateMonsterUsingCreateMonsterMethodWithNilPos() {
@@ -77,7 +86,15 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
var fbb = FlatBufferBuilder(initialSize: 1)
let name = fbb.create(string: "Barney")
let mStart = Monster.startMonster(&fbb)
Monster.add(pos: MyGame_Example_Vec3(x: 10, y: 0, z: 0, test1: 0, test2: .blue, test3: .init()), &fbb)
Monster.add(
pos: MyGame_Example_Vec3(
x: 10,
y: 0,
z: 0,
test1: 0,
test2: .blue,
test3: .init()),
&fbb)
Monster.add(name: name, &fbb)
let root = Monster.endMonster(&fbb, start: mStart)
fbb.finish(offset: root)
@@ -88,9 +105,13 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
}
func testReadMonsterFromUnsafePointerWithoutCopying() {
// swiftformat:disable all
var array: [UInt8] = [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0]
// swiftformat:enable all
let unpacked = array.withUnsafeMutableBytes { (memory) -> MyGame_Example_MonsterT in
let bytes = ByteBuffer(assumingMemoryBound: memory.baseAddress!, capacity: memory.count)
let bytes = ByteBuffer(
assumingMemoryBound: memory.baseAddress!,
capacity: memory.count)
var monster = Monster.getRootAsMonster(bb: bytes)
readFlatbufferMonster(monster: &monster)
let unpacked = monster.unpack()
@@ -104,7 +125,10 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
var fbb = FlatBufferBuilder(initialSize: 1)
let name = fbb.create(string: "Frodo")
let bools = fbb.createVector(boolArray)
let root = Monster.createMonster(&fbb, nameOffset: name, testarrayofboolsVectorOffset: bools)
let root = Monster.createMonster(
&fbb,
nameOffset: name,
testarrayofboolsVectorOffset: bools)
fbb.finish(offset: root)
let monster = Monster.getRootAsMonster(bb: fbb.sizedBuffer)
@@ -117,8 +141,13 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
}
}
func readMonster(fb: ByteBuffer) {
var monster = Monster.getRootAsMonster(bb: fb)
func readVerifiedMonster(fb: ByteBuffer) {
var byteBuffer = fb
XCTAssertNoThrow(try readMonster(monster: getCheckedRoot(byteBuffer: &byteBuffer) as MyGame_Example_Monster))
}
func readMonster(monster: Monster) {
var monster = monster
readFlatbufferMonster(monster: &monster)
let unpacked: MyGame_Example_MonsterT? = monster.unpack()
readObjectApi(monster: unpacked!)
@@ -129,7 +158,11 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
func createMonster(withPrefix prefix: Bool) -> FlatBufferBuilder {
var fbb = FlatBufferBuilder(initialSize: 1)
let names = [fbb.create(string: "Frodo"), fbb.create(string: "Barney"), fbb.create(string: "Wilma")]
let names = [
fbb.create(string: "Frodo"),
fbb.create(string: "Barney"),
fbb.create(string: "Wilma"),
]
var offsets: [Offset] = []
let start1 = Monster.startMonster(&fbb)
Monster.add(name: names[0], &fbb)
@@ -161,7 +194,15 @@ class FlatBuffersMonsterWriterTests: XCTestCase {
let stringTestVector = fbb.createVector(ofOffsets: [test1, test2])
let mStart = Monster.startMonster(&fbb)
Monster.add(pos: MyGame_Example_Vec3(x: 1, y: 2, z: 3, test1: 3, test2: .green, test3: .init(a: 5, b: 6)), &fbb)
Monster.add(
pos: MyGame_Example_Vec3(
x: 1,
y: 2,
z: 3,
test1: 3,
test2: .green,
test3: .init(a: 5, b: 6)),
&fbb)
Monster.add(hp: 80, &fbb)
Monster.add(name: str, &fbb)
Monster.addVectorOf(inventory: inv, &fbb)

View File

@@ -52,22 +52,36 @@ final class FlatBuffersTests: XCTestCase {
func testCreateFinish() {
var b = FlatBufferBuilder(initialSize: 16)
let countryOff = Country.createCountry(builder: &b, name: country, log: 200, lan: 100)
let countryOff = Country.createCountry(
builder: &b,
name: country,
log: 200,
lan: 100)
b.finish(offset: countryOff)
// swiftformat:disable all
let v: [UInt8] = [16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]
// swiftformat:enable all
XCTAssertEqual(b.sizedByteArray, v)
}
func testCreateFinishWithPrefix() {
var b = FlatBufferBuilder(initialSize: 16)
let countryOff = Country.createCountry(builder: &b, name: country, log: 200, lan: 100)
let countryOff = Country.createCountry(
builder: &b,
name: country,
log: 200,
lan: 100)
b.finish(offset: countryOff, addPrefix: true)
// swiftformat:disable all
let v: [UInt8] = [44, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]
// swiftformat:enable all
XCTAssertEqual(b.sizedByteArray, v)
}
func testReadCountry() {
// swiftformat:disable all
let v: [UInt8] = [16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 12, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]
// swiftformat:enable all
let buffer = ByteBuffer(bytes: v)
let c = Country.getRootAsCountry(buffer)
XCTAssertEqual(c.lan, 100)
@@ -115,14 +129,20 @@ class Country {
__t = t
}
var lan: Int32 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer(of: Int32.self, at: o) }
var log: Int32 { let o = __t.offset(8); return o == 0 ? 0 : __t.readBuffer(of: Int32.self, at: o) }
var lan: Int32 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer(
of: Int32.self,
at: o) }
var log: Int32 { let o = __t.offset(8); return o == 0 ? 0 : __t.readBuffer(
of: Int32.self,
at: o) }
var nameVector: [UInt8]? { __t.getVector(at: 4) }
var name: String? { let o = __t.offset(4); return o == 0 ? nil : __t.string(at: o) }
@inlinable
static func getRootAsCountry(_ bb: ByteBuffer) -> Country {
Country(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0))))
Country(Table(
bb: bb,
position: Int32(bb.read(def: UOffset.self, position: 0))))
}
@inlinable
@@ -132,7 +152,11 @@ class Country {
log: Int32,
lan: Int32) -> Offset
{
createCountry(builder: &builder, offset: builder.create(string: name), log: log, lan: lan)
createCountry(
builder: &builder,
offset: builder.create(string: name),
log: log,
lan: lan)
}
@inlinable

View File

@@ -34,7 +34,9 @@ final class FlatBuffersUnionTests: XCTestCase {
equippedOffset: weapon.o)
b.finish(offset: root)
let buffer = b.sizedByteArray
// swiftformat:disable all
XCTAssertEqual(buffer, [16, 0, 0, 0, 0, 0, 10, 0, 16, 0, 8, 0, 7, 0, 12, 0, 10, 0, 0, 0, 0, 0, 0, 1, 8, 0, 0, 0, 20, 0, 0, 0, 1, 0, 0, 0, 12, 0, 0, 0, 8, 0, 12, 0, 8, 0, 6, 0, 8, 0, 0, 0, 0, 0, 5, 0, 4, 0, 0, 0, 3, 0, 0, 0, 65, 120, 101, 0])
// swiftformat:enable all
let monster = LocalMonster.getRootAsMonster(bb: ByteBuffer(bytes: buffer))
XCTAssertEqual(monster.weapon(at: 0)?.dmg, dmg)
XCTAssertEqual(monster.weapon(at: 0)?.name, str)
@@ -71,7 +73,9 @@ final class FlatBuffersUnionTests: XCTestCase {
equippedOffset: weaponTwo,
path: path)
builder.finish(offset: orc)
// swiftformat:disable all
XCTAssertEqual(builder.sizedByteArray, [32, 0, 0, 0, 0, 0, 26, 0, 48, 0, 36, 0, 0, 0, 34, 0, 28, 0, 0, 0, 24, 0, 23, 0, 16, 0, 15, 0, 8, 0, 4, 0, 26, 0, 0, 0, 44, 0, 0, 0, 104, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 0, 0, 0, 0, 64, 0, 0, 0, 76, 0, 0, 0, 0, 0, 44, 1, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 2, 0, 0, 0, 0, 0, 128, 64, 0, 0, 160, 64, 0, 0, 192, 64, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 2, 0, 0, 0, 52, 0, 0, 0, 28, 0, 0, 0, 10, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 3, 0, 0, 0, 79, 114, 99, 0, 244, 255, 255, 255, 0, 0, 5, 0, 24, 0, 0, 0, 8, 0, 12, 0, 8, 0, 6, 0, 8, 0, 0, 0, 0, 0, 3, 0, 12, 0, 0, 0, 3, 0, 0, 0, 65, 120, 101, 0, 5, 0, 0, 0, 83, 119, 111, 114, 100, 0, 0, 0])
// swiftformat:enable all
}
func testEnumVector() {
@@ -83,7 +87,9 @@ final class FlatBuffersUnionTests: XCTestCase {
ColorsNameSpace.Monster.add(colors: off, &builder)
let end = ColorsNameSpace.Monster.endMonster(&builder, start: start)
builder.finish(offset: end)
// swiftformat:disable all
XCTAssertEqual(builder.sizedByteArray, [12, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0])
// swiftformat:enable all
let monster = ColorsNameSpace.Monster.getRootAsMonster(bb: builder.buffer)
XCTAssertEqual(monster.colorsCount, 2)
XCTAssertEqual(monster.colors(at: 0), .blue)
@@ -121,22 +127,36 @@ final class FlatBuffersUnionTests: XCTestCase {
XCTAssertEqual(movie.charactersType(at: i), characterType[Int(i)])
}
XCTAssertEqual(movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead, 7)
XCTAssertEqual(movie.characters(at: 1, type: Attacker.self)?.swordAttackDamage, swordDmg)
XCTAssertEqual(movie.characters(at: 2, type: BookReader_Mutable.self)?.booksRead, 2)
XCTAssertEqual(
movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead,
7)
XCTAssertEqual(
movie.characters(at: 1, type: Attacker.self)?.swordAttackDamage,
swordDmg)
XCTAssertEqual(
movie.characters(at: 2, type: BookReader_Mutable.self)?.booksRead,
2)
var objc: MovieT? = movie.unpack()
XCTAssertEqual(movie.charactersTypeCount, Int32(objc?.characters.count ?? 0))
XCTAssertEqual(movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead, (objc?.characters[0]?.value as? BookReader)?.booksRead)
XCTAssertEqual(
movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead,
(objc?.characters[0]?.value as? BookReader)?.booksRead)
fb.clear()
let newMovie = Movie.pack(&fb, obj: &objc)
fb.finish(offset: newMovie)
let packedMovie = Movie.getRootAsMovie(bb: fb.buffer)
XCTAssertEqual(packedMovie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead, movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead)
XCTAssertEqual(packedMovie.characters(at: 1, type: Attacker.self)?.swordAttackDamage, movie.characters(at: 1, type: Attacker.self)?.swordAttackDamage)
XCTAssertEqual(packedMovie.characters(at: 2, type: BookReader_Mutable.self)?.booksRead, movie.characters(at: 2, type: BookReader_Mutable.self)?.booksRead)
XCTAssertEqual(
packedMovie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead,
movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead)
XCTAssertEqual(
packedMovie.characters(at: 1, type: Attacker.self)?.swordAttackDamage,
movie.characters(at: 1, type: Attacker.self)?.swordAttackDamage)
XCTAssertEqual(
packedMovie.characters(at: 2, type: BookReader_Mutable.self)?.booksRead,
movie.characters(at: 2, type: BookReader_Mutable.self)?.booksRead)
}
func testStringUnion() {
@@ -162,7 +182,9 @@ final class FlatBuffersUnionTests: XCTestCase {
var movie = Movie.getRootAsMovie(bb: fb.sizedBuffer)
XCTAssertEqual(movie.mainCharacter(type: String.self), string)
XCTAssertEqual(movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead, 7)
XCTAssertEqual(
movie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead,
7)
XCTAssertEqual(movie.characters(at: 1, type: String.self), string)
var objc: MovieT? = movie.unpack()
@@ -175,7 +197,9 @@ final class FlatBuffersUnionTests: XCTestCase {
let packedMovie = Movie.getRootAsMovie(bb: fb.buffer)
XCTAssertEqual(packedMovie.mainCharacter(type: String.self), string)
XCTAssertEqual(packedMovie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead, 7)
XCTAssertEqual(
packedMovie.characters(at: 0, type: BookReader_Mutable.self)?.booksRead,
7)
XCTAssertEqual(packedMovie.characters(at: 1, type: String.self), string)
}
}
@@ -239,7 +263,10 @@ struct FinalMonster {
builder.add(offset: inventory, at: 14)
builder.add(element: color.rawValue, def: Color3.green.rawValue, at: 16)
builder.add(offset: weapons, at: 18)
builder.add(element: equipment.rawValue, def: Equipment.none.rawValue, at: 20)
builder.add(
element: equipment.rawValue,
def: Equipment.none.rawValue,
at: 20)
builder.add(offset: equippedOffset, at: 22)
builder.add(offset: path, at: 24)
return Offset(offset: builder.endTable(at: start))
@@ -262,7 +289,9 @@ struct LocalMonster {
}
static func getRootAsMonster(bb: ByteBuffer) -> LocalMonster {
LocalMonster(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0))))
LocalMonster(Table(
bb: bb,
position: Int32(bb.read(def: UOffset.self, position: 0))))
}
@inlinable
@@ -290,11 +319,15 @@ struct Weapon: FlatBufferObject {
init(_ t: Table) { __t = t }
init(_ fb: ByteBuffer, o: Int32) { __t = Table(bb: fb, position: o)}
var dmg: Int16 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer(of: Int16.self, at: o) }
var dmg: Int16 { let o = __t.offset(6); return o == 0 ? 0 : __t.readBuffer(
of: Int16.self,
at: o) }
var nameVector: [UInt8]? { __t.getVector(at: 4) }
var name: String? { let o = __t.offset(4); return o == 0 ? nil : __t.string(at: o) }
static func assign(_ i: Int32, _ bb: ByteBuffer) -> Weapon { Weapon(Table(bb: bb, position: i)) }
static func assign(_ i: Int32, _ bb: ByteBuffer) -> Weapon { Weapon(Table(
bb: bb,
position: i)) }
@inlinable
static func createWeapon(

View File

@@ -30,7 +30,9 @@ final class FlatBuffersVectors: XCTestCase {
let vector = [n, d]
let vectorOffset = b.createVector(ofOffsets: vector)
b.finish(offset: vectorOffset)
// swiftformat:disable all
XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 2, 0, 0, 0, 48, 0, 0, 0, 16, 0, 0, 0, 0, 0, 10, 0, 18, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 40, 0, 0, 0, 100, 0, 0, 0, 200, 0, 0, 0, 0, 0, 10, 0, 16, 0, 4, 0, 8, 0, 12, 0, 10, 0, 0, 0, 24, 0, 0, 0, 188, 2, 0, 0, 120, 3, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0])
// swiftformat:enable all
}
func testCreateIntArray() {
@@ -38,7 +40,9 @@ final class FlatBuffersVectors: XCTestCase {
var b = FlatBufferBuilder(initialSize: 20)
let o = b.createVector(numbers, size: numbers.count)
b.finish(offset: o)
// swiftformat:disable all
XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 5, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0])
// swiftformat:enable all
}
func testCreateEmptyIntArray() {
@@ -54,7 +58,9 @@ final class FlatBuffersVectors: XCTestCase {
var b = FlatBufferBuilder(initialSize: 20)
let o = b.createVector(ofStrings: strs)
b.finish(offset: o)
// swiftformat:disable all
XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 2, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0])
// swiftformat:enable all
}
func testCreateSharedStringVector() {
let norway = "Norway"
@@ -67,7 +73,9 @@ final class FlatBuffersVectors: XCTestCase {
let v = [noStr, deStr, _noStr, _deStr]
let end = b.createVector(ofOffsets: v)
b.finish(offset: end)
// swiftformat:disable all
XCTAssertEqual(b.sizedByteArray, [4, 0, 0, 0, 4, 0, 0, 0, 28, 0, 0, 0, 12, 0, 0, 0, 20, 0, 0, 0, 4, 0, 0, 0, 7, 0, 0, 0, 68, 101, 110, 109, 97, 114, 107, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0])
// swiftformat:enable all
}
func testReadInt32Array() {
@@ -101,7 +109,9 @@ struct Numbers {
@inlinable
static func getRootAsNumbers(_ bb: ByteBuffer) -> Numbers {
Numbers(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: 0))))
Numbers(Table(
bb: bb,
position: Int32(bb.read(def: UOffset.self, position: 0))))
}
var vArrayInt: [Int]? { __t.getVector(at: 4) }

View File

@@ -23,17 +23,39 @@ final class FlatBuffersDoubleTests: XCTestCase {
func testCreateFinish() {
var b = FlatBufferBuilder(initialSize: 16)
let countryOff = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100)
let countryOff = CountryDouble.createCountry(
builder: &b,
name: country,
log: 200,
lan: 100)
b.finish(offset: countryOff)
let v: [UInt8] = [16, 0, 0, 0, 0, 0, 10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]
// swiftformat:disable all
let v: [UInt8] = [
16, 0, 0, 0, 0, 0, 10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10,
0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0,
0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119,
97, 121, 0, 0
]
// swiftformat:enable all
XCTAssertEqual(b.sizedByteArray, v)
}
func testCreateFinishWithPrefix() {
var b = FlatBufferBuilder(initialSize: 16)
let countryOff = CountryDouble.createCountry(builder: &b, name: country, log: 200, lan: 100)
let countryOff = CountryDouble.createCountry(
builder: &b,
name: country,
log: 200,
lan: 100)
b.finish(offset: countryOff, addPrefix: true)
let v: [UInt8] = [60, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 28, 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0, 0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0]
// swiftformat:disable all
let v: [UInt8] = [
60, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 28
, 0, 4, 0, 8, 0, 16, 0, 10, 0, 0, 0, 24, 0, 0, 0, 0, 0,
0, 0, 0, 0, 89, 64, 0, 0, 0, 0, 0, 0, 105, 64, 0, 0, 0,
0, 6, 0, 0, 0, 78, 111, 114, 119, 97, 121, 0, 0
]
// swiftformat:enable all
XCTAssertEqual(b.sizedByteArray, v)
}
}
@@ -57,7 +79,11 @@ class CountryDouble {
log: Double,
lan: Double) -> Offset
{
createCountry(builder: &builder, offset: builder.create(string: name), log: log, lan: lan)
createCountry(
builder: &builder,
offset: builder.create(string: name),
log: log,
lan: lan)
}
static func createCountry(

View File

@@ -0,0 +1,238 @@
/*
* Copyright 2021 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 XCTest
@testable import FlatBuffers
final class FlatbuffersVerifierTests: XCTestCase {
lazy var validStorage: ByteBuffer.Storage = ByteBuffer.Storage(
count: Int(FlatBufferMaxSize) - 1,
alignment: 1)
lazy var errorStorage: ByteBuffer.Storage = ByteBuffer.Storage(
count: Int(FlatBufferMaxSize) + 1,
alignment: 1)
var buffer: ByteBuffer!
var validFlatbuffersObject: ByteBuffer!
var invalidFlatbuffersObject: ByteBuffer!
override func setUp() {
// swiftformat:disable all
buffer = ByteBuffer(initialSize: 32)
add(buffer: &buffer, v: 4, p: 16)
add(buffer: &buffer, v: 4, p: 20)
validFlatbuffersObject = ByteBuffer(bytes: [48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
invalidFlatbuffersObject = ByteBuffer(bytes: [0, 48, 0, 0, 0, 77, 79, 78, 83, 0, 0, 0, 0, 36, 0, 72, 0, 40, 0, 0, 0, 38, 0, 32, 0, 0, 0, 28, 0, 0, 0, 27, 0, 20, 0, 16, 0, 12, 0, 4, 0, 0, 0, 0, 0, 0, 0, 11, 0, 36, 0, 0, 0, 164, 0, 0, 0, 0, 0, 0, 1, 60, 0, 0, 0, 68, 0, 0, 0, 76, 0, 0, 0, 0, 0, 0, 1, 88, 0, 0, 0, 120, 0, 0, 0, 0, 0, 80, 0, 0, 0, 128, 63, 0, 0, 0, 64, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 64, 2, 0, 5, 0, 6, 0, 0, 0, 2, 0, 0, 0, 64, 0, 0, 0, 48, 0, 0, 0, 2, 0, 0, 0, 30, 0, 40, 0, 10, 0, 20, 0, 152, 255, 255, 255, 4, 0, 0, 0, 4, 0, 0, 0, 70, 114, 101, 100, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 2, 3, 4, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 50, 0, 0, 0, 5, 0, 0, 0, 116, 101, 115, 116, 49, 0, 0, 0, 9, 0, 0, 0, 77, 121, 77, 111, 110, 115, 116, 101, 114, 0, 0, 0, 3, 0, 0, 0, 20, 0, 0, 0, 36, 0, 0, 0, 4, 0, 0, 0, 240, 255, 255, 255, 32, 0, 0, 0, 248, 255, 255, 255, 36, 0, 0, 0, 12, 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 12, 0, 0, 0, 28, 0, 0, 0, 5, 0, 0, 0, 87, 105, 108, 109, 97, 0, 0, 0, 6, 0, 0, 0, 66, 97, 114, 110, 101, 121, 0, 0, 5, 0, 0, 0, 70, 114, 111, 100, 111, 0, 0, 0])
// swiftformat:enable all
}
func testVeriferInitPassing() {
var buffer = ByteBuffer(initialSize: 0)
buffer._storage = validStorage
XCTAssertNoThrow(try Verifier(buffer: &buffer))
}
func testVeriferInitFailing() {
var buffer = ByteBuffer(initialSize: 0)
buffer._storage = errorStorage
XCTAssertThrowsError(try Verifier(buffer: &buffer))
}
func testVerifierCheckAlignment() {
var verifier = try! Verifier(buffer: &buffer)
do {
try verifier.isAligned(position: 20, type: Int.self)
} catch {
XCTAssertEqual(
error as? FlatbuffersErrors,
.missAlignedPointer(position: 20, type: "Int"))
}
XCTAssertNoThrow(try verifier.isAligned(position: 16, type: Int.self))
var newVerifer = try! Verifier(buffer: &buffer, checkAlignment: false)
XCTAssertNoThrow(try newVerifer.isAligned(position: 16, type: Int.self))
}
func testRangeInBuffer() {
var verifier = try! Verifier(buffer: &buffer)
let size = MemoryLayout<Int64>.size
XCTAssertNoThrow(try verifier.rangeInBuffer(position: 24, size: size))
XCTAssertThrowsError(try verifier.rangeInBuffer(position: 26, size: size))
XCTAssertThrowsError(try verifier.rangeInBuffer(position: 26, size: size))
XCTAssertThrowsError(try verifier.rangeInBuffer(position: 30, size: size))
XCTAssertThrowsError(try verifier.rangeInBuffer(position: 32, size: size))
XCTAssertThrowsError(try verifier.rangeInBuffer(position: 34, size: size))
verifier = try! Verifier(
buffer: &buffer,
options: .init(maxDepth: 0, maxTableCount: 0, maxApparentSize: 4))
do {
try verifier.rangeInBuffer(position: 24, size: size)
} catch {
XCTAssertEqual(
error as! FlatbuffersErrors,
.apparentSizeTooLarge)
}
}
func testPositionInBuffer() {
var verifier = try! Verifier(buffer: &buffer)
XCTAssertNoThrow(try verifier.inBuffer(position: 0, of: Int64.self))
XCTAssertNoThrow(try verifier.inBuffer(position: 24, of: Int64.self))
XCTAssertThrowsError(try verifier.inBuffer(position: -9, of: Int64.self))
XCTAssertThrowsError(try verifier.inBuffer(position: 25, of: Int64.self))
XCTAssertThrowsError(try verifier.inBuffer(position: 26, of: Int32.self))
XCTAssertThrowsError(try verifier.inBuffer(position: 26, of: Int64.self))
XCTAssertThrowsError(try verifier.inBuffer(position: 30, of: Int64.self))
XCTAssertThrowsError(try verifier.inBuffer(position: 32, of: Int64.self))
XCTAssertThrowsError(try verifier.inBuffer(position: 34, of: Int64.self))
}
func testVisitTable() {
var verifier = try! Verifier(buffer: &validFlatbuffersObject)
XCTAssertNoThrow(try verifier.visitTable(at: 48))
verifier.reset()
}
func testTableVerifier() {
var verifier = try! Verifier(buffer: &validFlatbuffersObject)
var tableVerifer = try! verifier.visitTable(at: 48)
XCTAssertNoThrow(try tableVerifer.visit(
field: 4,
fieldName: "Vec",
required: false,
type: Vec3.self))
XCTAssertNoThrow(try tableVerifer.visit(
field: 8,
fieldName: "hp",
required: false,
type: Int16.self))
XCTAssertNoThrow(try tableVerifer.visit(
field: 10,
fieldName: "name",
required: true,
type: ForwardOffset<String>.self))
XCTAssertNoThrow(try tableVerifer.visit(
field: 14,
fieldName: "inventory",
required: false,
type: ForwardOffset<Vector<UInt8, UInt8>>.self))
XCTAssertNoThrow(try tableVerifer.visit(
field: 22,
fieldName: "test4",
required: false,
type: ForwardOffset<Vector<MyGame_Example_Test, MyGame_Example_Test>>.self))
XCTAssertNoThrow(try tableVerifer.visit(
field: 24,
fieldName: "Vector of strings",
required: false,
type: ForwardOffset<Vector<ForwardOffset<String>, String>>.self))
do {
try tableVerifer.visit(
field: 13,
fieldName: "notvalid",
required: false,
type: Int16.self)
} catch {
XCTAssertEqual(
error as! FlatbuffersErrors,
.missAlignedPointer(position: 25, type: "UInt16"))
}
do {
try tableVerifer.visit(
unionKey: 18,
unionField: 20,
unionKeyName: "testType",
fieldName: "test",
required: false,
completion: { (verifier, key: MyGame_Example_Any_, pos) in
switch key {
case .none_:
break
case .monster:
try ForwardOffset<MyGame_Example_Monster>.verify(
&verifier,
at: pos,
of: MyGame_Example_Monster.self)
case .testsimpletablewithenum:
break
case .mygameExample2Monster:
break
}
})
} catch {
XCTAssertEqual(
error as! FlatbuffersErrors,
.missAlignedPointer(position: 25, type: "UInt16"))
}
}
func testVerifyUnionVectors() {
// swiftformat:disable all
var byteBuffer = ByteBuffer(bytes: [20, 0, 0, 0, 77, 79, 86, 73, 12, 0, 12, 0, 0, 0, 0, 0, 8, 0, 4, 0, 12, 0, 0, 0, 8, 0, 0, 0, 20, 0, 0, 0, 3, 0, 0, 0, 24, 0, 0, 0, 32, 0, 0, 0, 12, 0, 0, 0, 3, 0, 0, 0, 3, 1, 4, 0, 2, 0, 0, 0, 7, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 8, 0, 0, 0])
// swiftformat:enable all
XCTAssertNoThrow(try getCheckedRoot(byteBuffer: &byteBuffer) as Movie)
}
func testFullVerifier() {
XCTAssertNoThrow(try getCheckedRoot(byteBuffer: &validFlatbuffersObject) as MyGame_Example_Monster)
}
func testInvalidBuffer() {
XCTAssertThrowsError(try getCheckedRoot(byteBuffer: &invalidFlatbuffersObject) as MyGame_Example_Monster)
}
func testValidUnionBuffer() {
let string = "Awesome \\\\t\t\nstring!"
var fb = FlatBufferBuilder()
let stringOffset = fb.create(string: string)
let characterType: [Character] = [.bookfan, .other]
let characters = [
fb.create(struct: BookReader(booksRead: 7)),
stringOffset,
]
let types = fb.createVector(characterType)
let characterVector = fb.createVector(ofOffsets: characters)
let end = Movie.createMovie(
&fb,
mainCharacterType: .other,
mainCharacterOffset: Offset(offset: stringOffset.o),
charactersTypeVectorOffset: types,
charactersVectorOffset: characterVector)
Movie.finish(&fb, end: end)
var buf = fb.sizedBuffer
XCTAssertNoThrow(try getCheckedRoot(byteBuffer: &buf) as Movie)
}
func add(buffer: inout ByteBuffer, v: Int32, p: Int) {
buffer.write(value: v, index: p)
}
}

View File

@@ -36,11 +36,17 @@ extension FlatBuffersMonsterWriterTests {
("testCreateMonster", testCreateMonster),
("testCreateMonsterPrefixed", testCreateMonsterPrefixed),
("testCreateMonsterResizedBuffer", testCreateMonsterResizedBuffer),
("testCreateMonsterUsingCreateMonsterMethodWithNilPos", testCreateMonsterUsingCreateMonsterMethodWithNilPos),
("testCreateMonsterUsingCreateMonsterMethodWithPosX", testCreateMonsterUsingCreateMonsterMethodWithPosX),
(
"testCreateMonsterUsingCreateMonsterMethodWithNilPos",
testCreateMonsterUsingCreateMonsterMethodWithNilPos),
(
"testCreateMonsterUsingCreateMonsterMethodWithPosX",
testCreateMonsterUsingCreateMonsterMethodWithPosX),
("testData", testData),
("testReadFromOtherLanguages", testReadFromOtherLanguages),
("testReadMonsterFromUnsafePointerWithoutCopying", testReadMonsterFromUnsafePointerWithoutCopying),
(
"testReadMonsterFromUnsafePointerWithoutCopying",
testReadMonsterFromUnsafePointerWithoutCopying),
]
}
@@ -108,6 +114,25 @@ extension FlatBuffersVectors {
]
}
extension FlatbuffersVerifierTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__FlatbuffersVerifierTests = [
("testFullVerifier", testFullVerifier),
("testInvalidBuffer", testInvalidBuffer),
("testPositionInBuffer", testPositionInBuffer),
("testRangeInBuffer", testRangeInBuffer),
("testTableVerifier", testTableVerifier),
("testValidUnionBuffer", testValidUnionBuffer),
("testVeriferInitFailing", testVeriferInitFailing),
("testVeriferInitPassing", testVeriferInitPassing),
("testVerifierCheckAlignment", testVerifierCheckAlignment),
("testVerifyUnionVectors", testVerifyUnionVectors),
("testVisitTable", testVisitTable),
]
}
public func __allTests() -> [XCTestCaseEntry] {
[
testCase(FlatBuffersDoubleTests.__allTests__FlatBuffersDoubleTests),
@@ -117,6 +142,7 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(FlatBuffersTests.__allTests__FlatBuffersTests),
testCase(FlatBuffersUnionTests.__allTests__FlatBuffersUnionTests),
testCase(FlatBuffersVectors.__allTests__FlatBuffersVectors),
testCase(FlatbuffersVerifierTests.__allTests__FlatbuffersVerifierTests),
]
}
#endif

View File

@@ -5,7 +5,7 @@
import FlatBuffers
/// Composite components of Monster color.
public enum MyGame_Example_Color: UInt8, Enum {
public enum MyGame_Example_Color: UInt8, Enum, Verifiable {
public typealias T = UInt8
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
public var value: UInt8 { return self.rawValue }
@@ -15,13 +15,12 @@ public enum MyGame_Example_Color: UInt8, Enum {
case green = 2
/// \brief color Blue (1u << 3)
case blue = 8
public static var max: MyGame_Example_Color { return .blue }
public static var min: MyGame_Example_Color { return .red }
}
public enum MyGame_Example_Race: Int8, Enum {
public enum MyGame_Example_Race: Int8, Enum, Verifiable {
public typealias T = Int8
public static var byteSize: Int { return MemoryLayout<Int8>.size }
public var value: Int8 { return self.rawValue }
@@ -29,21 +28,24 @@ public enum MyGame_Example_Race: Int8, Enum {
case human = 0
case dwarf = 1
case elf = 2
public static var max: MyGame_Example_Race { return .elf }
public static var min: MyGame_Example_Race { return .none_ }
}
public enum MyGame_Example_Any_: UInt8, Enum {
public enum MyGame_Example_Any_: UInt8, UnionEnum {
public typealias T = UInt8
public init?(value: T) {
self.init(rawValue: value)
}
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
public var value: UInt8 { return self.rawValue }
case none_ = 0
case monster = 1
case testsimpletablewithenum = 2
case mygameExample2Monster = 3
public static var max: MyGame_Example_Any_ { return .mygameExample2Monster }
public static var min: MyGame_Example_Any_ { return .none_ }
@@ -71,15 +73,19 @@ public struct MyGame_Example_Any_Union {
}
}
}
public enum MyGame_Example_AnyUniqueAliases: UInt8, Enum {
public enum MyGame_Example_AnyUniqueAliases: UInt8, UnionEnum {
public typealias T = UInt8
public init?(value: T) {
self.init(rawValue: value)
}
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
public var value: UInt8 { return self.rawValue }
case none_ = 0
case m = 1
case ts = 2
case m2 = 3
public static var max: MyGame_Example_AnyUniqueAliases { return .m2 }
public static var min: MyGame_Example_AnyUniqueAliases { return .none_ }
@@ -107,15 +113,19 @@ public struct MyGame_Example_AnyUniqueAliasesUnion {
}
}
}
public enum MyGame_Example_AnyAmbiguousAliases: UInt8, Enum {
public enum MyGame_Example_AnyAmbiguousAliases: UInt8, UnionEnum {
public typealias T = UInt8
public init?(value: T) {
self.init(rawValue: value)
}
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
public var value: UInt8 { return self.rawValue }
case none_ = 0
case m1 = 1
case m2 = 2
case m3 = 3
public static var max: MyGame_Example_AnyAmbiguousAliases { return .m3 }
public static var min: MyGame_Example_AnyAmbiguousAliases { return .none_ }
@@ -143,7 +153,7 @@ public struct MyGame_Example_AnyAmbiguousAliasesUnion {
}
}
}
public struct MyGame_Example_Test: NativeStruct, NativeObject {
public struct MyGame_Example_Test: NativeStruct, Verifiable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -168,6 +178,10 @@ public struct MyGame_Example_Test: NativeStruct, NativeObject {
public var a: Int16 { _a }
public var b: Int8 { _b }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: MyGame_Example_Test.self)
}
}
public struct MyGame_Example_Test_Mutable: FlatBufferObject {
@@ -197,7 +211,7 @@ public struct MyGame_Example_Test_Mutable: FlatBufferObject {
}
}
public struct MyGame_Example_Vec3: NativeStruct, NativeObject {
public struct MyGame_Example_Vec3: NativeStruct, Verifiable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -245,6 +259,10 @@ public struct MyGame_Example_Vec3: NativeStruct, NativeObject {
public var test1: Double { _test1 }
public var test2: MyGame_Example_Color { MyGame_Example_Color(rawValue: _test2)! }
public var test3: MyGame_Example_Test { _test3 }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: MyGame_Example_Vec3.self)
}
}
public struct MyGame_Example_Vec3_Mutable: FlatBufferObject {
@@ -281,7 +299,7 @@ public struct MyGame_Example_Vec3_Mutable: FlatBufferObject {
}
}
public struct MyGame_Example_Ability: NativeStruct, NativeObject {
public struct MyGame_Example_Ability: NativeStruct, Verifiable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -305,6 +323,10 @@ public struct MyGame_Example_Ability: NativeStruct, NativeObject {
public var id: UInt32 { _id }
public var distance: UInt32 { _distance }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: MyGame_Example_Ability.self)
}
}
public struct MyGame_Example_Ability_Mutable: FlatBufferObject {
@@ -334,7 +356,7 @@ public struct MyGame_Example_Ability_Mutable: FlatBufferObject {
}
}
public struct MyGame_Example_StructOfStructs: NativeStruct, NativeObject {
public struct MyGame_Example_StructOfStructs: NativeStruct, Verifiable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -366,6 +388,10 @@ public struct MyGame_Example_StructOfStructs: NativeStruct, NativeObject {
public var a: MyGame_Example_Ability { _a }
public var b: MyGame_Example_Test { _b }
public var c: MyGame_Example_Ability { _c }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: MyGame_Example_StructOfStructs.self)
}
}
public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferObject {
@@ -394,7 +420,7 @@ public struct MyGame_Example_StructOfStructs_Mutable: FlatBufferObject {
}
}
public struct MyGame_InParentNamespace: FlatBufferObject, ObjectAPIPacker {
public struct MyGame_InParentNamespace: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -422,6 +448,11 @@ public struct MyGame_InParentNamespace: FlatBufferObject, ObjectAPIPacker {
let __root = MyGame_InParentNamespace.startInParentNamespace(&builder)
return MyGame_InParentNamespace.endInParentNamespace(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
_v.finish()
}
}
public class MyGame_InParentNamespaceT: NativeObject {
@@ -436,7 +467,7 @@ public class MyGame_InParentNamespaceT: NativeObject {
public func serialize() -> ByteBuffer { return serialize(type: MyGame_InParentNamespace.self) }
}
public struct MyGame_Example2_Monster: FlatBufferObject, ObjectAPIPacker {
public struct MyGame_Example2_Monster: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -464,6 +495,11 @@ public struct MyGame_Example2_Monster: FlatBufferObject, ObjectAPIPacker {
let __root = MyGame_Example2_Monster.startMonster(&builder)
return MyGame_Example2_Monster.endMonster(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
_v.finish()
}
}
public class MyGame_Example2_MonsterT: NativeObject {
@@ -478,7 +514,7 @@ public class MyGame_Example2_MonsterT: NativeObject {
public func serialize() -> ByteBuffer { return serialize(type: MyGame_Example2_Monster.self) }
}
internal struct MyGame_Example_TestSimpleTableWithEnum: FlatBufferObject, ObjectAPIPacker {
internal struct MyGame_Example_TestSimpleTableWithEnum: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
internal var __buffer: ByteBuffer! { return _accessor.bb }
@@ -524,6 +560,12 @@ internal struct MyGame_Example_TestSimpleTableWithEnum: FlatBufferObject, Object
MyGame_Example_TestSimpleTableWithEnum.add(color: obj.color, &builder)
return MyGame_Example_TestSimpleTableWithEnum.endTestSimpleTableWithEnum(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.color.p, fieldName: "color", required: false, type: MyGame_Example_Color.self)
_v.finish()
}
}
internal class MyGame_Example_TestSimpleTableWithEnumT: NativeObject {
@@ -541,7 +583,7 @@ internal class MyGame_Example_TestSimpleTableWithEnumT: NativeObject {
internal func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_TestSimpleTableWithEnum.self) }
}
public struct MyGame_Example_Stat: FlatBufferObject, ObjectAPIPacker {
public struct MyGame_Example_Stat: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -632,6 +674,14 @@ public struct MyGame_Example_Stat: FlatBufferObject, ObjectAPIPacker {
MyGame_Example_Stat.add(count: obj.count, &builder)
return MyGame_Example_Stat.endStat(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.id.p, fieldName: "id", required: false, type: ForwardOffset<String>.self)
try _v.visit(field: VTOFFSET.val.p, fieldName: "val", required: false, type: Int64.self)
try _v.visit(field: VTOFFSET.count.p, fieldName: "count", required: false, type: UInt16.self)
_v.finish()
}
}
public class MyGame_Example_StatT: NativeObject {
@@ -654,7 +704,7 @@ public class MyGame_Example_StatT: NativeObject {
public func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_Stat.self) }
}
public struct MyGame_Example_Referrable: FlatBufferObject, ObjectAPIPacker {
public struct MyGame_Example_Referrable: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -724,6 +774,12 @@ public struct MyGame_Example_Referrable: FlatBufferObject, ObjectAPIPacker {
MyGame_Example_Referrable.add(id: obj.id, &builder)
return MyGame_Example_Referrable.endReferrable(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.id.p, fieldName: "id", required: false, type: UInt64.self)
_v.finish()
}
}
public class MyGame_Example_ReferrableT: NativeObject {
@@ -742,7 +798,7 @@ public class MyGame_Example_ReferrableT: NativeObject {
}
/// an example documentation comment: "monster object"
public struct MyGame_Example_Monster: FlatBufferObject, ObjectAPIPacker {
public struct MyGame_Example_Monster: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -1253,6 +1309,91 @@ public struct MyGame_Example_Monster: FlatBufferObject, ObjectAPIPacker {
MyGame_Example_Monster.addVectorOf(scalarKeySortedTables: __scalarKeySortedTables, &builder)
return MyGame_Example_Monster.endMonster(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.pos.p, fieldName: "pos", required: false, type: MyGame_Example_Vec3.self)
try _v.visit(field: VTOFFSET.mana.p, fieldName: "mana", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.hp.p, fieldName: "hp", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.name.p, fieldName: "name", required: true, type: ForwardOffset<String>.self)
try _v.visit(field: VTOFFSET.inventory.p, fieldName: "inventory", required: false, type: ForwardOffset<Vector<UInt8, UInt8>>.self)
try _v.visit(field: VTOFFSET.color.p, fieldName: "color", required: false, type: MyGame_Example_Color.self)
try _v.visit(unionKey: VTOFFSET.testType.p, unionField: VTOFFSET.test.p, unionKeyName: "testType", fieldName: "test", required: false, completion: { (verifier, key: MyGame_Example_Any_, pos) in
switch key {
case .none_:
break // NOTE - SWIFT doesnt support none
case .monster:
try ForwardOffset<MyGame_Example_Monster>.verify(&verifier, at: pos, of: MyGame_Example_Monster.self)
case .testsimpletablewithenum:
try ForwardOffset<MyGame_Example_TestSimpleTableWithEnum>.verify(&verifier, at: pos, of: MyGame_Example_TestSimpleTableWithEnum.self)
case .mygameExample2Monster:
try ForwardOffset<MyGame_Example2_Monster>.verify(&verifier, at: pos, of: MyGame_Example2_Monster.self)
}
})
try _v.visit(field: VTOFFSET.test4.p, fieldName: "test4", required: false, type: ForwardOffset<Vector<MyGame_Example_Test, MyGame_Example_Test>>.self)
try _v.visit(field: VTOFFSET.testarrayofstring.p, fieldName: "testarrayofstring", required: false, type: ForwardOffset<Vector<ForwardOffset<String>, String>>.self)
try _v.visit(field: VTOFFSET.testarrayoftables.p, fieldName: "testarrayoftables", required: false, type: ForwardOffset<Vector<ForwardOffset<MyGame_Example_Monster>, MyGame_Example_Monster>>.self)
try _v.visit(field: VTOFFSET.enemy.p, fieldName: "enemy", required: false, type: ForwardOffset<MyGame_Example_Monster>.self)
try _v.visit(field: VTOFFSET.testnestedflatbuffer.p, fieldName: "testnestedflatbuffer", required: false, type: ForwardOffset<Vector<UInt8, UInt8>>.self)
try _v.visit(field: VTOFFSET.testempty.p, fieldName: "testempty", required: false, type: ForwardOffset<MyGame_Example_Stat>.self)
try _v.visit(field: VTOFFSET.testbool.p, fieldName: "testbool", required: false, type: Bool.self)
try _v.visit(field: VTOFFSET.testhashs32Fnv1.p, fieldName: "testhashs32Fnv1", required: false, type: Int32.self)
try _v.visit(field: VTOFFSET.testhashu32Fnv1.p, fieldName: "testhashu32Fnv1", required: false, type: UInt32.self)
try _v.visit(field: VTOFFSET.testhashs64Fnv1.p, fieldName: "testhashs64Fnv1", required: false, type: Int64.self)
try _v.visit(field: VTOFFSET.testhashu64Fnv1.p, fieldName: "testhashu64Fnv1", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.testhashs32Fnv1a.p, fieldName: "testhashs32Fnv1a", required: false, type: Int32.self)
try _v.visit(field: VTOFFSET.testhashu32Fnv1a.p, fieldName: "testhashu32Fnv1a", required: false, type: UInt32.self)
try _v.visit(field: VTOFFSET.testhashs64Fnv1a.p, fieldName: "testhashs64Fnv1a", required: false, type: Int64.self)
try _v.visit(field: VTOFFSET.testhashu64Fnv1a.p, fieldName: "testhashu64Fnv1a", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.testarrayofbools.p, fieldName: "testarrayofbools", required: false, type: ForwardOffset<Vector<Bool, Bool>>.self)
try _v.visit(field: VTOFFSET.testf.p, fieldName: "testf", required: false, type: Float32.self)
try _v.visit(field: VTOFFSET.testf2.p, fieldName: "testf2", required: false, type: Float32.self)
try _v.visit(field: VTOFFSET.testf3.p, fieldName: "testf3", required: false, type: Float32.self)
try _v.visit(field: VTOFFSET.testarrayofstring2.p, fieldName: "testarrayofstring2", required: false, type: ForwardOffset<Vector<ForwardOffset<String>, String>>.self)
try _v.visit(field: VTOFFSET.testarrayofsortedstruct.p, fieldName: "testarrayofsortedstruct", required: false, type: ForwardOffset<Vector<MyGame_Example_Ability, MyGame_Example_Ability>>.self)
try _v.visit(field: VTOFFSET.flex.p, fieldName: "flex", required: false, type: ForwardOffset<Vector<UInt8, UInt8>>.self)
try _v.visit(field: VTOFFSET.test5.p, fieldName: "test5", required: false, type: ForwardOffset<Vector<MyGame_Example_Test, MyGame_Example_Test>>.self)
try _v.visit(field: VTOFFSET.vectorOfLongs.p, fieldName: "vectorOfLongs", required: false, type: ForwardOffset<Vector<Int64, Int64>>.self)
try _v.visit(field: VTOFFSET.vectorOfDoubles.p, fieldName: "vectorOfDoubles", required: false, type: ForwardOffset<Vector<Double, Double>>.self)
try _v.visit(field: VTOFFSET.parentNamespaceTest.p, fieldName: "parentNamespaceTest", required: false, type: ForwardOffset<MyGame_InParentNamespace>.self)
try _v.visit(field: VTOFFSET.vectorOfReferrables.p, fieldName: "vectorOfReferrables", required: false, type: ForwardOffset<Vector<ForwardOffset<MyGame_Example_Referrable>, MyGame_Example_Referrable>>.self)
try _v.visit(field: VTOFFSET.singleWeakReference.p, fieldName: "singleWeakReference", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.vectorOfWeakReferences.p, fieldName: "vectorOfWeakReferences", required: false, type: ForwardOffset<Vector<UInt64, UInt64>>.self)
try _v.visit(field: VTOFFSET.vectorOfStrongReferrables.p, fieldName: "vectorOfStrongReferrables", required: false, type: ForwardOffset<Vector<ForwardOffset<MyGame_Example_Referrable>, MyGame_Example_Referrable>>.self)
try _v.visit(field: VTOFFSET.coOwningReference.p, fieldName: "coOwningReference", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.vectorOfCoOwningReferences.p, fieldName: "vectorOfCoOwningReferences", required: false, type: ForwardOffset<Vector<UInt64, UInt64>>.self)
try _v.visit(field: VTOFFSET.nonOwningReference.p, fieldName: "nonOwningReference", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.vectorOfNonOwningReferences.p, fieldName: "vectorOfNonOwningReferences", required: false, type: ForwardOffset<Vector<UInt64, UInt64>>.self)
try _v.visit(unionKey: VTOFFSET.anyUniqueType.p, unionField: VTOFFSET.anyUnique.p, unionKeyName: "anyUniqueType", fieldName: "anyUnique", required: false, completion: { (verifier, key: MyGame_Example_AnyUniqueAliases, pos) in
switch key {
case .none_:
break // NOTE - SWIFT doesnt support none
case .m:
try ForwardOffset<MyGame_Example_Monster>.verify(&verifier, at: pos, of: MyGame_Example_Monster.self)
case .ts:
try ForwardOffset<MyGame_Example_TestSimpleTableWithEnum>.verify(&verifier, at: pos, of: MyGame_Example_TestSimpleTableWithEnum.self)
case .m2:
try ForwardOffset<MyGame_Example2_Monster>.verify(&verifier, at: pos, of: MyGame_Example2_Monster.self)
}
})
try _v.visit(unionKey: VTOFFSET.anyAmbiguousType.p, unionField: VTOFFSET.anyAmbiguous.p, unionKeyName: "anyAmbiguousType", fieldName: "anyAmbiguous", required: false, completion: { (verifier, key: MyGame_Example_AnyAmbiguousAliases, pos) in
switch key {
case .none_:
break // NOTE - SWIFT doesnt support none
case .m1:
try ForwardOffset<MyGame_Example_Monster>.verify(&verifier, at: pos, of: MyGame_Example_Monster.self)
case .m2:
try ForwardOffset<MyGame_Example_Monster>.verify(&verifier, at: pos, of: MyGame_Example_Monster.self)
case .m3:
try ForwardOffset<MyGame_Example_Monster>.verify(&verifier, at: pos, of: MyGame_Example_Monster.self)
}
})
try _v.visit(field: VTOFFSET.vectorOfEnums.p, fieldName: "vectorOfEnums", required: false, type: ForwardOffset<Vector<MyGame_Example_Color, MyGame_Example_Color>>.self)
try _v.visit(field: VTOFFSET.signedEnum.p, fieldName: "signedEnum", required: false, type: MyGame_Example_Race.self)
try _v.visit(field: VTOFFSET.testrequirednestedflatbuffer.p, fieldName: "testrequirednestedflatbuffer", required: false, type: ForwardOffset<Vector<UInt8, UInt8>>.self)
try _v.visit(field: VTOFFSET.scalarKeySortedTables.p, fieldName: "scalarKeySortedTables", required: false, type: ForwardOffset<Vector<ForwardOffset<MyGame_Example_Stat>, MyGame_Example_Stat>>.self)
_v.finish()
}
}
public class MyGame_Example_MonsterT: NativeObject {
@@ -1505,7 +1646,7 @@ public class MyGame_Example_MonsterT: NativeObject {
public func serialize() -> ByteBuffer { return serialize(type: MyGame_Example_Monster.self) }
}
public struct MyGame_Example_TypeAliases: FlatBufferObject, ObjectAPIPacker {
public struct MyGame_Example_TypeAliases: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -1634,6 +1775,23 @@ public struct MyGame_Example_TypeAliases: FlatBufferObject, ObjectAPIPacker {
MyGame_Example_TypeAliases.addVectorOf(vf64: __vf64, &builder)
return MyGame_Example_TypeAliases.endTypeAliases(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.i8.p, fieldName: "i8", required: false, type: Int8.self)
try _v.visit(field: VTOFFSET.u8.p, fieldName: "u8", required: false, type: UInt8.self)
try _v.visit(field: VTOFFSET.i16.p, fieldName: "i16", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.u16.p, fieldName: "u16", required: false, type: UInt16.self)
try _v.visit(field: VTOFFSET.i32.p, fieldName: "i32", required: false, type: Int32.self)
try _v.visit(field: VTOFFSET.u32.p, fieldName: "u32", required: false, type: UInt32.self)
try _v.visit(field: VTOFFSET.i64.p, fieldName: "i64", required: false, type: Int64.self)
try _v.visit(field: VTOFFSET.u64.p, fieldName: "u64", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.f32.p, fieldName: "f32", required: false, type: Float32.self)
try _v.visit(field: VTOFFSET.f64.p, fieldName: "f64", required: false, type: Double.self)
try _v.visit(field: VTOFFSET.v8.p, fieldName: "v8", required: false, type: ForwardOffset<Vector<Int8, Int8>>.self)
try _v.visit(field: VTOFFSET.vf64.p, fieldName: "vf64", required: false, type: ForwardOffset<Vector<Double, Double>>.self)
_v.finish()
}
}
public class MyGame_Example_TypeAliasesT: NativeObject {

View File

@@ -4,20 +4,19 @@
import FlatBuffers
public enum ABC: Int32, Enum {
public enum ABC: Int32, Enum, Verifiable {
public typealias T = Int32
public static var byteSize: Int { return MemoryLayout<Int32>.size }
public var value: Int32 { return self.rawValue }
case a = 0
case b = 1
case c = 2
public static var max: ABC { return .c }
public static var min: ABC { return .a }
}
public struct MoreDefaults: FlatBufferObject, ObjectAPIPacker {
public struct MoreDefaults: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -118,6 +117,17 @@ public struct MoreDefaults: FlatBufferObject, ObjectAPIPacker {
MoreDefaults.addVectorOf(bools: __bools, &builder)
return MoreDefaults.endMoreDefaults(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.ints.p, fieldName: "ints", required: false, type: ForwardOffset<Vector<Int32, Int32>>.self)
try _v.visit(field: VTOFFSET.floats.p, fieldName: "floats", required: false, type: ForwardOffset<Vector<Float32, Float32>>.self)
try _v.visit(field: VTOFFSET.emptyString.p, fieldName: "emptyString", required: false, type: ForwardOffset<String>.self)
try _v.visit(field: VTOFFSET.someString.p, fieldName: "someString", required: false, type: ForwardOffset<String>.self)
try _v.visit(field: VTOFFSET.abcs.p, fieldName: "abcs", required: false, type: ForwardOffset<Vector<ABC, ABC>>.self)
try _v.visit(field: VTOFFSET.bools.p, fieldName: "bools", required: false, type: ForwardOffset<Vector<Bool, Bool>>.self)
_v.finish()
}
}
public class MoreDefaultsT: NativeObject {

View File

@@ -4,20 +4,19 @@
import FlatBuffers
public enum optional_scalars_OptionalByte: Int8, Enum {
public enum optional_scalars_OptionalByte: Int8, Enum, Verifiable {
public typealias T = Int8
public static var byteSize: Int { return MemoryLayout<Int8>.size }
public var value: Int8 { return self.rawValue }
case none_ = 0
case one = 1
case two = 2
public static var max: optional_scalars_OptionalByte { return .two }
public static var min: optional_scalars_OptionalByte { return .none_ }
}
public struct optional_scalars_ScalarStuff: FlatBufferObject {
public struct optional_scalars_ScalarStuff: FlatBufferObject, Verifiable {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -224,5 +223,46 @@ public struct optional_scalars_ScalarStuff: FlatBufferObject {
optional_scalars_ScalarStuff.add(defaultEnum: defaultEnum, &fbb)
return optional_scalars_ScalarStuff.endScalarStuff(&fbb, start: __start)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.justI8.p, fieldName: "justI8", required: false, type: Int8.self)
try _v.visit(field: VTOFFSET.maybeI8.p, fieldName: "maybeI8", required: false, type: Int8.self)
try _v.visit(field: VTOFFSET.defaultI8.p, fieldName: "defaultI8", required: false, type: Int8.self)
try _v.visit(field: VTOFFSET.justU8.p, fieldName: "justU8", required: false, type: UInt8.self)
try _v.visit(field: VTOFFSET.maybeU8.p, fieldName: "maybeU8", required: false, type: UInt8.self)
try _v.visit(field: VTOFFSET.defaultU8.p, fieldName: "defaultU8", required: false, type: UInt8.self)
try _v.visit(field: VTOFFSET.justI16.p, fieldName: "justI16", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.maybeI16.p, fieldName: "maybeI16", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.defaultI16.p, fieldName: "defaultI16", required: false, type: Int16.self)
try _v.visit(field: VTOFFSET.justU16.p, fieldName: "justU16", required: false, type: UInt16.self)
try _v.visit(field: VTOFFSET.maybeU16.p, fieldName: "maybeU16", required: false, type: UInt16.self)
try _v.visit(field: VTOFFSET.defaultU16.p, fieldName: "defaultU16", required: false, type: UInt16.self)
try _v.visit(field: VTOFFSET.justI32.p, fieldName: "justI32", required: false, type: Int32.self)
try _v.visit(field: VTOFFSET.maybeI32.p, fieldName: "maybeI32", required: false, type: Int32.self)
try _v.visit(field: VTOFFSET.defaultI32.p, fieldName: "defaultI32", required: false, type: Int32.self)
try _v.visit(field: VTOFFSET.justU32.p, fieldName: "justU32", required: false, type: UInt32.self)
try _v.visit(field: VTOFFSET.maybeU32.p, fieldName: "maybeU32", required: false, type: UInt32.self)
try _v.visit(field: VTOFFSET.defaultU32.p, fieldName: "defaultU32", required: false, type: UInt32.self)
try _v.visit(field: VTOFFSET.justI64.p, fieldName: "justI64", required: false, type: Int64.self)
try _v.visit(field: VTOFFSET.maybeI64.p, fieldName: "maybeI64", required: false, type: Int64.self)
try _v.visit(field: VTOFFSET.defaultI64.p, fieldName: "defaultI64", required: false, type: Int64.self)
try _v.visit(field: VTOFFSET.justU64.p, fieldName: "justU64", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.maybeU64.p, fieldName: "maybeU64", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.defaultU64.p, fieldName: "defaultU64", required: false, type: UInt64.self)
try _v.visit(field: VTOFFSET.justF32.p, fieldName: "justF32", required: false, type: Float32.self)
try _v.visit(field: VTOFFSET.maybeF32.p, fieldName: "maybeF32", required: false, type: Float32.self)
try _v.visit(field: VTOFFSET.defaultF32.p, fieldName: "defaultF32", required: false, type: Float32.self)
try _v.visit(field: VTOFFSET.justF64.p, fieldName: "justF64", required: false, type: Double.self)
try _v.visit(field: VTOFFSET.maybeF64.p, fieldName: "maybeF64", required: false, type: Double.self)
try _v.visit(field: VTOFFSET.defaultF64.p, fieldName: "defaultF64", required: false, type: Double.self)
try _v.visit(field: VTOFFSET.justBool.p, fieldName: "justBool", required: false, type: Bool.self)
try _v.visit(field: VTOFFSET.maybeBool.p, fieldName: "maybeBool", required: false, type: Bool.self)
try _v.visit(field: VTOFFSET.defaultBool.p, fieldName: "defaultBool", required: false, type: Bool.self)
try _v.visit(field: VTOFFSET.justEnum.p, fieldName: "justEnum", required: false, type: optional_scalars_OptionalByte.self)
try _v.visit(field: VTOFFSET.maybeEnum.p, fieldName: "maybeEnum", required: false, type: optional_scalars_OptionalByte.self)
try _v.visit(field: VTOFFSET.defaultEnum.p, fieldName: "defaultEnum", required: false, type: optional_scalars_OptionalByte.self)
_v.finish()
}
}

View File

@@ -4,8 +4,13 @@
import FlatBuffers
public enum Character: UInt8, Enum {
public enum Character: UInt8, UnionEnum {
public typealias T = UInt8
public init?(value: T) {
self.init(rawValue: value)
}
public static var byteSize: Int { return MemoryLayout<UInt8>.size }
public var value: UInt8 { return self.rawValue }
case none_ = 0
@@ -15,7 +20,6 @@ public enum Character: UInt8, Enum {
case bookfan = 4
case other = 5
case unused = 6
public static var max: Character { return .unused }
public static var min: Character { return .none_ }
@@ -52,7 +56,7 @@ public struct CharacterUnion {
}
}
}
public struct Rapunzel: NativeStruct, NativeObject {
public struct Rapunzel: NativeStruct, Verifiable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -71,6 +75,10 @@ public struct Rapunzel: NativeStruct, NativeObject {
}
public var hairLength: Int32 { _hairLength }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: Rapunzel.self)
}
}
public struct Rapunzel_Mutable: FlatBufferObject {
@@ -98,7 +106,7 @@ public struct Rapunzel_Mutable: FlatBufferObject {
}
}
public struct BookReader: NativeStruct, NativeObject {
public struct BookReader: NativeStruct, Verifiable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -117,6 +125,10 @@ public struct BookReader: NativeStruct, NativeObject {
}
public var booksRead: Int32 { _booksRead }
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
try verifier.inBuffer(position: position, of: BookReader.self)
}
}
public struct BookReader_Mutable: FlatBufferObject {
@@ -144,7 +156,7 @@ public struct BookReader_Mutable: FlatBufferObject {
}
}
public struct Attacker: FlatBufferObject, ObjectAPIPacker {
public struct Attacker: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -190,6 +202,12 @@ public struct Attacker: FlatBufferObject, ObjectAPIPacker {
Attacker.add(swordAttackDamage: obj.swordAttackDamage, &builder)
return Attacker.endAttacker(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(field: VTOFFSET.swordAttackDamage.p, fieldName: "swordAttackDamage", required: false, type: Int32.self)
_v.finish()
}
}
public class AttackerT: NativeObject {
@@ -207,7 +225,7 @@ public class AttackerT: NativeObject {
public func serialize() -> ByteBuffer { return serialize(type: Attacker.self) }
}
public struct Movie: FlatBufferObject, ObjectAPIPacker {
public struct Movie: FlatBufferObject, Verifiable, ObjectAPIPacker {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
public var __buffer: ByteBuffer! { return _accessor.bb }
@@ -283,6 +301,47 @@ public struct Movie: FlatBufferObject, ObjectAPIPacker {
Movie.addVectorOf(characters: __characters, &builder)
return Movie.endMovie(&builder, start: __root)
}
public static func verify<T>(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable {
var _v = try verifier.visitTable(at: position)
try _v.visit(unionKey: VTOFFSET.mainCharacterType.p, unionField: VTOFFSET.mainCharacter.p, unionKeyName: "mainCharacterType", fieldName: "mainCharacter", required: false, completion: { (verifier, key: Character, pos) in
switch key {
case .none_:
break // NOTE - SWIFT doesnt support none
case .mulan:
try ForwardOffset<Attacker>.verify(&verifier, at: pos, of: Attacker.self)
case .rapunzel:
try Rapunzel.verify(&verifier, at: pos, of: Rapunzel.self)
case .belle:
try BookReader.verify(&verifier, at: pos, of: BookReader.self)
case .bookfan:
try BookReader.verify(&verifier, at: pos, of: BookReader.self)
case .other:
try ForwardOffset<String>.verify(&verifier, at: pos, of: String.self)
case .unused:
try ForwardOffset<String>.verify(&verifier, at: pos, of: String.self)
}
})
try _v.visitUnionVector(unionKey: VTOFFSET.charactersType.p, unionField: VTOFFSET.characters.p, unionKeyName: "charactersType", fieldName: "characters", required: false, completion: { (verifier, key: Character, pos) in
switch key {
case .none_:
break // NOTE - SWIFT doesnt support none
case .mulan:
try ForwardOffset<Attacker>.verify(&verifier, at: pos, of: Attacker.self)
case .rapunzel:
try Rapunzel.verify(&verifier, at: pos, of: Rapunzel.self)
case .belle:
try BookReader.verify(&verifier, at: pos, of: BookReader.self)
case .bookfan:
try BookReader.verify(&verifier, at: pos, of: BookReader.self)
case .other:
try ForwardOffset<String>.verify(&verifier, at: pos, of: String.self)
case .unused:
try ForwardOffset<String>.verify(&verifier, at: pos, of: String.self)
}
})
_v.finish()
}
}
public class MovieT: NativeObject {