Commit 5d7bcc6
committed
Add
This PR adds helpers that make it a little easier to work with optional
query expressions in a builder.
For example, if you want to execute a `LIKE` operator on an optional
string, you currently have to resort to one of the following
workarounds:
```swift
.where { ($0.title ?? "").like("%foo%") }
// or:
.where { #sql("\($0.title) LIKE '%foo%') }
```
This PR introduces `map` and `flatMap` operations on optional
`QueryExpression`s that unwraps the expression, giving you additional
flexibility in how you express your builder code:
```swift
.where { $0.title.map { $0.like("%foo%") } ?? false }
```
While this is more code than the above options, some may prefer its
readability, and should we merge the other optional helpers from #61, it
could be further shortened:
```swift
.where { $0.title.map { $0.like("%foo%") } }
```QueryExpression<Optional>.map,flatMap
1 parent ea20f9c commit 5d7bcc6
File tree
2 files changed
+45
-0
lines changed- Sources/StructuredQueriesCore
- Documentation.docc/Extensions
2 files changed
+45
-0
lines changedLines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
0 commit comments