Skip to content

Commit

Permalink
Merge pull request #80 from unsignedapps/reset-flag-values
Browse files Browse the repository at this point in the history
Added support for removing/resetting all flag values in a given `FlagValueSource`
  • Loading branch information
bok- authored Jun 12, 2021
2 parents 03b0eed + dca7ff4 commit 9f0e6dd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SWIFT_DOCKER_IMAGE = swift:latest
.PHONY: test docs

test:
docker run --rm --volume "$(shell pwd):/src" --workdir "/src" $(SWIFT_DOCKER_IMAGE) swift test --enable-test-discovery
docker run --rm --platform linux/amd64 -e QEMU_CPU=max --volume "$(shell pwd):/src" --workdir "/src" $(SWIFT_DOCKER_IMAGE) swift test --enable-test-discovery

docs:
rm -f website/content/guides/*.md
Expand All @@ -24,4 +24,4 @@ docs:
cd website && hugo
rm -f website/content/guides/*.md
rm -f website/content/api/*.md
rm -f website/content/_index.md
rm -f website/content/_index.md
20 changes: 19 additions & 1 deletion Sources/Vexil/Pole.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public class FlagPole<RootGroup> where RootGroup: FlagContainer {
}


// MARK: - Copying Flag Values
// MARK: - Mutating Flag Values

/// Copies the flag values from one `FlagValueSource` to another.
///
Expand All @@ -312,6 +312,24 @@ public class FlagPole<RootGroup> where RootGroup: FlagContainer {
try self.save(snapshot: snapshot, to: destination)
}

/// Removes all of the flag values from the specified flag value source.
///
/// All flag values for the given source are expected to return `nil` after this
/// method is called. This is useful if you want to provide a button or the capability
/// to "reset" a source back to its defaults, or clear any overrides in the given source.
///
public func removeFlagValues (in source: FlagValueSource) throws {
let flagsInSource = FlagValueDictionary()
try self.copyFlagValues(from: source, to: flagsInSource)

for key in flagsInSource.keys {

// setFlagValue<Value> needs to specialise the generic, so we picked `Bool` at
// random so we can pass in the nil
try source.setFlagValue(Bool?.none, key: key)
}
}

}


Expand Down
4 changes: 4 additions & 0 deletions Sources/Vexil/Sources/FlagValueDictionary+Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ extension FlagValueDictionary: Collection {
return self.storage.index(after: i)
}

public var keys: DictionaryType.Keys {
return self.storage.keys
}

}
3 changes: 3 additions & 0 deletions Sources/Vexil/Sources/FlagValueSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public protocol FlagValueSource {
func flagValue<Value> (key: String) -> Value? where Value: FlagValue

/// And to save values – if your source does not support saving just do nothing
///
/// It is expected if the value passed in is `nil` then the flag value would be cleared
///
func setFlagValue<Value> (_ value: Value?, key: String) throws where Value: FlagValue

#if !os(Linux)
Expand Down
17 changes: 17 additions & 0 deletions Tests/VexilTests/FlagValueSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,23 @@ final class FlagValueSourceTests: XCTestCase {

}

func testSourceRemovesAllVales () throws {

// GIVEN a dictionary with some values
let source = FlagValueDictionary([
"test-flag": true,
"subgroup.test-flag": true
])

// WHEN we remove all values from that source
let pole = FlagPole(hoist: TestFlags.self, sources: [])
try pole.removeFlagValues(in: source)

// THEN the source should now be empty
XCTAssertTrue(source.isEmpty)

}

}


Expand Down

0 comments on commit 9f0e6dd

Please sign in to comment.