DataLireCoder swift package

This commit is contained in:
2025-04-27 12:53:43 +03:00
parent 2cca986016
commit 5aec6ea578
60 changed files with 7144 additions and 0 deletions

View File

@@ -0,0 +1,37 @@
import Foundation
private import DLCCommon
final class FailedEncoder: Swift.Encoder {
// MARK: - Properties
let codingPath: [any CodingKey]
let userInfo: [CodingUserInfoKey: Any]
// MARK: - Inits
init(
codingPath: [any CodingKey],
userInfo: [CodingUserInfoKey: Any] = [:]
) {
self.codingPath = codingPath
self.userInfo = userInfo
}
// MARK: - Methods
func container<Key: CodingKey>(
keyedBy type: Key.Type
) -> KeyedEncodingContainer<Key> {
let container = FailedEncodingContainer<Key>(codingPath: codingPath)
return KeyedEncodingContainer(container)
}
func unkeyedContainer() -> any UnkeyedEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
func singleValueContainer() -> any SingleValueEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
}

View File

@@ -0,0 +1,84 @@
import Foundation
private import DLCCommon
final class FailedEncodingContainer<Key: CodingKey>: SingleValueEncodingContainer, UnkeyedEncodingContainer, KeyedEncodingContainerProtocol {
// MARK: - Properties
let codingPath: [any CodingKey]
let count: Int = 0
// MARK: - Inits
init(codingPath: [any CodingKey]) {
self.codingPath = codingPath
}
// MARK: - Methods
func encodeNil() throws {
throw encodingError(codingPath: codingPath)
}
func encodeNil(forKey key: Key) throws {
throw encodingError(codingPath: codingPath + [key])
}
func encode<T: Encodable>(_ value: T) throws {
throw encodingError(codingPath: codingPath)
}
func encode<T: Encodable>(_ value: T, forKey key: Key) throws {
throw encodingError(codingPath: codingPath + [key])
}
func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type
) -> KeyedEncodingContainer<NestedKey> {
let container = FailedEncodingContainer<NestedKey>(
codingPath: codingPath
)
return KeyedEncodingContainer(container)
}
func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type,
forKey key: Key
) -> KeyedEncodingContainer<NestedKey> {
let container = FailedEncodingContainer<NestedKey>(
codingPath: codingPath + [key]
)
return KeyedEncodingContainer(container)
}
func nestedUnkeyedContainer() -> any UnkeyedEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
func nestedUnkeyedContainer(forKey key: Key) -> any UnkeyedEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath + [key])
}
func superEncoder() -> any Swift.Encoder {
FailedEncoder(codingPath: codingPath)
}
func superEncoder(forKey key: Key) -> any Swift.Encoder {
FailedEncoder(codingPath: codingPath + [key])
}
}
// MARK: - Private
private extension FailedEncodingContainer {
func encodingError(
_ function: String = #function,
codingPath: [any CodingKey]
) -> Error {
let info = "\(function) is not supported for this encoding path."
let context = EncodingError.Context(
codingPath: codingPath,
debugDescription: info
)
return EncodingError.invalidValue((), context)
}
}

View File

@@ -0,0 +1,201 @@
import Foundation
import DataLiteCore
private import DLCCommon
final class KeyedContainer<Encoder: RowEncoder, Key: CodingKey>: Container, KeyedEncodingContainerProtocol {
// MARK: - Properties
let encoder: Encoder
let codingPath: [any CodingKey]
// MARK: - Inits
init(
encoder: Encoder,
codingPath: [any CodingKey]
) {
self.encoder = encoder
self.codingPath = codingPath
}
// MARK: - Container Methods
func encodeNil(forKey key: Key) throws {
try encoder.encodeNil(for: key)
}
func encode<T: Encodable>(_ value: T, forKey key: Key) throws {
switch value {
case let value as Date:
try encoder.encodeDate(value, for: key)
case let value as SQLiteRawBindable:
try encoder.encode(value, for: key)
default:
let valueEncoder = try encoder.encoder(for: key)
try value.encode(to: valueEncoder)
try encoder.set(valueEncoder.sqliteData, for: key)
}
}
func encodeIfPresent(_ value: Bool?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: String?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: Double?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: Float?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: Int?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: Int8?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: Int16?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: Int32?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: Int64?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: UInt?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: UInt8?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func encodeIfPresent<T: Encodable>(_ value: T?, forKey key: Key) throws {
switch value {
case .some(let value):
try encode(value, forKey: key)
case .none:
try encodeNil(forKey: key)
}
}
func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type,
forKey key: Key
) -> KeyedEncodingContainer<NestedKey> {
let container = FailedEncodingContainer<NestedKey>(
codingPath: codingPath + [key]
)
return KeyedEncodingContainer(container)
}
func nestedUnkeyedContainer(
forKey key: Key
) -> any UnkeyedEncodingContainer {
FailedEncodingContainer<RowCodingKey>(
codingPath: codingPath + [key]
)
}
func superEncoder() -> any Swift.Encoder {
FailedEncoder(codingPath: codingPath)
}
func superEncoder(forKey key: Key) -> any Swift.Encoder {
FailedEncoder(codingPath: codingPath + [key])
}
}

