Skip to content

Commit bf76940

Browse files
authored
add untyped array methods to PostgresData (#66)
* add untyped array methods to PostgresData * test fluent integration
1 parent 43e3a77 commit bf76940

File tree

4 files changed

+73
-18
lines changed

4 files changed

+73
-18
lines changed

.github/workflows/test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,25 @@ jobs:
3434
steps:
3535
- uses: actions/checkout@v1
3636
- run: swift test --enable-test-discovery --sanitize=thread
37+
fluent:
38+
container:
39+
image: vapor/swift:5.1
40+
services:
41+
psql:
42+
image: postgres
43+
ports:
44+
- 5432:5432
45+
env:
46+
POSTGRES_USER: vapor_username
47+
POSTGRES_DB: vapor_database
48+
POSTGRES_PASSWORD: vapor_password
49+
runs-on: ubuntu-latest
50+
steps:
51+
- run: git clone -b master https://github.com/vapor/fluent-postgres-driver.git
52+
working-directory: ./
53+
- run: swift package edit postgres-nio --revision ${{ github.sha }}
54+
working-directory: ./fluent-postgres-driver
55+
- run: swift test --enable-test-discovery --sanitize=thread
56+
working-directory: ./fluent-postgres-driver
57+
env:
58+
POSTGRES_HOSTNAME: psql

Sources/PostgresNIO/Data/PostgresData+Array.swift

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ extension PostgresData {
22
public init<T>(array: [T])
33
where T: PostgresDataConvertible
44
{
5-
let elementType = T.postgresDataType
6-
guard let arrayType = elementType.arrayType else {
7-
fatalError("No array type for \(elementType)")
8-
}
5+
self.init(
6+
array: array.map { $0.postgresData },
7+
elementType: T.postgresDataType
8+
)
9+
}
10+
public init(array: [PostgresData?], elementType: PostgresDataType) {
911
var buffer = ByteBufferAllocator().buffer(capacity: 0)
1012
// 0 if empty, 1 if not
1113
buffer.writeInteger(array.isEmpty ? 0 : 1, as: UInt32.self)
@@ -22,7 +24,7 @@ extension PostgresData {
2224
buffer.writeInteger(1, as: UInt32.self)
2325

2426
for item in array {
25-
if var value = item.postgresData?.value {
27+
if let item = item, var value = item.value {
2628
buffer.writeInteger(numericCast(value.readableBytes), as: UInt32.self)
2729
buffer.writeBuffer(&value)
2830
} else {
@@ -31,12 +33,35 @@ extension PostgresData {
3133
}
3234
}
3335

34-
self.init(type: arrayType, typeModifier: nil, formatCode: .binary, value: buffer)
36+
guard let arrayType = elementType.arrayType else {
37+
fatalError("No array type for \(elementType)")
38+
}
39+
self.init(
40+
type: arrayType,
41+
typeModifier: nil,
42+
formatCode: .binary,
43+
value: buffer
44+
)
3545
}
3646

3747
public func array<T>(of type: T.Type = T.self) -> [T]?
3848
where T: PostgresDataConvertible
3949
{
50+
guard let array = self.array else {
51+
return nil
52+
}
53+
var items: [T] = []
54+
for data in array {
55+
guard let item = T(postgresData: data) else {
56+
// if we fail to convert any data, fail the entire array
57+
return nil
58+
}
59+
items.append(item)
60+
}
61+
return items
62+
}
63+
64+
public var array: [PostgresData]? {
4065
guard var value = self.value else {
4166
return nil
4267
}
@@ -67,17 +92,18 @@ extension PostgresData {
6792
}
6893
assert(dimensions == 1, "Multi-dimensional arrays not yet supported")
6994

70-
var array: [T] = []
95+
var array: [PostgresData] = []
7196
while
7297
let itemLength = value.readInteger(as: UInt32.self),
7398
let itemValue = value.readSlice(length: numericCast(itemLength))
7499
{
75-
let data = PostgresData(type: type, typeModifier: nil, formatCode: self.formatCode, value: itemValue)
76-
guard let t = T(postgresData: data) else {
77-
// if we fail to convert any data, fail the entire array
78-
return nil
79-
}
80-
array.append(t)
100+
let data = PostgresData(
101+
type: type,
102+
typeModifier: nil,
103+
formatCode: self.formatCode,
104+
value: itemValue
105+
)
106+
array.append(data)
81107
}
82108
return array
83109
}

Sources/PostgresNIO/Data/PostgresData+JSONB.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ extension PostgresData {
66
public init(jsonb jsonData: Data) {
77
let jsonBDataBytes = [UInt8](jsonData)
88

9-
var buffer = ByteBufferAllocator().buffer(capacity: jsonBVersionBytes.count + jsonBDataBytes.count)
9+
var buffer = ByteBufferAllocator()
10+
.buffer(capacity: jsonBVersionBytes.count + jsonBDataBytes.count)
1011
buffer.writeBytes(jsonBVersionBytes)
1112
buffer.writeBytes(jsonBDataBytes)
1213

Sources/PostgresNIO/Data/PostgresData.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertibl
5050
description = self.date?.description
5151
case .text:
5252
description = self.string?.debugDescription
53-
case .textArray:
54-
description = self.array(of: String.self)?.description
5553
case .uuid:
5654
description = self.uuid?.description
55+
case .json:
56+
description = String(decoding: value.readableBytesView, as: UTF8.self)
57+
case .jsonb:
58+
var value = value
59+
value.moveReaderIndex(forwardBy: 1)
60+
description = String(decoding: value.readableBytesView, as: UTF8.self)
5761
case .uuidArray:
5862
description = self.array(of: UUID.self)?.description
5963
case .int8Array:
@@ -62,8 +66,10 @@ public struct PostgresData: CustomStringConvertible, CustomDebugStringConvertibl
6266
description = self.array(of: Double.self)?.description
6367
case .float4Array:
6468
description = self.array(of: Float.self)?.description
65-
case .jsonb:
66-
description = String(decoding: value.readableBytesView, as: UTF8.self)
69+
case .textArray:
70+
description = self.array(of: String.self)?.description
71+
case .jsonbArray:
72+
description = self.array?.description
6773
default:
6874
description = nil
6975
}

0 commit comments

Comments
 (0)