Skip to content

Commit 107ce11

Browse files
committed
fix: better ObjectData
1 parent c2de4d0 commit 107ce11

File tree

2 files changed

+14
-42
lines changed

2 files changed

+14
-42
lines changed
Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1+
import com.algolia.search.model.QueryID
12
import com.algolia.search.serialize.internal.Key
2-
import com.algolia.search.serialize.internal.asJsonInput
3-
import com.algolia.search.serialize.internal.asJsonOutput
4-
import com.algolia.search.serialize.internal.jsonPrimitiveOrNull
5-
import kotlinx.serialization.*
6-
import kotlinx.serialization.encoding.Decoder
7-
import kotlinx.serialization.encoding.Encoder
8-
import kotlinx.serialization.json.*
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
95

106
/**
117
* ObjectData
@@ -15,43 +11,19 @@ import kotlinx.serialization.json.*
1511
* @param quantity Quantity of a product that has been purchased or added to the cart. The total purchase value is the sum of `quantity` multiplied with the `price` for each purchased item.
1612
* @param discount
1713
*/
18-
@Serializable(ObjectData.Companion::class)
14+
@Serializable
1915
public data class ObjectData(
20-
21-
/** Unique identifier for a search query, used to track purchase events with multiple records that originate from different searches. */
22-
@SerialName(value = Key.QueryID) val queryID: String? = null,
16+
/**
17+
* Unique identifier for a search query, used to track purchase events with multiple records that originate from different searches.
18+
*/
19+
@SerialName(value = Key.QueryID) val queryID: QueryID? = null,
2320

2421
@SerialName(value = Key.Price) val price: String? = null,
2522

26-
/** Quantity of a product that has been purchased or added to the cart. The total purchase value is the sum of `quantity` multiplied with the `price` for each purchased item. */
23+
/**
24+
* Quantity of a product that has been purchased or added to the cart. The total purchase value is the sum of `quantity` multiplied with the `price` for each purchased item.
25+
*/
2726
@SerialName(value = Key.Quantity) val quantity: Int? = null,
2827

2928
@SerialName(value = Key.Discount) val discount: String? = null,
30-
31-
32-
) {
33-
@Serializer(ObjectData::class)
34-
@OptIn(ExperimentalSerializationApi::class)
35-
public companion object : KSerializer<ObjectData> {
36-
37-
override fun serialize(encoder: Encoder, value: ObjectData) {
38-
val json = buildJsonObject {
39-
value.queryID?.let { put(Key.QueryID, it) }
40-
value.price?.let { put(Key.Price, it) }
41-
value.quantity?.let { put(Key.Quantity, it) }
42-
value.discount?.let { put(Key.Discount, it) }
43-
}
44-
encoder.asJsonOutput().encodeJsonElement(json)
45-
}
46-
47-
override fun deserialize(decoder: Decoder): ObjectData {
48-
val json = decoder.asJsonInput().jsonObject
49-
return ObjectData(
50-
queryID = json.getValue(Key.QueryID).jsonPrimitiveOrNull?.contentOrNull,
51-
price = json.getValue(Key.Price).jsonPrimitiveOrNull?.contentOrNull,
52-
quantity = json.getValue(Key.Quantity).jsonPrimitiveOrNull?.intOrNull,
53-
discount = json.getValue(Key.Discount).jsonPrimitiveOrNull?.contentOrNull,
54-
)
55-
}
56-
}
57-
}
29+
)

client/src/commonTest/kotlin/model/insights/TestInsightsEvent.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ internal class TestInsightsEvent {
2626
eventName = eventName,
2727
indexName = indexA,
2828
objectData = listOf(
29-
ObjectData(price = "10", queryID = "queryID", quantity = 1),
30-
ObjectData(price = "20", queryID = "queryID2", quantity = 2)
29+
ObjectData(price = "10", queryID = "queryID".toQueryID(), quantity = 1),
30+
ObjectData(price = "20", queryID = "queryID2".toQueryID(), quantity = 2)
3131
)
3232
)
3333

0 commit comments

Comments
 (0)