Skip to content

Commit

Permalink
NetworkValue works with number
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 11, 2024
1 parent af64c5e commit bff7a51
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
10 changes: 3 additions & 7 deletions Sources/Common/NetowrkStruct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions Sources/Confidence/ConfidenceClient/NetworkTypeMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 7 additions & 14 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 @@ -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):
Expand All @@ -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))
Expand All @@ -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
Expand All @@ -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)
}
}
}

0 comments on commit bff7a51

Please sign in to comment.