Skip to content

Commit 71973ed

Browse files
committed
feat(kotlin): saveObjectsModified
1 parent 98bf2e7 commit 71973ed

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.io.File
2+
import kotlinx.serialization.builtins.ListSerializer
3+
import kotlinx.serialization.json.*
4+
5+
{{> snippets/import}}
6+
import com.algolia.client.model.search.*
7+
8+
suspend fun saveObjectsModified() {
9+
{{> snippets/init}}
10+
11+
val path = "/tmp/records.json"
12+
val json = try {
13+
File(path).readText()
14+
} catch (e: Exception) {
15+
throw RuntimeException("Failed to read file at $path", e)
16+
}
17+
val products: List<JsonObject> = Json.decodeFromString(ListSerializer(JsonObject.serializer()), json)
18+
19+
val records = products.map { product ->
20+
val reference = product["product_reference"].toString()
21+
val suffixes = reference.windowed(reference.length, 1, partialWindows = true).drop(1).map { JsonPrimitive(it) }
22+
JsonObject(
23+
product + ("product_reference_suffixes" to JsonArray(suffixes)),
24+
)
25+
}
26+
27+
client.{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
28+
}

0 commit comments

Comments
 (0)