Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rule): expose rule.consequence.promote.objectIDs string array #195

Merged
merged 3 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public class DSLPromotions(
return Promotion(this, position)
}

/**
* Create a [Promotion] with [this] and [position].
*/
public operator fun List<ObjectID>.invoke(position: Int): Promotion {
return Promotion(this, position)
}

public companion object : DSL<DSLPromotions, List<Promotion>> {

override operator fun invoke(block: DSLPromotions.() -> Unit): List<Promotion> {
Expand Down
50 changes: 42 additions & 8 deletions src/commonMain/kotlin/com/algolia/search/model/rule/Promotion.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
@file:Suppress("FunctionName")

package com.algolia.search.model.rule

import com.algolia.search.model.ObjectID
import com.algolia.search.serialize.KeyObjectID
import com.algolia.search.serialize.KeyObjectIDs
import com.algolia.search.serialize.KeyPosition
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
public data class Promotion(
/**
* Unique identifier of the object to promote.
*/
@SerialName(KeyObjectID) val objectID: ObjectID,
public sealed class Promotion {

/**
* Promoted rank for the object.
* Promoted rank.
*/
@SerialName(KeyPosition) val position: Int
)
abstract val position: Int
spinach marked this conversation as resolved.
Show resolved Hide resolved

@Serializable
data class Single(
/**
* Unique identifier of the object to promote.
*/
@SerialName(KeyObjectID) val objectID: ObjectID,
@SerialName(KeyPosition) override val position: Int
) : Promotion()

@Serializable
data class Multiple(
/**
* List of unique identifiers of the objects to promote.
*/
@SerialName(KeyObjectIDs) val objectIDs: List<ObjectID>,
@SerialName(KeyPosition) override val position: Int
) : Promotion()
}

/**
* Creates an instance of [Promotion.Single].
*
* @param objectID unique identifier of the object to promote
* @param position promoted rank for the object.
*/
public fun Promotion(objectID: ObjectID, position: Int): Promotion.Single = Promotion.Single(objectID, position)

/**
* Creates an instance of [Promotion.Multiple].
*
* @param objectIDs list of unique identifiers of the objects to promote.
* @param position promoted rank for the objects.
*/
public fun Promotion(objectIDs: List<ObjectID>, position: Int): Promotion.Multiple = Promotion.Multiple(objectIDs, position)
5 changes: 4 additions & 1 deletion src/commonTest/kotlin/dsl/rule/TestDSLPromotions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.algolia.search.dsl.rule.DSLPromotions
import com.algolia.search.model.ObjectID
import com.algolia.search.model.rule.Promotion
import objectIDA
import objectIDB
import shouldEqual
import kotlin.test.Test

Expand All @@ -14,11 +15,13 @@ internal class TestDSLPromotions {
val dsl = DSLPromotions {
+objectIDA(10)
+"objectIDB"(10)
+listOf(objectIDA, objectIDB)(10)
}

dsl shouldEqual listOf(
Promotion(objectIDA, 10),
Promotion(ObjectID("objectIDB"), 10)
Promotion(ObjectID("objectIDB"), 10),
Promotion(listOf(objectIDA, objectIDB), 10)
)
}
}
3 changes: 3 additions & 0 deletions src/commonTest/kotlin/serialize/rule/TestConsequence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,8 @@ internal class TestConsequence : TestSerializer<Consequence>(Consequence.seriali
edits = listOf(Edit("mobile"), Edit("phone"))
)
)

promotions[0].position
promotions[0].objectID
}
}