Skip to content

Commit 3b8b2a5

Browse files
authored
Unblocks nonmutating set on $TCDateWrappers (#55)
* Adds dependency on `NIOConcurrencyHelpers` to `TecoDateHelpers` * Uses `NIOLockedValueBox` to allow nonmutating set on `$dateWrapper`s * Simplifies `private` member naming for `TCDateWrapper`s
1 parent 6fddf6f commit 3b8b2a5

File tree

8 files changed

+62
-41
lines changed

8 files changed

+62
-41
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ let package = Package(
4343
]),
4444
.target(name: "TecoPaginationHelpers",
4545
dependencies: ["TecoCore"]),
46-
.target(name: "TecoDateHelpers"),
46+
.target(name: "TecoDateHelpers",
47+
dependencies: [.product(name: "NIOConcurrencyHelpers", package: "swift-nio")]),
4748
.target(name: "INIParser"),
4849
.target(
4950
name: "TecoSigner",

[email protected]

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ let package = Package(
4343
]),
4444
.target(name: "TecoPaginationHelpers",
4545
dependencies: ["TecoCore"]),
46-
.target(name: "TecoDateHelpers"),
46+
.target(name: "TecoDateHelpers",
47+
dependencies: [.product(name: "NIOConcurrencyHelpers", package: "swift-nio")]),
4748
.target(name: "INIParser"),
4849
.target(
4950
name: "TecoSigner",

[email protected]

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ let package = Package(
4343
]),
4444
.target(name: "TecoPaginationHelpers",
4545
dependencies: ["TecoCore"]),
46-
.target(name: "TecoDateHelpers"),
46+
.target(name: "TecoDateHelpers",
47+
dependencies: [.product(name: "NIOConcurrencyHelpers", package: "swift-nio")]),
4748
.target(name: "INIParser"),
4849
.target(
4950
name: "TecoSigner",

Sources/TecoDateHelpers/Property Wrappers/TCDateEncoding.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,41 @@ import struct Foundation.Date
1818
import struct Foundation.Locale
1919
import struct Foundation.TimeZone
2020
import class Foundation.DateFormatter
21+
@_implementationOnly import struct NIOConcurrencyHelpers.NIOLockedValueBox
2122

2223
@propertyWrapper
2324
public struct TCDateEncoding<WrappedValue: TCDateValue>: Codable {
2425
public var wrappedValue: WrappedValue {
25-
self._dateValue
26+
self.date
2627
}
2728

2829
public var projectedValue: StorageValue {
2930
get {
30-
self._stringValue
31+
self.string.withLockedValue {
32+
$0
33+
}
3134
}
32-
set {
33-
self._stringValue = newValue
35+
nonmutating set {
36+
self.string.withLockedValue {
37+
$0 = newValue
38+
}
3439
}
3540
}
3641

37-
private var _dateValue: WrappedValue
42+
private let date: WrappedValue
3843

39-
private var _stringValue: StorageValue
44+
private let string: NIOLockedValueBox<StorageValue>
4045

4146
public init(wrappedValue: WrappedValue) {
42-
self._dateValue = wrappedValue
43-
self._stringValue = wrappedValue.encode(formatter: Self._formatter)
47+
self.date = wrappedValue
48+
self.string = NIOLockedValueBox(wrappedValue.encode(formatter: Self._formatter))
4449
}
4550

4651
public init(from decoder: Decoder) throws {
4752
let container = try decoder.singleValueContainer()
48-
self._stringValue = try container.decode(StorageValue.self)
49-
self._dateValue = try WrappedValue.decode(from: self._stringValue, formatter: Self._formatter, container: container, wrapper: Self.self)
53+
let dateString = try container.decode(StorageValue.self)
54+
self.date = try WrappedValue.decode(from: dateString, formatter: Self._formatter, container: container, wrapper: Self.self)
55+
self.string = NIOLockedValueBox(dateString)
5056
}
5157
}
5258

Sources/TecoDateHelpers/Property Wrappers/TCTimestampEncoding.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,41 @@ import struct Foundation.Date
1818
import struct Foundation.Locale
1919
import struct Foundation.TimeZone
2020
import class Foundation.DateFormatter
21+
@_implementationOnly import struct NIOConcurrencyHelpers.NIOLockedValueBox
2122

2223
@propertyWrapper
2324
public struct TCTimestampEncoding<WrappedValue: TCDateValue>: Codable {
2425
public var wrappedValue: WrappedValue {
25-
self._dateValue
26+
self.date
2627
}
2728

2829
public var projectedValue: StorageValue {
2930
get {
30-
self._stringValue
31+
self.string.withLockedValue {
32+
$0
33+
}
3134
}
32-
set {
33-
self._stringValue = newValue
35+
nonmutating set {
36+
self.string.withLockedValue {
37+
$0 = newValue
38+
}
3439
}
3540
}
3641

37-
private var _dateValue: WrappedValue
42+
private let date: WrappedValue
3843

39-
private var _stringValue: StorageValue
44+
private let string: NIOLockedValueBox<StorageValue>
4045

4146
public init(wrappedValue: WrappedValue) {
42-
self._dateValue = wrappedValue
43-
self._stringValue = wrappedValue.encode(formatter: Self._formatter)
47+
self.date = wrappedValue
48+
self.string = NIOLockedValueBox(wrappedValue.encode(formatter: Self._formatter))
4449
}
4550

4651
public init(from decoder: Decoder) throws {
4752
let container = try decoder.singleValueContainer()
48-
self._stringValue = try container.decode(StorageValue.self)
49-
self._dateValue = try WrappedValue.decode(from: self._stringValue, formatter: Self._formatter, container: container, wrapper: Self.self)
53+
let dateString = try container.decode(StorageValue.self)
54+
self.date = try WrappedValue.decode(from: dateString, formatter: Self._formatter, container: container, wrapper: Self.self)
55+
self.string = NIOLockedValueBox(dateString)
5056
}
5157
}
5258

Sources/TecoDateHelpers/Property Wrappers/TCTimestampISO8601Encoding.swift

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,41 @@
1616

1717
import struct Foundation.Date
1818
import class Foundation.ISO8601DateFormatter
19+
@_implementationOnly import struct NIOConcurrencyHelpers.NIOLockedValueBox
1920

2021
@propertyWrapper
2122
public struct TCTimestampISO8601Encoding<WrappedValue: TCDateValue>: Codable {
2223
public var wrappedValue: WrappedValue {
23-
self._dateValue
24+
self.date
2425
}
2526

2627
public var projectedValue: StorageValue {
2728
get {
28-
self._stringValue
29+
self.string.withLockedValue {
30+
$0
31+
}
2932
}
30-
set {
31-
self._stringValue = newValue
33+
nonmutating set {
34+
self.string.withLockedValue {
35+
$0 = newValue
36+
}
3237
}
3338
}
3439

35-
private var _dateValue: WrappedValue
40+
private let date: WrappedValue
3641

37-
private var _stringValue: StorageValue
42+
private let string: NIOLockedValueBox<StorageValue>
3843

3944
public init(wrappedValue: WrappedValue) {
40-
self._dateValue = wrappedValue
41-
self._stringValue = wrappedValue.encode(formatter: Self._formatter)
45+
self.date = wrappedValue
46+
self.string = NIOLockedValueBox(wrappedValue.encode(formatter: Self._formatter))
4247
}
4348

4449
public init(from decoder: Decoder) throws {
4550
let container = try decoder.singleValueContainer()
46-
self._stringValue = try container.decode(StorageValue.self)
47-
self._dateValue = try WrappedValue.decode(from: self._stringValue, formatter: Self._formatter, container: container, wrapper: Self.self)
51+
let dateString = try container.decode(StorageValue.self)
52+
self.date = try WrappedValue.decode(from: dateString, formatter: Self._formatter, container: container, wrapper: Self.self)
53+
self.string = NIOLockedValueBox(dateString)
4854
}
4955
}
5056

Sources/TecoDateHelpers/Protocols/TCDateValue.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ extension Foundation.Date: TCDateValue {
2727
formatter.string(from: self)
2828
}
2929

30-
public static func decode<Wrapper: TCDateWrapper>(from stringValue: String, formatter: TCDateFormatter, container: SingleValueDecodingContainer, wrapper: Wrapper.Type = Wrapper.self) throws -> Date {
31-
guard let date = formatter.date(from: stringValue) else {
32-
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid \(wrapper._valueDescription): \(stringValue)")
30+
public static func decode<Wrapper: TCDateWrapper>(from string: String, formatter: TCDateFormatter, container: SingleValueDecodingContainer, wrapper: Wrapper.Type = Wrapper.self) throws -> Date {
31+
guard let date = formatter.date(from: string) else {
32+
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid \(wrapper._valueDescription): \(string)")
3333
}
3434
return date
3535
}
@@ -45,12 +45,12 @@ extension Swift.Optional: TCDateValue where Wrapped == Foundation.Date {
4545
}
4646
}
4747

48-
public static func decode<Wrapper: TCDateWrapper>(from stringValue: String?, formatter: TCDateFormatter, container: SingleValueDecodingContainer, wrapper: Wrapper.Type = Wrapper.self) throws -> Date? {
49-
guard let stringValue = stringValue else {
48+
public static func decode<Wrapper: TCDateWrapper>(from string: String?, formatter: TCDateFormatter, container: SingleValueDecodingContainer, wrapper: Wrapper.Type = Wrapper.self) throws -> Date? {
49+
guard let string = string else {
5050
return nil
5151
}
52-
guard let date = formatter.date(from: stringValue) else {
53-
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid \(wrapper._valueDescription): \(stringValue)")
52+
guard let date = formatter.date(from: string) else {
53+
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid \(wrapper._valueDescription): \(string)")
5454
}
5555
return date
5656
}

Sources/TecoDateHelpers/Protocols/TCDateWrapper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public protocol TCDateWrapper: Codable, _TecoDateSendable {
1616
associatedtype Formatter: TCDateFormatter
1717

1818
var wrappedValue: WrappedValue { get }
19-
var projectedValue: StorageValue { get }
19+
var projectedValue: StorageValue { get nonmutating set }
2020

2121
init(wrappedValue: WrappedValue)
2222

0 commit comments

Comments
 (0)