diff --git a/Sources/Common/NetowrkStruct.swift b/Sources/Common/NetowrkStruct.swift index fa272697..63ddf241 100644 --- a/Sources/Common/NetowrkStruct.swift +++ b/Sources/Common/NetowrkStruct.swift @@ -12,8 +12,6 @@ public enum NetworkStructValue: Equatable { case string(String) case number(Double) case boolean(Bool) - case date(DateComponents) - case timestamp(Date) case structure(NetworkStruct) case list([NetworkStructValue]) } @@ -31,20 +29,6 @@ extension NetworkStructValue: Codable { try container.encode(string) case .boolean(let boolean): try container.encode(boolean) - case .date(let dateComponents): - let dateFormatter = ISO8601DateFormatter() - dateFormatter.timeZone = TimeZone.current - dateFormatter.formatOptions = [.withFullDate] - if let date = Calendar.current.date(from: dateComponents) { - try container.encode(dateFormatter.string(from: date)) - } else { - throw ConfidenceError.internalError(message: "Could not create date from components") - } - case .timestamp(let date): - let timestampFormatter = ISO8601DateFormatter() - timestampFormatter.timeZone = TimeZone.init(identifier: "UTC") - let timestamp = timestampFormatter.string(from: date) - try container.encode(timestamp) case .structure(let structure): try container.encode(structure) case .list(let list): @@ -62,8 +46,6 @@ extension NetworkStructValue: Codable { self = .string(string) } else if let bool = try? container.decode(Bool.self) { self = .boolean(bool) - } else if let date = try? container.decode(Date.self) { - self = .timestamp(date) } else if let object = try? container.decode(NetworkStruct.self) { self = .structure(object) } else if let list = try? container.decode([NetworkStructValue].self) { diff --git a/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift b/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift index 0808df96..27e1e066 100644 --- a/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift +++ b/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift @@ -2,12 +2,12 @@ import Foundation import Common public enum NetworkTypeMapper { - public static func from(value: ConfidenceStruct) -> NetworkStruct { - NetworkStruct(fields: value.compactMapValues(convertValue)) + public static func from(value: ConfidenceStruct) throws -> NetworkStruct { + NetworkStruct(fields: try value.compactMapValues(convertValue)) } // swiftlint:disable:next cyclomatic_complexity - public static func convertValue(_ value: ConfidenceValue) -> NetworkStructValue? { + public static func convertValue(_ value: ConfidenceValue) throws -> NetworkStructValue? { switch value.type() { case .boolean: guard let value = value.asBoolean() else { @@ -30,25 +30,31 @@ public enum NetworkTypeMapper { } return NetworkStructValue.number(value) case .date: - guard let value = value.asDateComponents() else { - return nil + let dateFormatter = ISO8601DateFormatter() + dateFormatter.timeZone = TimeZone.current + dateFormatter.formatOptions = [.withFullDate] + guard let value = value.asDateComponents(), let dateString = Calendar.current.date(from: value) else { + throw ConfidenceError.internalError(message: "Could not create date from components") } - return NetworkStructValue.date(value) + return NetworkStructValue.string(dateFormatter.string(from: dateString)) case .timestamp: guard let value = value.asDate() else { return nil } - return NetworkStructValue.timestamp(value) + let timestampFormatter = ISO8601DateFormatter() + timestampFormatter.timeZone = TimeZone.init(identifier: "UTC") + let timestamp = timestampFormatter.string(from: value) + return NetworkStructValue.string(timestamp) case .list: guard let value = value.asList() else { return nil } - return NetworkStructValue.list(value.compactMap(convertValue)) + return try NetworkStructValue.list(value.compactMap(convertValue)) case .structure: guard let value = value.asStructure() else { return nil } - return NetworkStructValue.structure(NetworkStruct(fields: value.compactMapValues(convertValue))) + return try NetworkStructValue.structure(NetworkStruct(fields: value.compactMapValues(convertValue))) case .null: return nil } diff --git a/Sources/ConfidenceProvider/Utils/TypeMapper.swift b/Sources/ConfidenceProvider/Utils/TypeMapper.swift index d860dce0..f3693acf 100644 --- a/Sources/ConfidenceProvider/Utils/TypeMapper.swift +++ b/Sources/ConfidenceProvider/Utils/TypeMapper.swift @@ -18,8 +18,8 @@ public enum TypeMapper { static func from( object: NetworkStruct, schema: StructFlagSchema ) - throws - -> Value + throws + -> Value { return .structure( Dictionary( @@ -39,7 +39,10 @@ public enum TypeMapper { case .double(let value): return NetworkStructValue.number(value) case .date(let value): - return NetworkStructValue.timestamp(value) + let timestampFormatter = ISO8601DateFormatter() + timestampFormatter.timeZone = TimeZone.init(identifier: "UTC") + let timestamp = timestampFormatter.string(from: value) + return NetworkStructValue.string(timestamp) case .list(let values): return .list(values.compactMap(convertValueToStructValue)) case .structure(let values): @@ -73,13 +76,6 @@ public enum TypeMapper { return .string(value) case .boolean(let value): return .boolean(value) - case .date(let value): - guard let timestamp = Calendar.current.date(from: value) else { - throw OpenFeatureError.parseError(message: "Error converting date data") - } - return .date(timestamp) - case .timestamp(let value): - return .date(value) case .structure(let mapValue): guard case .structSchema(let structSchema) = fieldType else { throw OpenFeatureError.parseError(message: "Field is struct in schema but something else in value") @@ -93,7 +89,6 @@ public enum TypeMapper { guard case .listSchema(let listSchema) = fieldType else { throw OpenFeatureError.parseError(message: "Field is list in schema but something else in value") } - return .list( try listValue.map { fieldValue in try convertStructValueToValue(fieldValue, schema: listSchema)