Skip to content

Commit

Permalink
Fixes for Swift 5.5 compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Feb 8, 2023
1 parent cab0cbe commit e71e718
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 205 deletions.
4 changes: 3 additions & 1 deletion Sources/PotentASN1/Dates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public struct SuffixedDateFormatter {

public func date(from string: String) -> ZonedDate? {
let parsedDate: Date?
let zoneStartIndex = string.firstIndex { char in char == "-" || char == "+" || char == "Z" } ?? string.endIndex
let zoneStartIndex = string.firstIndex { (char: Character) in
char == "-" || char == "+" || char == "Z"
} ?? string.endIndex
let stringWithoutZone = String(string[string.startIndex ..< zoneStartIndex])
let hasSeconds = checkHasSeconds(stringWithoutZone)
let hasZone = string != stringWithoutZone
Expand Down
124 changes: 64 additions & 60 deletions Sources/PotentCBOR/CBORDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser

public static func unbox<T>(_ value: CBOR, type: T.Type, decoder: IVD) throws -> T?
where T: BinaryFloatingPoint, T: LosslessStringConvertible {

func decode(fromDecimalFraction items: [CBOR]) throws -> T {
guard
let exp = try? unbox(items[0], type: Int.self, decoder: decoder),
let man = try? unbox(items[1], type: BigInt.self, decoder: decoder)
else {
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}
guard let sig = Decimal(string: man.magnitude.description) else {
throw overflow(type, value: value, at: decoder.codingPath)
}
let dec = Decimal(sign: man.sign == .plus ? .plus : .minus, exponent: exp, significand: sig)
guard let result = T(dec.description) else {
throw overflow(type, value: value, at: decoder.codingPath)
}
return result
}

switch value {
case .simple(let int):
return try coerce(int, at: decoder.codingPath)
Expand All @@ -198,26 +216,24 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser
default:
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}
}

func decode(fromDecimalFraction items: [CBOR]) throws -> T {
public static func unbox(_ value: CBOR, type: Decimal.Type, decoder: IVD) throws -> Decimal? {

func decode(fromDecimalFraction items: [CBOR]) throws -> Decimal {
guard
let exp = try? unbox(items[0], type: Int.self, decoder: decoder),
let man = try? unbox(items[1], type: BigInt.self, decoder: decoder)
else {
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}
// CHECK: Decimal(exactly:) is a fatal error for all inputs?
guard let sig = Decimal(string: man.magnitude.description) else {
throw overflow(type, value: value, at: decoder.codingPath)
}
let dec = Decimal(sign: man.sign == .plus ? .plus : .minus, exponent: exp, significand: sig)
guard let result = T(dec.description) else {
throw overflow(type, value: value, at: decoder.codingPath)
throw overflow(Decimal.self, value: value, at: decoder.codingPath)
}
return result
return Decimal(sign: man.sign == .plus ? .plus : .minus, exponent: exp, significand: sig)
}
}

public static func unbox(_ value: CBOR, type: Decimal.Type, decoder: IVD) throws -> Decimal? {
switch value {
case .simple(let int):
return Decimal(int)
Expand Down Expand Up @@ -249,20 +265,6 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser
default:
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}

func decode(fromDecimalFraction items: [CBOR]) throws -> Decimal {
guard
let exp = try? unbox(items[0], type: Int.self, decoder: decoder),
let man = try? unbox(items[1], type: BigInt.self, decoder: decoder)
else {
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}
// CHECK: Decimal(exactly:) is a fatal error for all inputs?
guard let sig = Decimal(string: man.magnitude.description) else {
throw overflow(Decimal.self, value: value, at: decoder.codingPath)
}
return Decimal(sign: man.sign == .plus ? .plus : .minus, exponent: exp, significand: sig)
}
}

public static func unbox(_ value: CBOR, as type: Int.Type, decoder: IVD) throws -> Int? {
Expand Down Expand Up @@ -331,16 +333,6 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser
}

public static func unbox(_ value: CBOR, as type: UUID.Type, decoder: IVD) throws -> UUID? {
switch value {
case .utf8String(let string), .tagged(.uuid, .utf8String(let string)):
return try decode(from: string)
case .byteString(let data), .tagged(.uuid, .byteString(let data)):
return try decode(from: data)
case .tagged(_, let tagged): return try unbox(tagged, as: type, decoder: decoder)
case .null: return nil
default:
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}

func decode(from string: String) throws -> UUID {
guard let result = UUID(uuidString: string) else {
Expand All @@ -360,6 +352,17 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser
}
return UUID(uuid: uuid)
}

switch value {
case .utf8String(let string), .tagged(.uuid, .utf8String(let string)):
return try decode(from: string)
case .byteString(let data), .tagged(.uuid, .byteString(let data)):
return try decode(from: data)
case .tagged(_, let tagged): return try unbox(tagged, as: type, decoder: decoder)
case .null: return nil
default:
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}
}

public static func unbox(_ value: CBOR, as type: URL.Type, decoder: IVD) throws -> URL? {
Expand All @@ -378,6 +381,25 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser

public static func unbox(_ value: CBOR, as type: Date.Type, decoder: IVD) throws -> Date? {

func decode(from string: String) throws -> Date {
guard let date = ZonedDate(iso8601Encoded: string) else {
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath,
debugDescription: "Expected date string to be ISO8601-formatted."))
}
return date.utcDate
}

func decode(fromUntaggedNumericDate value: TimeInterval, unitsPerSeconds: Double) -> Date {
switch decoder.options.untaggedDateDecodingStrategy {
case .unitsSince1970:
return Date(timeIntervalSince1970: value / unitsPerSeconds)
case .millisecondsSince1970:
return Date(timeIntervalSince1970: value / 1000.0)
case .secondsSince1970:
return Date(timeIntervalSince1970: value)
}
}

switch value {
case .utf8String(let string), .tagged(.iso8601DateTime, .utf8String(let string)):
return try decode(from: string)
Expand All @@ -397,25 +419,6 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser
default:
throw DecodingError.typeMismatch(at: decoder.codingPath, expectation: type, reality: value)
}

func decode(from string: String) throws -> Date {
guard let date = ZonedDate(iso8601Encoded: string) else {
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath,
debugDescription: "Expected date string to be ISO8601-formatted."))
}
return date.utcDate
}

func decode(fromUntaggedNumericDate value: TimeInterval, unitsPerSeconds: Double) -> Date {
switch decoder.options.untaggedDateDecodingStrategy {
case .unitsSince1970:
return Date(timeIntervalSince1970: value / unitsPerSeconds)
case .millisecondsSince1970:
return Date(timeIntervalSince1970: value / 1000.0)
case .secondsSince1970:
return Date(timeIntervalSince1970: value)
}
}
}

