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 4f2afc8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 26 deletions.
10 changes: 0 additions & 10 deletions Sources/Common/NetowrkStruct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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")
Expand Down
19 changes: 11 additions & 8 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,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
Expand All @@ -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
}
Expand Down
10 changes: 2 additions & 8 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 Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down

0 comments on commit 4f2afc8

Please sign in to comment.