View File

@@ -0,0 +1,95 @@
import Foundation
import DataLiteCore
private import DLCCommon
public final class MultiRowEncoder: RowEncoder {
// MARK: - Properties
public let dateEncoder: any DateEncoder
public let codingPath: [any CodingKey]
public let userInfo: [CodingUserInfoKey : Any]
public private(set) var sqliteData = [SQLiteRow]()
public var count: Int { sqliteData.count }
// MARK: - Inits
public init(
dateEncoder: any DateEncoder,
codingPath: [any CodingKey],
userInfo: [CodingUserInfoKey : Any]
) {
self.dateEncoder = dateEncoder
self.codingPath = codingPath
self.userInfo = userInfo
}
// MARK: - Methods
public func set(_ value: Any, for key: any CodingKey) throws {
guard let value = value as? SQLiteRow else {
let info = "Expected value of type \(SQLiteRow.self)"
let context = EncodingError.Context(
codingPath: codingPath + [key],
debugDescription: info
)
throw EncodingError.invalidValue(value, context)
}
sqliteData.append(value)
}
public func encodeNil(for key: any CodingKey) throws {
let value = Optional<Any>.none as Any
let info = "Attempted to encode nil, but it's not supported."
let context = EncodingError.Context(
codingPath: codingPath + [key],
debugDescription: info
)
throw EncodingError.invalidValue(value, context)
}
public func encodeDate(_ date: Date, for key: any CodingKey) throws {
let info = "Attempted to encode Date, but it's not supported."
let context = EncodingError.Context(
codingPath: codingPath + [key],
debugDescription: info
)
throw EncodingError.invalidValue(date, context)
}
public func encode<T: SQLiteRawBindable>(_ value: T, for key: any CodingKey) throws {
let info = "Attempted to encode \(T.self), but it's not supported."
let context = EncodingError.Context(
codingPath: codingPath + [key],
debugDescription: info
)
throw EncodingError.invalidValue(value, context)
}
public func encoder(for key: any CodingKey) throws -> any Encoder {
SingleRowEncoder(
dateEncoder: dateEncoder,
codingPath: codingPath + [key],
userInfo: userInfo
)
}
public func container<Key: CodingKey>(
keyedBy type: Key.Type
) -> KeyedEncodingContainer<Key> {
let container = FailedEncodingContainer<Key>(
codingPath: codingPath
)
return KeyedEncodingContainer(container)
}
public func unkeyedContainer() -> any UnkeyedEncodingContainer {
UnkeyedContainer(encoder: self, codingPath: codingPath)
}
public func singleValueContainer() -> any SingleValueEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
}

View File

@@ -0,0 +1,79 @@
import Foundation
import DataLiteCore
private import DLCCommon
public final class SingleRowEncoder: RowEncoder {
// MARK: - Properties
public let dateEncoder: any DateEncoder
public let codingPath: [any CodingKey]
public let userInfo: [CodingUserInfoKey : Any]
public private(set) var sqliteData = SQLiteRow()
public var count: Int { sqliteData.count }
// MARK: - Inits
public init(
dateEncoder: any DateEncoder,
codingPath: [any CodingKey],
userInfo: [CodingUserInfoKey : Any],
) {
self.dateEncoder = dateEncoder
self.codingPath = codingPath
self.userInfo = userInfo
}
// MARK: - Methods
public func set(_ value: Any, for key: any CodingKey) throws {
guard let value = value as? SQLiteRawValue else {
let info = "The value does not match \(SQLiteRawValue.self)"
let context = EncodingError.Context(
codingPath: codingPath + [key],
debugDescription: info
)
throw EncodingError.invalidValue(value, context)
}
sqliteData[key] = value
}
public func encodeNil(for key: any CodingKey) throws {
sqliteData[key] = .null
}
public func encodeDate(_ date: Date, for key: any CodingKey) throws {
try dateEncoder.encode(date, for: key, to: self)
}
public func encode<T: SQLiteRawBindable>(_ value: T, for key: any CodingKey) throws {
sqliteData[key] = value.sqliteRawValue
}
public func encoder(for key: any CodingKey) throws -> any Encoder {
SingleValueEncoder(
dateEncoder: dateEncoder,
codingPath: codingPath + [key],
userInfo: userInfo
)
}
public func container<Key: CodingKey>(
keyedBy type: Key.Type
) -> KeyedEncodingContainer<Key> {
let container = KeyedContainer<SingleRowEncoder, Key>(
encoder: self, codingPath: codingPath
)
return KeyedEncodingContainer(container)
}
public func unkeyedContainer() -> any UnkeyedEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
public func singleValueContainer() -> any SingleValueEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
}

