Skip to content

Commit 83e4776

Browse files
authored
Add QueryExpression.jsonPatch (#105)
* Add `QueryExpression.jsonPatch` Found use in a downstream project, so mainlining this. * test
1 parent efd928f commit 83e4776

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Sources/StructuredQueriesCore/Documentation.docc/Extensions/QueryExpression.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
2929
- ``jsonArrayLength()``
3030
- ``jsonGroupArray(order:filter:)``
31+
- ``jsonPatch(_:)``
3132
3233
### Optionality
3334

Sources/StructuredQueriesCore/SQLite/JSONFunctions.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ import Foundation
22
import StructuredQueriesSupport
33

44
extension QueryExpression {
5+
/// Passes this expression and the given one to the `json_patch` function.
6+
///
7+
/// - Parameter other: A JSON object to patch this object with.
8+
/// - Returns: A JSON expression of the result of the invoking the `json_patch` function.
9+
public func jsonPatch<QueryOutput: Codable>(
10+
_ other: some QueryExpression<QueryValue>
11+
) -> some QueryExpression<QueryValue>
12+
where QueryValue == _CodableJSONRepresentation<QueryOutput> {
13+
QueryFunction("json_patch", self, other)
14+
}
15+
516
/// Wraps this expression with the `json_array_length` function.
617
///
718
/// ```swift

Tests/StructuredQueriesTests/JSONFunctionsTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,27 @@ extension SnapshotTests {
318318
"""
319319
}
320320
}
321+
322+
@Test func jsonPatch() {
323+
assertQuery(Values(#bind(["a": 1]).jsonPatch(#bind(["b": 2])))) {
324+
"""
325+
SELECT json_patch('{
326+
"a" : 1
327+
}', '{
328+
"b" : 2
329+
}')
330+
"""
331+
} results: {
332+
"""
333+
┌───────────┐
334+
│ [ │
335+
"a": 1, │
336+
"b": 2 │
337+
│ ] │
338+
└───────────┘
339+
"""
340+
}
341+
}
321342
}
322343
}
323344

0 commit comments

Comments
 (0)