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

Add CodingKeyRepresentable conditional conformance #66

Merged
merged 2 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions Sources/Tagged/Tagged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ extension Tagged: Encodable where RawValue: Encodable {
}
}

#if swift(>=5.6)
@available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *)
extension Tagged: CodingKeyRepresentable where RawValue: CodingKeyRepresentable {
public init?<T: CodingKey>(codingKey: T) {
guard let rawValue = RawValue(codingKey: codingKey)
else { return nil }
self.init(rawValue: rawValue)
}

public var codingKey: CodingKey {
self.rawValue.codingKey
}
}
#endif

extension Tagged: Equatable where RawValue: Equatable {}

extension Tagged: Error where RawValue: Error {}
Expand Down
13 changes: 13 additions & 0 deletions Tests/TaggedTests/TaggedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ final class TaggedTests: XCTestCase {
)
}

#if swift(>=5.6)
func testCodingKeyRepresentable() {
if #available(macOS 12.3, iOS 15.4, watchOS 8.5, tvOS 15.4, *) {
enum Key {}
let xs: [Tagged<Key, String>: String] = [Tagged("Hello"): "World"]
XCTAssertEqual(
String(decoding: try JSONEncoder().encode(xs), as: UTF8.self),
#"{"Hello":"World"}"#
)
}
}
#endif

func testEquatable() {
XCTAssertEqual(Tagged<Tag, Int>(rawValue: 1), Tagged<Tag, Int>(rawValue: 1))
}
Expand Down