123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- import Foundation
- 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: ()
- }
- }
- 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: ()
- }
- }
- #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
- 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
- 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: ()
- }
- }
- 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: ()
- }
- }
- #if !(swift(>=4.1.50) || (swift(>=3.4) && !swift(>=4.0)))
- 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
- 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
- }
- 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
- }
|