Skip to content

Commit

Permalink
Fix WireField adapter for useArray
Browse files Browse the repository at this point in the history
  • Loading branch information
oldergod committed Sep 5, 2023
1 parent 36e07c5 commit 290a9a0
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 601,
adapter = "com.squareup.wire.ProtoAdapter#INT32",
adapter = "com.squareup.wire.ProtoAdapter#INT32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 85,
)
Expand All @@ -817,7 +817,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 602,
adapter = "com.squareup.wire.ProtoAdapter#UINT32",
adapter = "com.squareup.wire.ProtoAdapter#UINT32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 86,
)
Expand All @@ -826,7 +826,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 603,
adapter = "com.squareup.wire.ProtoAdapter#SINT32",
adapter = "com.squareup.wire.ProtoAdapter#SINT32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 87,
)
Expand All @@ -835,7 +835,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 604,
adapter = "com.squareup.wire.ProtoAdapter#FIXED32",
adapter = "com.squareup.wire.ProtoAdapter#FIXED32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 88,
)
Expand All @@ -844,7 +844,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 605,
adapter = "com.squareup.wire.ProtoAdapter#SFIXED32",
adapter = "com.squareup.wire.ProtoAdapter#SFIXED32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 89,
)
Expand All @@ -853,7 +853,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 606,
adapter = "com.squareup.wire.ProtoAdapter#INT64",
adapter = "com.squareup.wire.ProtoAdapter#INT64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 90,
)
Expand All @@ -862,7 +862,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 607,
adapter = "com.squareup.wire.ProtoAdapter#UINT64",
adapter = "com.squareup.wire.ProtoAdapter#UINT64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 91,
)
Expand All @@ -871,7 +871,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 608,
adapter = "com.squareup.wire.ProtoAdapter#SINT64",
adapter = "com.squareup.wire.ProtoAdapter#SINT64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 92,
)
Expand All @@ -880,7 +880,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 609,
adapter = "com.squareup.wire.ProtoAdapter#FIXED64",
adapter = "com.squareup.wire.ProtoAdapter#FIXED64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 93,
)
Expand All @@ -889,7 +889,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 610,
adapter = "com.squareup.wire.ProtoAdapter#SFIXED64",
adapter = "com.squareup.wire.ProtoAdapter#SFIXED64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 94,
)
Expand All @@ -898,7 +898,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 611,
adapter = "com.squareup.wire.ProtoAdapter#FLOAT",
adapter = "com.squareup.wire.ProtoAdapter#FLOAT_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 95,
)
Expand All @@ -907,7 +907,7 @@ public class AllTypes private constructor(

@field:WireField(
tag = 612,
adapter = "com.squareup.wire.ProtoAdapter#DOUBLE",
adapter = "com.squareup.wire.ProtoAdapter#DOUBLE_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 96,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import okio.ByteString
public class ParameterValueWithArray(
@field:WireField(
tag = 1,
adapter = "com.squareup.wire.ProtoAdapter#FLOAT",
adapter = "com.squareup.wire.ProtoAdapter#FLOAT_ARRAY",
label = WireField.Label.PACKED,
declaredName = "data",
schemaIndex = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1606,7 +1606,7 @@ private AnnotationSpec wireFieldAnnotation(
}

private String adapterString(ProtoType type) {
String builtInAdapterString = builtInAdapterString(type);
String builtInAdapterString = builtInAdapterString(type, false);
if (builtInAdapterString != null) {
return builtInAdapterString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ class KotlinGenerator private constructor(
addMember("keyAdapter = %S", field.type!!.keyType!!.adapterString())
addMember("adapter = %S", field.type!!.valueType!!.adapterString())
} else {
addMember("adapter = %S", field.type!!.adapterString())
addMember("adapter = %S", field.type!!.adapterString(field.useArray))
}
}
.apply {
Expand Down Expand Up @@ -2169,13 +2169,13 @@ class KotlinGenerator private constructor(
}
}

private fun ProtoType.adapterString(): String {
private fun ProtoType.adapterString(useArray: Boolean = false): String {
val adapterConstant = profile.getAdapter(this)
if (adapterConstant != null) {
return "${adapterConstant.javaClassName.reflectionName()}#${adapterConstant.memberName}"
}

val builtInAdapterString = builtInAdapterString(this)
val builtInAdapterString = builtInAdapterString(this, useArray)
if (builtInAdapterString != null) return builtInAdapterString

return (typeName as ClassName).reflectionName() + "#ADAPTER"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1579,7 +1579,7 @@ class KotlinGeneratorTest {
|import "wire/extensions.proto";
|
|message Person {
| repeated float info = 1 [packed = true, (wire.use_array) = true];
| repeated float info = 1 [packed = true, (wire.use_array) = true];
|}
|
""".trimMargin(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ message BuildersOnlyMessage {
required int32 data = 2;
repeated int32 message = 3;
optional NestedMessage nested_message = 4;
repeated int64 int64 = 5 [packed = true, (wire.use_array) = true];
repeated int64 int64 = 5 [packed = true, (wire.use_array) = false];
map<string, string> map = 10;
oneof oneof {
int32 int32 = 15;
Expand Down
2 changes: 2 additions & 0 deletions wire-schema/api/wire-schema.api
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,8 @@ public final class com/squareup/wire/schema/internal/JvmLanguages {
public static final fun annotationName (Lcom/squareup/wire/schema/ProtoFile;Lcom/squareup/wire/schema/Field;Lcom/squareup/wire/schema/internal/NameFactory;)Ljava/lang/Object;
public static final fun annotationTargetType (Lcom/squareup/wire/schema/Extend;)Ljava/lang/annotation/ElementType;
public static final fun builtInAdapterString (Lcom/squareup/wire/schema/ProtoType;)Ljava/lang/String;
public static final fun builtInAdapterString (Lcom/squareup/wire/schema/ProtoType;Z)Ljava/lang/String;
public static synthetic fun builtInAdapterString$default (Lcom/squareup/wire/schema/ProtoType;ZILjava/lang/Object;)Ljava/lang/String;
public static final fun eligibleAsAnnotationMember (Lcom/squareup/wire/schema/Schema;Lcom/squareup/wire/schema/Field;)Z
public static final fun hasEponymousType (Lcom/squareup/wire/schema/Schema;Lcom/squareup/wire/schema/Field;)Z
public static final fun javaPackage (Lcom/squareup/wire/schema/ProtoFile;)Ljava/lang/String;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,27 @@ import java.util.Locale
* one language and other parts in another language.
*/

fun builtInAdapterString(type: ProtoType): String? {
@JvmOverloads
fun builtInAdapterString(type: ProtoType, useArray: Boolean = false): String? {
if (type.isScalar) {
if (useArray) {
return when (type) {
ProtoType.INT32 -> ProtoAdapter::class.java.name + "#INT32_ARRAY"
ProtoType.UINT32 -> ProtoAdapter::class.java.name + "#UINT32_ARRAY"
ProtoType.SINT32 -> ProtoAdapter::class.java.name + "#SINT32_ARRAY"
ProtoType.FIXED32 -> ProtoAdapter::class.java.name + "#FIXED32_ARRAY"
ProtoType.SFIXED32 -> ProtoAdapter::class.java.name + "#SFIXED32_ARRAY"
ProtoType.INT64 -> ProtoAdapter::class.java.name + "#INT64_ARRAY"
ProtoType.UINT64 -> ProtoAdapter::class.java.name + "#UINT64_ARRAY"
ProtoType.SINT64 -> ProtoAdapter::class.java.name + "#SINT64_ARRAY"
ProtoType.FIXED64 -> ProtoAdapter::class.java.name + "#FIXED64_ARRAY"
ProtoType.SFIXED64 -> ProtoAdapter::class.java.name + "#SFIXED64_ARRAY"
ProtoType.FLOAT -> ProtoAdapter::class.java.name + "#FLOAT_ARRAY"
ProtoType.DOUBLE -> ProtoAdapter::class.java.name + "#DOUBLE_ARRAY"
else -> throw IllegalArgumentException("No Array adapter for $type")
}
}

return "${ProtoAdapter::class.java.name}#${type.toString().uppercase(Locale.US)}"
}
return when (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,84 +422,84 @@ public class AllTypes(
map_string_enum: Map<String, NestedEnum> = emptyMap(),
@field:WireField(
tag = 601,
adapter = "com.squareup.wire.ProtoAdapter#INT32",
adapter = "com.squareup.wire.ProtoAdapter#INT32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 85,
)
public val array_int32: IntArray = intArrayOf(),
@field:WireField(
tag = 602,
adapter = "com.squareup.wire.ProtoAdapter#UINT32",
adapter = "com.squareup.wire.ProtoAdapter#UINT32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 86,
)
public val array_uint32: IntArray = intArrayOf(),
@field:WireField(
tag = 603,
adapter = "com.squareup.wire.ProtoAdapter#SINT32",
adapter = "com.squareup.wire.ProtoAdapter#SINT32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 87,
)
public val array_sint32: IntArray = intArrayOf(),
@field:WireField(
tag = 604,
adapter = "com.squareup.wire.ProtoAdapter#FIXED32",
adapter = "com.squareup.wire.ProtoAdapter#FIXED32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 88,
)
public val array_fixed32: IntArray = intArrayOf(),
@field:WireField(
tag = 605,
adapter = "com.squareup.wire.ProtoAdapter#SFIXED32",
adapter = "com.squareup.wire.ProtoAdapter#SFIXED32_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 89,
)
public val array_sfixed32: IntArray = intArrayOf(),
@field:WireField(
tag = 606,
adapter = "com.squareup.wire.ProtoAdapter#INT64",
adapter = "com.squareup.wire.ProtoAdapter#INT64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 90,
)
public val array_int64: LongArray = longArrayOf(),
@field:WireField(
tag = 607,
adapter = "com.squareup.wire.ProtoAdapter#UINT64",
adapter = "com.squareup.wire.ProtoAdapter#UINT64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 91,
)
public val array_uint64: LongArray = longArrayOf(),
@field:WireField(
tag = 608,
adapter = "com.squareup.wire.ProtoAdapter#SINT64",
adapter = "com.squareup.wire.ProtoAdapter#SINT64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 92,
)
public val array_sint64: LongArray = longArrayOf(),
@field:WireField(
tag = 609,
adapter = "com.squareup.wire.ProtoAdapter#FIXED64",
adapter = "com.squareup.wire.ProtoAdapter#FIXED64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 93,
)
public val array_fixed64: LongArray = longArrayOf(),
@field:WireField(
tag = 610,
adapter = "com.squareup.wire.ProtoAdapter#SFIXED64",
adapter = "com.squareup.wire.ProtoAdapter#SFIXED64_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 94,
)
public val array_sfixed64: LongArray = longArrayOf(),
@field:WireField(
tag = 611,
adapter = "com.squareup.wire.ProtoAdapter#FLOAT",
adapter = "com.squareup.wire.ProtoAdapter#FLOAT_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 95,
)
public val array_float: FloatArray = floatArrayOf(),
@field:WireField(
tag = 612,
adapter = "com.squareup.wire.ProtoAdapter#DOUBLE",
adapter = "com.squareup.wire.ProtoAdapter#DOUBLE_ARRAY",
label = WireField.Label.PACKED,
schemaIndex = 96,
)
Expand Down
Loading

0 comments on commit 290a9a0

Please sign in to comment.