From bff7a51e75ffc4eff3605f4319046645d56ce6b7 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Thu, 11 Apr 2024 17:58:47 +0200 Subject: [PATCH] NetworkValue works with number --- Sources/Common/NetowrkStruct.swift | 10 +++------ .../ConfidenceClient/NetworkTypeMapper.swift | 4 ++-- .../ConfidenceProvider/Utils/TypeMapper.swift | 21 +++++++------------ 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/Sources/Common/NetowrkStruct.swift b/Sources/Common/NetowrkStruct.swift index e79bf2c4..fa272697 100644 --- a/Sources/Common/NetowrkStruct.swift +++ b/Sources/Common/NetowrkStruct.swift @@ -9,9 +9,8 @@ public struct NetworkStruct: Equatable { public enum NetworkStructValue: Equatable { case null - case integer(Int64) case string(String) - case double(Double) + case number(Double) case boolean(Bool) case date(DateComponents) case timestamp(Date) @@ -20,16 +19,13 @@ public enum NetworkStructValue: Equatable { } extension NetworkStructValue: Codable { - // swiftlint:disable:next cyclomatic_complexity public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() switch self { case .null: try container.encodeNil() - case .integer(let integer): - try container.encode(integer) - case .double(let double): + case .number(let double): try container.encode(double) case .string(let string): try container.encode(string) @@ -61,7 +57,7 @@ extension NetworkStructValue: Codable { if container.decodeNil() { self = .null } else if let double = try? container.decode(Double.self) { - self = .double(double) + self = .number(double) } else if let string = try? container.decode(String.self) { self = .string(string) } else if let bool = try? container.decode(Bool.self) { diff --git a/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift b/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift index adcb1a83..0808df96 100644 --- a/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift +++ b/Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift @@ -23,12 +23,12 @@ public enum NetworkTypeMapper { guard let value = value.asInteger() else { return nil } - return NetworkStructValue.integer(value) + return NetworkStructValue.number(Double(value)) case .double: guard let value = value.asDouble() else { return nil } - return NetworkStructValue.double(value) + return NetworkStructValue.number(value) case .date: guard let value = value.asDateComponents() else { return nil diff --git a/Sources/ConfidenceProvider/Utils/TypeMapper.swift b/Sources/ConfidenceProvider/Utils/TypeMapper.swift index 849fee95..d860dce0 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( @@ -35,9 +35,9 @@ public enum TypeMapper { case .string(let value): return NetworkStructValue.string(value) case .integer(let value): - return NetworkStructValue.integer(value) + return NetworkStructValue.number(Double(value)) case .double(let value): - return NetworkStructValue.double(value) + return NetworkStructValue.number(value) case .date(let value): return NetworkStructValue.timestamp(value) case .list(let values): @@ -60,9 +60,7 @@ public enum TypeMapper { switch structValue { case .null: return .null - case .integer(let value): - return .integer(value) - case .double(let value): + case .number(let value): switch fieldType { case .intSchema: return .integer(Int64(value)) @@ -80,11 +78,12 @@ public enum TypeMapper { 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") } - return .structure( Dictionary( uniqueKeysWithValues: try mapValue.fields.map { field, fieldValue in @@ -100,12 +99,6 @@ public enum TypeMapper { try convertStructValueToValue(fieldValue, schema: listSchema) } ) - case .integer(let value): - return .integer(value) - case .double(let value): - return .double(value) - case .timestamp(let value): - return .date(value) } } }