Adds JSON encoding to swift (#6874)

Updates generated code & removes unneeded protocol

Updates cpp to only generate code when flag is passed

Updates code gen script
This commit is contained in:
mustiikhalil
2021-10-07 23:22:22 +02:00
committed by GitHub
parent 38295a1873
commit 4c7a9c10d3
17 changed files with 1324 additions and 20 deletions

View File

@@ -25,6 +25,21 @@ public enum Character: UInt8, UnionEnum {
public static var min: Character { return .none_ }
}
extension Character: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .none_: try container.encode("NONE")
case .mulan: try container.encode("MuLan")
case .rapunzel: try container.encode("Rapunzel")
case .belle: try container.encode("Belle")
case .bookfan: try container.encode("BookFan")
case .other: try container.encode("Other")
case .unused: try container.encode("Unused")
}
}
}
public struct CharacterUnion {
public var type: Character
public var value: NativeObject?
@@ -56,12 +71,17 @@ public struct CharacterUnion {
}
}
}
public struct Rapunzel: NativeStruct, Verifiable, NativeObject {
public struct Rapunzel: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
private var _hairLength: Int32
public init(_ bb: ByteBuffer, o: Int32) {
let _accessor = Struct(bb: bb, position: o)
_hairLength = _accessor.readBuffer(of: Int32.self, at: 0)
}
public init(hairLength: Int32) {
_hairLength = hairLength
}
@@ -81,6 +101,19 @@ public struct Rapunzel: NativeStruct, Verifiable, NativeObject {
}
}
extension Rapunzel: Encodable {
enum CodingKeys: String, CodingKey {
case hairLength = "hair_length"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if hairLength != 0 {
try container.encodeIfPresent(hairLength, forKey: .hairLength)
}
}
}
public struct Rapunzel_Mutable: FlatBufferObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -106,12 +139,17 @@ public struct Rapunzel_Mutable: FlatBufferObject {
}
}
public struct BookReader: NativeStruct, Verifiable, NativeObject {
public struct BookReader: NativeStruct, Verifiable, FlatbuffersInitializable, NativeObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
private var _booksRead: Int32
public init(_ bb: ByteBuffer, o: Int32) {
let _accessor = Struct(bb: bb, position: o)
_booksRead = _accessor.readBuffer(of: Int32.self, at: 0)
}
public init(booksRead: Int32) {
_booksRead = booksRead
}
@@ -131,6 +169,19 @@ public struct BookReader: NativeStruct, Verifiable, NativeObject {
}
}
extension BookReader: Encodable {
enum CodingKeys: String, CodingKey {
case booksRead = "books_read"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if booksRead != 0 {
try container.encodeIfPresent(booksRead, forKey: .booksRead)
}
}
}
public struct BookReader_Mutable: FlatBufferObject {
static func validateVersion() { FlatBuffersVersion_2_0_0() }
@@ -210,6 +261,19 @@ public struct Attacker: FlatBufferObject, Verifiable, ObjectAPIPacker {
}
}
extension Attacker: Encodable {
enum CodingKeys: String, CodingKey {
case swordAttackDamage = "sword_attack_damage"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if swordAttackDamage != 0 {
try container.encodeIfPresent(swordAttackDamage, forKey: .swordAttackDamage)
}
}
}
public class AttackerT: NativeObject {
public var swordAttackDamage: Int32
@@ -344,6 +408,72 @@ public struct Movie: FlatBufferObject, Verifiable, ObjectAPIPacker {
}
}
extension Movie: Encodable {
enum CodingKeys: String, CodingKey {
case mainCharacterType = "main_character_type"
case mainCharacter = "main_character"
case charactersType = "characters_type"
case characters = "characters"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if mainCharacterType != .none_ {
try container.encodeIfPresent(mainCharacterType, forKey: .mainCharacterType)
}
switch mainCharacterType {
case .mulan:
let _v = mainCharacter(type: Attacker.self)
try container.encodeIfPresent(_v, forKey: .mainCharacter)
case .rapunzel:
let _v = mainCharacter(type: Rapunzel.self)
try container.encodeIfPresent(_v, forKey: .mainCharacter)
case .belle:
let _v = mainCharacter(type: BookReader.self)
try container.encodeIfPresent(_v, forKey: .mainCharacter)
case .bookfan:
let _v = mainCharacter(type: BookReader.self)
try container.encodeIfPresent(_v, forKey: .mainCharacter)
case .other:
let _v = mainCharacter(type: String.self)
try container.encodeIfPresent(_v, forKey: .mainCharacter)
case .unused:
let _v = mainCharacter(type: String.self)
try container.encodeIfPresent(_v, forKey: .mainCharacter)
default: break;
}
if charactersCount > 0 {
var enumsEncoder = container.nestedUnkeyedContainer(forKey: .charactersType)
var contentEncoder = container.nestedUnkeyedContainer(forKey: .characters)
for index in 0..<charactersCount {
guard let type = charactersType(at: index) else { continue }
try enumsEncoder.encode(type)
switch type {
case .mulan:
let _v = characters(at: index, type: Attacker.self)
try contentEncoder.encode(_v)
case .rapunzel:
let _v = characters(at: index, type: Rapunzel.self)
try contentEncoder.encode(_v)
case .belle:
let _v = characters(at: index, type: BookReader.self)
try contentEncoder.encode(_v)
case .bookfan:
let _v = characters(at: index, type: BookReader.self)
try contentEncoder.encode(_v)
case .other:
let _v = characters(at: index, type: String.self)
try contentEncoder.encode(_v)
case .unused:
let _v = characters(at: index, type: String.self)
try contentEncoder.encode(_v)
default: break;
}
}
}
}
}
public class MovieT: NativeObject {
public var mainCharacter: CharacterUnion?