public static func unbox(_ value: CBOR, as type: Data.Type, decoder: IVD) throws -> Data? {
Expand Down Expand Up @@ -467,6 +470,15 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser
}

public static func unbox(_ value: CBOR, as type: AnyValue.Type, decoder: IVD) throws -> AnyValue {

func dictionary(from value: CBOR.Map) throws -> AnyValue {
return .dictionary(.init(uniqueKeysWithValues: try value.map { key, value in
let key = try unbox(key, as: AnyValue.self, decoder: decoder)
let value = try unbox(value, as: AnyValue.self, decoder: decoder)
return (key, value)
}))
}

switch value {
case .null, .undefined:
return .nil
Expand Down Expand Up @@ -521,14 +533,6 @@ public struct CBORDecoderTransform: InternalDecoderTransform, InternalValueDeser
case .tagged(_, let untagged):
return try unbox(untagged, as: AnyValue.self, decoder: decoder)
}

func dictionary(from value: CBOR.Map) throws -> AnyValue {
return .dictionary(.init(uniqueKeysWithValues: try value.map { key, value in
let key = try unbox(key, as: AnyValue.self, decoder: decoder)
let value = try unbox(value, as: AnyValue.self, decoder: decoder)
return (key, value)
}))
}
}

public static func valueToUnkeyedValues(_ value: CBOR, decoder: IVD) throws -> UnkeyedValues? {
Expand Down
17 changes: 9 additions & 8 deletions Sources/PotentCBOR/CBOREncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ public struct CBOREncoderTransform: InternalEncoderTransform, InternalValueSeria
}

