Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- run: swift test --enable-test-discovery --sanitize=thread
fluent:
container:
image: vapor/swift:5.1
image: vapor/swift:5.2
services:
psql:
image: postgres
Expand Down
11 changes: 1 addition & 10 deletions Sources/PostgresKit/PostgresDataDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public final class PostgresDataDecoder {
}

func unkeyedContainer() throws -> UnkeyedDecodingContainer {
print(self.data.type)
guard let data = self.data.array else {
throw Error.unexpectedDataType(self.data.type, expected: "array")
}
Expand Down Expand Up @@ -108,15 +107,7 @@ public final class PostgresDataDecoder {
mutating func decode<T>(_ type: T.Type) throws -> T where T : Decodable {
defer { self.currentIndex += 1 }
let data = self.data[self.currentIndex]
let jsonData: Data
if let jsonb = data.jsonb {
jsonData = jsonb
} else if let json = data.json {
jsonData = json
} else {
throw Error.unexpectedDataType(data.type, expected: "json")
}
return try self.json.decode(T.self, from: jsonData)
return try PostgresDataDecoder(json: self.json).decode(T.self, from: data)
}

mutating func nestedContainer<NestedKey>(
Expand Down
8 changes: 5 additions & 3 deletions Sources/PostgresKit/PostgresDataEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ public final class PostgresDataEncoder {
}

public func encode(_ value: Encodable) throws -> PostgresData {
if let custom = value as? PostgresDataConvertible {
return custom.postgresData!
if let custom = value as? PostgresDataConvertible, let data = custom.postgresData {
return data
} else {
let context = _Context()
try value.encode(to: _Encoder(context: context))
if let value = context.value {
return value
} else if let array = context.array {
return PostgresData(array: array, elementType: .jsonb)
let elementType = array.first?.type ?? .jsonb
assert(array.filter { $0.type != elementType }.isEmpty, "Array does not contain all: \(elementType)")
return PostgresData(array: array, elementType: elementType)
} else {
return try PostgresData(jsonb: self.json.encode(_Wrapper(value)))
}
Expand Down