Skip to content

Commit 03a52e5

Browse files
committed
Update values
1 parent a11a894 commit 03a52e5

7 files changed

Lines changed: 67 additions & 67 deletions

File tree

KakaJSON/Convert/Convertible.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ extension Convertible {
252252
}
253253

254254
// try to convert newValue to propertyType
255-
guard let value = Values.kj_value(newValue, propertyType) else {
255+
guard let value = Values.value(newValue, propertyType) else {
256256
property.set(newValue, for: model)
257257
continue
258258
}
@@ -320,7 +320,7 @@ extension Convertible {
320320
from: property.get(from: ptr)~!,
321321
property)~! else { continue }
322322

323-
guard let v = Values.kj_JSON(value) else { continue }
323+
guard let v = Values.JSONValue(value) else { continue }
324324

325325
// key filter
326326
json[mt.JSONKey(from: property.name,

KakaJSON/Convert/TypeProxy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extension TypeProxy {
2727
}
2828

2929
func typeProxy(_ type: Any.Type) -> TypeProxy.Type {
30-
// Any.Type(8 bytes) * 2 == Protocol.Type(16 bytes)
30+
// Any.Type(8 bytes) + Int(8 bytes) == Protocol.Type(16 bytes)
3131
return (type, 0) ~>> TypeProxy.Type.self
3232
}
3333

KakaJSON/Convert/Values.swift

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import CoreGraphics
1212
#endif
1313

1414
public struct Values {
15-
static func kj_value(_ value: Any?, _ type: Any.Type) -> Any? {
16-
guard let v = value.kj_value else { return nil }
15+
static func value(_ val: Any?, _ type: Any.Type) -> Any? {
16+
guard let v = val.kj_value else { return nil }
1717
if Swift.type(of: v) == type { return v }
1818

1919
switch type {
@@ -27,31 +27,31 @@ public struct Values {
2727
case is DataValue.Type: return _data(v, type)
2828
case is SetValue.Type: return _set(v, type)
2929
case let enumType as ConvertibleEnum.Type:
30-
let vv = kj_value(v, enumType.kj_valueType)
30+
let vv = value(v, enumType.kj_valueType)
3131
return enumType.kj_convert(from: vv as Any)
3232
default: return nil
3333
}
3434
}
3535

36-
static func kj_JSON(_ value: Any?) -> Any? {
36+
static func JSONValue(_ value: Any?) -> Any? {
3737
guard let v = value.kj_value else { return nil }
3838

3939
switch v {
40-
case let num as NumberValue: return _JSON(from: num)
40+
case let num as NumberValue: return _JSONValue(from: num)
4141
case let model as Convertible: return model.kj_JSONObject()
4242
case let date as Date: return date.timeIntervalSince1970
43-
case let array as [Any]: return _JSON(from: array)
44-
case let dict as [String: Any]: return _JSON(from: dict)
43+
case let array as [Any]: return _JSONValue(from: array)
44+
case let dict as [String: Any]: return _JSONValue(from: dict)
4545
case let url as URL: return url.absoluteString
46-
case let set as SetValue: return _JSON(from: set)
47-
case let `enum` as ConvertibleEnum: return kj_JSON(`enum`.kj_value)
46+
case let set as SetValue: return _JSONValue(from: set)
47+
case let `enum` as ConvertibleEnum: return JSONValue(`enum`.kj_value)
4848
default: return v as? NSCoding
4949
}
5050
}
5151

52-
static func kj_JSONString(_ value: Any?,
53-
prettyPrinted: Bool = false) -> String? {
54-
if let str = JSONSerialization.kj_string(kj_JSON(value),
52+
static func JSONString(_ value: Any?,
53+
prettyPrinted: Bool = false) -> String? {
54+
if let str = JSONSerialization.kj_string(JSONValue(value),
5555
prettyPrinted: prettyPrinted) {
5656
return str
5757
}
@@ -179,21 +179,21 @@ private extension Values {
179179
return Double("\(decimal)").flatMap { NSNumber(value: $0) }
180180
}
181181

182-
static func _JSON(from set: SetValue) -> Any? {
183-
return _JSON(from: set.kj_array())
182+
static func _JSONValue(from set: SetValue) -> Any? {
183+
return _JSONValue(from: set.kj_array())
184184
}
185185

186-
static func _JSON(from array: [Any]) -> Any? {
187-
let newArray = array.compactMap { kj_JSON($0) }
186+
static func _JSONValue(from array: [Any]) -> Any? {
187+
let newArray = array.compactMap { JSONValue($0) }
188188
return newArray.isEmpty ? nil : newArray
189189
}
190190

191-
static func _JSON(from dict: [String: Any]) -> Any? {
192-
let newDict = dict.compactMapValues { kj_JSON($0) }
191+
static func _JSONValue(from dict: [String: Any]) -> Any? {
192+
let newDict = dict.compactMapValues { JSONValue($0) }
193193
return newDict.isEmpty ? nil : newDict
194194
}
195195

196-
static func _JSON(from num: NumberValue) -> Any? {
196+
static func _JSONValue(from num: NumberValue) -> Any? {
197197
// stay Bool\IntegerValue
198198
if num is Bool || num is IntegerValue { return num }
199199
// return string for keeping precision
@@ -205,100 +205,100 @@ private extension Values {
205205

206206
public extension Values {
207207
/// get the most inner value
208-
static func value<T>(_ val: Any?, _ type: T.Type)-> T? {
209-
return kj_value(val, type) as? T
208+
static func value<T>(_ val: Any?, of type: T.Type)-> T? {
209+
return value(val, type) as? T
210210
}
211211

212212
/// convert value to String
213-
static func string(_ value: Any?)-> String? {
214-
return kj_value(value, String.self) as? String
213+
static func string(_ val: Any?)-> String? {
214+
return value(val, String.self) as? String
215215
}
216216

217217
/// convert value to Bool
218-
static func bool(_ value: Any?)-> Bool? {
219-
return kj_value(value, Bool.self) as? Bool
218+
static func bool(_ val: Any?)-> Bool? {
219+
return value(val, Bool.self) as? Bool
220220
}
221221

222222
/// convert value to Int
223-
static func int(_ value: Any?)-> Int? {
224-
return kj_value(value, Int.self) as? Int
223+
static func int(_ val: Any?)-> Int? {
224+
return value(val, Int.self) as? Int
225225
}
226226

227227
/// convert value to Int8
228-
static func int8(_ value: Any?)-> Int8? {
229-
return kj_value(value, Int8.self) as? Int8
228+
static func int8(_ val: Any?)-> Int8? {
229+
return value(val, Int8.self) as? Int8
230230
}
231231

232232
/// convert value to Int16
233-
static func int16(_ value: Any?)-> Int16? {
234-
return kj_value(value, Int16.self) as? Int16
233+
static func int16(_ val: Any?)-> Int16? {
234+
return value(val, Int16.self) as? Int16
235235
}
236236

237237
/// convert value to Int32
238-
static func int32(_ value: Any?)-> Int32? {
239-
return kj_value(value, Int32.self) as? Int32
238+
static func int32(_ val: Any?)-> Int32? {
239+
return value(val, Int32.self) as? Int32
240240
}
241241

242242
/// convert value to Int64
243-
static func int64(_ value: Any?)-> Int64? {
244-
return kj_value(value, Int64.self) as? Int64
243+
static func int64(_ val: Any?)-> Int64? {
244+
return value(val, Int64.self) as? Int64
245245
}
246246

247247
/// convert value to UInt
248-
static func uInt(_ value: Any?)-> UInt? {
249-
return kj_value(value, UInt.self) as? UInt
248+
static func uInt(_ val: Any?)-> UInt? {
249+
return value(val, UInt.self) as? UInt
250250
}
251251

252252
/// convert value to UInt8
253-
static func uInt8(_ value: Any?)-> UInt8? {
254-
return kj_value(value, UInt8.self) as? UInt8
253+
static func uInt8(_ val: Any?)-> UInt8? {
254+
return value(val, UInt8.self) as? UInt8
255255
}
256256

257257
/// convert value to UInt16
258-
static func uInt16(_ value: Any?)-> UInt16? {
259-
return kj_value(value, UInt16.self) as? UInt16
258+
static func uInt16(_ val: Any?)-> UInt16? {
259+
return value(val, UInt16.self) as? UInt16
260260
}
261261

262262
/// convert value to UInt32
263-
static func uInt32(_ value: Any?)-> UInt32? {
264-
return kj_value(value, UInt32.self) as? UInt32
263+
static func uInt32(_ val: Any?)-> UInt32? {
264+
return value(val, UInt32.self) as? UInt32
265265
}
266266

267267
/// convert value to UInt64
268-
static func uInt64(_ value: Any?)-> UInt64? {
269-
return kj_value(value, UInt64.self) as? UInt64
268+
static func uInt64(_ val: Any?)-> UInt64? {
269+
return value(val, UInt64.self) as? UInt64
270270
}
271271

272272
/// convert value to Float
273-
static func float(_ value: Any?)-> Float? {
274-
return kj_value(value, Float.self) as? Float
273+
static func float(_ val: Any?)-> Float? {
274+
return value(val, Float.self) as? Float
275275
}
276276

277277
/// convert value to Double
278-
static func double(_ value: Any?)-> Double? {
279-
return kj_value(value, Double.self) as? Double
278+
static func double(_ val: Any?)-> Double? {
279+
return value(val, Double.self) as? Double
280280
}
281281

282282
#if canImport(CoreGraphics)
283283
/// convert value to CGFloat
284-
static func cgFloat(_ value: Any?)-> CGFloat? {
285-
return kj_value(value, CGFloat.self) as? CGFloat
284+
static func cgFloat(_ val: Any?)-> CGFloat? {
285+
return value(val, CGFloat.self) as? CGFloat
286286
}
287287
#endif
288288

289289
/// convert value to Decimal
290-
static func decimal(_ value: Any?)-> Decimal? {
291-
return kj_value(value, Decimal.self) as? Decimal
290+
static func decimal(_ val: Any?)-> Decimal? {
291+
return value(val, Decimal.self) as? Decimal
292292
}
293293

294294
/// convert value to NSNumber
295-
static func number(_ value: Any?)-> NSNumber? {
296-
return kj_value(value, NSNumber.self) as? NSNumber
295+
static func number(_ val: Any?)-> NSNumber? {
296+
return value(val, NSNumber.self) as? NSNumber
297297
}
298298

299299
/// convert value to NSDecimalNumber
300-
static func decimalNumber(_ value: Any?)-> NSDecimalNumber? {
301-
return kj_value(value, NSDecimalNumber.self) as? NSDecimalNumber
300+
static func decimalNumber(_ val: Any?)-> NSDecimalNumber? {
301+
return value(val, NSDecimalNumber.self) as? NSDecimalNumber
302302
}
303303
}
304304

KakaJSON/Extension/Array+KJ.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public extension KJ where Base: ExpressibleByArrayLiteral & Sequence {
4646

4747
/// Array -> JSONArray
4848
func JSONArray() -> [Any]? {
49-
return Values.kj_JSON(base) as? [Any]
49+
return Values.JSONValue(base) as? [Any]
5050
}
5151

5252
/// Array -> JSONArray

KakaJSON/Global/Coding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public func write(_ value: Any,
3030
attributes: nil)
3131
}
3232
}
33-
let string = Values.kj_JSONString(value)
33+
let string = Values.JSONString(value)
3434
try? string?.write(to: URL,
3535
atomically: atomically,
3636
encoding: .utf8)
@@ -54,5 +54,5 @@ public func read<T>(_ type: T.Type,
5454
if value == nil {
5555
value = String(data: data, encoding: encoding)
5656
}
57-
return Values.kj_value(value, T.self) as? T
57+
return Values.value(value, T.self) as? T
5858
}

KakaJSON/Global/JSON.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public func JSONObjectArray<M: Convertible>(from models: [M]) -> [[String: Any]]
2626
}
2727

2828
public func JSONArray(from value: [Any]) -> [Any]? {
29-
return Values.kj_JSON(value) as? [Any]
29+
return Values.JSONValue(value) as? [Any]
3030
}
3131

3232
public func JSON(from value: Any) -> Any? {
33-
return Values.kj_JSON(value)
33+
return Values.JSONValue(value)
3434
}
3535

3636
public func JSONString(from value: Any,
3737
prettyPrinted: Bool = false) -> String? {
38-
return Values.kj_JSONString(value, prettyPrinted: prettyPrinted)
38+
return Values.JSONString(value, prettyPrinted: prettyPrinted)
3939
}

KakaJSONTests/Other/Global.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Global: XCTestCase {
3737
XCTAssert(Values.int(string1) == 1565922866)
3838
XCTAssert(Values.double(string1) == 1565922866)
3939
XCTAssert(Values.cgFloat(string1) == 1565922866)
40-
let date = Values.value(string1, Date.self)
40+
let date = Values.value(string1, of: Date.self)
4141
XCTAssert(date?.timeIntervalSince1970 == 1565922866)
4242

4343
let string2 = "true"

0 commit comments

Comments
 (0)