-
Notifications
You must be signed in to change notification settings - Fork 824
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds uncluttering dumped YAML manifest from
nil
entries. (#858)
* Adds uncluttering dumped YAML manifest from `nil` entries. * Uses required type of a dictionary * Update CHANGELOG.md Co-authored-by: Maciej Piotrowski <maciej.piotrowski@allegro.pl>
- Loading branch information
1 parent
98275e9
commit 4d6e63e
Showing
5 changed files
with
178 additions
and
2 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
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,9 @@ | ||
extension Array where Element == [String: Any?] { | ||
func removingEmptyArraysDictionariesAndNils() -> [[String: Any]] { | ||
var new: [[String: Any]] = [] | ||
forEach { element in | ||
new.append(element.removingEmptyArraysDictionariesAndNils()) | ||
} | ||
return new | ||
} | ||
} |
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,33 @@ | ||
extension Dictionary where Key == String, Value == Any? { | ||
func removingEmptyArraysDictionariesAndNils() -> [String: Any] { | ||
var new: [String: Any] = [:] | ||
filter(outNil).forEach { pair in | ||
let value: Any | ||
if let array = pair.value as? [[String: Any?]] { | ||
value = array.removingEmptyArraysDictionariesAndNils() | ||
} else if let dictionary = pair.value as? [String: Any?] { | ||
value = dictionary.removingEmptyArraysDictionariesAndNils() | ||
} else { | ||
value = pair.value! // nil is filtered out :) | ||
} | ||
new[pair.key] = value | ||
} | ||
return new | ||
.filter(outEmptyArrays) | ||
.filter(outEmptyDictionaries) | ||
} | ||
|
||
func outEmptyArrays(_ pair: (key: String, value: Any)) -> Bool { | ||
guard let array = pair.value as? [Any] else { return true } | ||
return !array.isEmpty | ||
} | ||
|
||
func outEmptyDictionaries(_ pair: (key: String, value: Any)) -> Bool { | ||
guard let dictionary = pair.value as? [String: Any] else { return true } | ||
return !dictionary.isEmpty | ||
} | ||
|
||
func outNil(_ pair: (key: String, value: Any?)) -> Bool { | ||
return pair.value != nil | ||
} | ||
} |
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
130 changes: 130 additions & 0 deletions
130
Tests/ProjectSpecTests/Dictionary+Extension_Tests.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
@testable import ProjectSpec | ||
import XCTest | ||
|
||
final class DictionaryExtensionTests: XCTestCase { | ||
func testRemovingNil_ShouldReturnNewDictionaryWithoutOptionalValues() { | ||
// Arrange | ||
let input: [String: Any?] = inputDictionary | ||
let expected: [String: Any] = outputDictionary | ||
XCTAssertNotEqual(input as NSDictionary, expected as NSDictionary) | ||
|
||
// Act | ||
let sut: [String: Any] = input.removingEmptyArraysDictionariesAndNils() | ||
|
||
// Assert | ||
XCTAssertEqual(sut as NSDictionary, expected as NSDictionary) | ||
} | ||
} | ||
|
||
extension DictionaryExtensionTests { | ||
var inputDictionary: [String: Any?] { | ||
let inner1: [String: Any?] = [ | ||
"inner1": "value1", | ||
"inner2": Optional("value2"), | ||
"inner3": nil, | ||
"inner4": Optional([1, 2, 3]) | ||
] | ||
let inner2: [String: Any?] = [ | ||
"inner1": "value1", | ||
"inner2": Optional("value2"), | ||
"inner3": inner1, | ||
"inner4": [1, 2, 3] | ||
] | ||
let inner3: [String: Any?] = [ | ||
"inner1": "value1", | ||
"inner2": Optional("value2"), | ||
"inner3": Optional(inner1), | ||
"inner4": [1, 2, 3], | ||
"inner5": inner2 | ||
] | ||
let inner4: [String: Any?] = [ | ||
"inner1": inner1, | ||
"inner2": inner2, | ||
"inner3": inner3, | ||
"inner4": Optional("value4"), | ||
"inner5": nil | ||
] | ||
|
||
let inner6: [String: Any?] = [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner3": [inner1, inner1, inner1] | ||
] | ||
|
||
let input: [String: Any?] = [ | ||
"inner1": "value1", | ||
"inner2": Optional("value2"), | ||
"inner3": nil, | ||
"inner4": inner4, | ||
"inner5": [], | ||
"inner6": inner6, | ||
"inner7": [:] | ||
] | ||
|
||
return input | ||
} | ||
|
||
var outputDictionary: [String: Any] { | ||
let expected: [String: Any] = [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [ | ||
"inner1": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [1, 2, 3] | ||
], | ||
"inner2": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner3": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [1, 2, 3] | ||
], | ||
"inner4": [1, 2, 3] | ||
], | ||
"inner3": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner3": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [1, 2, 3] | ||
], | ||
"inner4": [1, 2, 3], | ||
"inner5": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner3": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [1, 2, 3] | ||
], | ||
"inner4": [1, 2, 3] | ||
] | ||
], | ||
"inner4": "value4" | ||
], | ||
"inner6": [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner3": [[ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [1, 2, 3] | ||
], [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [1, 2, 3] | ||
], [ | ||
"inner1": "value1", | ||
"inner2": "value2", | ||
"inner4": [1, 2, 3] | ||
]] | ||
] | ||
] | ||
|
||
return expected | ||
} | ||
} |