Skip to content

Commit e1317d2

Browse files
authored
Merge pull request #2 from rcarver/array-component
improve composability for arrays
2 parents 92205da + 98b0bb8 commit e1317d2

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

Sources/ElasticsearchQueryBuilder/Components.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,19 @@ public enum esb {}
3939

4040
extension esb {
4141

42+
/// The type of a top level query structure.
43+
///
44+
/// Used with `@ElasticsearchQueryBuilder`
4245
public typealias QueryDSL = RootQueryable
4346

47+
/// The type of an array sub-component of a query.
48+
///
49+
/// Used to create a reusable function to fill in the
50+
/// body of another component.
51+
///
52+
/// Used with `@QueryArrayBuilder`
53+
public typealias QueryArray = ArrayComponent
54+
4455
/// An empty component, adding nothing to the query syntax.
4556
public struct Nothing: DictComponent {
4657
public init() {}

Sources/ElasticsearchQueryBuilder/QueryBuilder.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,19 @@ public struct QueryDictBuilder {
3535
@resultBuilder
3636
public struct QueryArrayBuilder {
3737

38+
public static func buildPartialBlock<C: ArrayComponent>(first: C) -> AppendDictValues {
39+
.init(wrapped: first.makeArray())
40+
}
3841
public static func buildPartialBlock<C: DictComponent>(first: C) -> AppendDictValues {
3942
.init(wrapped: [first.makeDict()])
4043
}
4144
public static func buildPartialBlock(first: AppendDictValues) -> AppendDictValues {
4245
first
4346
}
4447

48+
public static func buildPartialBlock<C: ArrayComponent>(accumulated: AppendDictValues, next: C) -> AppendDictValues {
49+
.init(wrapped: accumulated.wrapped + next.makeArray())
50+
}
4551
public static func buildPartialBlock<C: DictComponent>(accumulated: AppendDictValues, next: C) -> AppendDictValues {
4652
.init(wrapped: accumulated.wrapped + [next.makeDict()])
4753
}

Tests/ElasticsearchQueryBuilderTests/ComponentTests.swift

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,87 @@ final class KeyTests: XCTestCase {
4343
}
4444
}
4545

46+
final class ComposableBuilderTests: XCTestCase {
47+
func testBuildDict() throws {
48+
@QueryDictBuilder func makeKey() -> some DictComponent {
49+
esb.Key("match_bool_prefix") {
50+
[
51+
"message": "quick brown f"
52+
]
53+
}
54+
}
55+
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
56+
esb.Query {
57+
makeKey()
58+
}
59+
}
60+
XCTAssertNoDifference(build().makeQuery(), [
61+
"query": [
62+
"match_bool_prefix": [
63+
"message": "quick brown f"
64+
]
65+
]
66+
])
67+
}
68+
func testBuildArray() throws {
69+
@QueryArrayBuilder func makeKey(_ isEnabled: Bool) -> some esb.QueryArray {
70+
if isEnabled {
71+
esb.Key("match_bool_prefix") {
72+
[
73+
"message": "quick brown f"
74+
]
75+
}
76+
}
77+
}
78+
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
79+
esb.Filter {
80+
makeKey(true)
81+
}
82+
}
83+
XCTAssertNoDifference(build().makeQuery(), [
84+
"filter": [
85+
[
86+
"match_bool_prefix": [
87+
"message": "quick brown f"
88+
]
89+
]
90+
]
91+
])
92+
}
93+
func testBuildArray2() throws {
94+
@QueryArrayBuilder func makeKey(_ isEnabled: Bool, _ c: Int) -> esb.QueryArray {
95+
if isEnabled {
96+
esb.Key("match_bool_prefix") {
97+
[
98+
"message": .string("quick brown \(c)")
99+
]
100+
}
101+
}
102+
}
103+
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {
104+
esb.Filter {
105+
makeKey(true, 1)
106+
makeKey(false, 2)
107+
makeKey(true, 3)
108+
}
109+
}
110+
XCTAssertNoDifference(build().makeQuery(), [
111+
"filter": [
112+
[
113+
"match_bool_prefix": [
114+
"message": "quick brown 1"
115+
]
116+
],
117+
[
118+
"match_bool_prefix": [
119+
"message": "quick brown 3"
120+
]
121+
]
122+
]
123+
])
124+
}
125+
}
126+
46127
final class QueryTests: XCTestCase {
47128
func testBuild() throws {
48129
@ElasticsearchQueryBuilder func build() -> some esb.QueryDSL {

0 commit comments

Comments
 (0)