Skip to content

Commit

Permalink
Fix Date decoding with custom strategy (#24)
Browse files Browse the repository at this point in the history
* Coerce (#15)

* Added coerce to be able to switch between compatible representations where only the tag changes

* Change coerce to explicitly pass the new tag. Fixed spacing issues

* Renamed coerce to coerced

* Apply suggestions from code review

Incorporated fixes for argument prefix

Co-Authored-By: buscarini <buscarini@gmail.com>

* XcodeGen (#17)

* XcodeGen

* 4.2

* Bump sim

* Update package names

* Bump Xcode

* Add workspace (#18)

* Add tests for playgrounds (#20)

* Add tests for playgrounds.

* Update Makefile

Co-Authored-By: mbrandonw <mbw234@gmail.com>

* Time != Money (#10)

* Time

* Money

* typo

* podspecs

* update readme

* comment about currencies

* date conversions and more tests

* positive examples in readme

* clean up

* more cleanup

* Update README.md

* wip

* Update Tagged.podspec

* clean up

* clean u

* Bump

* Update Podspecs

* run make xcodeproj

* dependency

* make xcodeproj

* Bundle XcodeGen. Ignore Package.resolved

* Fix podspecs

* Prep pod release

* Fix pod name

* Swift 5 (#19)

* Add workspace

* Swift 5

* Bumps

* Workaround for Swift bug.

* Bumps

* Bump 2

* Podspecs

* Use Swift 5 Docker

* Conditionally include development dependencies

* Use PF-prefixed environment variable to scope

* Update TaggedTests.swift

* Revert "Update TaggedTests.swift"

This reverts commit b5bea0b.

* Update README.md

* Remove unneeded return

* Update podspecs to point to 0.4.0.

* Fixed date parsing with a custom decoding strategy

* Fixed encoding with a custom date strategy
  • Loading branch information
buscarini authored and stephencelis committed Apr 9, 2019
1 parent 588090c commit d39bcf4
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
10 changes: 8 additions & 2 deletions Sources/Tagged/Tagged.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ extension Tagged: Comparable where RawValue: Comparable {

extension Tagged: Decodable where RawValue: Decodable {
public init(from decoder: Decoder) throws {
self.init(rawValue: try .init(from: decoder))
do {
self.init(rawValue: try decoder.singleValueContainer().decode(RawValue.self))
}
catch {
self.init(rawValue: try .init(from: decoder))
}
}
}

extension Tagged: Encodable where RawValue: Encodable {
public func encode(to encoder: Encoder) throws {
try rawValue.encode(to: encoder)
var container = encoder.singleValueContainer()
try container.encode(self.rawValue)
}
}

Expand Down
55 changes: 52 additions & 3 deletions Tests/TaggedTests/TaggedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,50 @@ final class TaggedTests: XCTestCase {
try JSONDecoder().decode([Tagged<Tag, Int>].self, from: Data("[1]".utf8))
)
}

func testDecodableCustomDates() {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom { decoder in
let seconds = try decoder.singleValueContainer().decode(Int.self)
return Date(timeIntervalSince1970: TimeInterval(seconds))
}

XCTAssertEqual(
[Date(timeIntervalSince1970: 1)],
try decoder.decode([Date].self, from: Data("[1]".utf8))
)

XCTAssertEqual(
[Tagged<Tag, Date>(rawValue: Date(timeIntervalSince1970: 1))],
try decoder.decode([Tagged<Tag, Date>].self, from: Data("[1]".utf8))
)
}

func testEncodable() {
XCTAssertEqual(
Data("[1]".utf8),
try JSONEncoder().encode([Tagged<Tag, Int>(rawValue: 1)])
)
}

func testEncodableCustomDates() {
let encoder = JSONEncoder()
encoder.dateEncodingStrategy = .custom { date, encoder in
var container = encoder.singleValueContainer()
let seconds = Int(date.timeIntervalSince1970)
try container.encode(seconds)
}

XCTAssertEqual(
Data("[1]".utf8),
try encoder.encode([Date(timeIntervalSince1970: 1)])
)

XCTAssertEqual(
Data("[1]".utf8),
try encoder.encode([Tagged<Tag, Date>(rawValue: Date(timeIntervalSince1970: 1))])
)
}

func testEquatable() {
XCTAssertEqual(Tagged<Tag, Int>(rawValue: 1), Tagged<Tag, Int>(rawValue: 1))
Expand Down Expand Up @@ -79,8 +116,8 @@ final class TaggedTests: XCTestCase {

func testOptionalRawTypeAndNilValueDecodesCorrectly() {
struct Container: Decodable {
typealias Idenitifer = Tagged<Container, String?>
let id: Idenitifer
typealias Identifier = Tagged<Container, String?>
let id: Identifier
}

XCTAssertNoThrow(try {
Expand All @@ -90,8 +127,20 @@ final class TaggedTests: XCTestCase {
XCTAssertEqual(containers.first?.id.rawValue, nil)
}())
}

func testOptionalRawTypeAndNilValueEncodesCorrectly() {
struct Container: Encodable {
typealias Identifier = Tagged<Container, String?>
let id: Identifier
}

XCTAssertNoThrow(try {
let data = try JSONEncoder().encode([Container(id: Tagged<Container, String?>(rawValue: nil))])
XCTAssertEqual(data, Data("[{\"id\":null}]".utf8))
}())
}

func testCoerce() {
func testCoerce() {
let x: Tagged<Tag, Int> = 1

enum Tag2 {}
Expand Down

0 comments on commit d39bcf4

Please sign in to comment.