Skip to content

Commit

Permalink
Change how we declare functions to encode and decode primitive arrays (
Browse files Browse the repository at this point in the history
…#2443)

* Target API from Wire 4.5.5

* Change how we declare functions to encode and decode primitive arrays

Previously we did it polymorphically which required public subtypes
for Int, Long, Double, and Float. This just uses a bunch of internal
functions.

The goal is to fix an unintended binary compatibility break introduced
in 4.5.6.
  • Loading branch information
squarejesse authored May 1, 2023
1 parent 0d6a439 commit 965a877
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,12 @@ import com.squareup.kotlinpoet.buildCodeBlock
import com.squareup.kotlinpoet.joinToCode
import com.squareup.kotlinpoet.jvm.jvmField
import com.squareup.kotlinpoet.jvm.jvmStatic
import com.squareup.wire.internal.DoubleArrayList
import com.squareup.wire.EnumAdapter
import com.squareup.wire.FieldEncoding
import com.squareup.wire.internal.FloatArrayList
import com.squareup.wire.GrpcCall
import com.squareup.wire.GrpcClient
import com.squareup.wire.GrpcMethod
import com.squareup.wire.GrpcStreamingCall
import com.squareup.wire.internal.IntArrayList
import com.squareup.wire.internal.LongArrayList
import com.squareup.wire.Message
import com.squareup.wire.MessageSink
import com.squareup.wire.MessageSource
Expand All @@ -74,6 +70,10 @@ import com.squareup.wire.WireEnum
import com.squareup.wire.WireEnumConstant
import com.squareup.wire.WireField
import com.squareup.wire.WireRpc
import com.squareup.wire.internal.DoubleArrayList
import com.squareup.wire.internal.FloatArrayList
import com.squareup.wire.internal.IntArrayList
import com.squareup.wire.internal.LongArrayList
import com.squareup.wire.internal.boxedOneOfClassName
import com.squareup.wire.internal.boxedOneOfKeyFieldName
import com.squareup.wire.internal.boxedOneOfKeysFieldName
Expand Down Expand Up @@ -110,11 +110,11 @@ import com.squareup.wire.schema.internal.javaPackage
import com.squareup.wire.schema.internal.legacyQualifiedFieldName
import com.squareup.wire.schema.internal.optionValueToInt
import com.squareup.wire.schema.internal.optionValueToLong
import java.util.Locale
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import okio.ByteString
import okio.ByteString.Companion.encode
import java.util.Locale

class KotlinGenerator private constructor(
val schema: Schema,
Expand Down Expand Up @@ -1618,12 +1618,14 @@ class KotlinGenerator private constructor(
add(fieldEqualsIdentityBlock(field, fieldName))
}
if (field.useArray && reverse) {
val encodeArray = MemberName("com.squareup.wire.internal", "encodeArray")
val encodeArray = MemberName(
"com.squareup.wire.internal",
"encodeArray_${field.type!!.simpleName}",
)
addStatement(
"%M(value.%L, %L, writer, %L)",
"%M(value.%L, writer, %L)",
encodeArray,
fieldName,
field.getAdapterName(),
field.tag,
)
} else {
Expand Down Expand Up @@ -1801,11 +1803,19 @@ class KotlinGenerator private constructor(
}

private fun decodeAndAssign(field: Field, fieldName: String, adapterName: CodeBlock): CodeBlock {
val decode = CodeBlock.of(
"%L.%L(reader)",
adapterName,
if (field.useArray) "decodePrimitive" else "decode",
)
val decode = if (field.useArray) {
CodeBlock.of(
"%M(reader)",
MemberName("com.squareup.wire.internal", "decodePrimitive_${field.type!!.simpleName}")
)
} else {
CodeBlock.of(
"%L.%L(reader)",
adapterName,
"decode",
)
}

return CodeBlock.of(
when {
field.useArray -> {
Expand Down
68 changes: 12 additions & 56 deletions wire-runtime/api/wire-runtime.api
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,6 @@ public final class com/squareup/wire/AnyMessage$Companion {
public final fun pack (Lcom/squareup/wire/Message;)Lcom/squareup/wire/AnyMessage;
}

public final class com/squareup/wire/DoubleProtoAdapter : com/squareup/wire/ProtoAdapter {
public fun <init> ()V
public fun decode (Lcom/squareup/wire/ProtoReader;)Ljava/lang/Double;
public synthetic fun decode (Lcom/squareup/wire/ProtoReader;)Ljava/lang/Object;
public final fun decodePrimitive (Lcom/squareup/wire/ProtoReader;)D
public fun encode (Lcom/squareup/wire/ProtoWriter;D)V
public synthetic fun encode (Lcom/squareup/wire/ProtoWriter;Ljava/lang/Object;)V
public fun encode (Lcom/squareup/wire/ReverseProtoWriter;D)V
public synthetic fun encode (Lcom/squareup/wire/ReverseProtoWriter;Ljava/lang/Object;)V
public final fun encodePrimitive (Lcom/squareup/wire/ReverseProtoWriter;D)V
public fun encodedSize (D)I
public synthetic fun encodedSize (Ljava/lang/Object;)I
public fun redact (D)Ljava/lang/Double;
public synthetic fun redact (Ljava/lang/Object;)Ljava/lang/Object;
}

public final class com/squareup/wire/DurationKt {
public static final fun durationOfSeconds (JJ)Ljava/time/Duration;
}
Expand Down Expand Up @@ -87,38 +71,10 @@ public final class com/squareup/wire/FieldEncoding : java/lang/Enum {
public final class com/squareup/wire/FieldEncoding$Companion {
}

public final class com/squareup/wire/FloatProtoAdapter : com/squareup/wire/ProtoAdapter {
public fun <init> ()V
public fun decode (Lcom/squareup/wire/ProtoReader;)Ljava/lang/Float;
public synthetic fun decode (Lcom/squareup/wire/ProtoReader;)Ljava/lang/Object;
public final fun decodePrimitive (Lcom/squareup/wire/ProtoReader;)F
public fun encode (Lcom/squareup/wire/ProtoWriter;F)V
public synthetic fun encode (Lcom/squareup/wire/ProtoWriter;Ljava/lang/Object;)V
public fun encode (Lcom/squareup/wire/ReverseProtoWriter;F)V
public synthetic fun encode (Lcom/squareup/wire/ReverseProtoWriter;Ljava/lang/Object;)V
public final fun encodePrimitive (Lcom/squareup/wire/ReverseProtoWriter;F)V
public fun encodedSize (F)I
public synthetic fun encodedSize (Ljava/lang/Object;)I
public fun redact (F)Ljava/lang/Float;
public synthetic fun redact (Ljava/lang/Object;)Ljava/lang/Object;
}

public final class com/squareup/wire/InstantKt {
public static final fun ofEpochSecond (JJ)Ljava/time/Instant;
}

public abstract class com/squareup/wire/IntProtoAdapter : com/squareup/wire/ProtoAdapter {
public fun <init> (Lcom/squareup/wire/FieldEncoding;)V
public abstract fun decodePrimitive (Lcom/squareup/wire/ProtoReader;)I
public abstract fun encodePrimitive (Lcom/squareup/wire/ReverseProtoWriter;I)V
}

public abstract class com/squareup/wire/LongProtoAdapter : com/squareup/wire/ProtoAdapter {
public fun <init> (Lcom/squareup/wire/FieldEncoding;)V
public abstract fun decodePrimitive (Lcom/squareup/wire/ProtoReader;)J
public abstract fun encodePrimitive (Lcom/squareup/wire/ReverseProtoWriter;J)V
}

public abstract class com/squareup/wire/Message : java/io/Serializable {
public static final field Companion Lcom/squareup/wire/Message$Companion;
protected field hashCode I
Expand Down Expand Up @@ -189,43 +145,43 @@ public abstract class com/squareup/wire/ProtoAdapter {
public static final field BYTES Lcom/squareup/wire/ProtoAdapter;
public static final field BYTES_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field Companion Lcom/squareup/wire/ProtoAdapter$Companion;
public static final field DOUBLE Lcom/squareup/wire/DoubleProtoAdapter;
public static final field DOUBLE Lcom/squareup/wire/ProtoAdapter;
public static final field DOUBLE_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field DOUBLE_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field DURATION Lcom/squareup/wire/ProtoAdapter;
public static final field EMPTY Lcom/squareup/wire/ProtoAdapter;
public static final field FIXED32 Lcom/squareup/wire/IntProtoAdapter;
public static final field FIXED32 Lcom/squareup/wire/ProtoAdapter;
public static final field FIXED32_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field FIXED64 Lcom/squareup/wire/LongProtoAdapter;
public static final field FIXED64 Lcom/squareup/wire/ProtoAdapter;
public static final field FIXED64_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field FLOAT Lcom/squareup/wire/FloatProtoAdapter;
public static final field FLOAT Lcom/squareup/wire/ProtoAdapter;
public static final field FLOAT_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field FLOAT_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field INSTANT Lcom/squareup/wire/ProtoAdapter;
public static final field INT32 Lcom/squareup/wire/IntProtoAdapter;
public static final field INT32 Lcom/squareup/wire/ProtoAdapter;
public static final field INT32_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field INT32_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field INT64 Lcom/squareup/wire/LongProtoAdapter;
public static final field INT64 Lcom/squareup/wire/ProtoAdapter;
public static final field INT64_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field INT64_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field SFIXED32 Lcom/squareup/wire/IntProtoAdapter;
public static final field SFIXED32 Lcom/squareup/wire/ProtoAdapter;
public static final field SFIXED32_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field SFIXED64 Lcom/squareup/wire/LongProtoAdapter;
public static final field SFIXED64 Lcom/squareup/wire/ProtoAdapter;
public static final field SFIXED64_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field SINT32 Lcom/squareup/wire/IntProtoAdapter;
public static final field SINT32 Lcom/squareup/wire/ProtoAdapter;
public static final field SINT32_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field SINT64 Lcom/squareup/wire/LongProtoAdapter;
public static final field SINT64 Lcom/squareup/wire/ProtoAdapter;
public static final field SINT64_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field STRING Lcom/squareup/wire/ProtoAdapter;
public static final field STRING_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field STRUCT_LIST Lcom/squareup/wire/ProtoAdapter;
public static final field STRUCT_MAP Lcom/squareup/wire/ProtoAdapter;
public static final field STRUCT_NULL Lcom/squareup/wire/ProtoAdapter;
public static final field STRUCT_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field UINT32 Lcom/squareup/wire/IntProtoAdapter;
public static final field UINT32 Lcom/squareup/wire/ProtoAdapter;
public static final field UINT32_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field UINT32_VALUE Lcom/squareup/wire/ProtoAdapter;
public static final field UINT64 Lcom/squareup/wire/LongProtoAdapter;
public static final field UINT64 Lcom/squareup/wire/ProtoAdapter;
public static final field UINT64_ARRAY Lcom/squareup/wire/ProtoAdapter;
public static final field UINT64_VALUE Lcom/squareup/wire/ProtoAdapter;
public fun <init> (Lcom/squareup/wire/FieldEncoding;Ljava/lang/Class;)V
Expand Down
Loading

0 comments on commit 965a877

Please sign in to comment.