Skip to content

Commit 8642701

Browse files
authored
Merge pull request #195 from algolia/feat/rule
feat(rule): expose rule.consequence.promote.objectIDs string array
2 parents 49a9337 + a096c5b commit 8642701

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/commonMain/kotlin/com/algolia/search/dsl/rule/DSLPromotions.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public class DSLPromotions(
3434
return Promotion(this, position)
3535
}
3636

37+
/**
38+
* Create a [Promotion] with [this] and [position].
39+
*/
40+
public operator fun List<ObjectID>.invoke(position: Int): Promotion {
41+
return Promotion(this, position)
42+
}
43+
3744
public companion object : DSL<DSLPromotions, List<Promotion>> {
3845

3946
override operator fun invoke(block: DSLPromotions.() -> Unit): List<Promotion> {
Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,53 @@
1+
@file:Suppress("FunctionName")
2+
13
package com.algolia.search.model.rule
24

35
import com.algolia.search.model.ObjectID
46
import com.algolia.search.serialize.KeyObjectID
7+
import com.algolia.search.serialize.KeyObjectIDs
58
import com.algolia.search.serialize.KeyPosition
69
import kotlinx.serialization.SerialName
710
import kotlinx.serialization.Serializable
811

912
@Serializable
10-
public data class Promotion(
11-
/**
12-
* Unique identifier of the object to promote.
13-
*/
14-
@SerialName(KeyObjectID) val objectID: ObjectID,
13+
public sealed class Promotion {
14+
1515
/**
16-
* Promoted rank for the object.
16+
* Promoted rank.
1717
*/
18-
@SerialName(KeyPosition) val position: Int
19-
)
18+
abstract val position: Int
19+
20+
@Serializable
21+
data class Single(
22+
/**
23+
* Unique identifier of the object to promote.
24+
*/
25+
@SerialName(KeyObjectID) val objectID: ObjectID,
26+
@SerialName(KeyPosition) override val position: Int
27+
) : Promotion()
28+
29+
@Serializable
30+
data class Multiple(
31+
/**
32+
* List of unique identifiers of the objects to promote.
33+
*/
34+
@SerialName(KeyObjectIDs) val objectIDs: List<ObjectID>,
35+
@SerialName(KeyPosition) override val position: Int
36+
) : Promotion()
37+
}
38+
39+
/**
40+
* Creates an instance of [Promotion.Single].
41+
*
42+
* @param objectID unique identifier of the object to promote
43+
* @param position promoted rank for the object.
44+
*/
45+
public fun Promotion(objectID: ObjectID, position: Int): Promotion.Single = Promotion.Single(objectID, position)
46+
47+
/**
48+
* Creates an instance of [Promotion.Multiple].
49+
*
50+
* @param objectIDs list of unique identifiers of the objects to promote.
51+
* @param position promoted rank for the objects.
52+
*/
53+
public fun Promotion(objectIDs: List<ObjectID>, position: Int): Promotion.Multiple = Promotion.Multiple(objectIDs, position)

src/commonTest/kotlin/dsl/rule/TestDSLPromotions.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.algolia.search.dsl.rule.DSLPromotions
44
import com.algolia.search.model.ObjectID
55
import com.algolia.search.model.rule.Promotion
66
import objectIDA
7+
import objectIDB
78
import shouldEqual
89
import kotlin.test.Test
910

@@ -14,11 +15,13 @@ internal class TestDSLPromotions {
1415
val dsl = DSLPromotions {
1516
+objectIDA(10)
1617
+"objectIDB"(10)
18+
+listOf(objectIDA, objectIDB)(10)
1719
}
1820

1921
dsl shouldEqual listOf(
2022
Promotion(objectIDA, 10),
21-
Promotion(ObjectID("objectIDB"), 10)
23+
Promotion(ObjectID("objectIDB"), 10),
24+
Promotion(listOf(objectIDA, objectIDB), 10)
2225
)
2326
}
2427
}

src/commonTest/kotlin/serialize/rule/TestConsequence.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,8 @@ internal class TestConsequence : TestSerializer<Consequence>(Consequence.seriali
9696
edits = listOf(Edit("mobile"), Edit("phone"))
9797
)
9898
)
99+
100+
promotions[0].position
101+
promotions[0].objectID
99102
}
100103
}

0 commit comments

Comments
 (0)