Merge branch 'feature/refactoring' into develop
This commit is contained in:
@@ -13,10 +13,7 @@ let package = Package(
|
|||||||
.library(name: "DataLiteCoder", targets: ["DataLiteCoder"])
|
.library(name: "DataLiteCoder", targets: ["DataLiteCoder"])
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(
|
.package(url: "https://github.com/angd-dev/data-lite-core.git", from: "1.0.0"),
|
||||||
url: "https://github.com/angd-dev/data-lite-core.git",
|
|
||||||
revision: "2b3ba846b06f865616787edfdb47f503317a0dc0"
|
|
||||||
),
|
|
||||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
|
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0")
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ It is designed to be used alongside DataLiteCore, which manages low-level intera
|
|||||||
|
|
||||||
To add DataLiteCoder to your project, use Swift Package Manager (SPM).
|
To add DataLiteCoder to your project, use Swift Package Manager (SPM).
|
||||||
|
|
||||||
> **Important:** The API of `DataLiteCoder` is currently unstable and may change without notice. It is **strongly recommended** to pin the dependency to a specific commit to ensure compatibility and avoid unexpected breakage when the API evolves.
|
|
||||||
|
|
||||||
### Adding to an Xcode Project
|
### Adding to an Xcode Project
|
||||||
|
|
||||||
1. Open your project in Xcode.
|
1. Open your project in Xcode.
|
||||||
@@ -38,7 +36,7 @@ import PackageDescription
|
|||||||
let package = Package(
|
let package = Package(
|
||||||
name: "YourProject",
|
name: "YourProject",
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/angd-dev/data-lite-coder.git", branch: "develop")
|
.package(url: "https://github.com/angd-dev/data-lite-coder.git", from: "1.0.0")
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.target(
|
.target(
|
||||||
@@ -53,7 +51,7 @@ let package = Package(
|
|||||||
|
|
||||||
## Additional Resources
|
## Additional Resources
|
||||||
|
|
||||||
For more information and usage examples, see the [documentation](https://docs.angd.dev/?package=data-lite-coder&version=develop).
|
For more information and usage examples, see the [documentation](https://docs.angd.dev/?package=data-lite-coder&version=1.0.0).
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import DataLiteCore
|
import DataLiteCore
|
||||||
|
|
||||||
public extension SQLiteRow {
|
package extension SQLiteRow {
|
||||||
func contains(_ key: CodingKey) -> Bool {
|
func contains(_ key: CodingKey) -> Bool {
|
||||||
if let index = key.intValue {
|
if let index = key.intValue {
|
||||||
0..<count ~= index
|
0..<count ~= index
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public struct RowCodingKey: CodingKey, Equatable {
|
package struct RowCodingKey: CodingKey, Equatable {
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
|
|
||||||
public let stringValue: String
|
package let stringValue: String
|
||||||
public let intValue: Int?
|
package let intValue: Int?
|
||||||
|
|
||||||
// MARK: - Inits
|
// MARK: - Inits
|
||||||
|
|
||||||
public init(stringValue: String) {
|
package init(stringValue: String) {
|
||||||
self.stringValue = stringValue
|
self.stringValue = stringValue
|
||||||
self.intValue = nil
|
self.intValue = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(intValue: Int) {
|
package init(intValue: Int) {
|
||||||
self.stringValue = "Index \(intValue)"
|
self.stringValue = "Index \(intValue)"
|
||||||
self.intValue = intValue
|
self.intValue = intValue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,19 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import DataLiteCore
|
import DataLiteCore
|
||||||
|
|
||||||
public final class MultiRowDecoder: RowDecoder {
|
package final class MultiRowDecoder: RowDecoder {
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
|
|
||||||
public let dateDecoder: any DateDecoder
|
package let dateDecoder: any DateDecoder
|
||||||
public let sqliteData: [SQLiteRow]
|
package let sqliteData: [SQLiteRow]
|
||||||
public let codingPath: [any CodingKey]
|
package let codingPath: [any CodingKey]
|
||||||
public let userInfo: [CodingUserInfoKey: Any]
|
package let userInfo: [CodingUserInfoKey: Any]
|
||||||
|
|
||||||
public var count: Int? { sqliteData.count }
|
package var count: Int? { sqliteData.count }
|
||||||
|
|
||||||
// MARK: Inits
|
// MARK: Inits
|
||||||
|
|
||||||
public init(
|
package init(
|
||||||
dateDecoder: any DateDecoder,
|
dateDecoder: any DateDecoder,
|
||||||
sqliteData: [SQLiteRow],
|
sqliteData: [SQLiteRow],
|
||||||
codingPath: [any CodingKey],
|
codingPath: [any CodingKey],
|
||||||
@@ -27,7 +27,7 @@ public final class MultiRowDecoder: RowDecoder {
|
|||||||
|
|
||||||
// MARK: Methods
|
// MARK: Methods
|
||||||
|
|
||||||
public func decodeNil(for key: any CodingKey) throws -> Bool {
|
package func decodeNil(for key: any CodingKey) throws -> Bool {
|
||||||
let info = "Attempted to decode nil, but it's not supported for an array of rows."
|
let info = "Attempted to decode nil, but it's not supported for an array of rows."
|
||||||
let context = DecodingError.Context(
|
let context = DecodingError.Context(
|
||||||
codingPath: codingPath + [key],
|
codingPath: codingPath + [key],
|
||||||
@@ -36,11 +36,11 @@ public final class MultiRowDecoder: RowDecoder {
|
|||||||
throw DecodingError.dataCorrupted(context)
|
throw DecodingError.dataCorrupted(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decodeDate(for key: any CodingKey) throws -> Date {
|
package func decodeDate(for key: any CodingKey) throws -> Date {
|
||||||
return try decode(Date.self, for: key)
|
return try decode(Date.self, for: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decode<T: SQLiteRepresentable>(
|
package func decode<T: SQLiteRepresentable>(
|
||||||
_ type: T.Type,
|
_ type: T.Type,
|
||||||
for key: any CodingKey
|
for key: any CodingKey
|
||||||
) throws -> T {
|
) throws -> T {
|
||||||
@@ -52,7 +52,7 @@ public final class MultiRowDecoder: RowDecoder {
|
|||||||
throw DecodingError.typeMismatch(type, context)
|
throw DecodingError.typeMismatch(type, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decoder(for key: any CodingKey) throws -> any Decoder {
|
package func decoder(for key: any CodingKey) throws -> any Decoder {
|
||||||
guard let index = key.intValue else {
|
guard let index = key.intValue else {
|
||||||
let info = "Expected an integer key, but found a non-integer key."
|
let info = "Expected an integer key, but found a non-integer key."
|
||||||
let context = DecodingError.Context(
|
let context = DecodingError.Context(
|
||||||
@@ -69,7 +69,7 @@ public final class MultiRowDecoder: RowDecoder {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func container<Key: CodingKey>(
|
package func container<Key: CodingKey>(
|
||||||
keyedBy type: Key.Type
|
keyedBy type: Key.Type
|
||||||
) throws -> KeyedDecodingContainer<Key> {
|
) throws -> KeyedDecodingContainer<Key> {
|
||||||
let info = "Expected a keyed container, but found an array of rows."
|
let info = "Expected a keyed container, but found an array of rows."
|
||||||
@@ -83,14 +83,14 @@ public final class MultiRowDecoder: RowDecoder {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
|
package func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
|
||||||
UnkeyedContainer(
|
UnkeyedContainer(
|
||||||
decoder: self,
|
decoder: self,
|
||||||
codingPath: codingPath
|
codingPath: codingPath
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func singleValueContainer() throws -> any SingleValueDecodingContainer {
|
package func singleValueContainer() throws -> any SingleValueDecodingContainer {
|
||||||
let info = "Expected a single value container, but found an array of rows."
|
let info = "Expected a single value container, but found an array of rows."
|
||||||
let context = DecodingError.Context(
|
let context = DecodingError.Context(
|
||||||
codingPath: codingPath,
|
codingPath: codingPath,
|
||||||
|
|||||||
@@ -3,19 +3,19 @@ import DataLiteCore
|
|||||||
|
|
||||||
private import DLCCommon
|
private import DLCCommon
|
||||||
|
|
||||||
public final class SingleRowDecoder: RowDecoder, KeyCheckingDecoder {
|
package final class SingleRowDecoder: RowDecoder, KeyCheckingDecoder {
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
|
|
||||||
public let dateDecoder: any DateDecoder
|
package let dateDecoder: any DateDecoder
|
||||||
public let sqliteData: SQLiteRow
|
package let sqliteData: SQLiteRow
|
||||||
public let codingPath: [any CodingKey]
|
package let codingPath: [any CodingKey]
|
||||||
public let userInfo: [CodingUserInfoKey: Any]
|
package let userInfo: [CodingUserInfoKey: Any]
|
||||||
|
|
||||||
public var count: Int? { sqliteData.count }
|
package var count: Int? { sqliteData.count }
|
||||||
|
|
||||||
// MARK: Inits
|
// MARK: Inits
|
||||||
|
|
||||||
public init(
|
package init(
|
||||||
dateDecoder: any DateDecoder,
|
dateDecoder: any DateDecoder,
|
||||||
sqliteData: SQLiteRow,
|
sqliteData: SQLiteRow,
|
||||||
codingPath: [any CodingKey],
|
codingPath: [any CodingKey],
|
||||||
@@ -29,11 +29,11 @@ public final class SingleRowDecoder: RowDecoder, KeyCheckingDecoder {
|
|||||||
|
|
||||||
// MARK: - Methods
|
// MARK: - Methods
|
||||||
|
|
||||||
public func contains(_ key: any CodingKey) -> Bool {
|
package func contains(_ key: any CodingKey) -> Bool {
|
||||||
sqliteData.contains(key)
|
sqliteData.contains(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decodeNil(for key: any CodingKey) throws -> Bool {
|
package func decodeNil(for key: any CodingKey) throws -> Bool {
|
||||||
guard sqliteData.contains(key) else {
|
guard sqliteData.contains(key) else {
|
||||||
let info = "No value associated with key \(key)."
|
let info = "No value associated with key \(key)."
|
||||||
let context = DecodingError.Context(
|
let context = DecodingError.Context(
|
||||||
@@ -45,11 +45,11 @@ public final class SingleRowDecoder: RowDecoder, KeyCheckingDecoder {
|
|||||||
return sqliteData[key] == .null
|
return sqliteData[key] == .null
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decodeDate(for key: any CodingKey) throws -> Date {
|
package func decodeDate(for key: any CodingKey) throws -> Date {
|
||||||
try dateDecoder.decode(from: self, for: key)
|
try dateDecoder.decode(from: self, for: key)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decode<T: SQLiteRepresentable>(
|
package func decode<T: SQLiteRepresentable>(
|
||||||
_ type: T.Type,
|
_ type: T.Type,
|
||||||
for key: any CodingKey
|
for key: any CodingKey
|
||||||
) throws -> T {
|
) throws -> T {
|
||||||
@@ -83,7 +83,7 @@ public final class SingleRowDecoder: RowDecoder, KeyCheckingDecoder {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
public func decoder(for key: any CodingKey) throws -> any Decoder {
|
package func decoder(for key: any CodingKey) throws -> any Decoder {
|
||||||
guard let data = sqliteData[key] else {
|
guard let data = sqliteData[key] else {
|
||||||
let info = "No value associated with key \(key)."
|
let info = "No value associated with key \(key)."
|
||||||
let context = DecodingError.Context(
|
let context = DecodingError.Context(
|
||||||
@@ -100,7 +100,7 @@ public final class SingleRowDecoder: RowDecoder, KeyCheckingDecoder {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func container<Key: CodingKey>(
|
package func container<Key: CodingKey>(
|
||||||
keyedBy type: Key.Type
|
keyedBy type: Key.Type
|
||||||
) throws -> KeyedDecodingContainer<Key> {
|
) throws -> KeyedDecodingContainer<Key> {
|
||||||
let allKeys = sqliteData.compactMap { (column, _) in
|
let allKeys = sqliteData.compactMap { (column, _) in
|
||||||
@@ -114,14 +114,14 @@ public final class SingleRowDecoder: RowDecoder, KeyCheckingDecoder {
|
|||||||
return KeyedDecodingContainer(container)
|
return KeyedDecodingContainer(container)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
|
package func unkeyedContainer() throws -> any UnkeyedDecodingContainer {
|
||||||
UnkeyedContainer(
|
UnkeyedContainer(
|
||||||
decoder: self,
|
decoder: self,
|
||||||
codingPath: codingPath
|
codingPath: codingPath
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func singleValueContainer() throws -> any SingleValueDecodingContainer {
|
package func singleValueContainer() throws -> any SingleValueDecodingContainer {
|
||||||
let info = "Expected a single value container, but found a row value."
|
let info = "Expected a single value container, but found a row value."
|
||||||
let context = DecodingError.Context(
|
let context = DecodingError.Context(
|
||||||
codingPath: codingPath,
|
codingPath: codingPath,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public protocol DateDecoder {
|
package protocol DateDecoder {
|
||||||
func decode(from decoder: any ValueDecoder) throws -> Date
|
func decode(from decoder: any ValueDecoder) throws -> Date
|
||||||
func decode(from decoder: any RowDecoder, for key: any CodingKey) throws -> Date
|
func decode(from decoder: any RowDecoder, for key: any CodingKey) throws -> Date
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public protocol Decoder: Swift.Decoder {
|
package protocol Decoder: Swift.Decoder {
|
||||||
associatedtype SQLiteData
|
associatedtype SQLiteData
|
||||||
|
|
||||||
var dateDecoder: any DateDecoder { get }
|
var dateDecoder: any DateDecoder { get }
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public protocol KeyCheckingDecoder: Decoder {
|
package protocol KeyCheckingDecoder: Decoder {
|
||||||
func contains(_ key: CodingKey) -> Bool
|
func contains(_ key: CodingKey) -> Bool
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import DataLiteCore
|
import DataLiteCore
|
||||||
|
|
||||||
public protocol RowDecoder: Decoder {
|
package protocol RowDecoder: Decoder {
|
||||||
var count: Int? { get }
|
var count: Int? { get }
|
||||||
|
|
||||||
func decodeNil(for key: CodingKey) throws -> Bool
|
func decodeNil(for key: CodingKey) throws -> Bool
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import DataLiteCore
|
import DataLiteCore
|
||||||
|
|
||||||
public protocol ValueDecoder: Decoder {
|
package protocol ValueDecoder: Decoder {
|
||||||
func decodeNil() -> Bool
|
func decodeNil() -> Bool
|
||||||
func decodeDate() throws -> Date
|
func decodeDate() throws -> Date
|
||||||
func decode<T: SQLiteRepresentable>(_ type: T.Type) throws -> T
|
func decode<T: SQLiteRepresentable>(_ type: T.Type) throws -> T
|
||||||
|
|||||||
@@ -3,20 +3,20 @@ import DataLiteCore
|
|||||||
|
|
||||||
private import DLCCommon
|
private import DLCCommon
|
||||||
|
|
||||||
public final class MultiRowEncoder: RowEncoder {
|
package final class MultiRowEncoder: RowEncoder {
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
|
|
||||||
public let dateEncoder: any DateEncoder
|
package let dateEncoder: any DateEncoder
|
||||||
public let codingPath: [any CodingKey]
|
package let codingPath: [any CodingKey]
|
||||||
public let userInfo: [CodingUserInfoKey : Any]
|
package let userInfo: [CodingUserInfoKey : Any]
|
||||||
|
|
||||||
public private(set) var sqliteData = [SQLiteRow]()
|
package private(set) var sqliteData = [SQLiteRow]()
|
||||||
|
|
||||||
public var count: Int { sqliteData.count }
|
package var count: Int { sqliteData.count }
|
||||||
|
|
||||||
// MARK: - Inits
|
// MARK: - Inits
|
||||||
|
|
||||||
public init(
|
package init(
|
||||||
dateEncoder: any DateEncoder,
|
dateEncoder: any DateEncoder,
|
||||||
codingPath: [any CodingKey],
|
codingPath: [any CodingKey],
|
||||||
userInfo: [CodingUserInfoKey : Any]
|
userInfo: [CodingUserInfoKey : Any]
|
||||||
@@ -28,7 +28,7 @@ public final class MultiRowEncoder: RowEncoder {
|
|||||||
|
|
||||||
// MARK: - Methods
|
// MARK: - Methods
|
||||||
|
|
||||||
public func set(_ value: Any, for key: any CodingKey) throws {
|
package func set(_ value: Any, for key: any CodingKey) throws {
|
||||||
guard let value = value as? SQLiteRow else {
|
guard let value = value as? SQLiteRow else {
|
||||||
let info = "Expected value of type \(SQLiteRow.self)"
|
let info = "Expected value of type \(SQLiteRow.self)"
|
||||||
let context = EncodingError.Context(
|
let context = EncodingError.Context(
|
||||||
@@ -40,7 +40,7 @@ public final class MultiRowEncoder: RowEncoder {
|
|||||||
sqliteData.append(value)
|
sqliteData.append(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encodeNil(for key: any CodingKey) throws {
|
package func encodeNil(for key: any CodingKey) throws {
|
||||||
let value = Optional<Any>.none as Any
|
let value = Optional<Any>.none as Any
|
||||||
let info = "Attempted to encode nil, but it's not supported."
|
let info = "Attempted to encode nil, but it's not supported."
|
||||||
let context = EncodingError.Context(
|
let context = EncodingError.Context(
|
||||||
@@ -50,7 +50,7 @@ public final class MultiRowEncoder: RowEncoder {
|
|||||||
throw EncodingError.invalidValue(value, context)
|
throw EncodingError.invalidValue(value, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encodeDate(_ date: Date, for key: any CodingKey) throws {
|
package func encodeDate(_ date: Date, for key: any CodingKey) throws {
|
||||||
let info = "Attempted to encode Date, but it's not supported."
|
let info = "Attempted to encode Date, but it's not supported."
|
||||||
let context = EncodingError.Context(
|
let context = EncodingError.Context(
|
||||||
codingPath: codingPath + [key],
|
codingPath: codingPath + [key],
|
||||||
@@ -59,7 +59,7 @@ public final class MultiRowEncoder: RowEncoder {
|
|||||||
throw EncodingError.invalidValue(date, context)
|
throw EncodingError.invalidValue(date, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encode<T: SQLiteBindable>(_ value: T, for key: any CodingKey) throws {
|
package func encode<T: SQLiteBindable>(_ value: T, for key: any CodingKey) throws {
|
||||||
let info = "Attempted to encode \(T.self), but it's not supported."
|
let info = "Attempted to encode \(T.self), but it's not supported."
|
||||||
let context = EncodingError.Context(
|
let context = EncodingError.Context(
|
||||||
codingPath: codingPath + [key],
|
codingPath: codingPath + [key],
|
||||||
@@ -68,7 +68,7 @@ public final class MultiRowEncoder: RowEncoder {
|
|||||||
throw EncodingError.invalidValue(value, context)
|
throw EncodingError.invalidValue(value, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encoder(for key: any CodingKey) throws -> any Encoder {
|
package func encoder(for key: any CodingKey) throws -> any Encoder {
|
||||||
SingleRowEncoder(
|
SingleRowEncoder(
|
||||||
dateEncoder: dateEncoder,
|
dateEncoder: dateEncoder,
|
||||||
codingPath: codingPath + [key],
|
codingPath: codingPath + [key],
|
||||||
@@ -76,7 +76,7 @@ public final class MultiRowEncoder: RowEncoder {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func container<Key: CodingKey>(
|
package func container<Key: CodingKey>(
|
||||||
keyedBy type: Key.Type
|
keyedBy type: Key.Type
|
||||||
) -> KeyedEncodingContainer<Key> {
|
) -> KeyedEncodingContainer<Key> {
|
||||||
let container = FailedEncodingContainer<Key>(
|
let container = FailedEncodingContainer<Key>(
|
||||||
@@ -85,11 +85,11 @@ public final class MultiRowEncoder: RowEncoder {
|
|||||||
return KeyedEncodingContainer(container)
|
return KeyedEncodingContainer(container)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func unkeyedContainer() -> any UnkeyedEncodingContainer {
|
package func unkeyedContainer() -> any UnkeyedEncodingContainer {
|
||||||
UnkeyedContainer(encoder: self, codingPath: codingPath)
|
UnkeyedContainer(encoder: self, codingPath: codingPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func singleValueContainer() -> any SingleValueEncodingContainer {
|
package func singleValueContainer() -> any SingleValueEncodingContainer {
|
||||||
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
|
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,20 +3,20 @@ import DataLiteCore
|
|||||||
|
|
||||||
private import DLCCommon
|
private import DLCCommon
|
||||||
|
|
||||||
public final class SingleRowEncoder: RowEncoder {
|
package final class SingleRowEncoder: RowEncoder {
|
||||||
// MARK: - Properties
|
// MARK: - Properties
|
||||||
|
|
||||||
public let dateEncoder: any DateEncoder
|
package let dateEncoder: any DateEncoder
|
||||||
public let codingPath: [any CodingKey]
|
package let codingPath: [any CodingKey]
|
||||||
public let userInfo: [CodingUserInfoKey : Any]
|
package let userInfo: [CodingUserInfoKey : Any]
|
||||||
|
|
||||||
public private(set) var sqliteData = SQLiteRow()
|
package private(set) var sqliteData = SQLiteRow()
|
||||||
|
|
||||||
public var count: Int { sqliteData.count }
|
package var count: Int { sqliteData.count }
|
||||||
|
|
||||||
// MARK: - Inits
|
// MARK: - Inits
|
||||||
|
|
||||||
public init(
|
package init(
|
||||||
dateEncoder: any DateEncoder,
|
dateEncoder: any DateEncoder,
|
||||||
codingPath: [any CodingKey],
|
codingPath: [any CodingKey],
|
||||||
userInfo: [CodingUserInfoKey : Any],
|
userInfo: [CodingUserInfoKey : Any],
|
||||||
@@ -28,7 +28,7 @@ public final class SingleRowEncoder: RowEncoder {
|
|||||||
|
|
||||||
// MARK: - Methods
|
// MARK: - Methods
|
||||||
|
|
||||||
public func set(_ value: Any, for key: any CodingKey) throws {
|
package func set(_ value: Any, for key: any CodingKey) throws {
|
||||||
guard let value = value as? SQLiteValue else {
|
guard let value = value as? SQLiteValue else {
|
||||||
let info = "The value does not match \(SQLiteValue.self)"
|
let info = "The value does not match \(SQLiteValue.self)"
|
||||||
let context = EncodingError.Context(
|
let context = EncodingError.Context(
|
||||||
@@ -40,19 +40,19 @@ public final class SingleRowEncoder: RowEncoder {
|
|||||||
sqliteData[key] = value
|
sqliteData[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encodeNil(for key: any CodingKey) throws {
|
package func encodeNil(for key: any CodingKey) throws {
|
||||||
sqliteData[key] = .null
|
sqliteData[key] = .null
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encodeDate(_ date: Date, for key: any CodingKey) throws {
|
package func encodeDate(_ date: Date, for key: any CodingKey) throws {
|
||||||
try dateEncoder.encode(date, for: key, to: self)
|
try dateEncoder.encode(date, for: key, to: self)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encode<T: SQLiteBindable>(_ value: T, for key: any CodingKey) throws {
|
package func encode<T: SQLiteBindable>(_ value: T, for key: any CodingKey) throws {
|
||||||
sqliteData[key] = value.sqliteValue
|
sqliteData[key] = value.sqliteValue
|
||||||
}
|
}
|
||||||
|
|
||||||
public func encoder(for key: any CodingKey) throws -> any Encoder {
|
package func encoder(for key: any CodingKey) throws -> any Encoder {
|
||||||
SingleValueEncoder(
|
SingleValueEncoder(
|
||||||
dateEncoder: dateEncoder,
|
dateEncoder: dateEncoder,
|
||||||
codingPath: codingPath + [key],
|
codingPath: codingPath + [key],
|
||||||
@@ -60,7 +60,7 @@ public final class SingleRowEncoder: RowEncoder {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func container<Key: CodingKey>(
|
package func container<Key: CodingKey>(
|
||||||
keyedBy type: Key.Type
|
keyedBy type: Key.Type
|
||||||
) -> KeyedEncodingContainer<Key> {
|
) -> KeyedEncodingContainer<Key> {
|
||||||
let container = KeyedContainer<SingleRowEncoder, Key>(
|
let container = KeyedContainer<SingleRowEncoder, Key>(
|
||||||
@@ -69,11 +69,11 @@ public final class SingleRowEncoder: RowEncoder {
|
|||||||
return KeyedEncodingContainer(container)
|
return KeyedEncodingContainer(container)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func unkeyedContainer() -> any UnkeyedEncodingContainer {
|
package func unkeyedContainer() -> any UnkeyedEncodingContainer {
|
||||||
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
|
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
public func singleValueContainer() -> any SingleValueEncodingContainer {
|
package func singleValueContainer() -> any SingleValueEncodingContainer {
|
||||||
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
|
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public protocol DateEncoder {
|
package protocol DateEncoder {
|
||||||
func encode(_ date: Date, to encoder: any ValueEncoder) throws
|
func encode(_ date: Date, to encoder: any ValueEncoder) throws
|
||||||
func encode(_ date: Date, for key: any CodingKey, to encoder: any RowEncoder) throws
|
func encode(_ date: Date, for key: any CodingKey, to encoder: any RowEncoder) throws
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
public protocol Encoder: Swift.Encoder {
|
package protocol Encoder: Swift.Encoder {
|
||||||
associatedtype SQLiteData
|
associatedtype SQLiteData
|
||||||
|
|
||||||
var dateEncoder: any DateEncoder { get }
|
var dateEncoder: any DateEncoder { get }
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import DataLiteCore
|
import DataLiteCore
|
||||||
|
|
||||||
public protocol RowEncoder: Encoder {
|
package protocol RowEncoder: Encoder {
|
||||||
var count: Int { get }
|
var count: Int { get }
|
||||||
|
|
||||||
func set(_ value: Any, for key: CodingKey) throws
|
func set(_ value: Any, for key: CodingKey) throws
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import DataLiteCore
|
import DataLiteCore
|
||||||
|
|
||||||
public protocol ValueEncoder: Encoder {
|
package protocol ValueEncoder: Encoder {
|
||||||
func encodeNil() throws
|
func encodeNil() throws
|
||||||
func encodeDate(_ date: Date) throws
|
func encodeDate(_ date: Date) throws
|
||||||
func encode<T: SQLiteBindable>(_ value: T) throws
|
func encode<T: SQLiteBindable>(_ value: T) throws
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
internal import DLCDecoder
|
internal import DLCDecoder
|
||||||
|
|
||||||
extension RowDecoder {
|
extension RowDecoder {
|
||||||
|
|||||||
Reference in New Issue
Block a user