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/kotlin/squareup/wire/mutable/MutablePayload.kt b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt index b8e3722934..2b363717cc 100644 --- a/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt +++ b/wire-golden-files/src/main/kotlin/squareup/wire/mutable/MutablePayload.kt @@ -7,6 +7,7 @@ package squareup.wire.mutable +import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding import com.squareup.wire.Message import com.squareup.wire.ProtoAdapter @@ -14,8 +15,12 @@ import com.squareup.wire.ProtoReader import com.squareup.wire.ProtoWriter import com.squareup.wire.ReverseProtoWriter import com.squareup.wire.Syntax.PROTO_2 +import com.squareup.wire.WireEnum import com.squareup.wire.WireField import com.squareup.wire.`internal`.JvmField +import com.squareup.wire.`internal`.JvmStatic +import com.squareup.wire.`internal`.immutableCopyOf +import com.squareup.wire.`internal`.sanitize import kotlin.Any import kotlin.Boolean import kotlin.Deprecated @@ -26,17 +31,39 @@ import kotlin.Nothing import kotlin.String import kotlin.Suppress import kotlin.UnsupportedOperationException +import kotlin.collections.List import okio.ByteString public class MutablePayload( @field:WireField( tag = 1, - adapter = "com.squareup.wire.ProtoAdapter#BYTES", + adapter = "com.squareup.wire.ProtoAdapter#STRING", schemaIndex = 0, ) + public var preamble: String? = null, + @field:WireField( + tag = 2, + adapter = "com.squareup.wire.ProtoAdapter#BYTES", + schemaIndex = 1, + ) public var content: ByteString? = null, + @field:WireField( + tag = 3, + adapter = "squareup.wire.mutable.MutablePayload${'$'}Type#ADAPTER", + schemaIndex = 2, + ) + public var type: Type? = null, + footers: List = emptyList(), override var unknownFields: ByteString = ByteString.EMPTY, ) : Message(ADAPTER, unknownFields) { + @field:WireField( + tag = 4, + adapter = "com.squareup.wire.ProtoAdapter#STRING", + label = WireField.Label.REPEATED, + schemaIndex = 3, + ) + public var footers: List = immutableCopyOf("footers", footers) + @Deprecated( message = "Shouldn't be used in Kotlin", level = DeprecationLevel.HIDDEN, @@ -47,20 +74,29 @@ public class MutablePayload( if (other === this) return true if (other !is MutablePayload) return false if (unknownFields != other.unknownFields) return false + if (preamble != other.preamble) return false if (content != other.content) return false + if (type != other.type) return false + if (footers != other.footers) return false return true } override fun hashCode(): Int { var result = 0 result = unknownFields.hashCode() + result = result * 37 + (preamble?.hashCode() ?: 0) result = result * 37 + (content?.hashCode() ?: 0) + result = result * 37 + (type?.hashCode() ?: 0) + result = result * 37 + footers.hashCode() return result } override fun toString(): String { val result = mutableListOf() + if (preamble != null) result += """preamble=${sanitize(preamble!!)}""" if (content != null) result += """content=$content""" + if (type != null) result += """type=$type""" + if (footers.isNotEmpty()) result += """footers=${sanitize(footers)}""" return result.joinToString(prefix = "MutablePayload{", separator = ", ", postfix = "}") } @@ -76,30 +112,52 @@ public class MutablePayload( ) { override fun encodedSize(`value`: MutablePayload): Int { var size = value.unknownFields.size - size += ProtoAdapter.BYTES.encodedSizeWithTag(1, value.content) + size += ProtoAdapter.STRING.encodedSizeWithTag(1, value.preamble) + size += ProtoAdapter.BYTES.encodedSizeWithTag(2, value.content) + size += Type.ADAPTER.encodedSizeWithTag(3, value.type) + size += ProtoAdapter.STRING.asRepeated().encodedSizeWithTag(4, value.footers) return size } override fun encode(writer: ProtoWriter, `value`: MutablePayload) { - ProtoAdapter.BYTES.encodeWithTag(writer, 1, value.content) + ProtoAdapter.STRING.encodeWithTag(writer, 1, value.preamble) + ProtoAdapter.BYTES.encodeWithTag(writer, 2, value.content) + Type.ADAPTER.encodeWithTag(writer, 3, value.type) + ProtoAdapter.STRING.asRepeated().encodeWithTag(writer, 4, value.footers) writer.writeBytes(value.unknownFields) } override fun encode(writer: ReverseProtoWriter, `value`: MutablePayload) { writer.writeBytes(value.unknownFields) - ProtoAdapter.BYTES.encodeWithTag(writer, 1, value.content) + ProtoAdapter.STRING.asRepeated().encodeWithTag(writer, 4, value.footers) + Type.ADAPTER.encodeWithTag(writer, 3, value.type) + ProtoAdapter.BYTES.encodeWithTag(writer, 2, value.content) + ProtoAdapter.STRING.encodeWithTag(writer, 1, value.preamble) } override fun decode(reader: ProtoReader): MutablePayload { + var preamble: String? = null var content: ByteString? = null + var type: Type? = null + val footers = mutableListOf() val unknownFields = reader.forEachTag { tag -> when (tag) { - 1 -> content = ProtoAdapter.BYTES.decode(reader) + 1 -> preamble = ProtoAdapter.STRING.decode(reader) + 2 -> content = ProtoAdapter.BYTES.decode(reader) + 3 -> try { + type = Type.ADAPTER.decode(reader) + } catch (e: ProtoAdapter.EnumConstantNotFoundException) { + reader.addUnknownField(tag, FieldEncoding.VARINT, e.value.toLong()) + } + 4 -> footers.add(ProtoAdapter.STRING.decode(reader)) else -> reader.readUnknownField(tag) } } return MutablePayload( + preamble = preamble, content = content, + type = type, + footers = footers, unknownFields = unknownFields ) } @@ -109,4 +167,36 @@ public class MutablePayload( private const val serialVersionUID: Long = 0L } + + public enum class Type( + override val `value`: Int, + ) : WireEnum { + TYPE_TEXT_PLAIN(1), + TYPE_TEXT_HTML(2), + TYPE_IMAGE_JPEG(3), + TYPE_IMAGE_PNG(4), + TYPE_UNKNOWN(10), + ; + + public companion object { + @JvmField + public val ADAPTER: ProtoAdapter = object : EnumAdapter( + Type::class, + PROTO_2, + null + ) { + override fun fromValue(`value`: Int): Type? = Type.fromValue(`value`) + } + + @JvmStatic + public fun fromValue(`value`: Int): Type? = when (`value`) { + 1 -> TYPE_TEXT_PLAIN + 2 -> TYPE_TEXT_HTML + 3 -> TYPE_IMAGE_JPEG + 4 -> TYPE_IMAGE_PNG + 10 -> TYPE_UNKNOWN + else -> null + } + } + } } 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 7ea115bb87..a501b6ee6c 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 @@ -7,10 +7,20 @@ message Header { } message Payload { - optional bytes content = 1; + enum Type { + TYPE_TEXT_PLAIN = 1; + TYPE_TEXT_HTML = 2; + TYPE_IMAGE_JPEG = 3; + TYPE_IMAGE_PNG = 4; + TYPE_UNKNOWN = 10; + } + optional string preamble = 1; + optional bytes content = 2; + optional Type type = 3; + repeated string footers = 4; } message Packet { optional Header header = 1; - optional Payload payload = 2; + repeated Payload payload = 2; } diff --git a/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt b/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt index a53c39229e..effc980f23 100644 --- a/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt +++ b/wire-kotlin-generator/src/main/java/com/squareup/wire/kotlin/KotlinGenerator.kt @@ -1382,7 +1382,11 @@ class KotlinGenerator private constructor( add("=$DOUBLE_FULL_BLOCK") } else { if (fieldOrOneOf.type == ProtoType.STRING) { - add("=\${%M(%N)}", sanitizeMember, fieldName) + if (mutableTypes && !fieldOrOneOf.isRepeated) { + add("=\${%M(%N!!)}", sanitizeMember, fieldName) + } else { + add("=\${%M(%N)}", sanitizeMember, fieldName) + } } else if (fieldOrOneOf.useArray) { add("=\${") add("%N", fieldName) @@ -3138,7 +3142,7 @@ class KotlinGenerator private constructor( fun putAll(kotlinPackage: String, enclosingClassName: ClassName?, types: List) { for (type in types) { val simpleName = type.type.simpleName - val name = if (mutableTypes) "Mutable$simpleName" else simpleName + val name = if (mutableTypes && type !is EnumType) "Mutable$simpleName" else simpleName val className = enclosingClassName?.nestedClass(name) ?: ClassName(kotlinPackage, name) typeToKotlinName[type.type] = className diff --git a/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt b/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt index 271d612ed7..45a2426bde 100644 --- a/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt +++ b/wire-kotlin-generator/src/test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt @@ -2315,7 +2315,15 @@ class KotlinGeneratorTest { } message Payload { + enum Type { + TYPE_TEXT_PLAIN = 1; + TYPE_TEXT_HTML = 2; + TYPE_IMAGE_JPEG = 3; + TYPE_IMAGE_PNG = 4; + TYPE_UNKNOWN = 10; + } optional bytes content = 1; + optional Type type = 2; } message Packet {