public static func box(_ value: AnyValue, encoder: IVE) throws -> CBOR {

func encode(_ value: AnyValue.AnyDictionary) throws -> CBOR {
return .map(.init(uniqueKeysWithValues: try value.map { key, value in
let key = try box(key, encoder: encoder)
let value = try box(value, encoder: encoder)
return (key, value)
}))
}

switch value {
case .nil:
return .null
Expand Down Expand Up @@ -231,14 +240,6 @@ public struct CBOREncoderTransform: InternalEncoderTransform, InternalValueSeria
case .dictionary(let value):
return try encode(value)
}

func encode(_ value: AnyValue.AnyDictionary) throws -> CBOR {
return .map(.init(uniqueKeysWithValues: try value.map { key, value in
let key = try box(key, encoder: encoder)
let value = try box(value, encoder: encoder)
return (key, value)
}))
}
}

public static func unkeyedValuesToValue(_ values: UnkeyedValues, encoder: IVE) -> CBOR {
Expand Down
39 changes: 20 additions & 19 deletions Sources/PotentCodables/AnyValue/AnyValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ extension AnyValue {
}

public var unwrapped: Any? {

func unwrap(dictionary: AnyDictionary) -> Any {
return Dictionary(uniqueKeysWithValues: dictionary.compactMap {
guard let value = $1.unwrapped else {
return nil
}
if let key = $0.stringValue {
return (AnyHashable(key), value)
}
else if let key = $0.integerValue(Int.self) {
return (AnyHashable(key), value)
}
else {
// It is an unsupported key type, but we are returning
// nil(s) rather than throwing errors
return nil
}
}) as [AnyHashable: Any]
}

switch self {
case .nil: return nil
case .bool(let value): return value
Expand All @@ -401,25 +421,6 @@ extension AnyValue {
case .array(let value): return Array(value.map(\.unwrapped))
case .dictionary(let value): return unwrap(dictionary: value)
}

func unwrap(dictionary: AnyDictionary) -> Any {
return Dictionary(uniqueKeysWithValues: dictionary.compactMap {
guard let value = $1.unwrapped else {
return nil
}
if let key = $0.stringValue {
return (AnyHashable(key), value)
}
else if let key = $0.integerValue(Int.self) {
return (AnyHashable(key), value)
}
else {
// It is an unsupported key type, but we are returning
// nil(s) rather than throwing errors
return nil
}
}) as [AnyHashable: Any]
}
}

}
Expand Down
4 changes: 3 additions & 1 deletion Sources/PotentCodables/ZonedDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ private struct ISO8601FlexibleDateFormatter {
let timeZone: TimeZone
if let timeStartIndex = string.firstIndex(of: "T") {
let time = string[timeStartIndex...]
let zoneStartIndex = time.firstIndex { char in char == "-" || char == "+" || char == "Z" } ?? time.endIndex
let zoneStartIndex = time.firstIndex { (char: Character) in
char == "-" || char == "+" || char == "Z"
} ?? time.endIndex
let timeWithoutZone = time[time.startIndex ..< zoneStartIndex]
hasSeconds = timeWithoutZone.contains(".")
hasZone = zoneStartIndex != time.endIndex
Expand Down
Loading

0 comments on commit e71e718

Please sign in to comment.