View File

@@ -0,0 +1,36 @@
import Foundation
import DataLiteCore
final class SingleValueContainer<Encoder: ValueEncoder>: Container, SingleValueEncodingContainer {
// MARK: - Properties
let encoder: Encoder
let codingPath: [any CodingKey]
// MARK: - Inits
init(
encoder: Encoder,
codingPath: [any CodingKey]
) {
self.encoder = encoder
self.codingPath = codingPath
}
// MARK: - Container Methods
func encodeNil() throws {
try encoder.encodeNil()
}
func encode<T: Encodable>(_ value: T) throws {
switch value {
case let value as Date:
try encoder.encodeDate(value)
case let value as SQLiteRawBindable:
try encoder.encode(value)
default:
try value.encode(to: encoder)
}
}
}

View File

@@ -0,0 +1,55 @@
import Foundation
import DataLiteCore
private import DLCCommon
final class SingleValueEncoder: ValueEncoder {
// MARK: - Properties
let dateEncoder: any DateEncoder
let codingPath: [any CodingKey]
let userInfo: [CodingUserInfoKey: Any]
private(set) var sqliteData: SQLiteRawValue?
// MARK: - Inits
init(
dateEncoder: any DateEncoder,
codingPath: [any CodingKey],
userInfo: [CodingUserInfoKey: Any]
) {
self.dateEncoder = dateEncoder
self.codingPath = codingPath
self.userInfo = userInfo
}
// MARK: - Methods
func encodeNil() throws {
sqliteData = .null
}
func encodeDate(_ date: Date) throws {
try dateEncoder.encode(date, to: self)
}
func encode<T: SQLiteRawBindable>(_ value: T) throws {
sqliteData = value.sqliteRawValue
}
func container<Key: CodingKey>(
keyedBy type: Key.Type
) -> KeyedEncodingContainer<Key> {
let container = FailedEncodingContainer<Key>(codingPath: codingPath)
return KeyedEncodingContainer(container)
}
func unkeyedContainer() -> any UnkeyedEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
func singleValueContainer() -> any SingleValueEncodingContainer {
SingleValueContainer(encoder: self, codingPath: codingPath)
}
}

View File

@@ -0,0 +1,59 @@
import Foundation
import DataLiteCore
private import DLCCommon
final class UnkeyedContainer<Encoder: RowEncoder>: Container, UnkeyedEncodingContainer {
// MARK: - Properties
let encoder: Encoder
let codingPath: [any CodingKey]
var count: Int { encoder.count }
private var currentKey: CodingKey {
RowCodingKey(intValue: count)
}
// MARK: - Inits
init(encoder: Encoder, codingPath: [any CodingKey]) {
self.encoder = encoder
self.codingPath = codingPath
}
// MARK: - Container Methods
func encodeNil() throws {
}
func encode<T: Encodable>(_ value: T) throws {
if let value = value as? Flattenable {
if let value = value.flattened() as? Encodable {
try encode(value)
} else {
try encodeNil()
}
} else {
let valueEncoder = try encoder.encoder(for: currentKey)
try value.encode(to: valueEncoder)
try encoder.set(valueEncoder.sqliteData, for: currentKey)
}
}
func nestedContainer<NestedKey: CodingKey>(
keyedBy keyType: NestedKey.Type
) -> KeyedEncodingContainer<NestedKey> {
let container = FailedEncodingContainer<NestedKey>(
codingPath: codingPath
)
return KeyedEncodingContainer(container)
}
func nestedUnkeyedContainer() -> any UnkeyedEncodingContainer {
FailedEncodingContainer<RowCodingKey>(codingPath: codingPath)
}
func superEncoder() -> any Swift.Encoder {
FailedEncoder(codingPath: codingPath)
}
}