Skip to content

Commit c938f8e

Browse files
authored
Merge pull request #93 from unsignedapps/feature/flagvaluedictionary-boxed
Changed `FlagValueDictionary` to use `[String: BoxedFlagValue]` as its internal storage
2 parents ed209f2 + 5a35726 commit c938f8e

6 files changed

Lines changed: 25 additions & 22 deletions

File tree

Sources/Vexil/Sources/FlagValueDictionary+FlagValueSource.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ extension FlagValueDictionary: FlagValueSource {
1616
}
1717

1818
public func flagValue<Value>(key: String) -> Value? where Value: FlagValue {
19-
return self.storage[key] as? Value
19+
guard let value = self.storage[key] else {
20+
return nil
21+
}
22+
return Value(boxedFlagValue: value)
2023
}
2124

2225
public func setFlagValue<Value>(_ value: Value?, key: String) throws where Value: FlagValue {
2326
if let value = value {
24-
self.storage.updateValue(value, forKey: key)
27+
self.storage.updateValue(value.boxedFlagValue, forKey: key)
2528
} else {
2629
self.storage.removeValue(forKey: key)
2730
}

Sources/Vexil/Sources/FlagValueDictionary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open class FlagValueDictionary: Identifiable, ExpressibleByDictionaryLiteral {
2222
public let id = UUID()
2323

2424
/// Our internal dictionary type
25-
public typealias DictionaryType = [String: Any]
25+
public typealias DictionaryType = [String: BoxedFlagValue]
2626

2727
internal var storage: DictionaryType
2828

@@ -41,7 +41,7 @@ open class FlagValueDictionary: Identifiable, ExpressibleByDictionaryLiteral {
4141

4242
/// Initialises a `FlagValueDictionary` using a dictionary literal
4343
///
44-
public required init(dictionaryLiteral elements: (String, Any)...) {
44+
public required init(dictionaryLiteral elements: (String, BoxedFlagValue)...) {
4545
self.storage = elements.reduce(into: [:]) { dict, pair in
4646
dict.updateValue(pair.1, forKey: pair.0)
4747
}

Tests/VexilTests/EquatableTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ final class EquatableTests: XCTestCase {
9393
}
9494

9595
// WHEN we emit, then change some values and emit more
96-
dictionary["untracked-key"] = true // 1
97-
dictionary["top-level-flag"] = true // 2
98-
dictionary["second-test-flag"] = true // 3
99-
dictionary["subgroup.second-level-flag"] = true // 4
100-
dictionary["subgroup.double-subgroup.third-level-flag"] = true // 5
96+
dictionary["untracked-key"] = .bool(true) // 1
97+
dictionary["top-level-flag"] = .bool(true) // 2
98+
dictionary["second-test-flag"] = .bool(true) // 3
99+
dictionary["subgroup.second-level-flag"] = .bool(true) // 4
100+
dictionary["subgroup.double-subgroup.third-level-flag"] = .bool(true) // 5
101101

102102
// THEN we should have 6 snapshots of varying equatability
103103
wait(for: [ expectation ], timeout: 0.1)

Tests/VexilTests/FlagValueDictionaryTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class FlagValueDictionaryTests: XCTestCase {
1414

1515
func testReadsValues () {
1616
let source: FlagValueDictionary = [
17-
"top-level-flag": true
17+
"top-level-flag": .bool(true)
1818
]
1919

2020
let flagPole = FlagPole(hoist: TestFlags.self, sources: [ source ])
@@ -35,8 +35,8 @@ final class FlagValueDictionaryTests: XCTestCase {
3535
snapshot.oneFlagGroup.secondLevelFlag = false
3636
try flagPole.save(snapshot: snapshot, to: source)
3737

38-
XCTAssertEqual(source.storage["top-level-flag"] as? Bool, true)
39-
XCTAssertEqual(source.storage["one-flag-group.second-level-flag"] as? Bool, false)
38+
XCTAssertEqual(source.storage["top-level-flag"], .bool(true))
39+
XCTAssertEqual(source.storage["one-flag-group.second-level-flag"], .bool(false))
4040
}
4141
}
4242

@@ -59,8 +59,8 @@ final class FlagValueDictionaryTests: XCTestCase {
5959
expectation.fulfill()
6060
}
6161

62-
source["top-level-flag"] = true
63-
source["one-flag-group.second-level-flag"] = true
62+
source["top-level-flag"] = .bool(true)
63+
source["one-flag-group.second-level-flag"] = .bool(true)
6464

6565
self.wait(for: [ expectation ], timeout: 1)
6666

Tests/VexilTests/FlagValueSourceTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ final class FlagValueSourceTests: XCTestCase {
5757

5858
// GIVEN two dictionaries
5959
let source = FlagValueDictionary([
60-
"test-flag": true,
61-
"subgroup.test-flag": true
60+
"test-flag": .bool(true),
61+
"subgroup.test-flag": .bool(true)
6262
])
6363
let destination = FlagValueDictionary()
6464

@@ -68,17 +68,17 @@ final class FlagValueSourceTests: XCTestCase {
6868

6969
// THEN we expect those two dictionaries to match
7070
XCTAssertEqual(destination.count, 2)
71-
XCTAssertEqual(destination["test-flag"] as? Bool, true)
72-
XCTAssertEqual(destination["subgroup.test-flag"] as? Bool, true)
71+
XCTAssertEqual(destination["test-flag"], .bool(true))
72+
XCTAssertEqual(destination["subgroup.test-flag"], .bool(true))
7373

7474
}
7575

7676
func testSourceRemovesAllVales () throws {
7777

7878
// GIVEN a dictionary with some values
7979
let source = FlagValueDictionary([
80-
"test-flag": true,
81-
"subgroup.test-flag": true
80+
"test-flag": .bool(true),
81+
"subgroup.test-flag": .bool(true)
8282
])
8383

8484
// WHEN we remove all values from that source

Tests/VexilTests/SnapshotTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ final class SnapshotTests: XCTestCase {
9292
// GIVEN a FlagPole and a dictionary that is not a part it
9393
let pole = FlagPole(hoist: TestFlags.self, sources: [])
9494
let dictionary = FlagValueDictionary([
95-
"top-level-flag": true,
96-
"subgroup.double-subgroup.third-level-flag": true
95+
"top-level-flag": .bool(true),
96+
"subgroup.double-subgroup.third-level-flag": .bool(true)
9797
])
9898

9999
// WHEN we take a snapshot of that source

0 commit comments

Comments
 (0)