Skip to content

Commit 65fd412

Browse files
committed
feat: add NullEncodable property wrapper
1 parent fc881f0 commit 65fd412

File tree

4 files changed

+92
-10
lines changed

4 files changed

+92
-10
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// NullEncodable.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 19/12/23.
6+
//
7+
8+
import Foundation
9+
10+
@propertyWrapper
11+
public struct NullEncodable<T>: Encodable where T: Encodable {
12+
public var wrappedValue: T?
13+
14+
public init(wrappedValue: T? = nil) {
15+
self.wrappedValue = wrappedValue
16+
}
17+
18+
public func encode(to encoder: Encoder) throws {
19+
var container = encoder.singleValueContainer()
20+
21+
if let wrappedValue {
22+
try container.encode(wrappedValue)
23+
} else {
24+
try container.encodeNil()
25+
}
26+
}
27+
}
28+
29+
extension NullEncodable: Equatable where T: Equatable {}
30+
extension NullEncodable: Hashable where T: Hashable {}
31+
32+
extension NullEncodable: Decodable where T: Decodable {
33+
public init(from decoder: Decoder) throws {
34+
let container = try decoder.singleValueContainer()
35+
36+
if container.decodeNil() {
37+
self.init(wrappedValue: nil)
38+
} else {
39+
try self.init(wrappedValue: container.decode(T.self))
40+
}
41+
}
42+
}

Sources/PostgREST/PostgrestQueryBuilder.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,6 @@ public final class PostgrestQueryBuilder: PostgrestBuilder {
6969
if !prefersHeaders.isEmpty {
7070
$0.request.headers["Prefer"] = prefersHeaders.joined(separator: ",")
7171
}
72-
73-
// TODO: How to do this in Swift?
74-
// if (Array.isArray(values)) {
75-
// const columns = values.reduce((acc, x) => acc.concat(Object.keys(x)), [] as string[])
76-
// if (columns.length > 0) {
77-
// const uniqueColumns = [...new Set(columns)].map((column) => `"${column}"`)
78-
// this.url.searchParams.set('columns', uniqueColumns.join(','))
79-
// }
80-
// }
8172
}
8273

8374
return PostgrestFilterBuilder(self)

Tests/PostgRESTTests/BuildURLRequestTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ final class BuildURLRequestTests: XCTestCase {
110110
await client.schema("storage")
111111
.from("objects")
112112
.select()
113-
}
113+
},
114114
]
115115

116116
for testCase in testCases {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// NullEncodableTests.swift
3+
//
4+
//
5+
// Created by Guilherme Souza on 19/12/23.
6+
//
7+
8+
import PostgREST
9+
import XCTest
10+
11+
final class NullEncodableTests: XCTestCase {
12+
struct Item: Encodable {
13+
var email: String
14+
@NullEncodable var username: String?
15+
}
16+
17+
func testEncode() throws {
18+
let items = [
19+
Item(email: "[email protected]", username: .init(wrappedValue: "example")),
20+
Item(email: "[email protected]", username: .init(wrappedValue: nil)),
21+
Item(email: "[email protected]", username: .init(wrappedValue: "example3")),
22+
]
23+
24+
let encoder = JSONEncoder()
25+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
26+
let data = try encoder.encode(items)
27+
let jsonString = try XCTUnwrap(String(data: data, encoding: .utf8))
28+
29+
XCTAssertEqual(
30+
jsonString,
31+
"""
32+
[
33+
{
34+
"email" : "[email protected]",
35+
"username" : "example"
36+
},
37+
{
38+
"email" : "[email protected]",
39+
"username" : null
40+
},
41+
{
42+
"email" : "[email protected]",
43+
"username" : "example3"
44+
}
45+
]
46+
"""
47+
)
48+
}
49+
}

0 commit comments

Comments
 (0)