Skip to content

Commit

Permalink
Network model better represents JSON types
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 11, 2024
1 parent bff7a51 commit e1be093
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 38 deletions.
18 changes: 0 additions & 18 deletions Sources/Common/NetowrkStruct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand All @@ -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):
Expand All @@ -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) {
Expand Down
24 changes: 15 additions & 9 deletions Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
17 changes: 6 additions & 11 deletions Sources/ConfidenceProvider/Utils/TypeMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public enum TypeMapper {
static func from(
object: NetworkStruct, schema: StructFlagSchema
)
throws
-> Value
throws
-> Value
{
return .structure(
Dictionary(
Expand All @@ -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):
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit e1be093

Please sign in to comment.