Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/improve decodable configuration #8

Merged
merged 5 commits into from
Jun 2, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve API for configuring decoder for Option and OptionSet
feature/improve-decodable-configuration
roanutil committed May 24, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 4e996d99cfe8415ccbfc4bd8f45ef565adece394
9 changes: 4 additions & 5 deletions Sources/ArgumentEncoding/Option.swift
Original file line number Diff line number Diff line change
@@ -284,8 +284,7 @@ extension Option: DecodableWithConfiguration where Value: Decodable {

extension Option: Decodable where Value: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
guard let configurationCodingUserInfoKey = Self.configurationCodingUserInfoKey(for: Value.Type.self) else {
guard let configurationCodingUserInfoKey = Self.configurationCodingUserInfoKey() else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "No CodingUserInfoKey found for accessing the DecodingConfiguration.",
@@ -307,11 +306,11 @@ extension Option: Decodable where Value: Decodable {
underlyingError: nil
))
}
try self.init(wrappedValue: container.decode(Value.self), nil, configuration)
try self.init(from: decoder, configuration: configuration)
}

public static func configurationCodingUserInfoKey(for _: (some Any).Type) -> CodingUserInfoKey? {
CodingUserInfoKey(rawValue: ObjectIdentifier(Self.self).debugDescription)
public static func configurationCodingUserInfoKey() -> CodingUserInfoKey? {
CodingUserInfoKey(rawValue: "\(Self.self) - " + ObjectIdentifier(Self.self).debugDescription)
}
}

9 changes: 4 additions & 5 deletions Sources/ArgumentEncoding/OptionSet.swift
Original file line number Diff line number Diff line change
@@ -210,8 +210,7 @@ extension OptionSet: DecodableWithConfiguration where Value: Decodable {

extension OptionSet: Decodable where Value: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
guard let configurationCodingUserInfoKey = Self.configurationCodingUserInfoKey(for: Value.Type.self) else {
guard let configurationCodingUserInfoKey = Self.configurationCodingUserInfoKey() else {
throw DecodingError.dataCorrupted(DecodingError.Context(
codingPath: decoder.codingPath,
debugDescription: "No CodingUserInfoKey found for accessing the DecodingConfiguration.",
@@ -233,11 +232,11 @@ extension OptionSet: Decodable where Value: Decodable {
underlyingError: nil
))
}
try self.init(wrappedValue: container.decode(Value.self), nil, configuration)
try self.init(from: decoder, configuration: configuration)
}

public static func configurationCodingUserInfoKey(for _: (some Any).Type) -> CodingUserInfoKey? {
CodingUserInfoKey(rawValue: ObjectIdentifier(Self.self).debugDescription)
public static func configurationCodingUserInfoKey() -> CodingUserInfoKey? {
CodingUserInfoKey(rawValue: "\(Self.self) - " + ObjectIdentifier(Self.self).debugDescription)
}
}