Skip to content

Commit

Permalink
add PostgresData+JSON support (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Jan 22, 2020
1 parent 82c1ffa commit 5807407
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
56 changes: 56 additions & 0 deletions Sources/PostgresNIO/Data/PostgresData+JSON.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Foundation

extension PostgresData {
public init(json jsonData: Data) {
let jsonData = [UInt8](jsonData)

var buffer = ByteBufferAllocator()
.buffer(capacity: jsonData.count)
buffer.writeBytes(jsonData)
self.init(type: .json, formatCode: .binary, value: buffer)
}

public init<T>(json value: T) throws where T: Encodable {
let jsonData = try JSONEncoder().encode(value)
self.init(json: jsonData)
}

public var json: Data? {
guard var value = self.value else {
return nil
}
guard case .json = self.type else {
return nil
}
guard let data = value.readBytes(length: value.readableBytes) else {
return nil
}
return Data(data)
}

public func json<T>(as type: T.Type) throws -> T? where T: Decodable {
guard let data = self.json else {
return nil
}
return try JSONDecoder().decode(T.self, from: data)
}
}

public protocol PostgresJSONCodable: Codable, PostgresDataConvertible { }

extension PostgresJSONCodable {
public static var postgresDataType: PostgresDataType {
return .json
}

public var postgresData: PostgresData? {
return try? .init(json: self)
}

public init?(postgresData: PostgresData) {
guard let value = try? postgresData.json(as: Self.self) else {
return nil
}
self = value
}
}
20 changes: 11 additions & 9 deletions Sources/PostgresNIO/Data/PostgresData+JSONB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ fileprivate let jsonBVersionBytes: [UInt8] = [0x01]

extension PostgresData {
public init(jsonb jsonData: Data) {
let jsonBDataBytes = [UInt8](jsonData)
let jsonBData = [UInt8](jsonData)

var buffer = ByteBufferAllocator()
.buffer(capacity: jsonBVersionBytes.count + jsonBDataBytes.count)
.buffer(capacity: jsonBVersionBytes.count + jsonBData.count)
buffer.writeBytes(jsonBVersionBytes)
buffer.writeBytes(jsonBDataBytes)
buffer.writeBytes(jsonBData)

self.init(type: .jsonb, formatCode: .binary, value: buffer)
}
Expand All @@ -23,29 +23,31 @@ extension PostgresData {
guard var value = self.value else {
return nil
}
guard case .jsonb = self.type else {
return nil
}

guard let versionBytes = value.readBytes(length: jsonBVersionBytes.count), [UInt8](versionBytes) == jsonBVersionBytes else {
return nil
}

guard let dataBytes = value.readBytes(length: value.readableBytes) else {
guard let data = value.readBytes(length: value.readableBytes) else {
return nil
}

return Data(dataBytes)
return Data(data)
}

public func jsonb<T>(as type: T.Type) throws -> T? where T: Decodable {
guard let jsonData = jsonb else {
guard let data = jsonb else {
return nil
}

return try JSONDecoder().decode(T.self, from: jsonData)
return try JSONDecoder().decode(T.self, from: data)
}
}

public protocol PostgresJSONBCodable: Codable, PostgresDataConvertible {
}
public protocol PostgresJSONBCodable: Codable, PostgresDataConvertible { }

extension PostgresJSONBCodable {
public static var postgresDataType: PostgresDataType {
Expand Down

0 comments on commit 5807407

Please sign in to comment.