-
Notifications
You must be signed in to change notification settings - Fork 2
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 #3 from sidepelican/dateCoding
Add date coding strategy options
- Loading branch information
Showing
7 changed files
with
159 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,34 @@ | ||
import Foundation | ||
|
||
public protocol D1ParameterBindable: Sendable { | ||
func encodeToD1Parameter() -> String | ||
func encodeToD1Parameter(options: D1ParameterEncodingOptions) -> String | ||
} | ||
|
||
extension String: D1ParameterBindable { | ||
public func encodeToD1Parameter() -> String { | ||
public func encodeToD1Parameter(options: D1ParameterEncodingOptions) -> String { | ||
self | ||
} | ||
} | ||
|
||
extension Substring: D1ParameterBindable { | ||
public func encodeToD1Parameter() -> String { | ||
public func encodeToD1Parameter(options: D1ParameterEncodingOptions) -> String { | ||
String(self) | ||
} | ||
} | ||
|
||
extension Date: D1ParameterBindable { | ||
public func encodeToD1Parameter() -> String { | ||
DateFormatter.sqlite.string(from: self) | ||
public func encodeToD1Parameter(options: D1ParameterEncodingOptions) -> String { | ||
switch options.dateEncodingStrategy { | ||
case .secondsSince1970: | ||
return Int(timeIntervalSince1970).description | ||
case .millisecondsSince1970: | ||
return Int(timeIntervalSince1970 * 1000).description | ||
case .iso8601: | ||
return ISO8601DateFormatter().string(from: self) | ||
case .formatted(let formatter): | ||
return formatter.string(from: self) | ||
case .custom(let custom): | ||
return custom(self, options) | ||
} | ||
} | ||
} |
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,47 @@ | ||
import Foundation | ||
|
||
public protocol D1ParameterEncodingOptionKey { | ||
associatedtype Value: Sendable | ||
static var defaultValue: Self.Value { get } | ||
} | ||
|
||
public struct D1ParameterEncodingOptions: Sendable { | ||
private var storage: [ObjectIdentifier: any Sendable] = [:] | ||
|
||
public init() {} | ||
|
||
public subscript<Key: D1ParameterEncodingOptionKey>(key: Key.Type) -> Key.Value { | ||
get { | ||
let i = ObjectIdentifier(key) | ||
if let value = storage[i] as? Key.Value { | ||
return value | ||
} | ||
return Key.defaultValue | ||
} | ||
set { | ||
let i = ObjectIdentifier(key) | ||
storage[i] = newValue | ||
} | ||
} | ||
} | ||
|
||
public enum D1DateEncodingStrategy { | ||
case secondsSince1970 | ||
case millisecondsSince1970 | ||
case iso8601 | ||
case formatted(DateFormatter) | ||
@preconcurrency case custom(@Sendable (Date, D1ParameterEncodingOptions) -> String) | ||
} | ||
|
||
public struct D1DateEncodingStrategyKey: D1ParameterEncodingOptionKey { | ||
public static var defaultValue: D1DateEncodingStrategy { | ||
.secondsSince1970 | ||
} | ||
} | ||
|
||
extension D1ParameterEncodingOptions { | ||
public var dateEncodingStrategy: D1DateEncodingStrategy { | ||
get { self[D1DateEncodingStrategyKey.self] } | ||
set { self[D1DateEncodingStrategyKey.self] = newValue } | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
Sources/D1Kit/SQLiteDateFormatter.swift → Tests/D1KitTests/SQLiteDateFormatter.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