From 2cdc44f244b3cdc2768866c81aaf042bb7425bc6 Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Tue, 16 Jul 2024 14:03:34 +0200 Subject: [PATCH] Kotlin: Generate a serializer for array wrappers --- glean_parser/templates/kotlin.jinja2 | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/glean_parser/templates/kotlin.jinja2 b/glean_parser/templates/kotlin.jinja2 index 0c739bf92..9fca992d5 100644 --- a/glean_parser/templates/kotlin.jinja2 +++ b/glean_parser/templates/kotlin.jinja2 @@ -68,7 +68,7 @@ data class {{ obj.name|Camelize }}{{ suffix }}( {%- macro generate_structure(name, struct) %} {%- if struct.type == "array" -%} - @Serializable + @Serializable(with = {{name}}.Serializer::class) data class {{ name }}(var items: MutableList<{{ name }}Item> = mutableListOf()) : ObjectSerialize { fun add(elem: {{ name }}Item) = items.add(elem) @@ -85,6 +85,20 @@ data class {{ obj.name|Camelize }}{{ suffix }}( override fun intoSerializedObject(): String { return Json.encodeToString(items) } + + internal object Serializer : KSerializer<{{name}}> { + @OptIn(kotlinx.serialization.ExperimentalSerializationApi::class) + override val descriptor = listSerialDescriptor() + + override fun deserialize(decoder: Decoder): {{ name }} { + val list = decoder.decodeSerializableValue(ListSerializer(serializer<{{name}}Item>())) + return {{name}}(items = list.toMutableList()) + } + + override fun serialize(encoder: Encoder, value: {{name}}) { + encoder.encodeSerializableValue(ListSerializer(serializer<{{name}}Item>()), value.items) + } + } } {{ generate_structure(name ~ "Item", struct["items"]) }} @@ -160,9 +174,15 @@ import {{ glean_namespace }}.private.{{ obj_type }} // ktlint-disable import-ord import {{ glean_namespace }}.private.LabeledMetricType // ktlint-disable import-ordering {% endif %} {% if has_object_metrics %} +import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.descriptors.listSerialDescriptor import kotlinx.serialization.encodeToString +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.json.Json +import kotlinx.serialization.serializer {% endif %} {# HACK HACK HACK -- typealiases MUST BE top-level #}