Skip to content

Commit

Permalink
Merge pull request #3233 from tikurahul/mutable-enums
Browse files Browse the repository at this point in the history
Fix code generation for mutable types.
  • Loading branch information
oldergod authored Jan 7, 2025
2 parents bf70ae7 + eef9cb3 commit 0b7cabb
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -36,14 +38,17 @@ public class MutablePacket(
schemaIndex = 0,
)
public var header_: MutableHeader? = null,
payload: List<MutablePayload> = emptyList(),
override var unknownFields: ByteString = ByteString.EMPTY,
) : Message<MutablePacket, Nothing>(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<MutablePacket, Nothing>(ADAPTER, unknownFields) {
public var payload: List<MutablePayload> = immutableCopyOf("payload", payload)

@Deprecated(
message = "Shouldn't be used in Kotlin",
level = DeprecationLevel.HIDDEN,
Expand All @@ -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<String>()
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 = "}")
}

Expand All @@ -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<MutablePayload>()
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@

package squareup.wire.mutable

import com.squareup.wire.EnumAdapter
import com.squareup.wire.FieldEncoding
import com.squareup.wire.Message
import com.squareup.wire.ProtoAdapter
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
Expand All @@ -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<String> = emptyList(),
override var unknownFields: ByteString = ByteString.EMPTY,
) : Message<MutablePayload, Nothing>(ADAPTER, unknownFields) {
@field:WireField(
tag = 4,
adapter = "com.squareup.wire.ProtoAdapter#STRING",
label = WireField.Label.REPEATED,
schemaIndex = 3,
)
public var footers: List<String> = immutableCopyOf("footers", footers)

@Deprecated(
message = "Shouldn't be used in Kotlin",
level = DeprecationLevel.HIDDEN,
Expand All @@ -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<String>()
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 = "}")
}

Expand All @@ -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<String>()
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
)
}
Expand All @@ -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<Type> = object : EnumAdapter<Type>(
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
}
}
}
}
14 changes: 12 additions & 2 deletions wire-golden-files/src/main/proto/squareup/wire/mutable_types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -3138,7 +3142,7 @@ class KotlinGenerator private constructor(
fun putAll(kotlinPackage: String, enclosingClassName: ClassName?, types: List<Type>) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0b7cabb

Please sign in to comment.