Skip to content

Commit 343f184

Browse files
committed
Implicity unary plus for DSLFilter
1 parent c7141f4 commit 343f184

File tree

15 files changed

+140
-122
lines changed

15 files changed

+140
-122
lines changed

src/commonMain/kotlin/com/algolia/search/dsl/filtering/DSLFacet.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,47 @@ import com.algolia.search.model.filter.Filter
1111
*/
1212
public interface DSLFacet {
1313

14+
public operator fun Filter.Facet.unaryPlus()
15+
1416
/**
1517
* Convenience method.
1618
*/
17-
fun facet(name: String, value: Number, score: Int? = null): Filter.Facet {
18-
return facet(Attribute(name), value, score)
19+
fun facet(name: String, value: Number, score: Int? = null, isNegated: Boolean = false) {
20+
facet(Attribute(name), value, score, isNegated)
1921
}
2022

2123
/**
2224
* Build a [Filter.Facet] using [attribute], a numeric [value] and an optional [score].
2325
*/
24-
fun facet(attribute: Attribute, value: Number, score: Int? = null): Filter.Facet {
25-
return Filter.Facet(attribute, value, score)
26+
fun facet(attribute: Attribute, value: Number, score: Int? = null, isNegated: Boolean = false) {
27+
+Filter.Facet(attribute, value, score, isNegated)
2628
}
2729

2830
/**
2931
* Convenience method.
3032
*/
31-
fun facet(name: String, value: String, score: Int? = null): Filter.Facet {
32-
return facet(Attribute(name), value, score)
33+
fun facet(name: String, value: String, score: Int? = null, isNegated: Boolean = false) {
34+
facet(Attribute(name), value, score, isNegated)
3335
}
3436

3537
/**
3638
* Build a [Filter.Facet] using [attribute], a [String] [value] and an optional [score].
3739
*/
38-
fun facet(attribute: Attribute, value: String, score: Int? = null): Filter.Facet {
39-
return Filter.Facet(attribute, value, score)
40+
fun facet(attribute: Attribute, value: String, score: Int? = null, isNegated: Boolean = false) {
41+
+Filter.Facet(attribute, value, score, isNegated)
4042
}
4143

4244
/**
4345
* Convenience method.
4446
*/
45-
fun facet(name: String, value: Boolean, score: Int? = null): Filter.Facet {
46-
return facet(Attribute(name), value, score)
47+
fun facet(name: String, value: Boolean, score: Int? = null, isNegated: Boolean = false) {
48+
facet(Attribute(name), value, score, isNegated)
4749
}
4850

4951
/**
5052
* Build a [Filter.Facet] using [attribute], a [Boolean] [value] and an optional [score].
5153
*/
52-
fun facet(attribute: Attribute, value: Boolean, score: Int? = null): Filter.Facet {
53-
return Filter.Facet(attribute, value, score)
54+
fun facet(attribute: Attribute, value: Boolean, score: Int? = null, isNegated: Boolean = false) {
55+
+Filter.Facet(attribute, value, score, isNegated)
5456
}
5557
}

src/commonMain/kotlin/com/algolia/search/dsl/filtering/DSLGroup.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public class DSLGroupFilter(
7474
override val filters: MutableSet<Filter> = mutableSetOf()
7575
) : DSLGroup<Filter>(), DSLFacet, DSLTag, DSLNumeric {
7676

77+
override fun Filter.Facet.unaryPlus() {
78+
filters += this
79+
}
80+
81+
override fun Filter.Tag.unaryPlus() {
82+
filters += this
83+
}
84+
85+
override fun Filter.Numeric.unaryPlus() {
86+
filters += this
87+
}
88+
7789
public companion object : DSL<DSLGroupFilter, Set<Filter>> {
7890

7991
override fun invoke(block: DSLGroupFilter.() -> Unit): Set<Filter> {

src/commonMain/kotlin/com/algolia/search/dsl/filtering/DSLNumeric.kt

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,75 @@ public interface DSLNumeric {
1919
val Greater get() = NumericOperator.Greater
2020
val GreaterOrEquals get() = NumericOperator.GreaterOrEquals
2121

22+
public operator fun Filter.Numeric.unaryPlus()
23+
2224
/**
2325
* Convenience method.
2426
*/
25-
fun range(name: String, range: IntRange): Filter.Numeric {
26-
return range(Attribute(name), range)
27+
fun range(name: String, range: IntRange, isNegated: Boolean = false) {
28+
range(Attribute(name), range, isNegated)
2729
}
2830

2931
/**
3032
* Create a [Filter.Numeric] with an [attribute] and a [range] of [IntRange].
3133
*/
32-
fun range(attribute: Attribute, range: IntRange): Filter.Numeric {
33-
return Filter.Numeric(attribute, range)
34+
fun range(attribute: Attribute, range: IntRange, isNegated: Boolean = false) {
35+
+Filter.Numeric(attribute, range, isNegated)
3436
}
3537

3638
/**
3739
* Convenience method.
3840
*/
39-
fun range(name: String, range: LongRange): Filter.Numeric {
40-
return range(Attribute(name), range)
41+
fun range(name: String, range: LongRange, isNegated: Boolean = false) {
42+
range(Attribute(name), range, isNegated)
4143
}
4244

4345
/**
4446
* Create a [Filter.Numeric] with an [attribute] and a [range] of [LongRange].
4547
*/
46-
fun range(attribute: Attribute, range: LongRange): Filter.Numeric {
47-
return Filter.Numeric(attribute, range)
48+
fun range(attribute: Attribute, range: LongRange, isNegated: Boolean = false) {
49+
+Filter.Numeric(attribute, range, isNegated)
4850
}
4951

5052
/**
5153
* Convenience method.
5254
*/
53-
fun range(name: String, lowerBound: Float, upperBound: Float): Filter.Numeric {
54-
return range(Attribute(name), lowerBound, upperBound)
55+
fun range(name: String, lowerBound: Float, upperBound: Float, isNegated: Boolean = false) {
56+
range(Attribute(name), lowerBound, upperBound, isNegated)
5557
}
5658

5759
/**
5860
* Create a [Filter.Numeric] with an [attribute] and a [lowerBound] and [upperBound] of [Float].
5961
*/
60-
fun range(attribute: Attribute, lowerBound: Float, upperBound: Float): Filter.Numeric {
61-
return Filter.Numeric(attribute, lowerBound, upperBound)
62+
fun range(attribute: Attribute, lowerBound: Float, upperBound: Float, isNegated: Boolean = false) {
63+
+Filter.Numeric(attribute, lowerBound, upperBound, isNegated)
6264
}
6365

6466
/**
6567
* Convenience method.
6668
*/
67-
fun range(name: String, lowerBound: Double, upperBound: Double): Filter.Numeric {
68-
return range(Attribute(name), lowerBound, upperBound)
69+
fun range(name: String, lowerBound: Double, upperBound: Double, isNegated: Boolean = false) {
70+
range(Attribute(name), lowerBound, upperBound, isNegated)
6971
}
7072

7173
/**
7274
* Create a [Filter.Numeric] with an [attribute] and a [lowerBound] and [upperBound] of [Double].
7375
*/
74-
fun range(attribute: Attribute, lowerBound: Double, upperBound: Double): Filter.Numeric {
75-
return Filter.Numeric(attribute, lowerBound, upperBound)
76+
fun range(attribute: Attribute, lowerBound: Double, upperBound: Double, isNegated: Boolean = false) {
77+
+Filter.Numeric(attribute, lowerBound, upperBound, isNegated)
7678
}
7779

7880
/**
7981
* Convenience method.
8082
*/
81-
fun comparison(name: String, operator: NumericOperator, value: Number): Filter.Numeric {
82-
return comparison(Attribute(name), operator, value)
83+
fun comparison(name: String, operator: NumericOperator, value: Number, isNegated: Boolean = false) {
84+
comparison(Attribute(name), operator, value, isNegated)
8385
}
8486

8587
/**
8688
* Create a [Filter.Numeric] with an [attribute], an [operator] and a [value].
8789
*/
88-
fun comparison(attribute: Attribute, operator: NumericOperator, value: Number): Filter.Numeric {
89-
return Filter.Numeric(attribute, operator, value)
90+
fun comparison(attribute: Attribute, operator: NumericOperator, value: Number, isNegated: Boolean = false) {
91+
+Filter.Numeric(attribute, operator, value, isNegated)
9092
}
9193
}

src/commonMain/kotlin/com/algolia/search/dsl/filtering/DSLTag.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import com.algolia.search.model.filter.Filter
99
*/
1010
public interface DSLTag {
1111

12+
public operator fun Filter.Tag.unaryPlus()
13+
1214
/**
1315
* Create a [Filter.Tag] with an [value].
1416
*/
15-
fun tag(value: String): Filter.Tag {
16-
return Filter.Tag(value)
17+
fun tag(value: String, isNegated: Boolean = false) {
18+
+Filter.Tag(value, isNegated)
1719
}
1820
}

src/commonTest/kotlin/dsl/TestDSLDeleteByQuery.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal class TestDSLDeleteByQuery {
1616
fun filters() {
1717
val deleteByQuery = deleteByQuery {
1818
filters {
19-
and { +facet(attributeA, 0) }
19+
and { facet(attributeA, 0) }
2020
}
2121
}
2222

@@ -27,7 +27,7 @@ internal class TestDSLDeleteByQuery {
2727
fun facetFilters() {
2828
val deleteByQuery = deleteByQuery {
2929
facetFilters {
30-
and { +facet(attributeA, 0) }
30+
and { facet(attributeA, 0) }
3131
}
3232
}
3333

@@ -38,7 +38,7 @@ internal class TestDSLDeleteByQuery {
3838
fun numericFilters() {
3939
val deleteByQuery = deleteByQuery {
4040
numericFilters {
41-
and { +range(attributeA, 0..1) }
41+
and { range(attributeA, 0..1) }
4242
}
4343
}
4444

@@ -49,7 +49,7 @@ internal class TestDSLDeleteByQuery {
4949
fun tagFilters() {
5050
val deleteByQuery = deleteByQuery {
5151
tagFilters {
52-
and { +tag(unknown) }
52+
and { tag(unknown) }
5353
}
5454
}
5555

src/commonTest/kotlin/dsl/TestDSLQuery.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class TestDSLQuery {
3434
fun filters() {
3535
val query = query {
3636
filters {
37-
and { +facet(attributeA, 0) }
37+
and { facet(attributeA, 0) }
3838
}
3939
}
4040

@@ -45,7 +45,7 @@ internal class TestDSLQuery {
4545
fun facetFilters() {
4646
val query = query {
4747
facetFilters {
48-
and { +facet(attributeA, 0) }
48+
and { facet(attributeA, 0) }
4949
}
5050
}
5151

@@ -56,7 +56,7 @@ internal class TestDSLQuery {
5656
fun numericFilters() {
5757
val query = query {
5858
numericFilters {
59-
and { +range(attributeA, 0..1) }
59+
and { range(attributeA, 0..1) }
6060
}
6161
}
6262

@@ -67,7 +67,7 @@ internal class TestDSLQuery {
6767
fun tagFilters() {
6868
val query = query {
6969
tagFilters {
70-
and { +tag(unknown) }
70+
and { tag(unknown) }
7171
}
7272
}
7373

@@ -78,7 +78,7 @@ internal class TestDSLQuery {
7878
fun optionalFilters() {
7979
val query = query {
8080
optionalFilters {
81-
and { +facet(attributeA, 0) }
81+
and { facet(attributeA, 0) }
8282
}
8383
}
8484

src/commonTest/kotlin/dsl/filtering/TestDSLFacetFilters.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ internal class TestDSLFacetFilters {
1515
fun and() {
1616
val dsl = DSLFacetFilters {
1717
and {
18-
+facet(attributeA, 0)
19-
+facet(attributeA, 1)
18+
facet(attributeA, 0)
19+
facet(attributeA, 1)
2020
}
2121
}
2222

@@ -29,11 +29,11 @@ internal class TestDSLFacetFilters {
2929
fun or() {
3030
val dsl = DSLFacetFilters {
3131
or {
32-
+facet(attributeA, 0)
33-
+facet(attributeB, 1)
32+
facet(attributeA, 0)
33+
facet(attributeB, 1)
3434
}
3535
or {
36-
+facet(attributeA, 0)
36+
facet(attributeA, 0)
3737
}
3838
}
3939

src/commonTest/kotlin/dsl/filtering/TestDSLFilterGroupFacet.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ internal class TestDSLFilterGroupFacet {
1313
@Test
1414
fun string() {
1515
val dsl = DSLGroupFacet {
16-
+facet(attributeA.raw, 0)
17-
+facet(attributeA.raw, unknown)
18-
+facet(attributeA.raw, true, 0)
16+
facet(attributeA.raw, 0)
17+
facet(attributeA.raw, unknown)
18+
facet(attributeA.raw, true, 0)
1919
}
2020

2121
dsl shouldEqual setOf(
@@ -28,9 +28,9 @@ internal class TestDSLFilterGroupFacet {
2828
@Test
2929
fun attribute() {
3030
val dsl = DSLGroupFacet {
31-
+facet(attributeA, 0)
32-
+facet(attributeA, unknown)
33-
+facet(attributeA, true, 0)
31+
facet(attributeA, 0)
32+
facet(attributeA, unknown)
33+
facet(attributeA, true, 0)
3434
}
3535

3636
dsl shouldEqual setOf(

src/commonTest/kotlin/dsl/filtering/TestDSLFilterGroupFilter.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ internal class TestDSLFilterGroupFilter {
1414
@Test
1515
fun dx() {
1616
val dsl = DSLGroupFilter {
17-
+tag(unknown)
18-
+facet(attributeA, 0)
19-
+range(attributeA, 0 until 2)
20-
+comparison(attributeA, LessOrEquals, 0)
17+
tag(unknown)
18+
facet(attributeA, 0)
19+
range(attributeA, 0 until 2)
20+
comparison(attributeA, LessOrEquals, 0)
2121
}
2222

2323
dsl shouldEqual setOf(

src/commonTest/kotlin/dsl/filtering/TestDSLFilterGroupNumeric.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ internal class TestDSLFilterGroupNumeric {
1313
@Test
1414
fun rangeString() {
1515
val dsl = DSLGroupNumeric {
16-
+range(attributeA.raw, 0 until 2)
17-
+range(attributeA.raw, 0L until 2L)
18-
+range(attributeA.raw, 0f, 1f)
19-
+range(attributeA.raw, 0.0, 1.0)
16+
range(attributeA.raw, 0 until 2)
17+
range(attributeA.raw, 0L until 2L)
18+
range(attributeA.raw, 0f, 1f)
19+
range(attributeA.raw, 0.0, 1.0)
2020
}
2121

2222
dsl shouldEqual setOf(
@@ -30,10 +30,10 @@ internal class TestDSLFilterGroupNumeric {
3030
@Test
3131
fun rangeAttribute() {
3232
val dsl = DSLGroupNumeric {
33-
+range(attributeA, 0 until 2)
34-
+range(attributeA, 0L until 2L)
35-
+range(attributeA, 0f, 1f)
36-
+range(attributeA, 0.0, 1.0)
33+
range(attributeA, 0 until 2)
34+
range(attributeA, 0L until 2L)
35+
range(attributeA, 0f, 1f)
36+
range(attributeA, 0.0, 1.0)
3737
}
3838

3939
dsl shouldEqual setOf(
@@ -47,8 +47,8 @@ internal class TestDSLFilterGroupNumeric {
4747
@Test
4848
fun comparison() {
4949
val dsl = DSLGroupNumeric {
50-
+comparison(attributeA.raw, Less, 0)
51-
+comparison(attributeA, Greater, 0)
50+
comparison(attributeA.raw, Less, 0)
51+
comparison(attributeA, Greater, 0)
5252
}
5353

5454
dsl shouldEqual setOf(

0 commit comments

Comments
 (0)