From 4f2afc862372bab00b442fd072dc01f848dcb8c7 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Thu, 11 Apr 2024 18:05:30 +0200 Subject: [PATCH] Network model better represents JSON types --- Sources/Common/NetowrkStruct.swift | 10 ---------- .../ConfidenceClient/NetworkTypeMapper.swift | 19 +++++++++++-------- .../ConfidenceProvider/Utils/TypeMapper.swift | 10 ++-------- 3 files changed, 13 insertions(+), 26 deletions(-) diff --git a/Sources/Common/NetowrkStruct.swift b/Sources/Common/NetowrkStruct.swift index fa272697..ccce66ec 100644 --- a/Sources/Common/NetowrkStruct.swift +++ b/Sources/Common/NetowrkStruct.swift @@ -12,7 +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,15 +30,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") diff --git a/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift b/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift index 0808df96..2737dbfc 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,10 +30,13 @@ 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 @@ -43,12 +46,12 @@ public enum NetworkTypeMapper { 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..1ebd10a8 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( @@ -73,11 +73,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): @@ -93,7 +88,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)