-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add resolving against confidence context (#94)
* add resolving against confidence context * send context hash to the provider cash * evaluate against flatten of context * fixup! evaluate against flatten of context * use datecomponents for date * fix lint * hash only exists for confidence struct * Update Sources/Confidence/ConfidenceValue.swift Co-authored-by: Fabrizio Demaria <[email protected]> * write open feature keys flatten and remove the flattening logi * update test to remove the flattening logic * fixup! update test to remove the flattening logic * fixup! Merge branch 'main' into of-resolve-conf-context * fixup! fixup! Merge branch 'main' into of-resolve-conf-context * internal constructor --------- Co-authored-by: Fabrizio Demaria <[email protected]>
- Loading branch information
1 parent
3c4febf
commit a7cbb19
Showing
20 changed files
with
295 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import CryptoKit | ||
import Foundation | ||
|
||
public extension ConfidenceStruct { | ||
func hash() -> String { | ||
hashConfidenceValue(context: self) | ||
} | ||
} | ||
|
||
func hashConfidenceValue(context: ConfidenceStruct) -> String { | ||
var hasher = SHA256() | ||
|
||
context.sorted { $0.key < $1.key }.forEach { key, value in | ||
hasher.update(data: key.data) | ||
hashValue(value: value, hasher: &hasher) | ||
} | ||
|
||
let digest = hasher.finalize() | ||
|
||
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: | ||
if let booleanData = value.asBoolean()?.data { | ||
hasher.update(data: booleanData) | ||
} | ||
|
||
case .string: | ||
if let stringData = value.asString()?.data { | ||
hasher.update(data: stringData) | ||
} | ||
|
||
case .integer: | ||
if let integerData = value.asInteger()?.data { | ||
hasher.update(data: integerData) | ||
} | ||
|
||
case .double: | ||
if let doubleData = value.asDouble()?.data { | ||
hasher.update(data: doubleData) | ||
} | ||
|
||
case .date: | ||
if let dateData = value.asDateComponents()?.date?.data { | ||
hasher.update(data: dateData) | ||
} | ||
|
||
case .list: | ||
value.asList()?.forEach { listValue in | ||
hashValue(value: listValue, hasher: &hasher) | ||
} | ||
|
||
case .timestamp: | ||
if let timestampData = value.asDate()?.data { | ||
hasher.update(data: timestampData) | ||
} | ||
|
||
case .structure: | ||
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) | ||
} | ||
} | ||
|
||
extension StringProtocol { | ||
var data: Data { .init(utf8) } | ||
} | ||
|
||
extension Numeric { | ||
var data: Data { | ||
var source = self | ||
return .init(bytes: &source, count: MemoryLayout<Self>.size) | ||
} | ||
} | ||
|
||
extension Bool { | ||
var data: Data { UInt8(self ? 1 : 0).data } | ||
} | ||
|
||
extension Date { | ||
var data: Data { | ||
self.timeIntervalSince1970.data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
Sources/ConfidenceProvider/ConfidenceClient/ConfidenceClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.