diff --git a/scripts/generate_code.py b/scripts/generate_code.py index 5420ed9e0..5b3c225ef 100755 --- a/scripts/generate_code.py +++ b/scripts/generate_code.py @@ -354,6 +354,11 @@ flatc( schema="more_defaults.fbs", prefix=swift_prefix, ) +flatc( + SWIFT_OPTS + BASE_OPTS, + schema="MutatingBool.fbs", + prefix=swift_prefix, +) # --filename-suffix and --filename-ext tests flatc( diff --git a/src/idl_gen_swift.cpp b/src/idl_gen_swift.cpp index 262041507..b053a70ba 100644 --- a/src/idl_gen_swift.cpp +++ b/src/idl_gen_swift.cpp @@ -220,8 +220,10 @@ class SwiftGenerator : public BaseGenerator { IsEnum(field.value.type) ? "{{BASEVALUE}}" : "{{VALUETYPE}}"; code_ += "private var _{{VALUENAME}}: " + valueType; auto accessing_value = IsEnum(field.value.type) ? ".value" : ""; - auto base_value = - IsStruct(field.value.type) ? (type + "()") : field.value.constant; + auto is_bool = IsBool(field.value.type.base_type); + auto base_value = IsStruct(field.value.type) ? (type + "()") + : is_bool ? ("0" == field.value.constant ? "false" : "true") + : field.value.constant; main_constructor.push_back("_" + name + " = " + name + accessing_value); base_constructor.push_back("_" + name + " = " + base_value); diff --git a/tests/FlatBuffers.Test.Swift/SwiftTest.sh b/tests/FlatBuffers.Test.Swift/SwiftTest.sh index 6972c562c..99ab1f1be 100755 --- a/tests/FlatBuffers.Test.Swift/SwiftTest.sh +++ b/tests/FlatBuffers.Test.Swift/SwiftTest.sh @@ -8,6 +8,7 @@ cd ${swift_dir}/Tests/FlatBuffers.Test.SwiftTests fbc --swift --gen-mutable --grpc --gen-json-emit --gen-object-api -I ${test_dir}/include_test ${test_dir}/monster_test.fbs ${test_dir}/union_vector/union_vector.fbs fbc --swift --gen-json-emit ${test_dir}/optional_scalars.fbs fbc --swift --gen-json-emit --gen-object-api ${test_dir}/more_defaults.fbs +fbc --swift --gen-json-emit --gen-mutable --gen-object-api ${test_dir}/MutatingBool.fbs cd ${swift_dir} cd ${swift_dir}/Sources/SwiftFlatBuffers diff --git a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift index 8b011fcb2..155080a7d 100644 --- a/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift +++ b/tests/FlatBuffers.Test.Swift/Tests/FlatBuffers.Test.SwiftTests/MutatingBool_generated.swift @@ -1,49 +1,153 @@ // automatically generated by the FlatBuffers compiler, do not modify +// swiftlint:disable all +// swiftformat:disable all import FlatBuffers -public struct Property: NativeStruct { +public struct Property: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject { static func validateVersion() { FlatBuffersVersion_2_0_0() } - public var property: Bool + private var _property: Bool + + public init(_ bb: ByteBuffer, o: Int32) { + let _accessor = Struct(bb: bb, position: o) + _property = _accessor.readBuffer(of: Bool.self, at: 0) + } + + public init(property: Bool) { + _property = property + } + + public init() { + _property = false + } + + public init(_ _t: inout Property_Mutable) { + _property = _t.property + } + + public var property: Bool { _property } + + public static func verify(_ verifier: inout Verifier, at position: Int, of type: T.Type) throws where T: Verifiable { + try verifier.inBuffer(position: position, of: Property.self) + } +} + +extension Property: Encodable { + + enum CodingKeys: String, CodingKey { + case property = "property" + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + if property != false { + try container.encodeIfPresent(property, forKey: .property) + } + } } public struct Property_Mutable: FlatBufferObject { - static func validateVersion() { FlatBuffersVersion_2_0_0() } - public var __buffer: ByteBuffer! { return _accessor.bb } - private var _accessor: Struct + static func validateVersion() { FlatBuffersVersion_2_0_0() } + public var __buffer: ByteBuffer! { return _accessor.bb } + private var _accessor: Struct - public static var size = 1 - public static var alignment = 1 - public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } + public init(_ bb: ByteBuffer, o: Int32) { _accessor = Struct(bb: bb, position: o) } - public var property: Bool { return _accessor.readBuffer(of: Bool.self, at: 0) } - @discardableResult public func mutate(property: Bool) -> Bool { return _accessor.mutate(property, index: 0) } + public var property: Bool { return _accessor.readBuffer(of: Bool.self, at: 0) } + @discardableResult public func mutate(property: Bool) -> Bool { return _accessor.mutate(property, index: 0) } + + + public mutating func unpack() -> Property { + return Property(&self) + } + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout Property?) -> Offset { + guard var obj = obj else { return Offset() } + return pack(&builder, obj: &obj) + } + + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout Property) -> Offset { + return builder.create(struct: obj) + } } -public struct TestMutatingBool: FlatBufferObject { +public struct TestMutatingBool: FlatBufferObject, Verifiable, ObjectAPIPacker { - static func validateVersion() { FlatBuffersVersion_2_0_0() } - public var __buffer: ByteBuffer! { return _accessor.bb } - private var _accessor: Table + static func validateVersion() { FlatBuffersVersion_2_0_0() } + public var __buffer: ByteBuffer! { return _accessor.bb } + private var _accessor: Table - public static func getRootAsTestMutatingBool(bb: ByteBuffer) -> TestMutatingBool { return TestMutatingBool(Table(bb: bb, position: Int32(bb.read(def: UOffset.self, position: bb.reader)) + Int32(bb.reader))) } + public static func getRootAsTestMutatingBool(bb: ByteBuffer) -> TestMutatingBool { return TestMutatingBool(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 init(_ t: Table) { _accessor = t } + public init(_ bb: ByteBuffer, o: Int32) { _accessor = Table(bb: bb, position: o) } - enum VTOFFSET: VOffset { - case b = 4 - var v: Int32 { Int32(self.rawValue) } - var p: VOffset { self.rawValue } - } + private enum VTOFFSET: VOffset { + case b = 4 + var v: Int32 { Int32(self.rawValue) } + var p: VOffset { self.rawValue } + } - public var b: Property? { let o = _accessor.offset(VTOFFSET.b.v); return o == 0 ? nil : _accessor.readBuffer(of: Property.self, at: o) } - public var mutableB: Property_Mutable? { let o = _accessor.offset(VTOFFSET.b.v); return o == 0 ? nil : Property_Mutable(_accessor.bb, o: o + _accessor.postion) } - public static func startTestMutatingBool(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) } - public static func add(b: Property?, _ fbb: inout FlatBufferBuilder) { guard let b = b else { return }; _ = fbb.create(struct: b, position: VTOFFSET.b.p) } - public static func endTestMutatingBool(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } + public var b: Property? { let o = _accessor.offset(VTOFFSET.b.v); return o == 0 ? nil : _accessor.readBuffer(of: Property.self, at: o) } + public var mutableB: Property_Mutable? { let o = _accessor.offset(VTOFFSET.b.v); return o == 0 ? nil : Property_Mutable(_accessor.bb, o: o + _accessor.postion) } + public static func startTestMutatingBool(_ fbb: inout FlatBufferBuilder) -> UOffset { fbb.startTable(with: 1) } + public static func add(b: Property?, _ fbb: inout FlatBufferBuilder) { guard let b = b else { return }; fbb.create(struct: b, position: VTOFFSET.b.p) } + public static func endTestMutatingBool(_ fbb: inout FlatBufferBuilder, start: UOffset) -> Offset { let end = Offset(offset: fbb.endTable(at: start)); return end } + public static func createTestMutatingBool( + _ fbb: inout FlatBufferBuilder, + b: Property? = nil + ) -> Offset { + let __start = TestMutatingBool.startTestMutatingBool(&fbb) + TestMutatingBool.add(b: b, &fbb) + return TestMutatingBool.endTestMutatingBool(&fbb, start: __start) + } + + + public mutating func unpack() -> TestMutatingBoolT { + return TestMutatingBoolT(&self) + } + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TestMutatingBoolT?) -> Offset { + guard var obj = obj else { return Offset() } + return pack(&builder, obj: &obj) + } + + public static func pack(_ builder: inout FlatBufferBuilder, obj: inout TestMutatingBoolT) -> Offset { + let __root = TestMutatingBool.startTestMutatingBool(&builder) + TestMutatingBool.add(b: obj.b, &builder) + return TestMutatingBool.endTestMutatingBool(&builder, start: __root) + } + + public static func verify(_ 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.b.p, fieldName: "b", required: false, type: Property.self) + _v.finish() + } } +extension TestMutatingBool: Encodable { + + enum CodingKeys: String, CodingKey { + case b = "b" + } + public func encode(to encoder: Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encodeIfPresent(b, forKey: .b) + } +} + +public class TestMutatingBoolT: NativeObject { + + public var b: Property? + + public init(_ _t: inout TestMutatingBool) { + b = _t.b + } + + public init() { + b = Property() + } + + public func serialize() -> ByteBuffer { return serialize(type: TestMutatingBool.self) } + +} diff --git a/tests/MutatingBool.fbs b/tests/MutatingBool.fbs new file mode 100644 index 000000000..961db6edb --- /dev/null +++ b/tests/MutatingBool.fbs @@ -0,0 +1,8 @@ + +struct Property { + property: bool; +} + +table TestMutatingBool { + b: Property; +}