Skip to content

Instantly share code, notes, and snippets.

@dskvr
Last active February 10, 2019 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dskvr/e13ba423ae369c9cfbd3bff5d74e1257 to your computer and use it in GitHub Desktop.
Save dskvr/e13ba423ae369c9cfbd3bff5d74e1257 to your computer and use it in GitHub Desktop.
Comparing openapi-generator to quicktype for models generation
/* TransactionVariant */
anyOf:
- $ref: 'TransactionHash.yaml'
- $ref: 'PackedTransaction.yaml'
/* TransactionHash */
type: string
/*PackedTransaction*/
type: object
properties:
id:
type: string
signatures:
type: array
items:
$ref: 'Signature.yaml'
compression:
type: string
packed_context_free_data:
type: string
context_free_data:
type: array
items:
type: string
context_free_actions:
type: array
items:
type: string
packed_trx:
type: string
transaction:
$ref: 'Transaction.yaml'
//
// TransactionVariant.swift
//
// Generated by openapi-generator
// https://openapi-generator.tech
//
import Foundation
public struct TransactionVariant: Codable {
public init() {
}
}
// To parse the JSON, add this file to your project and do:
//
// let transactionVariant = try TransactionVariant(json)
import Foundation
enum TransactionVariant: Codable {
case string(String)
case transactionVariantClass(TransactionVariantClass)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
if let x = try? container.decode(TransactionVariantClass.self) {
self = .transactionVariantClass(x)
return
}
throw DecodingError.typeMismatch(TransactionVariant.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for TransactionVariant"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .string(let x):
try container.encode(x)
case .transactionVariantClass(let x):
try container.encode(x)
}
}
}
struct TransactionVariantClass: Codable {
let compression: String?
let contextFreeActions, contextFreeData: [String]?
let id, packedContextFreeData, packedTrx: String?
let signatures: [String]?
let transaction: Transaction?
enum CodingKeys: String, CodingKey {
case compression
case contextFreeActions = "context_free_actions"
case contextFreeData = "context_free_data"
case id
case packedContextFreeData = "packed_context_free_data"
case packedTrx = "packed_trx"
case signatures, transaction
}
}
struct Transaction: Codable {
let actions: Actions?
let delaySEC: Int?
/// Date/time string in the format YYYY-MM-DDTHH:MM:SS.sss
let expiration: String?
/// A number that may be larger than uint64. If lte uint64, it will return an `int` type if
/// lt uint64 will return `string` type
let maxCPUUsageMS: JSONAny?
/// A number that may be larger than uint64. If lte uint64, it will return an `int` type if
/// lt uint64 will return `string` type
let maxNetUsageWords: JSONAny?
let refBlockNum, refBlockPrefix: Int?
let transactionExtensions: [[TransactionExtension]]?
enum CodingKeys: String, CodingKey {
case actions
case delaySEC = "delay_sec"
case expiration
case maxCPUUsageMS = "max_cpu_usage_ms"
case maxNetUsageWords = "max_net_usage_words"
case refBlockNum = "ref_block_num"
case refBlockPrefix = "ref_block_prefix"
case transactionExtensions = "transaction_extensions"
}
}
struct Actions: Codable {
/// String representation of EOSIO name type
let account: String?
let authorization: [Authorization]?
let data: JSONAny?
let hexData: String?
/// String representation of EOSIO name type
let name: String?
enum CodingKeys: String, CodingKey {
case account, authorization, data
case hexData = "hex_data"
case name
}
}
struct Authorization: Codable {
/// String representation of EOSIO name type
let actor: String?
/// String representation of EOSIO name type
let permission: String?
}
enum TransactionExtension: Codable {
case integer(Int)
case string(String)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Int.self) {
self = .integer(x)
return
}
if let x = try? container.decode(String.self) {
self = .string(x)
return
}
throw DecodingError.typeMismatch(TransactionExtension.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for TransactionExtension"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let x):
try container.encode(x)
case .string(let x):
try container.encode(x)
}
}
}
// MARK: Convenience initializers and mutators
extension TransactionVariantClass {
init(data: Data) throws {
self = try newJSONDecoder().decode(TransactionVariantClass.self, from: data)
}
init(_ json: String, using encoding: String.Encoding = .utf8) throws {
guard let data = json.data(using: encoding) else {
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
}
try self.init(data: data)
}
init(fromURL url: URL) throws {
try self.init(data: try Data(contentsOf: url))
}
func with(
compression: String?? = nil,
contextFreeActions: [String]?? = nil,
contextFreeData: [String]?? = nil,
id: String?? = nil,
packedContextFreeData: String?? = nil,
packedTrx: String?? = nil,
signatures: [String]?? = nil,
transaction: Transaction?? = nil
) -> TransactionVariantClass {
return TransactionVariantClass(
compression: compression ?? self.compression,
contextFreeActions: contextFreeActions ?? self.contextFreeActions,
contextFreeData: contextFreeData ?? self.contextFreeData,
id: id ?? self.id,
packedContextFreeData: packedContextFreeData ?? self.packedContextFreeData,
packedTrx: packedTrx ?? self.packedTrx,
signatures: signatures ?? self.signatures,
transaction: transaction ?? self.transaction
)
}
func jsonData() throws -> Data {
return try newJSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
extension Transaction {
init(data: Data) throws {
self = try newJSONDecoder().decode(Transaction.self, from: data)
}
init(_ json: String, using encoding: String.Encoding = .utf8) throws {
guard let data = json.data(using: encoding) else {
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
}
try self.init(data: data)
}
init(fromURL url: URL) throws {
try self.init(data: try Data(contentsOf: url))
}
func with(
actions: Actions?? = nil,
delaySEC: Int?? = nil,
expiration: String?? = nil,
maxCPUUsageMS: JSONAny?? = nil,
maxNetUsageWords: JSONAny?? = nil,
refBlockNum: Int?? = nil,
refBlockPrefix: Int?? = nil,
transactionExtensions: [[TransactionExtension]]?? = nil
) -> Transaction {
return Transaction(
actions: actions ?? self.actions,
delaySEC: delaySEC ?? self.delaySEC,
expiration: expiration ?? self.expiration,
maxCPUUsageMS: maxCPUUsageMS ?? self.maxCPUUsageMS,
maxNetUsageWords: maxNetUsageWords ?? self.maxNetUsageWords,
refBlockNum: refBlockNum ?? self.refBlockNum,
refBlockPrefix: refBlockPrefix ?? self.refBlockPrefix,
transactionExtensions: transactionExtensions ?? self.transactionExtensions
)
}
func jsonData() throws -> Data {
return try newJSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
extension Actions {
init(data: Data) throws {
self = try newJSONDecoder().decode(Actions.self, from: data)
}
init(_ json: String, using encoding: String.Encoding = .utf8) throws {
guard let data = json.data(using: encoding) else {
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
}
try self.init(data: data)
}
init(fromURL url: URL) throws {
try self.init(data: try Data(contentsOf: url))
}
func with(
account: String?? = nil,
authorization: [Authorization]?? = nil,
data: JSONAny?? = nil,
hexData: String?? = nil,
name: String?? = nil
) -> Actions {
return Actions(
account: account ?? self.account,
authorization: authorization ?? self.authorization,
data: data ?? self.data,
hexData: hexData ?? self.hexData,
name: name ?? self.name
)
}
func jsonData() throws -> Data {
return try newJSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
extension Authorization {
init(data: Data) throws {
self = try newJSONDecoder().decode(Authorization.self, from: data)
}
init(_ json: String, using encoding: String.Encoding = .utf8) throws {
guard let data = json.data(using: encoding) else {
throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
}
try self.init(data: data)
}
init(fromURL url: URL) throws {
try self.init(data: try Data(contentsOf: url))
}
func with(
actor: String?? = nil,
permission: String?? = nil
) -> Authorization {
return Authorization(
actor: actor ?? self.actor,
permission: permission ?? self.permission
)
}
func jsonData() throws -> Data {
return try newJSONEncoder().encode(self)
}
func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
return String(data: try self.jsonData(), encoding: encoding)
}
}
// MARK: Encode/decode helpers
class JSONNull: Codable, Hashable {
public static func == (lhs: JSONNull, rhs: JSONNull) -> Bool {
return true
}
public var hashValue: Int {
return 0
}
public init() {}
public required init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if !container.decodeNil() {
throw DecodingError.typeMismatch(JSONNull.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for JSONNull"))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encodeNil()
}
}
class JSONCodingKey: CodingKey {
let key: String
required init?(intValue: Int) {
return nil
}
required init?(stringValue: String) {
key = stringValue
}
var intValue: Int? {
return nil
}
var stringValue: String {
return key
}
}
class JSONAny: Codable {
let value: Any
static func decodingError(forCodingPath codingPath: [CodingKey]) -> DecodingError {
let context = DecodingError.Context(codingPath: codingPath, debugDescription: "Cannot decode JSONAny")
return DecodingError.typeMismatch(JSONAny.self, context)
}
static func encodingError(forValue value: Any, codingPath: [CodingKey]) -> EncodingError {
let context = EncodingError.Context(codingPath: codingPath, debugDescription: "Cannot encode JSONAny")
return EncodingError.invalidValue(value, context)
}
static func decode(from container: SingleValueDecodingContainer) throws -> Any {
if let value = try? container.decode(Bool.self) {
return value
}
if let value = try? container.decode(Int64.self) {
return value
}
if let value = try? container.decode(Double.self) {
return value
}
if let value = try? container.decode(String.self) {
return value
}
if container.decodeNil() {
return JSONNull()
}
throw decodingError(forCodingPath: container.codingPath)
}
static func decode(from container: inout UnkeyedDecodingContainer) throws -> Any {
if let value = try? container.decode(Bool.self) {
return value
}
if let value = try? container.decode(Int64.self) {
return value
}
if let value = try? container.decode(Double.self) {
return value
}
if let value = try? container.decode(String.self) {
return value
}
if let value = try? container.decodeNil() {
if value {
return JSONNull()
}
}
if var container = try? container.nestedUnkeyedContainer() {
return try decodeArray(from: &container)
}
if var container = try? container.nestedContainer(keyedBy: JSONCodingKey.self) {
return try decodeDictionary(from: &container)
}
throw decodingError(forCodingPath: container.codingPath)
}
static func decode(from container: inout KeyedDecodingContainer<JSONCodingKey>, forKey key: JSONCodingKey) throws -> Any {
if let value = try? container.decode(Bool.self, forKey: key) {
return value
}
if let value = try? container.decode(Int64.self, forKey: key) {
return value
}
if let value = try? container.decode(Double.self, forKey: key) {
return value
}
if let value = try? container.decode(String.self, forKey: key) {
return value
}
if let value = try? container.decodeNil(forKey: key) {
if value {
return JSONNull()
}
}
if var container = try? container.nestedUnkeyedContainer(forKey: key) {
return try decodeArray(from: &container)
}
if var container = try? container.nestedContainer(keyedBy: JSONCodingKey.self, forKey: key) {
return try decodeDictionary(from: &container)
}
throw decodingError(forCodingPath: container.codingPath)
}
static func decodeArray(from container: inout UnkeyedDecodingContainer) throws -> [Any] {
var arr: [Any] = []
while !container.isAtEnd {
let value = try decode(from: &container)
arr.append(value)
}
return arr
}
static func decodeDictionary(from container: inout KeyedDecodingContainer<JSONCodingKey>) throws -> [String: Any] {
var dict = [String: Any]()
for key in container.allKeys {
let value = try decode(from: &container, forKey: key)
dict[key.stringValue] = value
}
return dict
}
static func encode(to container: inout UnkeyedEncodingContainer, array: [Any]) throws {
for value in array {
if let value = value as? Bool {
try container.encode(value)
} else if let value = value as? Int64 {
try container.encode(value)
} else if let value = value as? Double {
try container.encode(value)
} else if let value = value as? String {
try container.encode(value)
} else if value is JSONNull {
try container.encodeNil()
} else if let value = value as? [Any] {
var container = container.nestedUnkeyedContainer()
try encode(to: &container, array: value)
} else if let value = value as? [String: Any] {
var container = container.nestedContainer(keyedBy: JSONCodingKey.self)
try encode(to: &container, dictionary: value)
} else {
throw encodingError(forValue: value, codingPath: container.codingPath)
}
}
}
static func encode(to container: inout KeyedEncodingContainer<JSONCodingKey>, dictionary: [String: Any]) throws {
for (key, value) in dictionary {
let key = JSONCodingKey(stringValue: key)!
if let value = value as? Bool {
try container.encode(value, forKey: key)
} else if let value = value as? Int64 {
try container.encode(value, forKey: key)
} else if let value = value as? Double {
try container.encode(value, forKey: key)
} else if let value = value as? String {
try container.encode(value, forKey: key)
} else if value is JSONNull {
try container.encodeNil(forKey: key)
} else if let value = value as? [Any] {
var container = container.nestedUnkeyedContainer(forKey: key)
try encode(to: &container, array: value)
} else if let value = value as? [String: Any] {
var container = container.nestedContainer(keyedBy: JSONCodingKey.self, forKey: key)
try encode(to: &container, dictionary: value)
} else {
throw encodingError(forValue: value, codingPath: container.codingPath)
}
}
}
static func encode(to container: inout SingleValueEncodingContainer, value: Any) throws {
if let value = value as? Bool {
try container.encode(value)
} else if let value = value as? Int64 {
try container.encode(value)
} else if let value = value as? Double {
try container.encode(value)
} else if let value = value as? String {
try container.encode(value)
} else if value is JSONNull {
try container.encodeNil()
} else {
throw encodingError(forValue: value, codingPath: container.codingPath)
}
}
public required init(from decoder: Decoder) throws {
if var arrayContainer = try? decoder.unkeyedContainer() {
self.value = try JSONAny.decodeArray(from: &arrayContainer)
} else if var container = try? decoder.container(keyedBy: JSONCodingKey.self) {
self.value = try JSONAny.decodeDictionary(from: &container)
} else {
let container = try decoder.singleValueContainer()
self.value = try JSONAny.decode(from: container)
}
}
public func encode(to encoder: Encoder) throws {
if let arr = self.value as? [Any] {
var container = encoder.unkeyedContainer()
try JSONAny.encode(to: &container, array: arr)
} else if let dict = self.value as? [String: Any] {
var container = encoder.container(keyedBy: JSONCodingKey.self)
try JSONAny.encode(to: &container, dictionary: dict)
} else {
var container = encoder.singleValueContainer()
try JSONAny.encode(to: &container, value: self.value)
}
}
}
func newJSONDecoder() -> JSONDecoder {
let decoder = JSONDecoder()
if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
decoder.dateDecodingStrategy = .iso8601
}
return decoder
}
func newJSONEncoder() -> JSONEncoder {
let encoder = JSONEncoder()
if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
encoder.dateEncodingStrategy = .iso8601
}
return encoder
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment