Skip to content

Commit

Permalink
array coding support; fix fatal errors (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored Dec 11, 2019
1 parent 4578434 commit a8538f3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 16 deletions.
23 changes: 17 additions & 6 deletions Sources/PostgresKit/PostgresDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ public struct PostgresDataDecoder {
}

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

func container<Key>(keyedBy type: Key.Type) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
var buffer = self.data.value!
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)
func container<Key>(
keyedBy type: Key.Type
) throws -> KeyedDecodingContainer<Key> where Key : CodingKey {
try self.jsonDecoder().container(keyedBy: Key.self)
}

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

func singleValueContainer() throws -> SingleValueDecodingContainer {
Expand Down
59 changes: 49 additions & 10 deletions Sources/PostgresKit/PostgresDataEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public struct PostgresDataEncoder {
}

func container<Key>(keyedBy type: Key.Type) -> KeyedEncodingContainer<Key> where Key : CodingKey {
return .init(_KeyedValueEncoder(self))
.init(_KeyedValueEncoder(self))
}

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

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

Expand All @@ -55,9 +55,45 @@ public struct PostgresDataEncoder {
}
}

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
self.encoder.codingPath
}

let encoder: _Encoder
Expand All @@ -66,27 +102,30 @@ public struct PostgresDataEncoder {
}

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 a8538f3

Please sign in to comment.