-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Swift - UniFFI boundary - update
into_custom
and from_custom
espe…
…cially for date. (#153) Swift - change UniFFI into_custom and from_custom to use ISO8601DateFormatter.
- Loading branch information
1 parent
e9a5edd
commit 2743c0a
Showing
6 changed files
with
129 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "sargon" | ||
version = "1.0.6" | ||
version = "1.0.7" | ||
edition = "2021" | ||
build = "build.rs" | ||
|
||
|
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,74 @@ | ||
import CustomDump | ||
import Foundation | ||
import Sargon | ||
import SargonUniFFI | ||
import XCTest | ||
|
||
final class DateTests: TestCase { | ||
|
||
func test_date() { | ||
|
||
/// This matches `[bindings.swift.custom_types.Timestamp]` | ||
/// inside `uniffi.toml` with the only difference that we use `$0` here but | ||
/// the toml uses `{}`; | ||
let into_custom: (String) -> Date = { | ||
let stringToDeserialize = $0 | ||
let formatter = ISO8601DateFormatter() | ||
let formatOptionMS = ISO8601DateFormatter.Options.withFractionalSeconds | ||
formatter.formatOptions.insert(formatOptionMS) | ||
|
||
func format() -> Date? { | ||
formatter.date(from: stringToDeserialize) | ||
} | ||
|
||
if let date = format() { | ||
return date | ||
} | ||
|
||
// try without fractional seconds | ||
formatter.formatOptions.remove(formatOptionMS) | ||
return format()! | ||
} | ||
|
||
/// This matches `[bindings.swift.custom_types.Timestamp]` | ||
/// inside `uniffi.toml` | ||
let from_custom: (Date) -> String = { | ||
let dateToSerialize = $0 | ||
let formatter = ISO8601DateFormatter() | ||
formatter.formatOptions.insert(.withFractionalSeconds) | ||
return formatter.string(from: dateToSerialize) | ||
} | ||
|
||
func testIntoThenFrom(_ vector: (String, String?)) { | ||
let sut = vector.0 | ||
let expected = vector.1 ?? sut | ||
|
||
let string = from_custom(into_custom(sut)) | ||
|
||
XCTAssertEqual(string, expected) | ||
} | ||
|
||
func testFromThenInto(_ vector: (String, String?)) { | ||
let lhs = vector.0 | ||
let rhs = vector.1 ?? lhs | ||
|
||
let lhsString = from_custom(into_custom(lhs)) | ||
let rhsString = from_custom(into_custom(rhs)) | ||
|
||
XCTAssertEqual(rhsString, rhs) | ||
|
||
XCTAssertEqual( | ||
into_custom(lhsString), | ||
into_custom(rhsString) | ||
) | ||
} | ||
|
||
let vectors = [ | ||
("2023-12-24T17:13:56.123456Z", "2023-12-24T17:13:56.123Z"), // precision lost (which is OK) | ||
("2023-12-24T17:13:56.123Z", nil), // unchanged | ||
("2023-12-24T17:13:56Z", "2023-12-24T17:13:56.000Z") // (000 added, which is OK) | ||
] | ||
vectors.forEach(testIntoThenFrom) | ||
vectors.forEach(testFromThenInto) | ||
} | ||
} |
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,24 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Alexander Cyon on 2024-05-31. | ||
// | ||
|
||
import Foundation | ||
|
||
extension JSONEncoder { | ||
static var iso8601: JSONEncoder { | ||
let encoder = JSONEncoder() | ||
encoder.dateEncodingStrategy = .iso8601 | ||
return encoder | ||
} | ||
} | ||
|
||
extension JSONDecoder { | ||
static var iso8601: JSONDecoder { | ||
let decoder = JSONDecoder() | ||
decoder.dateDecodingStrategy = .iso8601 | ||
return decoder | ||
} | ||
} |
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