Skip to content

Commit

Permalink
use datecomponents for date
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed Apr 16, 2024
1 parent cb4379e commit 66592bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
37 changes: 29 additions & 8 deletions Sources/Confidence/ConfidenceValueHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,50 @@ func hashConfidenceValue(context: ConfidenceStruct) -> String {
return digest.map { String(format: "%02hhx", $0) }.joined()
}

// swiftlint:disable:next cyclomatic_complexity
func hashValue(value: ConfidenceValue, hasher: inout some HashFunction) {
switch value.type() {
case .boolean:
hasher.update(data: value.asBoolean()!.data)
if let booleanData = value.asBoolean()?.data {
hasher.update(data: booleanData)
}

case .string:
hasher.update(data: value.asString()!.data)
if let stringData = value.asString()?.data {
hasher.update(data: stringData)
}

case .integer:
hasher.update(data: value.asInteger()!.data)
if let integerData = value.asInteger()?.data {
hasher.update(data: integerData)
}

case .double:
hasher.update(data: value.asDouble()!.data)
if let doubleData = value.asDouble()?.data {
hasher.update(data: doubleData)
}

case .date:
hasher.update(data: value.asDate()!.data)
if let dateData = value.asDateComponents()?.date?.data {
hasher.update(data: dateData)
}

case .list:
value.asList()!.forEach { listValue in
value.asList()?.forEach { listValue in
hashValue(value: listValue, hasher: &hasher)
}

case .timestamp:
hasher.update(data: value.asDate()!.data)
if let timestampData = value.asDate()?.data {
hasher.update(data: timestampData)
}

case .structure:
value.asStructure()!.sorted { $0.key < $1.key }.forEach { key, structureValue in
value.asStructure()?.sorted { $0.key < $1.key }.forEach { key, structureValue in
hasher.update(data: key.data)
hashValue(value: structureValue, hasher: &hasher)
}

case .null:
hasher.update(data: UInt8(0).data)
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public class ConfidenceFeatureProvider: FeatureProvider {

Task {
do {
let context = confidence?.getContext().flattenOpenFeature() ?? ConfidenceTypeMapper.from(ctx: initialContext)
let context = confidence?.getContext()
.flattenOpenFeature() ?? ConfidenceTypeMapper.from(ctx: initialContext)
let resolveResult = try await resolve(context: context)

// update cache with stored values
Expand Down Expand Up @@ -134,7 +135,8 @@ public class ConfidenceFeatureProvider: FeatureProvider {
self.updateConfidenceContext(context: newContext)
Task {
do {
let context = confidence?.getContext().flattenOpenFeature() ?? ConfidenceTypeMapper.from(ctx: newContext)
let context = confidence?.getContext()
.flattenOpenFeature() ?? ConfidenceTypeMapper.from(ctx: newContext)
let resolveResult = try await resolve(context: context)

// update the storage
Expand Down

0 comments on commit 66592bb

Please sign in to comment.