forked from alexaubry/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodableModels.swift
More file actions
95 lines (66 loc) · 2.09 KB
/
CodableModels.swift
File metadata and controls
95 lines (66 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import Foundation
import JavaScriptKit
// MARK: Empty Object
/// An object that does not encode a value.
class EmptyObject: Codable {
class Void: Encodable {
func encode(to encoder: Encoder) {}
}
let void = Void()
func encode(to encoder: Encoder) throws {
var singleValueContainer = encoder.singleValueContainer()
try singleValueContainer.encode(void)
}
init() {}
required init(from decoder: Decoder) throws {}
}
// MARK: - User
/// A simple structure
struct User: Codable, Hashable {
let displayName: String
let handle: String
var hashValue: Int {
return displayName.hashValue & handle.hashValue
}
static func == (lhs: User, rhs: User) -> Bool {
return (lhs.displayName == rhs.displayName) && (lhs.handle == rhs.handle)
}
}
// MARK: - Person
/// A structure with nested unkeyed containers containing keyed containers.
struct Company: Codable, Hashable {
let name: String
let address: Address
var childCompanies: [Company]
var hashValue: Int {
return name.hashValue & address.hashValue & childCompanies.reduce(0) { $0 & $1.hashValue }
}
static func == (lhs: Company, rhs: Company) -> Bool {
return (lhs.name == rhs.name) && (lhs.address == rhs.address) && (lhs.childCompanies == rhs.childCompanies)
}
}
// MARK: - Address
/// A structure with an optional field.
struct Address: Codable, Hashable {
let line1: String
let line2: String?
let zipCode: Int
let city: String
let country: Country
var hashValue: Int {
return line1.hashValue & (line2?.hashValue ?? 0) & zipCode & city.hashValue & country.hashValue
}
static func == (lhs: Address, rhs: Address) -> Bool {
return (lhs.line1 == rhs.line1) &&
(lhs.line2 == rhs.line2) &&
(lhs.zipCode == rhs.zipCode) &&
(lhs.city == rhs.city) &&
(lhs.country == rhs.country)
}
}
// MARK: - Country
/// A Raw Representable and Codable enum.
enum Country: String, Codable {
case unitedStates = "United States"
case france = "France"
}