Skip to content

Commit 38bdd47

Browse files
committed
Remove overloads for decode helpers. Add doc comments for decode helpers
feature/improve-decodable-configuration
1 parent e66839e commit 38bdd47

File tree

4 files changed

+43
-61
lines changed

4 files changed

+43
-61
lines changed

Sources/ArgumentEncoding/DecoderUserInfo+OptionHelpers.swift

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,25 @@
55

66
import Foundation
77

8+
/// Helper functions for configuring a decoder's `userInfo` dictionary for decoding `Option`.
9+
/// Each of the overloads that does not require the configuration closure, will configure both
10+
/// `Option<T>` and `Option<T?>`.
11+
///
12+
/// ```swift
13+
/// struct Container: ArgumentGroup, FormatterNode {
14+
/// let flagFormatter: FlagFormatter = .init(prefix: .doubleDash)
15+
/// let optionFormatter: OptionFormatter = .init(prefix: .doubleDash)
16+
/// @Option var option: String = "value"
17+
/// }
18+
/// let encoded = try JSONEncoder().encode(Container())
19+
/// let decoder = JSONDecoder()
20+
/// decoder.userInfo.addOptionConfiguration(for: String.self)
21+
/// let decoded = try decoder.decode(Container.self, from: encoded)
22+
/// // decoded = ["--option", "value"]
23+
/// ```
824
extension [CodingUserInfoKey: Any] {
925
public mutating func addOptionConfiguration<T>(
10-
for _: Option<T>.Type,
26+
for _: T.Type,
1127
configuration: @escaping Option<T>.DecodingConfiguration
1228
) where T: Decodable {
1329
guard let key = Option<T>.configurationCodingUserInfoKey() else {
@@ -16,46 +32,24 @@ extension [CodingUserInfoKey: Any] {
1632
self[key] = configuration
1733
}
1834

19-
public mutating func addOptionConfiguration<T>(for _: Option<T>.Type) where T: Decodable,
20-
T: CustomStringConvertible
21-
{
22-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T>.unwrap(_:))
23-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T?>.unwrap(_:))
24-
}
25-
26-
public mutating func addOptionConfiguration<T>(for _: Option<T>.Type) where T: Decodable, T: RawRepresentable,
27-
T.RawValue: CustomStringConvertible
28-
{
29-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T>.unwrap(_:))
30-
addOptionConfiguration(for: Option<T>.self, configuration: { $0.rawValue.description })
31-
}
32-
33-
public mutating func addOptionConfiguration<T>(for _: Option<T>.Type) where T: Decodable,
34-
T: CustomStringConvertible,
35-
T: RawRepresentable, T.RawValue: CustomStringConvertible
36-
{
37-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T>.unwrap(_:))
38-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T?>.unwrap(_:))
39-
}
40-
4135
public mutating func addOptionConfiguration<T>(for _: T.Type) where T: Decodable,
4236
T: CustomStringConvertible
4337
{
44-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T>.unwrap(_:))
45-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T?>.unwrap(_:))
38+
addOptionConfiguration(for: T.self, configuration: Option<T>.unwrap(_:))
39+
addOptionConfiguration(for: T.self, configuration: Option<T?>.unwrap(_:))
4640
}
4741

4842
public mutating func addOptionConfiguration<T>(for _: T.Type) where T: Decodable, T: RawRepresentable,
4943
T.RawValue: CustomStringConvertible
5044
{
51-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T>.unwrap(_:))
52-
addOptionConfiguration(for: Option<T>.self, configuration: { $0.rawValue.description })
45+
addOptionConfiguration(for: T.self, configuration: Option<T>.unwrap(_:))
46+
addOptionConfiguration(for: T.self, configuration: { $0.rawValue.description })
5347
}
5448

5549
public mutating func addOptionConfiguration<T>(for _: T.Type) where T: Decodable, T: CustomStringConvertible,
5650
T: RawRepresentable, T.RawValue: CustomStringConvertible
5751
{
58-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T>.unwrap(_:))
59-
addOptionConfiguration(for: Option<T>.self, configuration: Option<T?>.unwrap(_:))
52+
addOptionConfiguration(for: T.self, configuration: Option<T>.unwrap(_:))
53+
addOptionConfiguration(for: T.self, configuration: Option<T?>.unwrap(_:))
6054
}
6155
}

Sources/ArgumentEncoding/DecoderUserInfo+OptionSetHelpers.swift

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@
55

66
import Foundation
77

