Skip to content

Commit 5807407

Browse files
authored
add PostgresData+JSON support (#76)
1 parent 82c1ffa commit 5807407

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Foundation
2+
3+
extension PostgresData {
4+
public init(json jsonData: Data) {
5+
let jsonData = [UInt8](jsonData)
6+
7+
var buffer = ByteBufferAllocator()
8+
.buffer(capacity: jsonData.count)
9+
buffer.writeBytes(jsonData)
10+
self.init(type: .json, formatCode: .binary, value: buffer)
11+
}
12+
13+
public init<T>(json value: T) throws where T: Encodable {
14+
let jsonData = try JSONEncoder().encode(value)
15+
self.init(json: jsonData)
16+
}
17+
18+
public var json: Data? {
19+
guard var value = self.value else {
20+
return nil
21+
}
22+
guard case .json = self.type else {
23+
return nil
24+
}
25+
guard let data = value.readBytes(length: value.readableBytes) else {
26+
return nil
27+
}
28+
return Data(data)
29+
}
30+
31+
public func json<T>(as type: T.Type) throws -> T? where T: Decodable {
32+
guard let data = self.json else {
33+
return nil
34+
}
35+
return try JSONDecoder().decode(T.self, from: data)
36+
}
37+
}
38+
39+
public protocol PostgresJSONCodable: Codable, PostgresDataConvertible { }
40+
41+
extension PostgresJSONCodable {
42+
public static var postgresDataType: PostgresDataType {
43+
return .json
44+
}
45+
46+
public var postgresData: PostgresData? {
47+
return try? .init(json: self)
48+
}
49+
50+
public init?(postgresData: PostgresData) {
51+
guard let value = try? postgresData.json(as: Self.self) else {
52+
return nil
53+
}
54+
self = value
55+
}
56+
}

Sources/PostgresNIO/Data/PostgresData+JSONB.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ fileprivate let jsonBVersionBytes: [UInt8] = [0x01]
44

55
extension PostgresData {
66
public init(jsonb jsonData: Data) {
7-
let jsonBDataBytes = [UInt8](jsonData)
7+
let jsonBData = [UInt8](jsonData)
88

99
var buffer = ByteBufferAllocator()
10-
.buffer(capacity: jsonBVersionBytes.count + jsonBDataBytes.count)
10+
.buffer(capacity: jsonBVersionBytes.count + jsonBData.count)
1111
buffer.writeBytes(jsonBVersionBytes)
12-
buffer.writeBytes(jsonBDataBytes)
12+
buffer.writeBytes(jsonBData)
1313

1414
self.init(type: .jsonb, formatCode: .binary, value: buffer)
1515
}
@@ -23,29 +23,31 @@ extension PostgresData {
2323
guard var value = self.value else {
2424
return nil
2525
}
26+
guard case .jsonb = self.type else {
27+
return nil
28+
}
2629

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

31-
guard let dataBytes = value.readBytes(length: value.readableBytes) else {
34+
guard let data = value.readBytes(length: value.readableBytes) else {
3235
return nil
3336
}
3437

35-
return Data(dataBytes)
38+
return Data(data)
3639
}
3740

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

43-
return try JSONDecoder().decode(T.self, from: jsonData)
46+
return try JSONDecoder().decode(T.self, from: data)
4447
}
4548
}
4649

47-
public protocol PostgresJSONBCodable: Codable, PostgresDataConvertible {
48-
}
50+
public protocol PostgresJSONBCodable: Codable, PostgresDataConvertible { }
4951

5052
extension PostgresJSONBCodable {
5153
public static var postgresDataType: PostgresDataType {

0 commit comments

Comments
 (0)