Skip to content

Commit

Permalink
array support (#65)
Browse files Browse the repository at this point in the history
* array support

* fatalError -> Error

* fix typo

* fix fatal errors
  • Loading branch information
tanner0101 authored Dec 11, 2019
1 parent 4b31a9f commit f25b68e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
19 changes: 13 additions & 6 deletions Sources/SQLiteKit/SQLiteDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,23 @@ public struct SQLiteDataDecoder {
}

func unkeyedContainer() throws -> UnkeyedDecodingContainer {
fatalError()
try self.jsonDecoder().unkeyedContainer()
}

func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
guard case .blob(var buffer) = self.data else {
fatalError()
try self.jsonDecoder().container(keyedBy: Key.self)
}

func jsonDecoder() throws -> Decoder {
guard case .blob(let buffer) = self.data else {
throw DecodingError.valueNotFound(Any.self, .init(
codingPath: self.codingPath,
debugDescription: "Cannot decode JSON from nil data"
))
}
let data = buffer.readBytes(length: buffer.readableBytes)!
let unwrapper = try JSONDecoder().decode(DecoderUnwrapper.self, from: Data(data))
return try unwrapper.decoder.container(keyedBy: Key.self)
return try JSONDecoder()
.decode(DecoderUnwrapper.self, from: Data(buffer.readableBytesView))
.decoder
}

func singleValueContainer() throws -> SingleValueDecodingContainer {
Expand Down
62 changes: 54 additions & 8 deletions Sources/SQLiteKit/SQLiteDataEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public struct SQLiteDataEncoder {
}

func unkeyedContainer() -> UnkeyedEncodingContainer {
fatalError()
_UnkeyedEncodingContainer(self)
}

func singleValueContainer() -> SingleValueEncodingContainer {
return _SingleValueEncoder(self)
_SingleValueEncoder(self)
}
}

Expand All @@ -60,6 +60,47 @@ public struct SQLiteDataEncoder {
}
}

private struct _UnkeyedEncodingContainer: UnkeyedEncodingContainer {
var codingPath: [CodingKey] {
self.encoder.codingPath
}
var count: Int {
0
}

let encoder: _Encoder
init(_ encoder: _Encoder) {
self.encoder = encoder
}

mutating func encodeNil() throws {
throw DoJSON()
}

mutating func encode<T>(_ value: T) throws
where T: Encodable
{
throw DoJSON()
}


mutating func nestedContainer<NestedKey>(
keyedBy keyType: NestedKey.Type
) -> KeyedEncodingContainer<NestedKey>
where NestedKey : CodingKey
{
self.encoder.container(keyedBy: NestedKey.self)
}

mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
self.encoder.unkeyedContainer()
}

mutating func superEncoder() -> Encoder {
self.encoder
}
}

private struct _KeyedValueEncoder<Key>: KeyedEncodingContainerProtocol where Key: CodingKey {
var codingPath: [CodingKey] {
return self.encoder.codingPath
Expand All @@ -71,27 +112,32 @@ public struct SQLiteDataEncoder {
}

mutating func encodeNil(forKey key: Key) throws {
fatalError()
throw DoJSON()
}

mutating func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable {
throw DoJSON()
}

mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> where NestedKey : CodingKey {
fatalError()
mutating func nestedContainer<NestedKey>(
keyedBy keyType: NestedKey.Type,
forKey key: Key
) -> KeyedEncodingContainer<NestedKey>
where NestedKey : CodingKey
{
self.encoder.container(keyedBy: NestedKey.self)
}

mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
fatalError()
self.encoder.unkeyedContainer()
}

mutating func superEncoder() -> Encoder {
fatalError()
self.encoder
}

mutating func superEncoder(forKey key: Key) -> Encoder {
fatalError()
self.encoder
}
}

Expand Down

0 comments on commit f25b68e

Please sign in to comment.