8+
/// Helper functions for configuring a decoder's `userInfo` dictionary for decoding `OptionSet`.
9+
/// Each of the overloads that does not require the configuration closure, will configure both
10+
/// `OptionSet<T>` and `OptionSet<T?>`.
11+
///
12+
/// ```swift
13+
/// struct Container: ArgumentGroup, FormatterNode {
14+
/// let flagFormatter: FlagFormatter = .init(prefix: .doubleDash)
15+
/// let optionFormatter: OptionFormatter = .init(prefix: .doubleDash)
16+
/// @OptionSet var option: [String] = ["value1", "value2"]
17+
/// }
18+
/// let encoded = try JSONEncoder().encode(Container())
19+
/// let decoder = JSONDecoder()
20+
/// decoder.userInfo.addOptionConfiguration(for: String.self)
21+
/// let decoded = try decoder.decode(Container.self, from: encoded)
22+
/// // decoded = ["--option", "value1", "--option", "value2"]
23+
/// ```
824
extension [CodingUserInfoKey: Any] {
9-
public mutating func addOptionSetConfiguration<T>(
10-
for _: T.Type,
11-
configuration: @escaping Option<T>.DecodingConfiguration
12-
) where T: Decodable {
13-
guard let key = Option<T>.configurationCodingUserInfoKey() else {
14-
return
15-
}
16-
self[key] = configuration
17-
}
18-
1925
public mutating func addOptionSetConfiguration<T>(
2026
for _: OptionSet<T>.Type,
2127
configuration: @escaping OptionSet<T>.DecodingConfiguration
@@ -26,24 +32,6 @@ extension [CodingUserInfoKey: Any] {
2632
self[key] = configuration
2733
}
2834

29-
public mutating func addOptionSetConfiguration<T>(for _: OptionSet<T>.Type) where T: Decodable, T: Sequence,
30-
T.Element: CustomStringConvertible
31-
{
32-
addOptionSetConfiguration(for: OptionSet<T>.self, configuration: OptionSet<T>.unwrap(_:))
33-
}
34-
35-
public mutating func addOptionSetConfiguration<T>(for _: OptionSet<T>.Type) where T: Decodable, T: Sequence,
36-
T.Element: RawRepresentable, T.Element.RawValue: CustomStringConvertible
37-
{
38-
addOptionSetConfiguration(for: OptionSet<T>.self, configuration: OptionSet<T>.unwrap(_:))
39-
}
40-
41-
public mutating func addOptionSetConfiguration<T>(for _: OptionSet<T>.Type) where T: Decodable, T: Sequence,
42-
T.Element: CustomStringConvertible, T.Element: RawRepresentable, T.Element.RawValue: CustomStringConvertible
43-
{
44-
addOptionSetConfiguration(for: OptionSet<T>.self, configuration: OptionSet<T>.unwrap(_:))
45-
}
46-
4735
public mutating func addOptionSetConfiguration<T>(for _: T.Type) where T: Decodable, T: Sequence,
4836
T.Element: CustomStringConvertible
4937
{

Tests/ArgumentEncodingTests/OptionDecodingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class OptionDecodingTests: XCTestCase {
1414
let option = Option(wrappedValue: "value")
1515
let data = try encoder.encode(option)
1616
let decoder = JSONDecoder()
17-
decoder.userInfo.addOptionConfiguration(for: Option<String>.self)
17+
decoder.userInfo.addOptionConfiguration(for: String.self)
1818
let decoded = try decoder.decode(Option<String>.self, from: data)
1919
XCTAssertEqual(decoded, option)
2020
}
@@ -23,7 +23,7 @@ final class OptionDecodingTests: XCTestCase {
2323
let container = OptionContainer(option: "value")
2424
let data = try encoder.encode(container)
2525
let decoder = JSONDecoder()
26-
decoder.userInfo.addOptionConfiguration(for: Option<String>.self)
26+
decoder.userInfo.addOptionConfiguration(for: String.self)
2727
let decoded = try decoder.decode(OptionContainer.self, from: data)
2828
XCTAssertEqual(decoded, container)
2929
}

Tests/ArgumentEncodingTests/OptionSetDecodingTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class OptionSetDecodingTests: XCTestCase {
1414
let optionSet = OptionSet(wrappedValue: ["value1", "value2"])
1515
let data = try encoder.encode(optionSet)
1616
let decoder = JSONDecoder()
17-
decoder.userInfo.addOptionSetConfiguration(for: OptionSet<[String]>.self)
17+
decoder.userInfo.addOptionSetConfiguration(for: [String].self)
1818
let decoded = try decoder.decode(OptionSet<[String]>.self, from: data)
1919
XCTAssertEqual(decoded, optionSet)
2020
}
@@ -23,7 +23,7 @@ final class OptionSetDecodingTests: XCTestCase {
2323
let container = OptionSetContainer(option: ["value1", "value2"])
2424
let data = try encoder.encode(container)
2525
let decoder = JSONDecoder()
26-
decoder.userInfo.addOptionSetConfiguration(for: OptionSet<[String]>.self)
26+
decoder.userInfo.addOptionSetConfiguration(for: [String].self)
2727
let decoded = try decoder.decode(OptionSetContainer.self, from: data)
2828
XCTAssertEqual(decoded, container)
2929
}

0 commit comments

Comments
 (0)