Skip to content

Commit

Permalink
Removes Dead code & regenerate code (google#7744)
Browse files Browse the repository at this point in the history
Formats the swift project

Update Sample files

Update docc documentation

Updates swift docs in the website

Updates code for Wasm
  • Loading branch information
mustiikhalil authored and Wen Sun committed Jan 9, 2023
1 parent b4ad256 commit 9a62ae0
Show file tree
Hide file tree
Showing 61 changed files with 277 additions and 243 deletions.
2 changes: 1 addition & 1 deletion Package@swift-5.5.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ let package = Package(
.target(
name: "FlatBuffers",
dependencies: [],
path: "swift/Sources")
path: "swift/Sources"),
])

5 changes: 4 additions & 1 deletion docs/source/SwiftUsage.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ Now you can access values like this:
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.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.swift}
let monster = Monster.getRootAsMonster(bb: ByteBuffer(data: data))
var byteBuffer = ByteBuffer(bytes: data)
// Get an accessor to the root object inside the buffer.
let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
// let monster: Monster = getRoot(byteBuffer: &byteBuffer)
if !monster.mutate(hp: 10) {
fatalError("couldn't mutate")
Expand Down
6 changes: 3 additions & 3 deletions docs/source/Tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2472,10 +2472,10 @@ myGame.Monster monster = new myGame.Monster(data);
<div class="language-swift">
~~~{.swift}
// create a ByteBuffer(:) from an [UInt8] or Data()
let buf = // Get your data
var buf = // Get your data
// Get an accessor to the root object inside the buffer.
let monster = Monster.getRootAsMonster(bb: ByteBuffer(bytes: buf))
let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
// let monster: Monster = getRoot(byteBuffer: &byteBuffer)
~~~
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public struct models_HelloReply: FlatBufferObject, Verifiable {
public var __buffer: ByteBuffer! { return _accessor.bb }
private var _accessor: Table

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))) }

private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }

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

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))) }

private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }

Expand Down
2 changes: 1 addition & 1 deletion grpc/examples/swift/Greeter/Sources/client/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down
2 changes: 1 addition & 1 deletion grpc/examples/swift/Greeter/Sources/server/main.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down
65 changes: 54 additions & 11 deletions samples/monster_generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,51 @@

import FlatBuffers

public enum MyGame_Sample_Color: Int8, Enum {
public enum MyGame_Sample_Color: Int8, Enum, Verifiable {
public typealias T = Int8
public static var byteSize: Int { return MemoryLayout<Int8>.size }
public var value: Int8 { return self.rawValue }
case red = 0
case green = 1
case blue = 2


public static var max: MyGame_Sample_Color { return .blue }
public static var min: MyGame_Sample_Color { return .red }
}

public enum MyGame_Sample_Equipment: UInt8, Enum {

public enum MyGame_Sample_Equipment: 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 weapon = 1


public static var max: MyGame_Sample_Equipment { return .weapon }
public static var min: MyGame_Sample_Equipment { return .none_ }
}

public struct MyGame_Sample_Vec3: NativeStruct {

public struct MyGame_Sample_Vec3: NativeStruct, Verifiable, FlatbuffersInitializable {

static func validateVersion() { FlatBuffersVersion_23_1_4() }

private var _x: Float32
private var _y: Float32
private var _z: Float32

public init(_ bb: ByteBuffer, o: Int32) {
let _accessor = Struct(bb: bb, position: o)
_x = _accessor.readBuffer(of: Float32.self, at: 0)
_y = _accessor.readBuffer(of: Float32.self, at: 4)
_z = _accessor.readBuffer(of: Float32.self, at: 8)
}

public init(x: Float32, y: Float32, z: Float32) {
_x = x
_y = y
Expand All @@ -52,6 +64,10 @@ public struct MyGame_Sample_Vec3: NativeStruct {
public var x: Float32 { _x }
public var y: Float32 { _y }
public var z: Float32 { _z }

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_Sample_Vec3.self)
}
}

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

public struct MyGame_Sample_Monster: FlatBufferObject {
public struct MyGame_Sample_Monster: FlatBufferObject, Verifiable {

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

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))) }

private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }

