/
IntegerOperators.swift
151 lines (133 loc) · 3.71 KB
/
IntegerOperators.swift
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// IntegerOperators.swift
// ObjectMapper
//
// Created by Suyeol Jeon on 17/02/2017.
// Copyright © 2017 hearst. All rights reserved.
//
import Foundation
// MARK: - Signed Integer
/// SignedInteger mapping
public func <- <T: SignedInteger>(left: inout T, right: Map) {
switch right.mappingType {
case .fromJSON where right.isKeyPresent:
let value: T = toSignedInteger(right.currentValue) ?? 0
FromJSON.basicType(&left, object: value)
case .toJSON:
left >>> right
default: ()
}
}
/// Optional SignedInteger mapping
public func <- <T: SignedInteger>(left: inout T?, right: Map) {
switch right.mappingType {
case .fromJSON where right.isKeyPresent:
let value: T? = toSignedInteger(right.currentValue)
FromJSON.basicType(&left, object: value)
case .toJSON:
left >>> right
default: ()
}
}
// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// ImplicitlyUnwrappedOptional SignedInteger mapping
public func <- <T: SignedInteger>(left: inout T!, right: Map) {
switch right.mappingType {
case .fromJSON where right.isKeyPresent:
let value: T! = toSignedInteger(right.currentValue)
FromJSON.basicType(&left, object: value)
case .toJSON:
left >>> right
default: ()
}
}
#endif
// MARK: - Unsigned Integer
/// UnsignedInteger mapping
public func <- <T: UnsignedInteger>(left: inout T, right: Map) {
switch right.mappingType {
case .fromJSON where right.isKeyPresent:
let value: T = toUnsignedInteger(right.currentValue) ?? 0
FromJSON.basicType(&left, object: value)
case .toJSON:
left >>> right
default: ()
}
}
/// Optional UnsignedInteger mapping
public func <- <T: UnsignedInteger>(left: inout T?, right: Map) {
switch right.mappingType {
case .fromJSON where right.isKeyPresent:
let value: T? = toUnsignedInteger(right.currentValue)
FromJSON.basicType(&left, object: value)
case .toJSON:
left >>> right
default: ()
}
}
// Code targeting the Swift 4.1 compiler and below.
#if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
/// ImplicitlyUnwrappedOptional UnsignedInteger mapping
public func <- <T: UnsignedInteger>(left: inout T!, right: Map) {
switch right.mappingType {
case .fromJSON where right.isKeyPresent:
let value: T! = toUnsignedInteger(right.currentValue)
FromJSON.basicType(&left, object: value)
case .toJSON:
left >>> right
default: ()
}
}
#endif
// MARK: - Casting Utils
/// Convert any value to `SignedInteger`.
private func toSignedInteger<T: SignedInteger>(_ value: Any?) -> T? {
guard
let value = value,
case let number as NSNumber = value
else {
return nil
}
if T.self == Int.self, let x = Int(exactly: number.int64Value) {
return T.init(x)
}
if T.self == Int8.self, let x = Int8(exactly: number.int64Value) {
return T.init(x)
}
if T.self == Int16.self, let x = Int16(exactly: number.int64Value) {
return T.init(x)
}
if T.self == Int32.self, let x = Int32(exactly: number.int64Value) {
return T.init(x)
}
if T.self == Int64.self, let x = Int64(exactly: number.int64Value) {
return T.init(x)
}
return nil
}
/// Convert any value to `UnsignedInteger`.
private func toUnsignedInteger<T: UnsignedInteger>(_ value: Any?) -> T? {
guard
let value = value,
case let number as NSNumber = value
else {
return nil
}
if T.self == UInt.self, let x = UInt(exactly: number.uint64Value) {
return T.init(x)
}
if T.self == UInt8.self, let x = UInt8(exactly: number.uint64Value) {
return T.init(x)
}
if T.self == UInt16.self, let x = UInt16(exactly: number.uint64Value) {
return T.init(x)
}
if T.self == UInt32.self, let x = UInt32(exactly: number.uint64Value) {
return T.init(x)
}
if T.self == UInt64.self, let x = UInt64(exactly: number.uint64Value) {
return T.init(x)
}
return nil
}