-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from drekka/develop
Updates to property wrappers.
- Loading branch information
Showing
15 changed files
with
303 additions
and
106 deletions.
There are no files selected for viewing
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
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
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
27 changes: 27 additions & 0 deletions
27
Sources/Property wrappers/CodableAsEpochMilliseconds.swift
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,27 @@ | ||
// | ||
// EpochDay.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 9/1/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Identifies a ``Day`` property that reads and writes from an epoch time value expressed in seconds. | ||
@propertyWrapper | ||
public struct CodableAsEpochMilliseconds<T>: Codable where T: EpochCodable { | ||
|
||
public var wrappedValue: T | ||
|
||
public init(wrappedValue: T) { | ||
self.wrappedValue = wrappedValue | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
wrappedValue = try T(epochDecoder: decoder, factor: 0.001) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
try wrappedValue.encode(epochEncoder: encoder, factor: 0.001) | ||
} | ||
} |
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
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,52 @@ | ||
// | ||
// DayCodable.swift | ||
// | ||
// | ||
// Created by Derek Clarkson on 9/1/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Protocol that allows us to abstract the differences between ``Day`` and ``Day?``. | ||
/// | ||
/// By using this protocols for property wrappers we can reduce the number of wrappers needed because | ||
/// it erases the optional aspect of the values. | ||
public protocol EpochCodable { | ||
init(epochDecoder decoder: Decoder, factor: Double) throws | ||
func encode(epochEncoder encoder: Encoder, factor: Double) throws | ||
} | ||
|
||
extension Day: EpochCodable { | ||
|
||
public init(epochDecoder decoder: Decoder, factor: Double) throws { | ||
let container = try decoder.singleValueContainer() | ||
guard let epochTime = try? container.decode(TimeInterval.self) else { | ||
let context = DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Unable to read a Day value, expected an epoch.") | ||
throw DecodingError.dataCorrupted(context) | ||
} | ||
self = Day(date: Date(timeIntervalSince1970: epochTime * factor)) | ||
} | ||
|
||
public func encode(epochEncoder encoder: Encoder, factor: Double) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(date().timeIntervalSince1970 / factor) | ||
} | ||
} | ||
|
||
/// `Day?` support which mostly just handles `nil` before calling the main ``Day`` codable code. | ||
extension Day?: EpochCodable { | ||
|
||
public init(epochDecoder decoder: Decoder, factor: Double) throws { | ||
let container = try decoder.singleValueContainer() | ||
self = container.decodeNil() ? nil : try Day(epochDecoder: decoder, factor: factor) | ||
} | ||
|
||
public func encode(epochEncoder encoder: Encoder, factor: Double) throws { | ||
if let self { | ||
try self.encode(epochEncoder: encoder, factor: factor) | ||
} else { | ||
var container = encoder.singleValueContainer() | ||
try container.encodeNil() | ||
} | ||
} | ||
} |
Oops, something went wrong.