Expand All @@ -104,16 +118,19 @@ public struct MyGame_Sample_Monster: FlatBufferObject {
@discardableResult public func mutate(hp: Int16) -> Bool {let o = _accessor.offset(VTOFFSET.hp.v); return _accessor.mutate(hp, index: o) }
public var name: String? { let o = _accessor.offset(VTOFFSET.name.v); return o == 0 ? nil : _accessor.string(at: o) }
public var nameSegmentArray: [UInt8]? { return _accessor.getVector(at: VTOFFSET.name.v) }
public var hasInventory: Bool { let o = _accessor.offset(VTOFFSET.inventory.v); return o == 0 ? false : true }
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 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) }
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 }
@discardableResult public func mutate(color: MyGame_Sample_Color) -> Bool {let o = _accessor.offset(VTOFFSET.color.v); return _accessor.mutate(color.rawValue, index: o) }
public var hasWeapons: Bool { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? false : true }
public var weaponsCount: Int32 { let o = _accessor.offset(VTOFFSET.weapons.v); return o == 0 ? 0 : _accessor.vector(count: o) }
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)) }
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_ }
public func equipped<T: FlatbuffersInitializable>(type: T.Type) -> T? { let o = _accessor.offset(VTOFFSET.equipped.v); return o == 0 ? nil : _accessor.union(o) }
public var hasPath: Bool { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? false : true }
public var pathCount: Int32 { let o = _accessor.offset(VTOFFSET.path.v); return o == 0 ? 0 : _accessor.vector(count: o) }
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) }
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) }
Expand Down Expand Up @@ -158,16 +175,35 @@ public struct MyGame_Sample_Monster: FlatBufferObject {
MyGame_Sample_Monster.addVectorOf(path: path, &fbb)
return MyGame_Sample_Monster.endMonster(&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.pos.p, fieldName: "pos", required: false, type: MyGame_Sample_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: false, 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_Sample_Color.self)
try _v.visit(field: VTOFFSET.weapons.p, fieldName: "weapons", required: false, type: ForwardOffset<Vector<ForwardOffset<MyGame_Sample_Weapon>, MyGame_Sample_Weapon>>.self)
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
switch key {
case .none_:
break // NOTE - SWIFT doesnt support none
case .weapon:
try ForwardOffset<MyGame_Sample_Weapon>.verify(&verifier, at: pos, of: MyGame_Sample_Weapon.self)
}
})
try _v.visit(field: VTOFFSET.path.p, fieldName: "path", required: false, type: ForwardOffset<Vector<MyGame_Sample_Vec3, MyGame_Sample_Vec3>>.self)
_v.finish()
}
}

public struct MyGame_Sample_Weapon: FlatBufferObject {
public struct MyGame_Sample_Weapon: FlatBufferObject, Verifiable {

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

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))) }

private init(_ t: Table) { _accessor = t }
public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) }

Expand Down Expand Up @@ -196,5 +232,12 @@ public struct MyGame_Sample_Weapon: FlatBufferObject {
MyGame_Sample_Weapon.add(damage: damage, &fbb)
return MyGame_Sample_Weapon.endWeapon(&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.name.p, fieldName: "name", required: false, type: ForwardOffset<String>.self)
try _v.visit(field: VTOFFSET.damage.p, fieldName: "damage", required: false, type: Int16.self)
_v.finish()
}
}

6 changes: 3 additions & 3 deletions samples/sample_binary.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down Expand Up @@ -56,8 +56,8 @@ func main() {
equippedOffset: axe)
builder.finish(offset: orc)

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

assert(monster.mana == 150)
assert(monster.hp == 300)
Expand Down
9 changes: 9 additions & 0 deletions scripts/generate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,15 @@ def glob(path, pattern):
cwd=swift_code_gen
)

# Swift Wasm Tests
swift_Wasm_prefix = "swift/Wasm.tests/Tests/FlatBuffers.Test.Swift.WasmTests"
flatc(
SWIFT_OPTS + BASE_OPTS,
schema="monster_test.fbs",
include="include_test",
prefix=swift_Wasm_prefix,
)

