From 0ffceaf52c05602df5f4434579f50fa24670070a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Quenaudon?= Date: Tue, 7 Jan 2025 10:32:20 +0000 Subject: [PATCH] Add a repeatable field in the mutable type --- .../squareup/wire/mutable/MutablePacket.kt | 25 +++++++++++-------- .../proto/squareup/wire/mutable_types.proto | 2 +- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt index 55d52b8965..e543105c1f 100644 --- a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt +++ b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePacket.kt @@ -16,6 +16,7 @@ import com.squareup.wire.ReverseProtoWriter import com.squareup.wire.Syntax.PROTO_2 import com.squareup.wire.WireField import com.squareup.wire.`internal`.JvmField +import com.squareup.wire.`internal`.immutableCopyOf import kotlin.Any import kotlin.Boolean import kotlin.Deprecated @@ -26,6 +27,7 @@ import kotlin.Nothing import kotlin.String import kotlin.Suppress import kotlin.UnsupportedOperationException +import kotlin.collections.List import okio.ByteString public class MutablePacket( @@ -36,14 +38,17 @@ public class MutablePacket( schemaIndex = 0, ) public var header_: MutableHeader? = null, + payload: List = emptyList(), + override var unknownFields: ByteString = ByteString.EMPTY, +) : Message(ADAPTER, unknownFields) { @field:WireField( tag = 2, adapter = "squareup.wire.mutable.MutablePayload#ADAPTER", + label = WireField.Label.REPEATED, schemaIndex = 1, ) - public var payload: MutablePayload? = null, - override var unknownFields: ByteString = ByteString.EMPTY, -) : Message(ADAPTER, unknownFields) { + public var payload: List = immutableCopyOf("payload", payload) + @Deprecated( message = "Shouldn't be used in Kotlin", level = DeprecationLevel.HIDDEN, @@ -63,14 +68,14 @@ public class MutablePacket( var result = 0 result = unknownFields.hashCode() result = result * 37 + (header_?.hashCode() ?: 0) - result = result * 37 + (payload?.hashCode() ?: 0) + result = result * 37 + payload.hashCode() return result } override fun toString(): String { val result = mutableListOf() if (header_ != null) result += """header_=$header_""" - if (payload != null) result += """payload=$payload""" + if (payload.isNotEmpty()) result += """payload=$payload""" return result.joinToString(prefix = "MutablePacket{", separator = ", ", postfix = "}") } @@ -87,29 +92,29 @@ public class MutablePacket( override fun encodedSize(`value`: MutablePacket): Int { var size = value.unknownFields.size size += MutableHeader.ADAPTER.encodedSizeWithTag(1, value.header_) - size += MutablePayload.ADAPTER.encodedSizeWithTag(2, value.payload) + size += MutablePayload.ADAPTER.asRepeated().encodedSizeWithTag(2, value.payload) return size } override fun encode(writer: ProtoWriter, `value`: MutablePacket) { MutableHeader.ADAPTER.encodeWithTag(writer, 1, value.header_) - MutablePayload.ADAPTER.encodeWithTag(writer, 2, value.payload) + MutablePayload.ADAPTER.asRepeated().encodeWithTag(writer, 2, value.payload) writer.writeBytes(value.unknownFields) } override fun encode(writer: ReverseProtoWriter, `value`: MutablePacket) { writer.writeBytes(value.unknownFields) - MutablePayload.ADAPTER.encodeWithTag(writer, 2, value.payload) + MutablePayload.ADAPTER.asRepeated().encodeWithTag(writer, 2, value.payload) MutableHeader.ADAPTER.encodeWithTag(writer, 1, value.header_) } override fun decode(reader: ProtoReader): MutablePacket { var header_: MutableHeader? = null - var payload: MutablePayload? = null + val payload = mutableListOf() val unknownFields = reader.forEachTag { tag -> when (tag) { 1 -> header_ = MutableHeader.ADAPTER.decode(reader) - 2 -> payload = MutablePayload.ADAPTER.decode(reader) + 2 -> payload.add(MutablePayload.ADAPTER.decode(reader)) else -> reader.readUnknownField(tag) } } diff --git a/wire-golden-files/src/main/proto/squareup/wire/mutable_types.proto b/wire-golden-files/src/main/proto/squareup/wire/mutable_types.proto index 258c7a61ec..225f31ae95 100644 --- a/wire-golden-files/src/main/proto/squareup/wire/mutable_types.proto +++ b/wire-golden-files/src/main/proto/squareup/wire/mutable_types.proto @@ -21,5 +21,5 @@ message Payload { message Packet { optional Header header = 1; - optional Payload payload = 2; + repeated Payload payload = 2; }