# Nim Tests
NIM_OPTS = BASE_OPTS + ["--nim"]
flatc(NIM_OPTS, schema="monster_test.fbs", include="include_test")
Expand Down
6 changes: 0 additions & 6 deletions src/idl_gen_swift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,6 @@ class SwiftGenerator : public BaseGenerator {
"fileId: "
"{{STRUCTNAME}}.id, addPrefix: prefix) }";
}
code_ +=
"{{ACCESS_TYPE}} static func getRootAs{{SHORT_STRUCTNAME}}(bb: "
"ByteBuffer) -> "
"{{STRUCTNAME}} { return {{STRUCTNAME}}(Table(bb: bb, position: "
"Int32(bb.read(def: UOffset.self, position: bb.reader)) + "
"Int32(bb.reader))) }\n";
code_ += "private init(_ t: Table) { {{ACCESS}} = t }";
}
code_ +=
Expand Down
2 changes: 1 addition & 1 deletion swift.swiftformat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
--swiftversion 5.1
--swiftversion 5.7

# format
--indent 2
Expand Down
14 changes: 7 additions & 7 deletions swift/Sources/FlatBuffers/ByteBuffer.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down Expand Up @@ -429,13 +429,13 @@ public struct ByteBuffer {
}

/// Returns the written bytes into the ``ByteBuffer``
public var underlyingBytes: [UInt8] {
let cp = capacity &- writerIndex
let start = memory.advanced(by: writerIndex)
.bindMemory(to: UInt8.self, capacity: cp)
public var underlyingBytes: [UInt8] {
let cp = capacity &- writerIndex
let start = memory.advanced(by: writerIndex)
.bindMemory(to: UInt8.self, capacity: cp)

let ptr = UnsafeBufferPointer<UInt8>(start: start, count: cp)
return Array(ptr)
let ptr = UnsafeBufferPointer<UInt8>(start: start, count: cp)
return Array(ptr)
}

/// SkipPrefix Skips the first 4 bytes in case one of the following
Expand Down
12 changes: 6 additions & 6 deletions swift/Sources/FlatBuffers/Constants.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand All @@ -15,11 +15,11 @@
*/

#if !os(WASI)
#if os(Linux)
import CoreFoundation
#else
import Foundation
#endif
#if os(Linux)
import CoreFoundation
#else
import Foundation
#endif
#else
import SwiftOverlayShims
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Foundation
func run() {
// create a ByteBuffer(:) from an [UInt8] or Data()
let buf = [] // Get your data

var byteBuffer = ByteBuffer(bytes: buf)
// Get an accessor to the root object inside the buffer.
let monster: Monster = try! getCheckedRoot(byteBuffer: ByteBuffer(bytes: buf))
// let monster: Monster = getRoot(byteBuffer: ByteBuffer(bytes: buf))
let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
// let monster: Monster = getRoot(byteBuffer: &byteBuffer)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import Foundation
func run() {
// create a ByteBuffer(:) from an [UInt8] or Data()
let buf = [] // Get your data

var byteBuffer = ByteBuffer(bytes: buf)
// Get an accessor to the root object inside the buffer.
let monster: Monster = try! getCheckedRoot(byteBuffer: ByteBuffer(bytes: buf))
// let monster: Monster = getRoot(byteBuffer: ByteBuffer(bytes: buf))
let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
// let monster: Monster = getRoot(byteBuffer: &byteBuffer)

let hp = monster.hp
let mana = monster.mana
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import Foundation
func run() {
// create a ByteBuffer(:) from an [UInt8] or Data()
let buf = [] // Get your data

var byteBuffer = ByteBuffer(bytes: buf)
// Get an accessor to the root object inside the buffer.
let monster: Monster = try! getCheckedRoot(byteBuffer: ByteBuffer(bytes: buf))
// let monster: Monster = getRoot(byteBuffer: ByteBuffer(bytes: buf))
let monster: Monster = try! getCheckedRoot(byteBuffer: &byteBuffer)
// let monster: Monster = getRoot(byteBuffer: &byteBuffer)

let hp = monster.hp
let mana = monster.mana
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/Enum.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/FlatBufferBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/FlatBufferObject.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down
2 changes: 1 addition & 1 deletion swift/Sources/FlatBuffers/FlatBuffersUtils.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google Inc. All rights reserved.
* Copyright 2023 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.
Expand Down
Loading

0 comments on commit 9a62ae0

Please sign in to comment.