From 60dc552a5a409743753fb9371bf2f607cca5dc0c Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Wed, 12 Apr 2023 15:09:42 -0400 Subject: [PATCH 01/17] Add option to use primitive arrays for packed scalars --- .../squareup/wire/kotlin/KotlinGenerator.kt | 23 +++++++++-- .../wire/kotlin/KotlinGeneratorTest.kt | 18 ++++++++ .../com/squareup/wire/DoubleArrayList.kt | 41 +++++++++++++++++++ .../com/squareup/wire/FloatArrayList.kt | 41 +++++++++++++++++++ .../kotlin/com/squareup/wire/IntArrayList.kt | 41 +++++++++++++++++++ .../kotlin/com/squareup/wire/LongArrayList.kt | 41 +++++++++++++++++++ .../com/squareup/wire/DoubleArrayList.kt | 30 ++++++++++++++ .../com/squareup/wire/FloatArrayListTest.kt | 30 ++++++++++++++ .../com/squareup/wire/IntArrayListTest.kt | 30 ++++++++++++++ .../kotlin/com/squareup/wire/LongArrayList.kt | 30 ++++++++++++++ .../kotlin/com/squareup/wire/schema/Field.kt | 4 ++ .../schema/internal/parser/OptionElement.kt | 3 ++ .../jvmMain/resources/wire/extensions.proto | 2 + 13 files changed, 330 insertions(+), 4 deletions(-) create mode 100644 wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt create mode 100644 wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt create mode 100644 wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt create mode 100644 wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt create mode 100644 wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayList.kt create mode 100644 wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt create mode 100644 wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt create mode 100644 wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayList.kt 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 04ec5be04e..05e1b243b4 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 @@ -54,6 +54,7 @@ import com.squareup.kotlinpoet.jvm.jvmField import com.squareup.kotlinpoet.jvm.jvmStatic import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding +import com.squareup.wire.FloatArrayList import com.squareup.wire.GrpcCall import com.squareup.wire.GrpcClient import com.squareup.wire.GrpcMethod @@ -1075,6 +1076,9 @@ class KotlinGenerator private constructor( fieldName ) } + field.isPacked && field.isScalar && field.usePrimitiveArray -> { + CodeBlock.of("FloatArrayList(0)") + } field.isRepeated || field.isMap -> { CodeBlock.of( "%M(%S, %N)", @@ -1620,7 +1624,11 @@ class KotlinGenerator private constructor( } if (fieldOrOneOf.isPacked && fieldOrOneOf.isScalar) { - addStatement("%1L = %1L ?: listOf(),", fieldName) + if (fieldOrOneOf.usePrimitiveArray) { + addStatement("%1L = %1L ?: arrayOf(),", fieldName) + } else { + addStatement("%1L = %1L ?: listOf(),", fieldName) + } } else { addStatement("%1L = %1L%2L,", fieldName, throwExceptionBlock) } @@ -1708,7 +1716,7 @@ class KotlinGenerator private constructor( val decode = CodeBlock.of("%L.decode(reader)", adapterName) return CodeBlock.of( when { - field.isPacked && field.isScalar -> + field.isPacked && field.isScalar -> { buildCodeBlock { beginControlFlow("if (%L == null)", fieldName) addStatement("val minimumByteSize = ${field.getMinimumByteSize()}") @@ -1719,6 +1727,7 @@ class KotlinGenerator private constructor( endControlFlow() addStatement("%1L!!.add(%2L)", field, decode) }.toString() + } field.isRepeated -> "%L.add(%L)" field.isMap -> "%L.putAll(%L)" else -> "%L·= %L" @@ -2186,6 +2195,7 @@ class KotlinGenerator private constructor( } private fun Field.getDeclaration(allocatedName: String) = when { + isPacked && isScalar && usePrimitiveArray -> CodeBlock.of("var %N: %T? = null", allocatedName, FloatArrayList::class) isPacked && isScalar -> CodeBlock.of("var %N: MutableList<%T>? = null", allocatedName, type!!.typeName) isRepeated -> CodeBlock.of("val $allocatedName = mutableListOf<%T>()", type!!.typeName) isMap -> CodeBlock.of( @@ -2235,8 +2245,13 @@ class KotlinGenerator private constructor( return when (encodeMode!!) { EncodeMode.MAP -> Map::class.asTypeName().parameterizedBy(keyType.typeName, valueType.typeName) - EncodeMode.REPEATED, - EncodeMode.PACKED -> List::class.asClassName().parameterizedBy(type.typeName) + EncodeMode.PACKED -> { + when { + usePrimitiveArray -> FloatArrayList::class.asTypeName() + else -> List::class.asTypeName().parameterizedBy(type.typeName) + } + } + EncodeMode.REPEATED -> List::class.asClassName().parameterizedBy(type.typeName) EncodeMode.NULL_IF_ABSENT -> type.typeName.copy(nullable = true) EncodeMode.REQUIRED -> type.typeName EncodeMode.OMIT_IDENTITY -> { 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 3a54d28824..7c3db80989 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 @@ -1513,6 +1513,24 @@ class KotlinGeneratorTest { assertTrue(code.contains("import wire_package.Person")) } + @Test fun jeffTest() { + val schema = buildSchema { + add( + "proto_package/person.proto".toPath(), + """ + |package proto_package; + |import "wire/extensions.proto"; + | + |message Person { + | repeated float name = 1 [packed = true, wire.use_primitive_array = true]; + |} + |""".trimMargin() + ) + } + val code = KotlinWithProfilesGenerator(schema).generateKotlin("proto_package.Person") + assertContains(code, "foobarbazz") + } + @Test fun documentationEscapesBrackets() { val schema = buildSchema { add( diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt new file mode 100644 index 0000000000..58de08070e --- /dev/null +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt @@ -0,0 +1,41 @@ +package com.squareup.wire + +/** + * Inspired by org.jetbrains.kotlin.utils.IntArrayList + * + * Offers a nice wrapper around DoubleArray, that handles resizing the underlying array as needed and + * provides a trimToSize() method to truncate the underlying array to the current number of elements. + */ +class DoubleArrayList(initialCapacity: Int) { + private var data = DoubleArray(initialCapacity) + private var size = 0 + + /** + * Returns the underlying DoubleArray, truncating as necessary so that the returned array has + * the same size as the number of elements in it. + * + * Because this method truncates the underlying array, it should only be called after all + * elements have been added to the list, otherwise the next call to add() will cause the + * array to be resized. + */ + fun getTruncatedArray(): DoubleArray { + if (size < data.size) { + data = data.copyOf(size) + } + return data + } + + private fun ensureCapacity(minCapacity: Int) { + if (minCapacity > data.size) { + data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) + } + } + + fun add(double: Double) { + ensureCapacity(size + 1) + data[size++] = double + } + + override fun toString(): String = + (0 until size).joinToString(", ", "[", "]") { data[it].toString() } +} diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt new file mode 100644 index 0000000000..13b48cabf9 --- /dev/null +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt @@ -0,0 +1,41 @@ +package com.squareup.wire + +/** + * Inspired by org.jetbrains.kotlin.utils.IntArrayList + * + * Offers a nice wrapper around FloatArray, that handles resizing the underlying array as needed and + * provides a trimToSize() method to truncate the underlying array to the current number of elements. + */ +class FloatArrayList(initialCapacity: Int) { + private var data = FloatArray(initialCapacity) + private var size = 0 + + /** + * Returns the underlying FloatArray, truncating as necessary so that the returned array has + * the same size as the number of elements in it. + * + * Because this method truncates the underlying array, it should only be called after all + * elements have been added to the list, otherwise the next call to add() will cause the + * array to be resized. + */ + fun getTruncatedArray(): FloatArray { + if (size < data.size) { + data = data.copyOf(size) + } + return data + } + + private fun ensureCapacity(minCapacity: Int) { + if (minCapacity > data.size) { + data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) + } + } + + fun add(float: Float) { + ensureCapacity(size + 1) + data[size++] = float + } + + override fun toString(): String = + (0 until size).joinToString(", ", "[", "]") { data[it].toString() } +} diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt new file mode 100644 index 0000000000..87e6c10eee --- /dev/null +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt @@ -0,0 +1,41 @@ +package com.squareup.wire + +/** + * Inspired by org.jetbrains.kotlin.utils.IntArrayList + * + * Offers a nice wrapper around IntArray, that handles resizing the underlying array as needed and + * provides a trimToSize() method to truncate the underlying array to the current number of elements. + */ +class IntArrayList(initialCapacity: Int) { + private var data = IntArray(initialCapacity) + private var size = 0 + + /** + * Returns the underlying IntArray, truncating as necessary so that the returned array has + * the same size as the number of elements in it. + * + * Because this method truncates the underlying array, it should only be called after all + * elements have been added to the list, otherwise the next call to add() will cause the + * array to be resized. + */ + fun getTruncatedArray(): IntArray { + if (size < data.size) { + data = data.copyOf(size) + } + return data + } + + private fun ensureCapacity(minCapacity: Int) { + if (minCapacity > data.size) { + data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) + } + } + + fun add(int: Int) { + ensureCapacity(size + 1) + data[size++] = int + } + + override fun toString(): String = + (0 until size).joinToString(", ", "[", "]") { data[it].toString() } +} diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt new file mode 100644 index 0000000000..b20800b926 --- /dev/null +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt @@ -0,0 +1,41 @@ +package com.squareup.wire + +/** + * Inspired by org.jetbrains.kotlin.utils.IntArrayList + * + * Offers a nice wrapper around LongArray, that handles resizing the underlying array as needed and + * provides a trimToSize() method to truncate the underlying array to the current number of elements. + */ +class LongArrayList(initialCapacity: Int) { + private var data = LongArray(initialCapacity) + private var size = 0 + + /** + * Returns the underlying LongArray, truncating as necessary so that the returned array has + * the same size as the number of elements in it. + * + * Because this method truncates the underlying array, it should only be called after all + * elements have been added to the list, otherwise the next call to add() will cause the + * array to be resized. + */ + fun getTruncatedArray(): LongArray { + if (size < data.size) { + data = data.copyOf(size) + } + return data + } + + private fun ensureCapacity(minCapacity: Int) { + if (minCapacity > data.size) { + data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) + } + } + + fun add(long: Long) { + ensureCapacity(size + 1) + data[size++] = long + } + + override fun toString(): String = + (0 until size).joinToString(", ", "[", "]") { data[it].toString() } +} diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayList.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayList.kt new file mode 100644 index 0000000000..f070de3403 --- /dev/null +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayList.kt @@ -0,0 +1,30 @@ +package com.squareup.wire + +import kotlin.test.Test +import kotlin.test.assertEquals + +class DoubleArrayListTest { + @Test + fun getTruncatedArrayReturnsCorrectlySizedArray() { + val arrayList = DoubleArrayList(0) + arrayList.add(1.0) + arrayList.add(2.0) + arrayList.add(3.0) + + val array = arrayList.getTruncatedArray() + assertEquals(array.size, 3) + for (i in 0..2) { + assertEquals(array[i], (i + 1).toDouble()) + } + } + + @Test + fun toStringIsReasonable() { + val arrayList = DoubleArrayList(0) + arrayList.add(1.0) + arrayList.add(2.0) + arrayList.add(3.0) + + assertEquals(arrayList.toString(), "[1.0, 2.0, 3.0]") + } +} diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt new file mode 100644 index 0000000000..a19d4cfec1 --- /dev/null +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt @@ -0,0 +1,30 @@ +package com.squareup.wire + +import kotlin.test.Test +import kotlin.test.assertEquals + +class FloatArrayListTest { + @Test + fun getTruncatedArrayReturnsCorrectlySizedArray() { + val arrayList = FloatArrayList(0) + arrayList.add(1f) + arrayList.add(2f) + arrayList.add(3f) + + val array = arrayList.getTruncatedArray() + assertEquals(array.size, 3) + for (i in 0..2) { + assertEquals(array[i], (i + 1).toFloat()) + } + } + + @Test + fun toStringIsReasonable() { + val arrayList = FloatArrayList(0) + arrayList.add(1f) + arrayList.add(2f) + arrayList.add(3f) + + assertEquals(arrayList.toString(), "[1.0, 2.0, 3.0]") + } +} diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt new file mode 100644 index 0000000000..bf945ad7f3 --- /dev/null +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt @@ -0,0 +1,30 @@ +package com.squareup.wire + +import kotlin.test.Test +import kotlin.test.assertEquals + +class IntArrayListTest { + @Test + fun getTruncatedArrayReturnsCorrectlySizedArray() { + val arrayList = IntArrayList(0) + arrayList.add(1) + arrayList.add(2) + arrayList.add(3) + + val array = arrayList.getTruncatedArray() + assertEquals(array.size, 3) + for (i in 0..2) { + assertEquals(array[i], i + 1) + } + } + + @Test + fun toStringIsReasonable() { + val arrayList = IntArrayList(0) + arrayList.add(1) + arrayList.add(2) + arrayList.add(3) + + assertEquals(arrayList.toString(), "[1, 2, 3]") + } +} diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayList.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayList.kt new file mode 100644 index 0000000000..74b56786c2 --- /dev/null +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayList.kt @@ -0,0 +1,30 @@ +package com.squareup.wire + +import kotlin.test.Test +import kotlin.test.assertEquals + +class LongArrayListTest { + @Test + fun getTruncatedArrayReturnsCorrectlySizedArray() { + val arrayList = LongArrayList(0) + arrayList.add(1L) + arrayList.add(2L) + arrayList.add(3L) + + val array = arrayList.getTruncatedArray() + assertEquals(array.size, 3) + for (i in 0..2) { + assertEquals(array[i], (i + 1).toLong()) + } + } + + @Test + fun toStringIsReasonable() { + val arrayList = LongArrayList(0) + arrayList.add(1L) + arrayList.add(2L) + arrayList.add(3L) + + assertEquals(arrayList.toString(), "[1, 2, 3]") + } +} diff --git a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt index e8327f363f..1a9c1ada8d 100644 --- a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt +++ b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt @@ -18,6 +18,7 @@ package com.squareup.wire.schema import com.squareup.wire.schema.Options.Companion.FIELD_OPTIONS import com.squareup.wire.schema.Options.Companion.GOOGLE_PROTOBUF_OPTION_TYPES import com.squareup.wire.schema.internal.parser.FieldElement +import com.squareup.wire.schema.internal.parser.OptionElement import com.squareup.wire.schema.internal.parser.OptionElement.Companion.PACKED_OPTION_ELEMENT import kotlin.jvm.JvmStatic @@ -107,6 +108,9 @@ data class Field( val isPacked: Boolean get() = encodeMode == EncodeMode.PACKED + val usePrimitiveArray: Boolean + get() = options.elements.contains(OptionElement.USE_PRIMITIVE_ARRAY_OPTION_ELEMENT) + // Null until this field is linked. var jsonName: String? = null private set diff --git a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt index 9a47ee1e76..4039f58fb7 100644 --- a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt +++ b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt @@ -123,6 +123,9 @@ data class OptionElement( internal val PACKED_OPTION_ELEMENT = OptionElement("packed", BOOLEAN, value = "true", isParenthesized = false) + internal val USE_PRIMITIVE_ARRAY_OPTION_ELEMENT = + OptionElement("wire.use_primitive_array", BOOLEAN, value = "true", isParenthesized = false) + @JvmOverloads fun create( name: String, diff --git a/wire-schema/src/jvmMain/resources/wire/extensions.proto b/wire-schema/src/jvmMain/resources/wire/extensions.proto index 15b5dae9bc..13823d15b1 100644 --- a/wire-schema/src/jvmMain/resources/wire/extensions.proto +++ b/wire-schema/src/jvmMain/resources/wire/extensions.proto @@ -35,6 +35,8 @@ extend google.protobuf.FieldOptions { * any subsequent version. */ optional string until = 1077; + + optional string use_primitive_array = 1078; } extend google.protobuf.EnumValueOptions { From 58a7c1f397e239f4dafdb72aaa6404885993d968 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Wed, 12 Apr 2023 15:58:05 -0400 Subject: [PATCH 02/17] Have to avoid using FloatArrayList everywhere, and fix encoding/decoding --- .../squareup/wire/kotlin/KotlinGenerator.kt | 16 ++- .../com/squareup/wire/DoubleArrayList.kt | 12 +- .../com/squareup/wire/FloatArrayList.kt | 11 +- .../kotlin/com/squareup/wire/IntArrayList.kt | 12 +- .../kotlin/com/squareup/wire/LongArrayList.kt | 12 +- .../wire/protos/kotlin/alltypes/AllTypes.kt | 131 ++++++++++++++++-- .../commonTest/proto/kotlin/all_types.proto | 7 + 7 files changed, 167 insertions(+), 34 deletions(-) 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 05e1b243b4..e9dae56223 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 @@ -1625,7 +1625,7 @@ class KotlinGenerator private constructor( if (fieldOrOneOf.isPacked && fieldOrOneOf.isScalar) { if (fieldOrOneOf.usePrimitiveArray) { - addStatement("%1L = %1L ?: arrayOf(),", fieldName) + addStatement("%1L = %1L ?: FloatArrayList(0),", fieldName) } else { addStatement("%1L = %1L ?: listOf(),", fieldName) } @@ -1717,13 +1717,14 @@ class KotlinGenerator private constructor( return CodeBlock.of( when { field.isPacked && field.isScalar -> { + val type = if (field.usePrimitiveArray) "FloatArrayList" else "ArrayList" buildCodeBlock { beginControlFlow("if (%L == null)", fieldName) addStatement("val minimumByteSize = ${field.getMinimumByteSize()}") addStatement("val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize)") addStatement("⇥.coerceAtMost(Int.MAX_VALUE.toLong())") addStatement(".toInt()") - addStatement("⇤%L = ArrayList(initialCapacity)", fieldName) + addStatement("⇤%L = %L(initialCapacity)", fieldName, type) endControlFlow() addStatement("%1L!!.add(%2L)", field, decode) }.toString() @@ -1805,6 +1806,7 @@ class KotlinGenerator private constructor( private fun Field.redact(fieldName: String): CodeBlock? { if (isRedacted) { return when { + usePrimitiveArray -> CodeBlock.of("FloatArrayList(0)") isRepeated -> CodeBlock.of("emptyList()") isMap -> CodeBlock.of("emptyMap()") encodeMode!! == EncodeMode.NULL_IF_ABSENT -> CodeBlock.of("null") @@ -2269,8 +2271,14 @@ class KotlinGenerator private constructor( get() { return when (encodeMode!!) { EncodeMode.MAP -> CodeBlock.of("emptyMap()") - EncodeMode.REPEATED, - EncodeMode.PACKED -> CodeBlock.of("emptyList()") + EncodeMode.REPEATED -> CodeBlock.of("emptyList()") + EncodeMode.PACKED -> { + if (usePrimitiveArray) { + CodeBlock.of("FloatArrayList()") + } else { + CodeBlock.of("emptyList()") + } + } EncodeMode.NULL_IF_ABSENT -> CodeBlock.of("null") EncodeMode.OMIT_IDENTITY -> { val protoType = type!! diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt index 58de08070e..79ca785cdc 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt @@ -25,17 +25,19 @@ class DoubleArrayList(initialCapacity: Int) { return data } + fun add(double: Double) { + ensureCapacity(size + 1) + data[size++] = double + } + + fun isNotEmpty() : Boolean = size > 0 + private fun ensureCapacity(minCapacity: Int) { if (minCapacity > data.size) { data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) } } - fun add(double: Double) { - ensureCapacity(size + 1) - data[size++] = double - } - override fun toString(): String = (0 until size).joinToString(", ", "[", "]") { data[it].toString() } } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt index 13b48cabf9..0126a13eb2 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt @@ -25,16 +25,19 @@ class FloatArrayList(initialCapacity: Int) { return data } + fun add(float: Float) { + ensureCapacity(size + 1) + data[size++] = float + } + + fun isNotEmpty() : Boolean = size > 0 + private fun ensureCapacity(minCapacity: Int) { if (minCapacity > data.size) { data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) } } - fun add(float: Float) { - ensureCapacity(size + 1) - data[size++] = float - } override fun toString(): String = (0 until size).joinToString(", ", "[", "]") { data[it].toString() } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt index 87e6c10eee..53689dc65f 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt @@ -25,17 +25,19 @@ class IntArrayList(initialCapacity: Int) { return data } + fun add(int: Int) { + ensureCapacity(size + 1) + data[size++] = int + } + + fun isNotEmpty() : Boolean = size > 0 + private fun ensureCapacity(minCapacity: Int) { if (minCapacity > data.size) { data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) } } - fun add(int: Int) { - ensureCapacity(size + 1) - data[size++] = int - } - override fun toString(): String = (0 until size).joinToString(", ", "[", "]") { data[it].toString() } } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt index b20800b926..0e9bafa05f 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt @@ -25,17 +25,19 @@ class LongArrayList(initialCapacity: Int) { return data } + fun add(long: Long) { + ensureCapacity(size + 1) + data[size++] = long + } + + fun isNotEmpty() : Boolean = size > 0 + private fun ensureCapacity(minCapacity: Int) { if (minCapacity > data.size) { data = data.copyOf(maxOf(data.size * 3 / 2 + 1, minCapacity)) } } - fun add(long: Long) { - ensureCapacity(size + 1) - data[size++] = long - } - override fun toString(): String = (0 until size).joinToString(", ", "[", "]") { data[it].toString() } } diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 68cc51bd02..867a8a7077 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -4,6 +4,7 @@ package com.squareup.wire.protos.kotlin.alltypes import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding +import com.squareup.wire.FloatArrayList import com.squareup.wire.Message import com.squareup.wire.ProtoAdapter import com.squareup.wire.ProtoReader @@ -340,6 +341,10 @@ public class AllTypes( map_string_string: Map = emptyMap(), map_string_message: Map = emptyMap(), map_string_enum: Map = emptyMap(), + primitive_fixed32s: FloatArrayList = FloatArrayList(), + primitive_fixed64s: FloatArrayList = FloatArrayList(), + primitive_floats: FloatArrayList = FloatArrayList(), + primitive_doubles: FloatArrayList = FloatArrayList(), /** * Extension source: all_types.proto */ @@ -759,6 +764,34 @@ public class AllTypes( public val map_string_enum: Map = immutableCopyOf("map_string_enum", map_string_enum) + @field:WireField( + tag = 601, + adapter = "com.squareup.wire.ProtoAdapter#FIXED32", + label = WireField.Label.PACKED, + ) + public val primitive_fixed32s: FloatArrayList = FloatArrayList(0) + + @field:WireField( + tag = 602, + adapter = "com.squareup.wire.ProtoAdapter#FIXED64", + label = WireField.Label.PACKED, + ) + public val primitive_fixed64s: FloatArrayList = FloatArrayList(0) + + @field:WireField( + tag = 603, + adapter = "com.squareup.wire.ProtoAdapter#FLOAT", + label = WireField.Label.PACKED, + ) + public val primitive_floats: FloatArrayList = FloatArrayList(0) + + @field:WireField( + tag = 604, + adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", + label = WireField.Label.PACKED, + ) + public val primitive_doubles: FloatArrayList = FloatArrayList(0) + /** * Extension source: all_types.proto */ @@ -1168,6 +1201,10 @@ public class AllTypes( if (map_string_string != other.map_string_string) return false if (map_string_message != other.map_string_message) return false if (map_string_enum != other.map_string_enum) return false + if (primitive_fixed32s != other.primitive_fixed32s) return false + if (primitive_fixed64s != other.primitive_fixed64s) return false + if (primitive_floats != other.primitive_floats) return false + if (primitive_doubles != other.primitive_doubles) return false if (ext_opt_int32 != other.ext_opt_int32) return false if (ext_opt_uint32 != other.ext_opt_uint32) return false if (ext_opt_sint32 != other.ext_opt_sint32) return false @@ -1308,6 +1345,10 @@ public class AllTypes( result = result * 37 + map_string_string.hashCode() result = result * 37 + map_string_message.hashCode() result = result * 37 + map_string_enum.hashCode() + result = result * 37 + primitive_fixed32s.hashCode() + result = result * 37 + primitive_fixed64s.hashCode() + result = result * 37 + primitive_floats.hashCode() + result = result * 37 + primitive_doubles.hashCode() result = result * 37 + (ext_opt_int32?.hashCode() ?: 0) result = result * 37 + (ext_opt_uint32?.hashCode() ?: 0) result = result * 37 + (ext_opt_sint32?.hashCode() ?: 0) @@ -1448,6 +1489,10 @@ public class AllTypes( if (map_string_string.isNotEmpty()) result += """map_string_string=$map_string_string""" if (map_string_message.isNotEmpty()) result += """map_string_message=$map_string_message""" if (map_string_enum.isNotEmpty()) result += """map_string_enum=$map_string_enum""" + if (primitive_fixed32s.isNotEmpty()) result += """primitive_fixed32s=$primitive_fixed32s""" + if (primitive_fixed64s.isNotEmpty()) result += """primitive_fixed64s=$primitive_fixed64s""" + if (primitive_floats.isNotEmpty()) result += """primitive_floats=$primitive_floats""" + if (primitive_doubles.isNotEmpty()) result += """primitive_doubles=$primitive_doubles""" if (ext_opt_int32 != null) result += """ext_opt_int32=$ext_opt_int32""" if (ext_opt_uint32 != null) result += """ext_opt_uint32=$ext_opt_uint32""" if (ext_opt_sint32 != null) result += """ext_opt_sint32=$ext_opt_sint32""" @@ -1588,6 +1633,10 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, + primitive_fixed32s: FloatArrayList = this.primitive_fixed32s, + primitive_fixed64s: FloatArrayList = this.primitive_fixed64s, + primitive_floats: FloatArrayList = this.primitive_floats, + primitive_doubles: FloatArrayList = this.primitive_doubles, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -1650,17 +1699,17 @@ public class AllTypes( default_fixed32, default_sfixed32, default_int64, default_uint64, default_sint64, default_fixed64, default_sfixed64, default_bool, default_float, default_double, default_string, default_bytes, default_nested_enum, map_int32_int32, map_string_string, - map_string_message, map_string_enum, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, - ext_opt_fixed32, ext_opt_sfixed32, ext_opt_int64, ext_opt_uint64, ext_opt_sint64, - ext_opt_fixed64, ext_opt_sfixed64, ext_opt_bool, ext_opt_float, ext_opt_double, - ext_opt_string, ext_opt_bytes, ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, - ext_rep_uint32, ext_rep_sint32, ext_rep_fixed32, ext_rep_sfixed32, ext_rep_int64, - ext_rep_uint64, ext_rep_sint64, ext_rep_fixed64, ext_rep_sfixed64, ext_rep_bool, - ext_rep_float, ext_rep_double, ext_rep_string, ext_rep_bytes, ext_rep_nested_enum, - ext_rep_nested_message, ext_pack_int32, ext_pack_uint32, ext_pack_sint32, ext_pack_fixed32, - ext_pack_sfixed32, ext_pack_int64, ext_pack_uint64, ext_pack_sint64, ext_pack_fixed64, - ext_pack_sfixed64, ext_pack_bool, ext_pack_float, ext_pack_double, ext_pack_nested_enum, - unknownFields) + map_string_message, map_string_enum, primitive_fixed32s, primitive_fixed64s, primitive_floats, + primitive_doubles, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, ext_opt_fixed32, + ext_opt_sfixed32, ext_opt_int64, ext_opt_uint64, ext_opt_sint64, ext_opt_fixed64, + ext_opt_sfixed64, ext_opt_bool, ext_opt_float, ext_opt_double, ext_opt_string, ext_opt_bytes, + ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, ext_rep_uint32, ext_rep_sint32, + ext_rep_fixed32, ext_rep_sfixed32, ext_rep_int64, ext_rep_uint64, ext_rep_sint64, + ext_rep_fixed64, ext_rep_sfixed64, ext_rep_bool, ext_rep_float, ext_rep_double, + ext_rep_string, ext_rep_bytes, ext_rep_nested_enum, ext_rep_nested_message, ext_pack_int32, + ext_pack_uint32, ext_pack_sint32, ext_pack_fixed32, ext_pack_sfixed32, ext_pack_int64, + ext_pack_uint64, ext_pack_sint64, ext_pack_fixed64, ext_pack_sfixed64, ext_pack_bool, + ext_pack_float, ext_pack_double, ext_pack_nested_enum, unknownFields) public companion object { public const val DEFAULT_DEFAULT_INT32: Int = Int.MAX_VALUE @@ -1807,6 +1856,10 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) + size += ProtoAdapter.FIXED32.asPacked().encodedSizeWithTag(601, value.primitive_fixed32s) + size += ProtoAdapter.FIXED64.asPacked().encodedSizeWithTag(602, value.primitive_fixed64s) + size += ProtoAdapter.FLOAT.asPacked().encodedSizeWithTag(603, value.primitive_floats) + size += ProtoAdapter.DOUBLE.asPacked().encodedSizeWithTag(604, value.primitive_doubles) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -1945,6 +1998,10 @@ public class AllTypes( map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) + ProtoAdapter.FIXED32.asPacked().encodeWithTag(writer, 601, value.primitive_fixed32s) + ProtoAdapter.FIXED64.asPacked().encodeWithTag(writer, 602, value.primitive_fixed64s) + ProtoAdapter.FLOAT.asPacked().encodeWithTag(writer, 603, value.primitive_floats) + ProtoAdapter.DOUBLE.asPacked().encodeWithTag(writer, 604, value.primitive_doubles) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) @@ -2046,6 +2103,10 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) + ProtoAdapter.DOUBLE.asPacked().encodeWithTag(writer, 604, value.primitive_doubles) + ProtoAdapter.FLOAT.asPacked().encodeWithTag(writer, 603, value.primitive_floats) + ProtoAdapter.FIXED64.asPacked().encodeWithTag(writer, 602, value.primitive_fixed64s) + ProtoAdapter.FIXED32.asPacked().encodeWithTag(writer, 601, value.primitive_fixed32s) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -2219,6 +2280,10 @@ public class AllTypes( val map_string_string = mutableMapOf() val map_string_message = mutableMapOf() val map_string_enum = mutableMapOf() + var primitive_fixed32s: FloatArrayList? = null + var primitive_fixed64s: FloatArrayList? = null + var primitive_floats: FloatArrayList? = null + var primitive_doubles: FloatArrayList? = null var ext_opt_int32: Int? = null var ext_opt_uint32: Int? = null var ext_opt_sint32: Int? = null @@ -2491,6 +2556,46 @@ public class AllTypes( 502 -> map_string_string.putAll(map_string_stringAdapter.decode(reader)) 503 -> map_string_message.putAll(map_string_messageAdapter.decode(reader)) 504 -> map_string_enum.putAll(map_string_enumAdapter.decode(reader)) + 601 -> { + if (primitive_fixed32s == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_fixed32s = FloatArrayList(initialCapacity) + } + primitive_fixed32s!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decode(reader)) + } + 602 -> { + if (primitive_fixed64s == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_fixed64s = FloatArrayList(initialCapacity) + } + primitive_fixed64s!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decode(reader)) + } + 603 -> { + if (primitive_floats == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_floats = FloatArrayList(initialCapacity) + } + primitive_floats!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 604 -> { + if (primitive_doubles == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_doubles = FloatArrayList(initialCapacity) + } + primitive_doubles!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decode(reader)) + } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) 1003 -> ext_opt_sint32 = ProtoAdapter.SINT32.decode(reader) @@ -2759,6 +2864,10 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, + primitive_fixed32s = primitive_fixed32s ?: FloatArrayList(0), + primitive_fixed64s = primitive_fixed64s ?: FloatArrayList(0), + primitive_floats = primitive_floats ?: FloatArrayList(0), + primitive_doubles = primitive_doubles ?: FloatArrayList(0), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, diff --git a/wire-tests/src/commonTest/proto/kotlin/all_types.proto b/wire-tests/src/commonTest/proto/kotlin/all_types.proto index aff4b243ec..448af6e705 100644 --- a/wire-tests/src/commonTest/proto/kotlin/all_types.proto +++ b/wire-tests/src/commonTest/proto/kotlin/all_types.proto @@ -15,6 +15,8 @@ */ package squareup.protos.kotlin.alltypes; +import "wire/extensions.proto"; + option java_package = "com.squareup.wire.protos.kotlin.alltypes"; option java_outer_classname = "AlltypesProtos"; @@ -121,6 +123,11 @@ message AllTypes { map map_string_string = 502; map map_string_message = 503; map map_string_enum = 504; + + repeated fixed32 primitive_fixed32s = 601 [packed = true, wire.use_primitive_array = true]; + repeated fixed64 primitive_fixed64s = 602 [packed = true, wire.use_primitive_array = true]; + repeated float primitive_floats = 603 [packed = true, wire.use_primitive_array = true]; + repeated double primitive_doubles = 604 [packed = true, wire.use_primitive_array = true]; } extend AllTypes { From f6846ee81e11ae960e6bd91035dd17673a13ccd0 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Thu, 13 Apr 2023 10:31:47 -0400 Subject: [PATCH 03/17] Progress, need to actually populate the fields in AllTypes --- .../squareup/wire/kotlin/KotlinGenerator.kt | 37 ++++-- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 124 +++++++++++++++++- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 2 + .../kotlin/com/squareup/wire/ProtoAdapter.kt | 2 + .../kotlin/com/squareup/wire/ProtoAdapter.kt | 2 + .../wire/protos/kotlin/alltypes/AllTypes.kt | 119 ++++++++--------- .../squareup/wire/ProtoAdapterIdentityTest.kt | 6 + .../squareup/wire/ProtoAdapterTypeUrlTest.kt | 4 +- 8 files changed, 215 insertions(+), 81 deletions(-) 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 e9dae56223..990aff96ae 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 @@ -660,7 +660,11 @@ class KotlinGenerator private constructor( when (fieldOrOneOf) { is Field -> { val fieldName = localNameAllocator[fieldOrOneOf] - addStatement("if (%1L != %2N.%1L) return·false", fieldName, otherName) + if (fieldOrOneOf.usePrimitiveArray) { + addStatement("if (!%1L.contentEquals(%2N.%1L)) return·false", fieldName, otherName) + } else { + addStatement("if (%1L != %2N.%1L) return·false", fieldName, otherName) + } } is OneOf -> { val fieldName = localNameAllocator[fieldOrOneOf] @@ -714,7 +718,9 @@ class KotlinGenerator private constructor( is Field -> { val fieldName = localNameAllocator[fieldOrOneOf] add("%1N = %1N * 37 + ", resultName) - if (fieldOrOneOf.isRepeated || fieldOrOneOf.isRequired || fieldOrOneOf.isMap || !fieldOrOneOf.acceptsNull) { + if (fieldOrOneOf.usePrimitiveArray) { + addStatement("%L.contentHashCode()", fieldName) + } else if (fieldOrOneOf.isRepeated || fieldOrOneOf.isRequired || fieldOrOneOf.isMap || !fieldOrOneOf.acceptsNull) { addStatement("%L.hashCode()", fieldName) } else { addStatement("(%L?.hashCode() ?: 0)", fieldName) @@ -1077,7 +1083,7 @@ class KotlinGenerator private constructor( ) } field.isPacked && field.isScalar && field.usePrimitiveArray -> { - CodeBlock.of("FloatArrayList(0)") + CodeBlock.of(fieldName) } field.isRepeated || field.isMap -> { CodeBlock.of( @@ -1521,11 +1527,15 @@ class KotlinGenerator private constructor( } private fun adapterFor(field: Field) = buildCodeBlock { - add("%L", field.getAdapterName()) - if (field.isPacked) { - add(".asPacked()") - } else if (field.isRepeated) { - add(".asRepeated()") + if (field.usePrimitiveArray) { + add("%T.FLOAT_PRIMITIVE_ARRAY", ProtoAdapter::class) + } else { + add("%L", field.getAdapterName()) + if (field.isPacked) { + add(".asPacked()") + } else if (field.isRepeated) { + add(".asRepeated()") + } } } @@ -1625,7 +1635,7 @@ class KotlinGenerator private constructor( if (fieldOrOneOf.isPacked && fieldOrOneOf.isScalar) { if (fieldOrOneOf.usePrimitiveArray) { - addStatement("%1L = %1L ?: FloatArrayList(0),", fieldName) + addStatement("%1L = %1L?.getTruncatedArray() ?: FloatArray(0),", fieldName) } else { addStatement("%1L = %1L ?: listOf(),", fieldName) } @@ -1806,7 +1816,7 @@ class KotlinGenerator private constructor( private fun Field.redact(fieldName: String): CodeBlock? { if (isRedacted) { return when { - usePrimitiveArray -> CodeBlock.of("FloatArrayList(0)") + usePrimitiveArray -> CodeBlock.of("FloatArray(0)") isRepeated -> CodeBlock.of("emptyList()") isMap -> CodeBlock.of("emptyMap()") encodeMode!! == EncodeMode.NULL_IF_ABSENT -> CodeBlock.of("null") @@ -1836,6 +1846,9 @@ class KotlinGenerator private constructor( } private fun Field.getAdapterName(nameDelimiter: Char = '.'): CodeBlock { + if (usePrimitiveArray) { + return CodeBlock.of("%T.FLOAT", ProtoAdapter::class) + } return if (type!!.isMap) { CodeBlock.of("%N", "${name}Adapter") } else { @@ -2249,7 +2262,7 @@ class KotlinGenerator private constructor( Map::class.asTypeName().parameterizedBy(keyType.typeName, valueType.typeName) EncodeMode.PACKED -> { when { - usePrimitiveArray -> FloatArrayList::class.asTypeName() + usePrimitiveArray -> FloatArray::class.asTypeName() else -> List::class.asTypeName().parameterizedBy(type.typeName) } } @@ -2274,7 +2287,7 @@ class KotlinGenerator private constructor( EncodeMode.REPEATED -> CodeBlock.of("emptyList()") EncodeMode.PACKED -> { if (usePrimitiveArray) { - CodeBlock.of("FloatArrayList()") + CodeBlock.of("FloatArray(0)") } else { CodeBlock.of("emptyList()") } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 1cab112137..0db1f87335 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -26,15 +26,15 @@ import com.squareup.wire.ProtoWriter.Companion.tagSize import com.squareup.wire.ProtoWriter.Companion.varint32Size import com.squareup.wire.ProtoWriter.Companion.varint64Size import com.squareup.wire.internal.Throws +import kotlin.jvm.JvmField +import kotlin.jvm.JvmStatic +import kotlin.reflect.KClass import okio.Buffer import okio.BufferedSink import okio.BufferedSource import okio.ByteString import okio.IOException import okio.utf8Size -import kotlin.jvm.JvmField -import kotlin.jvm.JvmStatic -import kotlin.reflect.KClass expect abstract class ProtoAdapter( fieldEncoding: FieldEncoding, @@ -165,7 +165,8 @@ expect abstract class ProtoAdapter( value: Int, type: KClass<*>? ) : IllegalArgumentException { - @JvmField val value: Int + @JvmField + val value: Int } companion object { @@ -176,7 +177,8 @@ expect abstract class ProtoAdapter( * the returned adapter, only single-element maps will be returned and it is the caller's * responsibility to merge them into the final map. */ - @JvmStatic fun newMapAdapter( + @JvmStatic + fun newMapAdapter( keyAdapter: ProtoAdapter, valueAdapter: ProtoAdapter ): ProtoAdapter> @@ -188,6 +190,7 @@ expect abstract class ProtoAdapter( @JvmField val FIXED32: ProtoAdapter @JvmField val SFIXED32: ProtoAdapter @JvmField val INT64: ProtoAdapter + @JvmField val INT64_PRIMITIVE_ARRAY: ProtoAdapter /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. @@ -197,6 +200,7 @@ expect abstract class ProtoAdapter( @JvmField val FIXED64: ProtoAdapter @JvmField val SFIXED64: ProtoAdapter @JvmField val FLOAT: ProtoAdapter + @JvmField val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter @JvmField val DOUBLE: ProtoAdapter @JvmField val BYTES: ProtoAdapter @JvmField val STRING: ProtoAdapter @@ -437,6 +441,116 @@ internal class RepeatedProtoAdapter( override fun redact(value: List): List = emptyList() } +class LongArrayProtoAdapter( + private val originalAdapter: ProtoAdapter, +) : ProtoAdapter( + LENGTH_DELIMITED, + LongArray::class, + null, + originalAdapter.syntax, + LongArray(0), +) { + @Throws(IOException::class) + override fun encodeWithTag(writer: ProtoWriter, tag: Int, value: LongArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + @Throws(IOException::class) + override fun encodeWithTag(writer: ReverseProtoWriter, tag: Int, value: LongArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + override fun encodedSize(value: LongArray): Int { + var size = 0 + for (i in 0 until value.size) { + size += originalAdapter.encodedSize(value[i]) + } + return size + } + + override fun encodedSizeWithTag(tag: Int, value: LongArray?): Int { + return if (value == null || value.isEmpty()) 0 else super.encodedSizeWithTag(tag, value) + } + + @Throws(IOException::class) + override fun encode(writer: ProtoWriter, value: LongArray) { + for (i in 0 until value.size) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun encode(writer: ReverseProtoWriter, value: LongArray) { + for (i in (value.size - 1) downTo 0) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): LongArray = LongArray(1) { originalAdapter.decode(reader) } + + override fun redact(value: LongArray): LongArray = LongArray(0) +} + +class FloatArrayProtoAdapter( + private val originalAdapter: ProtoAdapter, +) : ProtoAdapter( + LENGTH_DELIMITED, + LongArray::class, + null, + originalAdapter.syntax, + FloatArray(0), +) { + @Throws(IOException::class) + override fun encodeWithTag(writer: ProtoWriter, tag: Int, value: FloatArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + @Throws(IOException::class) + override fun encodeWithTag(writer: ReverseProtoWriter, tag: Int, value: FloatArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + override fun encodedSize(value: FloatArray): Int { + var size = 0 + for (i in 0 until value.size) { + size += originalAdapter.encodedSize(value[i]) + } + return size + } + + override fun encodedSizeWithTag(tag: Int, value: FloatArray?): Int { + return if (value == null || value.isEmpty()) 0 else super.encodedSizeWithTag(tag, value) + } + + @Throws(IOException::class) + override fun encode(writer: ProtoWriter, value: FloatArray) { + for (i in 0 until value.size) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun encode(writer: ReverseProtoWriter, value: FloatArray) { + for (i in (value.size - 1) downTo 0) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): FloatArray = FloatArray(1) { originalAdapter.decode(reader) } + + override fun redact(value: FloatArray): FloatArray = FloatArray(0) +} + internal class MapProtoAdapter internal constructor( keyAdapter: ProtoAdapter, valueAdapter: ProtoAdapter diff --git a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 1963a38a09..f9310b8964 100644 --- a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -166,6 +166,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED32: ProtoAdapter = commonFixed32() actual val SFIXED32: ProtoAdapter = commonSfixed32() actual val INT64: ProtoAdapter = commonInt64() + actual val INT64_PRIMITIVE_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. @@ -175,6 +176,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED64: ProtoAdapter = commonFixed64() actual val SFIXED64: ProtoAdapter = commonSfixed64() actual val FLOAT: ProtoAdapter = commonFloat() + actual val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() diff --git a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 4a33347a02..318dd8c7a9 100644 --- a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -290,11 +290,13 @@ actual abstract class ProtoAdapter actual constructor( @JvmField actual val FIXED32: ProtoAdapter = commonFixed32() @JvmField actual val SFIXED32: ProtoAdapter = commonSfixed32() @JvmField actual val INT64: ProtoAdapter = commonInt64() + @JvmField actual val INT64_PRIMITIVE_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) @JvmField actual val UINT64: ProtoAdapter = commonUint64() @JvmField actual val SINT64: ProtoAdapter = commonSint64() @JvmField actual val FIXED64: ProtoAdapter = commonFixed64() @JvmField actual val SFIXED64: ProtoAdapter = commonSfixed64() @JvmField actual val FLOAT: ProtoAdapter = commonFloat() + @JvmField actual val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) @JvmField actual val DOUBLE: ProtoAdapter = commonDouble() @JvmField actual val BYTES: ProtoAdapter = commonBytes() @JvmField actual val STRING: ProtoAdapter = commonString() diff --git a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt index a987ba0780..ffa92a7b22 100644 --- a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -166,6 +166,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED32: ProtoAdapter = commonFixed32() actual val SFIXED32: ProtoAdapter = commonSfixed32() actual val INT64: ProtoAdapter = commonInt64() + actual val INT64_PRIMITIVE_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. @@ -175,6 +176,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED64: ProtoAdapter = commonFixed64() actual val SFIXED64: ProtoAdapter = commonSfixed64() actual val FLOAT: ProtoAdapter = commonFloat() + actual val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 867a8a7077..eaa5c8f623 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -25,6 +25,7 @@ import kotlin.Deprecated import kotlin.DeprecationLevel import kotlin.Double import kotlin.Float +import kotlin.FloatArray import kotlin.Int import kotlin.Long import kotlin.Nothing @@ -341,10 +342,30 @@ public class AllTypes( map_string_string: Map = emptyMap(), map_string_message: Map = emptyMap(), map_string_enum: Map = emptyMap(), - primitive_fixed32s: FloatArrayList = FloatArrayList(), - primitive_fixed64s: FloatArrayList = FloatArrayList(), - primitive_floats: FloatArrayList = FloatArrayList(), - primitive_doubles: FloatArrayList = FloatArrayList(), + @field:WireField( + tag = 601, + adapter = "com.squareup.wire.ProtoAdapter#FIXED32", + label = WireField.Label.PACKED, + ) + public val primitive_fixed32s: FloatArray = FloatArray(0), + @field:WireField( + tag = 602, + adapter = "com.squareup.wire.ProtoAdapter#FIXED64", + label = WireField.Label.PACKED, + ) + public val primitive_fixed64s: FloatArray = FloatArray(0), + @field:WireField( + tag = 603, + adapter = "com.squareup.wire.ProtoAdapter#FLOAT", + label = WireField.Label.PACKED, + ) + public val primitive_floats: FloatArray = FloatArray(0), + @field:WireField( + tag = 604, + adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", + label = WireField.Label.PACKED, + ) + public val primitive_doubles: FloatArray = FloatArray(0), /** * Extension source: all_types.proto */ @@ -764,34 +785,6 @@ public class AllTypes( public val map_string_enum: Map = immutableCopyOf("map_string_enum", map_string_enum) - @field:WireField( - tag = 601, - adapter = "com.squareup.wire.ProtoAdapter#FIXED32", - label = WireField.Label.PACKED, - ) - public val primitive_fixed32s: FloatArrayList = FloatArrayList(0) - - @field:WireField( - tag = 602, - adapter = "com.squareup.wire.ProtoAdapter#FIXED64", - label = WireField.Label.PACKED, - ) - public val primitive_fixed64s: FloatArrayList = FloatArrayList(0) - - @field:WireField( - tag = 603, - adapter = "com.squareup.wire.ProtoAdapter#FLOAT", - label = WireField.Label.PACKED, - ) - public val primitive_floats: FloatArrayList = FloatArrayList(0) - - @field:WireField( - tag = 604, - adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", - label = WireField.Label.PACKED, - ) - public val primitive_doubles: FloatArrayList = FloatArrayList(0) - /** * Extension source: all_types.proto */ @@ -1201,10 +1194,10 @@ public class AllTypes( if (map_string_string != other.map_string_string) return false if (map_string_message != other.map_string_message) return false if (map_string_enum != other.map_string_enum) return false - if (primitive_fixed32s != other.primitive_fixed32s) return false - if (primitive_fixed64s != other.primitive_fixed64s) return false - if (primitive_floats != other.primitive_floats) return false - if (primitive_doubles != other.primitive_doubles) return false + if (!primitive_fixed32s.contentEquals(other.primitive_fixed32s)) return false + if (!primitive_fixed64s.contentEquals(other.primitive_fixed64s)) return false + if (!primitive_floats.contentEquals(other.primitive_floats)) return false + if (!primitive_doubles.contentEquals(other.primitive_doubles)) return false if (ext_opt_int32 != other.ext_opt_int32) return false if (ext_opt_uint32 != other.ext_opt_uint32) return false if (ext_opt_sint32 != other.ext_opt_sint32) return false @@ -1345,10 +1338,10 @@ public class AllTypes( result = result * 37 + map_string_string.hashCode() result = result * 37 + map_string_message.hashCode() result = result * 37 + map_string_enum.hashCode() - result = result * 37 + primitive_fixed32s.hashCode() - result = result * 37 + primitive_fixed64s.hashCode() - result = result * 37 + primitive_floats.hashCode() - result = result * 37 + primitive_doubles.hashCode() + result = result * 37 + primitive_fixed32s.contentHashCode() + result = result * 37 + primitive_fixed64s.contentHashCode() + result = result * 37 + primitive_floats.contentHashCode() + result = result * 37 + primitive_doubles.contentHashCode() result = result * 37 + (ext_opt_int32?.hashCode() ?: 0) result = result * 37 + (ext_opt_uint32?.hashCode() ?: 0) result = result * 37 + (ext_opt_sint32?.hashCode() ?: 0) @@ -1633,10 +1626,10 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, - primitive_fixed32s: FloatArrayList = this.primitive_fixed32s, - primitive_fixed64s: FloatArrayList = this.primitive_fixed64s, - primitive_floats: FloatArrayList = this.primitive_floats, - primitive_doubles: FloatArrayList = this.primitive_doubles, + primitive_fixed32s: FloatArray = this.primitive_fixed32s, + primitive_fixed64s: FloatArray = this.primitive_fixed64s, + primitive_floats: FloatArray = this.primitive_floats, + primitive_doubles: FloatArray = this.primitive_doubles, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -1856,10 +1849,10 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) - size += ProtoAdapter.FIXED32.asPacked().encodedSizeWithTag(601, value.primitive_fixed32s) - size += ProtoAdapter.FIXED64.asPacked().encodedSizeWithTag(602, value.primitive_fixed64s) - size += ProtoAdapter.FLOAT.asPacked().encodedSizeWithTag(603, value.primitive_floats) - size += ProtoAdapter.DOUBLE.asPacked().encodedSizeWithTag(604, value.primitive_doubles) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(601, value.primitive_fixed32s) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(602, value.primitive_fixed64s) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(603, value.primitive_floats) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(604, value.primitive_doubles) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -1998,10 +1991,10 @@ public class AllTypes( map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) - ProtoAdapter.FIXED32.asPacked().encodeWithTag(writer, 601, value.primitive_fixed32s) - ProtoAdapter.FIXED64.asPacked().encodeWithTag(writer, 602, value.primitive_fixed64s) - ProtoAdapter.FLOAT.asPacked().encodeWithTag(writer, 603, value.primitive_floats) - ProtoAdapter.DOUBLE.asPacked().encodeWithTag(writer, 604, value.primitive_doubles) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) @@ -2103,10 +2096,10 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - ProtoAdapter.DOUBLE.asPacked().encodeWithTag(writer, 604, value.primitive_doubles) - ProtoAdapter.FLOAT.asPacked().encodeWithTag(writer, 603, value.primitive_floats) - ProtoAdapter.FIXED64.asPacked().encodeWithTag(writer, 602, value.primitive_fixed64s) - ProtoAdapter.FIXED32.asPacked().encodeWithTag(writer, 601, value.primitive_fixed32s) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -2564,7 +2557,7 @@ public class AllTypes( .toInt() primitive_fixed32s = FloatArrayList(initialCapacity) } - primitive_fixed32s!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decode(reader)) + primitive_fixed32s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 602 -> { if (primitive_fixed64s == null) { @@ -2574,7 +2567,7 @@ public class AllTypes( .toInt() primitive_fixed64s = FloatArrayList(initialCapacity) } - primitive_fixed64s!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decode(reader)) + primitive_fixed64s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 603 -> { if (primitive_floats == null) { @@ -2594,7 +2587,7 @@ public class AllTypes( .toInt() primitive_doubles = FloatArrayList(initialCapacity) } - primitive_doubles!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decode(reader)) + primitive_doubles!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) @@ -2864,10 +2857,10 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - primitive_fixed32s = primitive_fixed32s ?: FloatArrayList(0), - primitive_fixed64s = primitive_fixed64s ?: FloatArrayList(0), - primitive_floats = primitive_floats ?: FloatArrayList(0), - primitive_doubles = primitive_doubles ?: FloatArrayList(0), + primitive_fixed32s = primitive_fixed32s?.getTruncatedArray() ?: FloatArray(0), + primitive_fixed64s = primitive_fixed64s?.getTruncatedArray() ?: FloatArray(0), + primitive_floats = primitive_floats?.getTruncatedArray() ?: FloatArray(0), + primitive_doubles = primitive_doubles?.getTruncatedArray() ?: FloatArray(0), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, diff --git a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt index b5f05e1917..7eca5e2fa0 100644 --- a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt +++ b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt @@ -36,6 +36,12 @@ class ProtoAdapterIdentityTest { // StructNull's in Kotlin is in Java. assertThat(protoAdapter.identity).isNull() } + protoAdapter.type == FloatArray::class -> { + assertThat(protoAdapter.identity).isEqualTo(floatArrayOf()) + } + protoAdapter.type == LongArray::class -> { + assertThat(protoAdapter.identity).isEqualTo(longArrayOf()) + } protoAdapter.type.isPrimitive && protoAdapter.syntax === Syntax.PROTO_2 -> { // All other primitive types are numbers and must have 0 as their identity value. assertThat((protoAdapter.identity as Number).toDouble()).isEqualTo(0.0) diff --git a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt index d8c7cc0ab1..6a3cc9fe48 100644 --- a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt +++ b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt @@ -36,7 +36,9 @@ class ProtoAdapterTypeUrlTest { ( protoAdapter.type.isPrimitive || protoAdapter.type == String::class || - protoAdapter.type == ByteString::class + protoAdapter.type == ByteString::class || + protoAdapter.type == FloatArray::class || + protoAdapter.type == LongArray::class ) -> { // Scalar types don't have a type URL. assertThat(protoAdapter.typeUrl).isNull() From 934026b834b583a89492da8b25a36c309194100a Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Thu, 13 Apr 2023 11:18:12 -0400 Subject: [PATCH 04/17] Run :generateTests --- .../src/main/swift/AllTypes.swift | 88 ++++++++- .../wire/protos/kotlin/alltypes/AllTypes.kt | 172 ++++++++++++++++-- 2 files changed, 248 insertions(+), 12 deletions(-) diff --git a/wire-tests-swift/src/main/swift/AllTypes.swift b/wire-tests-swift/src/main/swift/AllTypes.swift index fda270be78..e6d55a502f 100644 --- a/wire-tests-swift/src/main/swift/AllTypes.swift +++ b/wire-tests-swift/src/main/swift/AllTypes.swift @@ -772,6 +772,42 @@ public struct AllTypes { storage.map_string_enum = newValue } } + public var primitive_fixed32s: [UInt32] { + get { + storage.primitive_fixed32s + } + set { + copyStorage() + storage.primitive_fixed32s = newValue + } + } + public var primitive_fixed64s: [UInt64] { + get { + storage.primitive_fixed64s + } + set { + copyStorage() + storage.primitive_fixed64s = newValue + } + } + public var primitive_floats: [Float] { + get { + storage.primitive_floats + } + set { + copyStorage() + storage.primitive_floats = newValue + } + } + public var primitive_doubles: [Double] { + get { + storage.primitive_doubles + } + set { + copyStorage() + storage.primitive_doubles = newValue + } + } public var ext_opt_int32: Int32? { get { storage.ext_opt_int32 @@ -1300,6 +1336,10 @@ public struct AllTypes { map_string_string: [String : String] = [:], map_string_message: [String : NestedMessage] = [:], map_string_enum: [String : NestedEnum] = [:], + primitive_fixed32s: [UInt32] = [], + primitive_fixed64s: [UInt64] = [], + primitive_floats: [Float] = [], + primitive_doubles: [Double] = [], ext_opt_int32: Int32? = nil, ext_opt_uint32: UInt32? = nil, ext_opt_sint32: Int32? = nil, @@ -1382,7 +1422,9 @@ public struct AllTypes { default_string: default_string, default_bytes: default_bytes, default_nested_enum: default_nested_enum, map_int32_int32: map_int32_int32, map_string_string: map_string_string, map_string_message: map_string_message, - map_string_enum: map_string_enum, ext_opt_int32: ext_opt_int32, + map_string_enum: map_string_enum, primitive_fixed32s: primitive_fixed32s, + primitive_fixed64s: primitive_fixed64s, primitive_floats: primitive_floats, + primitive_doubles: primitive_doubles, ext_opt_int32: ext_opt_int32, ext_opt_uint32: ext_opt_uint32, ext_opt_sint32: ext_opt_sint32, ext_opt_fixed32: ext_opt_fixed32, ext_opt_sfixed32: ext_opt_sfixed32, ext_opt_int64: ext_opt_int64, ext_opt_uint64: ext_opt_uint64, @@ -1530,6 +1572,10 @@ fileprivate struct _AllTypes { public var map_string_string: [String : String] public var map_string_message: [String : AllTypes.NestedMessage] public var map_string_enum: [String : AllTypes.NestedEnum] + public var primitive_fixed32s: [UInt32] + public var primitive_fixed64s: [UInt64] + public var primitive_floats: [Float] + public var primitive_doubles: [Double] public var ext_opt_int32: Int32? public var ext_opt_uint32: UInt32? public var ext_opt_sint32: Int32? @@ -1666,6 +1712,10 @@ fileprivate struct _AllTypes { map_string_string: [String : String], map_string_message: [String : AllTypes.NestedMessage], map_string_enum: [String : AllTypes.NestedEnum], + primitive_fixed32s: [UInt32], + primitive_fixed64s: [UInt64], + primitive_floats: [Float], + primitive_doubles: [Double], ext_opt_int32: Int32?, ext_opt_uint32: UInt32?, ext_opt_sint32: Int32?, @@ -1800,6 +1850,10 @@ fileprivate struct _AllTypes { self.map_string_string = map_string_string self.map_string_message = map_string_message self.map_string_enum = map_string_enum + self.primitive_fixed32s = primitive_fixed32s + self.primitive_fixed64s = primitive_fixed64s + self.primitive_floats = primitive_floats + self.primitive_doubles = primitive_doubles self.ext_opt_int32 = ext_opt_int32 self.ext_opt_uint32 = ext_opt_uint32 self.ext_opt_sint32 = ext_opt_sint32 @@ -2033,6 +2087,10 @@ extension _AllTypes : Proto2Codable { var map_string_string: [String : String] = [:] var map_string_message: [String : AllTypes.NestedMessage] = [:] var map_string_enum: [String : AllTypes.NestedEnum] = [:] + var primitive_fixed32s: [UInt32] = [] + var primitive_fixed64s: [UInt64] = [] + var primitive_floats: [Float] = [] + var primitive_doubles: [Double] = [] var ext_opt_int32: Int32? = nil var ext_opt_uint32: UInt32? = nil var ext_opt_sint32: Int32? = nil @@ -2170,6 +2228,10 @@ extension _AllTypes : Proto2Codable { case 502: try reader.decode(into: &map_string_string) case 503: try reader.decode(into: &map_string_message) case 504: try reader.decode(into: &map_string_enum) + case 601: try reader.decode(into: &primitive_fixed32s, encoding: .fixed) + case 602: try reader.decode(into: &primitive_fixed64s, encoding: .fixed) + case 603: try reader.decode(into: &primitive_floats) + case 604: try reader.decode(into: &primitive_doubles) case 1001: ext_opt_int32 = try reader.decode(Int32.self) case 1002: ext_opt_uint32 = try reader.decode(UInt32.self) case 1003: ext_opt_sint32 = try reader.decode(Int32.self, encoding: .signed) @@ -2308,6 +2370,10 @@ extension _AllTypes : Proto2Codable { self.map_string_string = map_string_string self.map_string_message = map_string_message self.map_string_enum = map_string_enum + self.primitive_fixed32s = primitive_fixed32s + self.primitive_fixed64s = primitive_fixed64s + self.primitive_floats = primitive_floats + self.primitive_doubles = primitive_doubles self.ext_opt_int32 = ext_opt_int32 self.ext_opt_uint32 = ext_opt_uint32 self.ext_opt_sint32 = ext_opt_sint32 @@ -2444,6 +2510,10 @@ extension _AllTypes : Proto2Codable { try writer.encode(tag: 502, value: self.map_string_string) try writer.encode(tag: 503, value: self.map_string_message) try writer.encode(tag: 504, value: self.map_string_enum) + try writer.encode(tag: 601, value: self.primitive_fixed32s, encoding: .fixed, packed: true) + try writer.encode(tag: 602, value: self.primitive_fixed64s, encoding: .fixed, packed: true) + try writer.encode(tag: 603, value: self.primitive_floats, packed: true) + try writer.encode(tag: 604, value: self.primitive_doubles, packed: true) try writer.encode(tag: 1001, value: self.ext_opt_int32) try writer.encode(tag: 1002, value: self.ext_opt_uint32) try writer.encode(tag: 1003, value: self.ext_opt_sint32, encoding: .signed) @@ -2599,6 +2669,10 @@ extension _AllTypes : Codable { self.map_string_string = try container.decodeProtoMap([String : String].self, firstOfKeys: "mapStringString", "map_string_string") self.map_string_message = try container.decodeProtoMap([String : AllTypes.NestedMessage].self, firstOfKeys: "mapStringMessage", "map_string_message") self.map_string_enum = try container.decodeProtoMap([String : AllTypes.NestedEnum].self, firstOfKeys: "mapStringEnum", "map_string_enum") + self.primitive_fixed32s = try container.decodeProtoArray(UInt32.self, firstOfKeys: "primitiveFixed32s", "primitive_fixed32s") + self.primitive_fixed64s = try container.decodeProtoArray(UInt64.self, firstOfKeys: "primitiveFixed64s", "primitive_fixed64s") + self.primitive_floats = try container.decodeProtoArray(Float.self, firstOfKeys: "primitiveFloats", "primitive_floats") + self.primitive_doubles = try container.decodeProtoArray(Double.self, firstOfKeys: "primitiveDoubles", "primitive_doubles") self.ext_opt_int32 = try container.decodeIfPresent(Int32.self, firstOfKeys: "extOptInt32", "ext_opt_int32") self.ext_opt_uint32 = try container.decodeIfPresent(UInt32.self, firstOfKeys: "extOptUint32", "ext_opt_uint32") self.ext_opt_sint32 = try container.decodeIfPresent(Int32.self, firstOfKeys: "extOptSint32", "ext_opt_sint32") @@ -2841,6 +2915,18 @@ extension _AllTypes : Codable { if includeDefaults || !self.map_string_enum.isEmpty { try container.encodeProtoMap(self.map_string_enum, forKey: preferCamelCase ? "mapStringEnum" : "map_string_enum") } + if includeDefaults || !self.primitive_fixed32s.isEmpty { + try container.encodeProtoArray(self.primitive_fixed32s, forKey: preferCamelCase ? "primitiveFixed32s" : "primitive_fixed32s") + } + if includeDefaults || !self.primitive_fixed64s.isEmpty { + try container.encodeProtoArray(self.primitive_fixed64s, forKey: preferCamelCase ? "primitiveFixed64s" : "primitive_fixed64s") + } + if includeDefaults || !self.primitive_floats.isEmpty { + try container.encodeProtoArray(self.primitive_floats, forKey: preferCamelCase ? "primitiveFloats" : "primitive_floats") + } + if includeDefaults || !self.primitive_doubles.isEmpty { + try container.encodeProtoArray(self.primitive_doubles, forKey: preferCamelCase ? "primitiveDoubles" : "primitive_doubles") + } try container.encodeIfPresent(self.ext_opt_int32, forKey: preferCamelCase ? "extOptInt32" : "ext_opt_int32") try container.encodeIfPresent(self.ext_opt_uint32, forKey: preferCamelCase ? "extOptUint32" : "ext_opt_uint32") try container.encodeIfPresent(self.ext_opt_sint32, forKey: preferCamelCase ? "extOptSint32" : "ext_opt_sint32") diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index cca1cbefd5..1923d88940 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -4,6 +4,7 @@ package com.squareup.wire.protos.kotlin.alltypes import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding +import com.squareup.wire.FloatArrayList import com.squareup.wire.Message import com.squareup.wire.ProtoAdapter import com.squareup.wire.ProtoReader @@ -22,6 +23,7 @@ import kotlin.Any import kotlin.Boolean import kotlin.Double import kotlin.Float +import kotlin.FloatArray import kotlin.Int import kotlin.Long import kotlin.String @@ -387,6 +389,34 @@ public class AllTypes( map_string_string: Map = emptyMap(), map_string_message: Map = emptyMap(), map_string_enum: Map = emptyMap(), + @field:WireField( + tag = 601, + adapter = "com.squareup.wire.ProtoAdapter#FIXED32", + label = WireField.Label.PACKED, + ) + @JvmField + public val primitive_fixed32s: FloatArray = FloatArray(0), + @field:WireField( + tag = 602, + adapter = "com.squareup.wire.ProtoAdapter#FIXED64", + label = WireField.Label.PACKED, + ) + @JvmField + public val primitive_fixed64s: FloatArray = FloatArray(0), + @field:WireField( + tag = 603, + adapter = "com.squareup.wire.ProtoAdapter#FLOAT", + label = WireField.Label.PACKED, + ) + @JvmField + public val primitive_floats: FloatArray = FloatArray(0), + @field:WireField( + tag = 604, + adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", + label = WireField.Label.PACKED, + ) + @JvmField + public val primitive_doubles: FloatArray = FloatArray(0), /** * Extension source: all_types.proto */ @@ -1289,6 +1319,10 @@ public class AllTypes( builder.map_string_string = map_string_string builder.map_string_message = map_string_message builder.map_string_enum = map_string_enum + builder.primitive_fixed32s = primitive_fixed32s + builder.primitive_fixed64s = primitive_fixed64s + builder.primitive_floats = primitive_floats + builder.primitive_doubles = primitive_doubles builder.ext_opt_int32 = ext_opt_int32 builder.ext_opt_uint32 = ext_opt_uint32 builder.ext_opt_sint32 = ext_opt_sint32 @@ -1430,6 +1464,10 @@ public class AllTypes( if (map_string_string != other.map_string_string) return false if (map_string_message != other.map_string_message) return false if (map_string_enum != other.map_string_enum) return false + if (!primitive_fixed32s.contentEquals(other.primitive_fixed32s)) return false + if (!primitive_fixed64s.contentEquals(other.primitive_fixed64s)) return false + if (!primitive_floats.contentEquals(other.primitive_floats)) return false + if (!primitive_doubles.contentEquals(other.primitive_doubles)) return false if (ext_opt_int32 != other.ext_opt_int32) return false if (ext_opt_uint32 != other.ext_opt_uint32) return false if (ext_opt_sint32 != other.ext_opt_sint32) return false @@ -1570,6 +1608,10 @@ public class AllTypes( result = result * 37 + map_string_string.hashCode() result = result * 37 + map_string_message.hashCode() result = result * 37 + map_string_enum.hashCode() + result = result * 37 + primitive_fixed32s.contentHashCode() + result = result * 37 + primitive_fixed64s.contentHashCode() + result = result * 37 + primitive_floats.contentHashCode() + result = result * 37 + primitive_doubles.contentHashCode() result = result * 37 + (ext_opt_int32?.hashCode() ?: 0) result = result * 37 + (ext_opt_uint32?.hashCode() ?: 0) result = result * 37 + (ext_opt_sint32?.hashCode() ?: 0) @@ -1710,6 +1752,10 @@ public class AllTypes( if (map_string_string.isNotEmpty()) result += """map_string_string=$map_string_string""" if (map_string_message.isNotEmpty()) result += """map_string_message=$map_string_message""" if (map_string_enum.isNotEmpty()) result += """map_string_enum=$map_string_enum""" + if (primitive_fixed32s.isNotEmpty()) result += """primitive_fixed32s=$primitive_fixed32s""" + if (primitive_fixed64s.isNotEmpty()) result += """primitive_fixed64s=$primitive_fixed64s""" + if (primitive_floats.isNotEmpty()) result += """primitive_floats=$primitive_floats""" + if (primitive_doubles.isNotEmpty()) result += """primitive_doubles=$primitive_doubles""" if (ext_opt_int32 != null) result += """ext_opt_int32=$ext_opt_int32""" if (ext_opt_uint32 != null) result += """ext_opt_uint32=$ext_opt_uint32""" if (ext_opt_sint32 != null) result += """ext_opt_sint32=$ext_opt_sint32""" @@ -1850,6 +1896,10 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, + primitive_fixed32s: FloatArray = this.primitive_fixed32s, + primitive_fixed64s: FloatArray = this.primitive_fixed64s, + primitive_floats: FloatArray = this.primitive_floats, + primitive_doubles: FloatArray = this.primitive_doubles, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -1912,17 +1962,17 @@ public class AllTypes( default_fixed32, default_sfixed32, default_int64, default_uint64, default_sint64, default_fixed64, default_sfixed64, default_bool, default_float, default_double, default_string, default_bytes, default_nested_enum, map_int32_int32, map_string_string, - map_string_message, map_string_enum, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, - ext_opt_fixed32, ext_opt_sfixed32, ext_opt_int64, ext_opt_uint64, ext_opt_sint64, - ext_opt_fixed64, ext_opt_sfixed64, ext_opt_bool, ext_opt_float, ext_opt_double, - ext_opt_string, ext_opt_bytes, ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, - ext_rep_uint32, ext_rep_sint32, ext_rep_fixed32, ext_rep_sfixed32, ext_rep_int64, - ext_rep_uint64, ext_rep_sint64, ext_rep_fixed64, ext_rep_sfixed64, ext_rep_bool, - ext_rep_float, ext_rep_double, ext_rep_string, ext_rep_bytes, ext_rep_nested_enum, - ext_rep_nested_message, ext_pack_int32, ext_pack_uint32, ext_pack_sint32, ext_pack_fixed32, - ext_pack_sfixed32, ext_pack_int64, ext_pack_uint64, ext_pack_sint64, ext_pack_fixed64, - ext_pack_sfixed64, ext_pack_bool, ext_pack_float, ext_pack_double, ext_pack_nested_enum, - unknownFields) + map_string_message, map_string_enum, primitive_fixed32s, primitive_fixed64s, primitive_floats, + primitive_doubles, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, ext_opt_fixed32, + ext_opt_sfixed32, ext_opt_int64, ext_opt_uint64, ext_opt_sint64, ext_opt_fixed64, + ext_opt_sfixed64, ext_opt_bool, ext_opt_float, ext_opt_double, ext_opt_string, ext_opt_bytes, + ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, ext_rep_uint32, ext_rep_sint32, + ext_rep_fixed32, ext_rep_sfixed32, ext_rep_int64, ext_rep_uint64, ext_rep_sint64, + ext_rep_fixed64, ext_rep_sfixed64, ext_rep_bool, ext_rep_float, ext_rep_double, + ext_rep_string, ext_rep_bytes, ext_rep_nested_enum, ext_rep_nested_message, ext_pack_int32, + ext_pack_uint32, ext_pack_sint32, ext_pack_fixed32, ext_pack_sfixed32, ext_pack_int64, + ext_pack_uint64, ext_pack_sint64, ext_pack_fixed64, ext_pack_sfixed64, ext_pack_bool, + ext_pack_float, ext_pack_double, ext_pack_nested_enum, unknownFields) public class Builder : Message.Builder() { @JvmField @@ -2180,6 +2230,18 @@ public class AllTypes( @JvmField public var map_string_enum: Map = emptyMap() + @JvmField + public var primitive_fixed32s: List = FloatArray(0) + + @JvmField + public var primitive_fixed64s: List = FloatArray(0) + + @JvmField + public var primitive_floats: List = FloatArray(0) + + @JvmField + public var primitive_doubles: List = FloatArray(0) + @JvmField public var ext_opt_int32: Int? = null @@ -2780,6 +2842,30 @@ public class AllTypes( return this } + public fun primitive_fixed32s(primitive_fixed32s: List): Builder { + checkElementsNotNull(primitive_fixed32s) + this.primitive_fixed32s = primitive_fixed32s + return this + } + + public fun primitive_fixed64s(primitive_fixed64s: List): Builder { + checkElementsNotNull(primitive_fixed64s) + this.primitive_fixed64s = primitive_fixed64s + return this + } + + public fun primitive_floats(primitive_floats: List): Builder { + checkElementsNotNull(primitive_floats) + this.primitive_floats = primitive_floats + return this + } + + public fun primitive_doubles(primitive_doubles: List): Builder { + checkElementsNotNull(primitive_doubles) + this.primitive_doubles = primitive_doubles + return this + } + public fun ext_opt_int32(ext_opt_int32: Int?): Builder { this.ext_opt_int32 = ext_opt_int32 return this @@ -3139,6 +3225,10 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, + primitive_fixed32s = primitive_fixed32s, + primitive_fixed64s = primitive_fixed64s, + primitive_floats = primitive_floats, + primitive_doubles = primitive_doubles, ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, @@ -3336,6 +3426,10 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(601, value.primitive_fixed32s) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(602, value.primitive_fixed64s) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(603, value.primitive_floats) + size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(604, value.primitive_doubles) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -3474,6 +3568,10 @@ public class AllTypes( map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) @@ -3575,6 +3673,10 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) + ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -3748,6 +3850,10 @@ public class AllTypes( val map_string_string = mutableMapOf() val map_string_message = mutableMapOf() val map_string_enum = mutableMapOf() + var primitive_fixed32s: FloatArrayList? = null + var primitive_fixed64s: FloatArrayList? = null + var primitive_floats: FloatArrayList? = null + var primitive_doubles: FloatArrayList? = null var ext_opt_int32: Int? = null var ext_opt_uint32: Int? = null var ext_opt_sint32: Int? = null @@ -4020,6 +4126,46 @@ public class AllTypes( 502 -> map_string_string.putAll(map_string_stringAdapter.decode(reader)) 503 -> map_string_message.putAll(map_string_messageAdapter.decode(reader)) 504 -> map_string_enum.putAll(map_string_enumAdapter.decode(reader)) + 601 -> { + if (primitive_fixed32s == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_fixed32s = FloatArrayList(initialCapacity) + } + primitive_fixed32s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 602 -> { + if (primitive_fixed64s == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_fixed64s = FloatArrayList(initialCapacity) + } + primitive_fixed64s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 603 -> { + if (primitive_floats == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_floats = FloatArrayList(initialCapacity) + } + primitive_floats!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 604 -> { + if (primitive_doubles == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + primitive_doubles = FloatArrayList(initialCapacity) + } + primitive_doubles!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) 1003 -> ext_opt_sint32 = ProtoAdapter.SINT32.decode(reader) @@ -4288,6 +4434,10 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, + primitive_fixed32s = primitive_fixed32s?.getTruncatedArray() ?: FloatArray(0), + primitive_fixed64s = primitive_fixed64s?.getTruncatedArray() ?: FloatArray(0), + primitive_floats = primitive_floats?.getTruncatedArray() ?: FloatArray(0), + primitive_doubles = primitive_doubles?.getTruncatedArray() ?: FloatArray(0), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, From c1f5edfd6f15965e912ee0af1732be0ba8c2db47 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Thu, 13 Apr 2023 11:39:26 -0400 Subject: [PATCH 05/17] Handle java interop better --- .../squareup/wire/kotlin/KotlinGenerator.kt | 12 ++++++++--- .../kotlin/com/squareup/wire/TestAllTypes.kt | 3 ++- .../wire/protos/kotlin/alltypes/AllTypes.kt | 20 ++++++++----------- 3 files changed, 19 insertions(+), 16 deletions(-) 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 990aff96ae..92b34025df 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 @@ -947,7 +947,7 @@ class KotlinGenerator private constructor( .build() ) } - if (field.isRepeated) { + if (field.isRepeated && !field.usePrimitiveArray) { val checkElementsNotNull = MemberName("com.squareup.wire.internal", "checkElementsNotNull") funBuilder.addStatement("%M(%L)", checkElementsNotNull, fieldName) } @@ -2241,8 +2241,14 @@ class KotlinGenerator private constructor( val type = type!! val baseClass = type.typeName return when (encodeMode!!) { - EncodeMode.REPEATED, - EncodeMode.PACKED -> List::class.asClassName().parameterizedBy(baseClass) + EncodeMode.REPEATED -> List::class.asClassName().parameterizedBy(baseClass) + EncodeMode.PACKED -> { + if (usePrimitiveArray) { + FloatArray::class.asTypeName() + } else { + List::class.asTypeName().parameterizedBy(baseClass) + } + } EncodeMode.MAP -> baseClass.copy(nullable = false) EncodeMode.NULL_IF_ABSENT -> baseClass.copy(nullable = true) else -> { diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt index 4c8aa037b9..526023d6ac 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt @@ -97,7 +97,8 @@ class TestAllTypes { pack_nested_enum = list(AllTypes.NestedEnum.A, numRepeated), ext_opt_bool = true, ext_rep_bool = list(true, numRepeated), - ext_pack_bool = list(true, numRepeated) + ext_pack_bool = list(true, numRepeated), + primitive_floats = list(122.0f, numRepeated).toFloatArray(), ) } diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 1923d88940..60e361c9b3 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -2231,16 +2231,16 @@ public class AllTypes( public var map_string_enum: Map = emptyMap() @JvmField - public var primitive_fixed32s: List = FloatArray(0) + public var primitive_fixed32s: FloatArray = FloatArray(0) @JvmField - public var primitive_fixed64s: List = FloatArray(0) + public var primitive_fixed64s: FloatArray = FloatArray(0) @JvmField - public var primitive_floats: List = FloatArray(0) + public var primitive_floats: FloatArray = FloatArray(0) @JvmField - public var primitive_doubles: List = FloatArray(0) + public var primitive_doubles: FloatArray = FloatArray(0) @JvmField public var ext_opt_int32: Int? = null @@ -2842,26 +2842,22 @@ public class AllTypes( return this } - public fun primitive_fixed32s(primitive_fixed32s: List): Builder { - checkElementsNotNull(primitive_fixed32s) + public fun primitive_fixed32s(primitive_fixed32s: FloatArray): Builder { this.primitive_fixed32s = primitive_fixed32s return this } - public fun primitive_fixed64s(primitive_fixed64s: List): Builder { - checkElementsNotNull(primitive_fixed64s) + public fun primitive_fixed64s(primitive_fixed64s: FloatArray): Builder { this.primitive_fixed64s = primitive_fixed64s return this } - public fun primitive_floats(primitive_floats: List): Builder { - checkElementsNotNull(primitive_floats) + public fun primitive_floats(primitive_floats: FloatArray): Builder { this.primitive_floats = primitive_floats return this } - public fun primitive_doubles(primitive_doubles: List): Builder { - checkElementsNotNull(primitive_doubles) + public fun primitive_doubles(primitive_doubles: FloatArray): Builder { this.primitive_doubles = primitive_doubles return this } From 34e6a0da661a123e56a07ccac3858b9275a8dac0 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Thu, 13 Apr 2023 16:53:32 -0400 Subject: [PATCH 06/17] Get TestAllTypes working? --- .../kotlin/com/squareup/wire/TestAllTypes.kt | 2 +- .../com/squareup/wire/TestAllTypesData.kt | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt index 526023d6ac..b05bc71f83 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt @@ -263,8 +263,8 @@ class TestAllTypes { @Test fun testWrite() { val output = adapter.encode(allTypes) - assertEquals(TestAllTypesData.expectedOutput.size, output.size) assertEquals(TestAllTypesData.expectedOutput, ByteString.of(*output)) + assertEquals(TestAllTypesData.expectedOutput.size, output.size) } @Test fun testWriteSource() { diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt index 7232447d2f..ff3f0304f6 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt @@ -36,8 +36,9 @@ object TestAllTypesData { "e{a=999}], pack_int32=[111, 111], pack_uint32=[112, 112], pack_sint32=[113, 113], pack_fix" + "ed32=[114, 114], pack_sfixed32=[115, 115], pack_int64=[116, 116], pack_uint64=[117, 117], " + "pack_sint64=[118, 118], pack_fixed64=[119, 119], pack_sfixed64=[120, 120], pack_bool=[true" + - ", true], pack_float=[122.0, 122.0], pack_double=[123.0, 123.0], pack_nested_enum=[A, A], e" + - "xt_opt_bool=true, ext_rep_bool=[true, true], ext_pack_bool=[true, true]}" + ", true], pack_float=[122.0, 122.0], pack_double=[123.0, 123.0], pack_nested_enum=[A, A], p" + + "rimitive_floats=[122.0, 122.0], ext_opt_bool=true, ext_rep_bool=[true, true], ext_pack_boo" + + "l=[true, true]}" ) val expectedOutput = ( "" + @@ -260,6 +261,10 @@ object TestAllTypesData { "02" + // length = 2 "01" + // value = 1 "01" + // value = 1 + "da25" + // tag 603, type = 2 + "08" + // length = 8 + "0000f442" + // value = 122.0F + "0000f442" + // value = 122.0F // extensions @@ -485,9 +490,9 @@ object TestAllTypesData { "01" + // value = 1 (true) "b813" + // tag = 311, type = 0 "01" + // value = 1 (true) - "c513" + // tag = 312, type = 0 + "c513" + // tag = 312, type = 5 "0000f442" + // value = 122.0F - "c513" + // tag = 312, type = 0 + "c513" + // tag = 312, type = 5 "0000f442" + // value = 122.0F "c913" + // tag = 313, type = 0 "0000000000c05e40" + // value = 123.0 @@ -497,6 +502,10 @@ object TestAllTypesData { "01" + // value = 1 "e013" + // tag = 316, type = 0 "01" + // value = 1 + "dd25" + // tag 603, type = 5 + "0000f442" + // value = 122.0F + "dd25" + // tag 603, type = 5 + "0000f442" + // value = 122.0F // extension From 5338dee1365eee937ce8f40b95c25eac38b1604c Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Fri, 14 Apr 2023 08:45:48 -0400 Subject: [PATCH 07/17] Rename option to use_array, add remaining types to AllTypes --- .../squareup/wire/kotlin/KotlinGenerator.kt | 44 +- .../wire/kotlin/KotlinGeneratorTest.kt | 2 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 4 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 4 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 4 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 4 +- .../kotlin/com/squareup/wire/schema/Field.kt | 4 +- .../schema/internal/parser/OptionElement.kt | 4 +- .../jvmMain/resources/wire/extensions.proto | 6 +- .../src/main/swift/AllTypes.swift | 289 ++++++++--- .../kotlin/com/squareup/wire/TestAllTypes.kt | 2 +- .../com/squareup/wire/TestAllTypesData.kt | 12 +- .../wire/protos/kotlin/alltypes/AllTypes.kt | 323 ++++++++++--- .../commonTest/proto/kotlin/all_types.proto | 16 +- .../wire/protos/kotlin/alltypes/AllTypes.kt | 451 ++++++++++++++---- 15 files changed, 927 insertions(+), 242 deletions(-) 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 92b34025df..6c2f0e4906 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 @@ -660,7 +660,7 @@ class KotlinGenerator private constructor( when (fieldOrOneOf) { is Field -> { val fieldName = localNameAllocator[fieldOrOneOf] - if (fieldOrOneOf.usePrimitiveArray) { + if (fieldOrOneOf.useArray) { addStatement("if (!%1L.contentEquals(%2N.%1L)) return·false", fieldName, otherName) } else { addStatement("if (%1L != %2N.%1L) return·false", fieldName, otherName) @@ -718,7 +718,7 @@ class KotlinGenerator private constructor( is Field -> { val fieldName = localNameAllocator[fieldOrOneOf] add("%1N = %1N * 37 + ", resultName) - if (fieldOrOneOf.usePrimitiveArray) { + if (fieldOrOneOf.useArray) { addStatement("%L.contentHashCode()", fieldName) } else if (fieldOrOneOf.isRepeated || fieldOrOneOf.isRequired || fieldOrOneOf.isMap || !fieldOrOneOf.acceptsNull) { addStatement("%L.hashCode()", fieldName) @@ -947,7 +947,7 @@ class KotlinGenerator private constructor( .build() ) } - if (field.isRepeated && !field.usePrimitiveArray) { + if (field.isRepeated && !field.useArray) { val checkElementsNotNull = MemberName("com.squareup.wire.internal", "checkElementsNotNull") funBuilder.addStatement("%M(%L)", checkElementsNotNull, fieldName) } @@ -1082,7 +1082,7 @@ class KotlinGenerator private constructor( fieldName ) } - field.isPacked && field.isScalar && field.usePrimitiveArray -> { + field.isPacked && field.isScalar && field.useArray -> { CodeBlock.of(fieldName) } field.isRepeated || field.isMap -> { @@ -1226,10 +1226,13 @@ class KotlinGenerator private constructor( when (fieldOrOneOf) { is Field -> { val fieldName = localNameAllocator[fieldOrOneOf] - if (fieldOrOneOf.isRepeated || fieldOrOneOf.isMap) { - add("if (%N.isNotEmpty()) ", fieldName) - } else if (fieldOrOneOf.acceptsNull) { - add("if (%N != null) ", fieldName) + when { + fieldOrOneOf.isRepeated || fieldOrOneOf.isMap -> { + add("if (%N.isNotEmpty()) ", fieldName) + } + fieldOrOneOf.acceptsNull -> { + add("if (%N != null) ", fieldName) + } } addStatement( "%N += %P", resultName, @@ -1240,6 +1243,11 @@ class KotlinGenerator private constructor( } else { if (fieldOrOneOf.type == ProtoType.STRING) { add("=\${%M($fieldName)}", sanitizeMember) + } else if (fieldOrOneOf.useArray) { + add("=\${") + add(fieldName) + add(".contentToString()") + add("}") } else { add("=\$") add(fieldName) @@ -1527,8 +1535,8 @@ class KotlinGenerator private constructor( } private fun adapterFor(field: Field) = buildCodeBlock { - if (field.usePrimitiveArray) { - add("%T.FLOAT_PRIMITIVE_ARRAY", ProtoAdapter::class) + if (field.useArray) { + add("%T.FLOAT_ARRAY", ProtoAdapter::class) } else { add("%L", field.getAdapterName()) if (field.isPacked) { @@ -1634,7 +1642,7 @@ class KotlinGenerator private constructor( } if (fieldOrOneOf.isPacked && fieldOrOneOf.isScalar) { - if (fieldOrOneOf.usePrimitiveArray) { + if (fieldOrOneOf.useArray) { addStatement("%1L = %1L?.getTruncatedArray() ?: FloatArray(0),", fieldName) } else { addStatement("%1L = %1L ?: listOf(),", fieldName) @@ -1727,7 +1735,7 @@ class KotlinGenerator private constructor( return CodeBlock.of( when { field.isPacked && field.isScalar -> { - val type = if (field.usePrimitiveArray) "FloatArrayList" else "ArrayList" + val type = if (field.useArray) "FloatArrayList" else "ArrayList" buildCodeBlock { beginControlFlow("if (%L == null)", fieldName) addStatement("val minimumByteSize = ${field.getMinimumByteSize()}") @@ -1816,7 +1824,7 @@ class KotlinGenerator private constructor( private fun Field.redact(fieldName: String): CodeBlock? { if (isRedacted) { return when { - usePrimitiveArray -> CodeBlock.of("FloatArray(0)") + useArray -> CodeBlock.of("FloatArray(0)") isRepeated -> CodeBlock.of("emptyList()") isMap -> CodeBlock.of("emptyMap()") encodeMode!! == EncodeMode.NULL_IF_ABSENT -> CodeBlock.of("null") @@ -1846,7 +1854,7 @@ class KotlinGenerator private constructor( } private fun Field.getAdapterName(nameDelimiter: Char = '.'): CodeBlock { - if (usePrimitiveArray) { + if (useArray) { return CodeBlock.of("%T.FLOAT", ProtoAdapter::class) } return if (type!!.isMap) { @@ -2210,7 +2218,7 @@ class KotlinGenerator private constructor( } private fun Field.getDeclaration(allocatedName: String) = when { - isPacked && isScalar && usePrimitiveArray -> CodeBlock.of("var %N: %T? = null", allocatedName, FloatArrayList::class) + isPacked && isScalar && useArray -> CodeBlock.of("var %N: %T? = null", allocatedName, FloatArrayList::class) isPacked && isScalar -> CodeBlock.of("var %N: MutableList<%T>? = null", allocatedName, type!!.typeName) isRepeated -> CodeBlock.of("val $allocatedName = mutableListOf<%T>()", type!!.typeName) isMap -> CodeBlock.of( @@ -2243,7 +2251,7 @@ class KotlinGenerator private constructor( return when (encodeMode!!) { EncodeMode.REPEATED -> List::class.asClassName().parameterizedBy(baseClass) EncodeMode.PACKED -> { - if (usePrimitiveArray) { + if (useArray) { FloatArray::class.asTypeName() } else { List::class.asTypeName().parameterizedBy(baseClass) @@ -2268,7 +2276,7 @@ class KotlinGenerator private constructor( Map::class.asTypeName().parameterizedBy(keyType.typeName, valueType.typeName) EncodeMode.PACKED -> { when { - usePrimitiveArray -> FloatArray::class.asTypeName() + useArray -> FloatArray::class.asTypeName() else -> List::class.asTypeName().parameterizedBy(type.typeName) } } @@ -2292,7 +2300,7 @@ class KotlinGenerator private constructor( EncodeMode.MAP -> CodeBlock.of("emptyMap()") EncodeMode.REPEATED -> CodeBlock.of("emptyList()") EncodeMode.PACKED -> { - if (usePrimitiveArray) { + if (useArray) { CodeBlock.of("FloatArray(0)") } else { CodeBlock.of("emptyList()") 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 7c3db80989..c8faa5e2bd 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 @@ -1522,7 +1522,7 @@ class KotlinGeneratorTest { |import "wire/extensions.proto"; | |message Person { - | repeated float name = 1 [packed = true, wire.use_primitive_array = true]; + | repeated float name = 1 [packed = true, wire.use_array = true]; |} |""".trimMargin() ) diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 0db1f87335..db9d3cf16f 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -190,7 +190,7 @@ expect abstract class ProtoAdapter( @JvmField val FIXED32: ProtoAdapter @JvmField val SFIXED32: ProtoAdapter @JvmField val INT64: ProtoAdapter - @JvmField val INT64_PRIMITIVE_ARRAY: ProtoAdapter + @JvmField val INT64_ARRAY: ProtoAdapter /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. @@ -200,7 +200,7 @@ expect abstract class ProtoAdapter( @JvmField val FIXED64: ProtoAdapter @JvmField val SFIXED64: ProtoAdapter @JvmField val FLOAT: ProtoAdapter - @JvmField val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter + @JvmField val FLOAT_ARRAY: ProtoAdapter @JvmField val DOUBLE: ProtoAdapter @JvmField val BYTES: ProtoAdapter @JvmField val STRING: ProtoAdapter diff --git a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt index f9310b8964..758c350a2f 100644 --- a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -166,7 +166,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED32: ProtoAdapter = commonFixed32() actual val SFIXED32: ProtoAdapter = commonSfixed32() actual val INT64: ProtoAdapter = commonInt64() - actual val INT64_PRIMITIVE_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) + actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. @@ -176,7 +176,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED64: ProtoAdapter = commonFixed64() actual val SFIXED64: ProtoAdapter = commonSfixed64() actual val FLOAT: ProtoAdapter = commonFloat() - actual val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) + actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() diff --git a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 318dd8c7a9..cf3b46fcb1 100644 --- a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -290,13 +290,13 @@ actual abstract class ProtoAdapter actual constructor( @JvmField actual val FIXED32: ProtoAdapter = commonFixed32() @JvmField actual val SFIXED32: ProtoAdapter = commonSfixed32() @JvmField actual val INT64: ProtoAdapter = commonInt64() - @JvmField actual val INT64_PRIMITIVE_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) + @JvmField actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) @JvmField actual val UINT64: ProtoAdapter = commonUint64() @JvmField actual val SINT64: ProtoAdapter = commonSint64() @JvmField actual val FIXED64: ProtoAdapter = commonFixed64() @JvmField actual val SFIXED64: ProtoAdapter = commonSfixed64() @JvmField actual val FLOAT: ProtoAdapter = commonFloat() - @JvmField actual val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) + @JvmField actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) @JvmField actual val DOUBLE: ProtoAdapter = commonDouble() @JvmField actual val BYTES: ProtoAdapter = commonBytes() @JvmField actual val STRING: ProtoAdapter = commonString() diff --git a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt index ffa92a7b22..460078f395 100644 --- a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -166,7 +166,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED32: ProtoAdapter = commonFixed32() actual val SFIXED32: ProtoAdapter = commonSfixed32() actual val INT64: ProtoAdapter = commonInt64() - actual val INT64_PRIMITIVE_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) + actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. @@ -176,7 +176,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED64: ProtoAdapter = commonFixed64() actual val SFIXED64: ProtoAdapter = commonSfixed64() actual val FLOAT: ProtoAdapter = commonFloat() - actual val FLOAT_PRIMITIVE_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) + actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() diff --git a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt index 1a9c1ada8d..86943b8a73 100644 --- a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt +++ b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt @@ -108,8 +108,8 @@ data class Field( val isPacked: Boolean get() = encodeMode == EncodeMode.PACKED - val usePrimitiveArray: Boolean - get() = options.elements.contains(OptionElement.USE_PRIMITIVE_ARRAY_OPTION_ELEMENT) + val useArray: Boolean + get() = options.elements.contains(OptionElement.USE_ARRAY_OPTION_ELEMENT) // Null until this field is linked. var jsonName: String? = null diff --git a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt index 4039f58fb7..1bde5aa994 100644 --- a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt +++ b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/internal/parser/OptionElement.kt @@ -123,8 +123,8 @@ data class OptionElement( internal val PACKED_OPTION_ELEMENT = OptionElement("packed", BOOLEAN, value = "true", isParenthesized = false) - internal val USE_PRIMITIVE_ARRAY_OPTION_ELEMENT = - OptionElement("wire.use_primitive_array", BOOLEAN, value = "true", isParenthesized = false) + internal val USE_ARRAY_OPTION_ELEMENT = + OptionElement("wire.use_array", BOOLEAN, value = "true", isParenthesized = false) @JvmOverloads fun create( diff --git a/wire-schema/src/jvmMain/resources/wire/extensions.proto b/wire-schema/src/jvmMain/resources/wire/extensions.proto index 13823d15b1..9f29b275c7 100644 --- a/wire-schema/src/jvmMain/resources/wire/extensions.proto +++ b/wire-schema/src/jvmMain/resources/wire/extensions.proto @@ -36,7 +36,11 @@ extend google.protobuf.FieldOptions { */ optional string until = 1077; - optional string use_primitive_array = 1078; + /** + * Uses an Array at runtime to hold the values, to avoid autoboxing on the JVM. Will use the appropriate + * array type, for example `repeated float` would be represented as a FloatArray. + */ + optional string use_array = 1078; } extend google.protobuf.EnumValueOptions { diff --git a/wire-tests-swift/src/main/swift/AllTypes.swift b/wire-tests-swift/src/main/swift/AllTypes.swift index e6d55a502f..8b2d3a7cc5 100644 --- a/wire-tests-swift/src/main/swift/AllTypes.swift +++ b/wire-tests-swift/src/main/swift/AllTypes.swift @@ -772,40 +772,112 @@ public struct AllTypes { storage.map_string_enum = newValue } } - public var primitive_fixed32s: [UInt32] { + public var array_int32: [Int32] { get { - storage.primitive_fixed32s + storage.array_int32 } set { copyStorage() - storage.primitive_fixed32s = newValue + storage.array_int32 = newValue } } - public var primitive_fixed64s: [UInt64] { + public var array_uint32: [UInt32] { get { - storage.primitive_fixed64s + storage.array_uint32 } set { copyStorage() - storage.primitive_fixed64s = newValue + storage.array_uint32 = newValue } } - public var primitive_floats: [Float] { + public var array_sint32: [Int32] { get { - storage.primitive_floats + storage.array_sint32 } set { copyStorage() - storage.primitive_floats = newValue + storage.array_sint32 = newValue } } - public var primitive_doubles: [Double] { + public var array_fixed32: [UInt32] { get { - storage.primitive_doubles + storage.array_fixed32 } set { copyStorage() - storage.primitive_doubles = newValue + storage.array_fixed32 = newValue + } + } + public var array_sfixed32: [Int32] { + get { + storage.array_sfixed32 + } + set { + copyStorage() + storage.array_sfixed32 = newValue + } + } + public var array_int64: [Int64] { + get { + storage.array_int64 + } + set { + copyStorage() + storage.array_int64 = newValue + } + } + public var array_uint64: [UInt64] { + get { + storage.array_uint64 + } + set { + copyStorage() + storage.array_uint64 = newValue + } + } + public var array_sint64: [Int64] { + get { + storage.array_sint64 + } + set { + copyStorage() + storage.array_sint64 = newValue + } + } + public var array_fixed64: [UInt64] { + get { + storage.array_fixed64 + } + set { + copyStorage() + storage.array_fixed64 = newValue + } + } + public var array_sfixed64: [Int64] { + get { + storage.array_sfixed64 + } + set { + copyStorage() + storage.array_sfixed64 = newValue + } + } + public var array_float: [Float] { + get { + storage.array_float + } + set { + copyStorage() + storage.array_float = newValue + } + } + public var array_double: [Double] { + get { + storage.array_double + } + set { + copyStorage() + storage.array_double = newValue } } public var ext_opt_int32: Int32? { @@ -1336,10 +1408,18 @@ public struct AllTypes { map_string_string: [String : String] = [:], map_string_message: [String : NestedMessage] = [:], map_string_enum: [String : NestedEnum] = [:], - primitive_fixed32s: [UInt32] = [], - primitive_fixed64s: [UInt64] = [], - primitive_floats: [Float] = [], - primitive_doubles: [Double] = [], + array_int32: [Int32] = [], + array_uint32: [UInt32] = [], + array_sint32: [Int32] = [], + array_fixed32: [UInt32] = [], + array_sfixed32: [Int32] = [], + array_int64: [Int64] = [], + array_uint64: [UInt64] = [], + array_sint64: [Int64] = [], + array_fixed64: [UInt64] = [], + array_sfixed64: [Int64] = [], + array_float: [Float] = [], + array_double: [Double] = [], ext_opt_int32: Int32? = nil, ext_opt_uint32: UInt32? = nil, ext_opt_sint32: Int32? = nil, @@ -1422,9 +1502,12 @@ public struct AllTypes { default_string: default_string, default_bytes: default_bytes, default_nested_enum: default_nested_enum, map_int32_int32: map_int32_int32, map_string_string: map_string_string, map_string_message: map_string_message, - map_string_enum: map_string_enum, primitive_fixed32s: primitive_fixed32s, - primitive_fixed64s: primitive_fixed64s, primitive_floats: primitive_floats, - primitive_doubles: primitive_doubles, ext_opt_int32: ext_opt_int32, + map_string_enum: map_string_enum, array_int32: array_int32, + array_uint32: array_uint32, array_sint32: array_sint32, + array_fixed32: array_fixed32, array_sfixed32: array_sfixed32, + array_int64: array_int64, array_uint64: array_uint64, array_sint64: array_sint64, + array_fixed64: array_fixed64, array_sfixed64: array_sfixed64, + array_float: array_float, array_double: array_double, ext_opt_int32: ext_opt_int32, ext_opt_uint32: ext_opt_uint32, ext_opt_sint32: ext_opt_sint32, ext_opt_fixed32: ext_opt_fixed32, ext_opt_sfixed32: ext_opt_sfixed32, ext_opt_int64: ext_opt_int64, ext_opt_uint64: ext_opt_uint64, @@ -1572,10 +1655,18 @@ fileprivate struct _AllTypes { public var map_string_string: [String : String] public var map_string_message: [String : AllTypes.NestedMessage] public var map_string_enum: [String : AllTypes.NestedEnum] - public var primitive_fixed32s: [UInt32] - public var primitive_fixed64s: [UInt64] - public var primitive_floats: [Float] - public var primitive_doubles: [Double] + public var array_int32: [Int32] + public var array_uint32: [UInt32] + public var array_sint32: [Int32] + public var array_fixed32: [UInt32] + public var array_sfixed32: [Int32] + public var array_int64: [Int64] + public var array_uint64: [UInt64] + public var array_sint64: [Int64] + public var array_fixed64: [UInt64] + public var array_sfixed64: [Int64] + public var array_float: [Float] + public var array_double: [Double] public var ext_opt_int32: Int32? public var ext_opt_uint32: UInt32? public var ext_opt_sint32: Int32? @@ -1712,10 +1803,18 @@ fileprivate struct _AllTypes { map_string_string: [String : String], map_string_message: [String : AllTypes.NestedMessage], map_string_enum: [String : AllTypes.NestedEnum], - primitive_fixed32s: [UInt32], - primitive_fixed64s: [UInt64], - primitive_floats: [Float], - primitive_doubles: [Double], + array_int32: [Int32], + array_uint32: [UInt32], + array_sint32: [Int32], + array_fixed32: [UInt32], + array_sfixed32: [Int32], + array_int64: [Int64], + array_uint64: [UInt64], + array_sint64: [Int64], + array_fixed64: [UInt64], + array_sfixed64: [Int64], + array_float: [Float], + array_double: [Double], ext_opt_int32: Int32?, ext_opt_uint32: UInt32?, ext_opt_sint32: Int32?, @@ -1850,10 +1949,18 @@ fileprivate struct _AllTypes { self.map_string_string = map_string_string self.map_string_message = map_string_message self.map_string_enum = map_string_enum - self.primitive_fixed32s = primitive_fixed32s - self.primitive_fixed64s = primitive_fixed64s - self.primitive_floats = primitive_floats - self.primitive_doubles = primitive_doubles + self.array_int32 = array_int32 + self.array_uint32 = array_uint32 + self.array_sint32 = array_sint32 + self.array_fixed32 = array_fixed32 + self.array_sfixed32 = array_sfixed32 + self.array_int64 = array_int64 + self.array_uint64 = array_uint64 + self.array_sint64 = array_sint64 + self.array_fixed64 = array_fixed64 + self.array_sfixed64 = array_sfixed64 + self.array_float = array_float + self.array_double = array_double self.ext_opt_int32 = ext_opt_int32 self.ext_opt_uint32 = ext_opt_uint32 self.ext_opt_sint32 = ext_opt_sint32 @@ -2087,10 +2194,18 @@ extension _AllTypes : Proto2Codable { var map_string_string: [String : String] = [:] var map_string_message: [String : AllTypes.NestedMessage] = [:] var map_string_enum: [String : AllTypes.NestedEnum] = [:] - var primitive_fixed32s: [UInt32] = [] - var primitive_fixed64s: [UInt64] = [] - var primitive_floats: [Float] = [] - var primitive_doubles: [Double] = [] + var array_int32: [Int32] = [] + var array_uint32: [UInt32] = [] + var array_sint32: [Int32] = [] + var array_fixed32: [UInt32] = [] + var array_sfixed32: [Int32] = [] + var array_int64: [Int64] = [] + var array_uint64: [UInt64] = [] + var array_sint64: [Int64] = [] + var array_fixed64: [UInt64] = [] + var array_sfixed64: [Int64] = [] + var array_float: [Float] = [] + var array_double: [Double] = [] var ext_opt_int32: Int32? = nil var ext_opt_uint32: UInt32? = nil var ext_opt_sint32: Int32? = nil @@ -2228,10 +2343,18 @@ extension _AllTypes : Proto2Codable { case 502: try reader.decode(into: &map_string_string) case 503: try reader.decode(into: &map_string_message) case 504: try reader.decode(into: &map_string_enum) - case 601: try reader.decode(into: &primitive_fixed32s, encoding: .fixed) - case 602: try reader.decode(into: &primitive_fixed64s, encoding: .fixed) - case 603: try reader.decode(into: &primitive_floats) - case 604: try reader.decode(into: &primitive_doubles) + case 601: try reader.decode(into: &array_int32) + case 602: try reader.decode(into: &array_uint32) + case 603: try reader.decode(into: &array_sint32, encoding: .signed) + case 604: try reader.decode(into: &array_fixed32, encoding: .fixed) + case 605: try reader.decode(into: &array_sfixed32) + case 606: try reader.decode(into: &array_int64) + case 607: try reader.decode(into: &array_uint64) + case 608: try reader.decode(into: &array_sint64, encoding: .signed) + case 609: try reader.decode(into: &array_fixed64, encoding: .fixed) + case 610: try reader.decode(into: &array_sfixed64) + case 612: try reader.decode(into: &array_float) + case 613: try reader.decode(into: &array_double) case 1001: ext_opt_int32 = try reader.decode(Int32.self) case 1002: ext_opt_uint32 = try reader.decode(UInt32.self) case 1003: ext_opt_sint32 = try reader.decode(Int32.self, encoding: .signed) @@ -2370,10 +2493,18 @@ extension _AllTypes : Proto2Codable { self.map_string_string = map_string_string self.map_string_message = map_string_message self.map_string_enum = map_string_enum - self.primitive_fixed32s = primitive_fixed32s - self.primitive_fixed64s = primitive_fixed64s - self.primitive_floats = primitive_floats - self.primitive_doubles = primitive_doubles + self.array_int32 = array_int32 + self.array_uint32 = array_uint32 + self.array_sint32 = array_sint32 + self.array_fixed32 = array_fixed32 + self.array_sfixed32 = array_sfixed32 + self.array_int64 = array_int64 + self.array_uint64 = array_uint64 + self.array_sint64 = array_sint64 + self.array_fixed64 = array_fixed64 + self.array_sfixed64 = array_sfixed64 + self.array_float = array_float + self.array_double = array_double self.ext_opt_int32 = ext_opt_int32 self.ext_opt_uint32 = ext_opt_uint32 self.ext_opt_sint32 = ext_opt_sint32 @@ -2510,10 +2641,18 @@ extension _AllTypes : Proto2Codable { try writer.encode(tag: 502, value: self.map_string_string) try writer.encode(tag: 503, value: self.map_string_message) try writer.encode(tag: 504, value: self.map_string_enum) - try writer.encode(tag: 601, value: self.primitive_fixed32s, encoding: .fixed, packed: true) - try writer.encode(tag: 602, value: self.primitive_fixed64s, encoding: .fixed, packed: true) - try writer.encode(tag: 603, value: self.primitive_floats, packed: true) - try writer.encode(tag: 604, value: self.primitive_doubles, packed: true) + try writer.encode(tag: 601, value: self.array_int32, packed: true) + try writer.encode(tag: 602, value: self.array_uint32, packed: true) + try writer.encode(tag: 603, value: self.array_sint32, encoding: .signed, packed: true) + try writer.encode(tag: 604, value: self.array_fixed32, encoding: .fixed, packed: true) + try writer.encode(tag: 605, value: self.array_sfixed32, packed: true) + try writer.encode(tag: 606, value: self.array_int64, packed: true) + try writer.encode(tag: 607, value: self.array_uint64, packed: true) + try writer.encode(tag: 608, value: self.array_sint64, encoding: .signed, packed: true) + try writer.encode(tag: 609, value: self.array_fixed64, encoding: .fixed, packed: true) + try writer.encode(tag: 610, value: self.array_sfixed64, packed: true) + try writer.encode(tag: 612, value: self.array_float, packed: true) + try writer.encode(tag: 613, value: self.array_double, packed: true) try writer.encode(tag: 1001, value: self.ext_opt_int32) try writer.encode(tag: 1002, value: self.ext_opt_uint32) try writer.encode(tag: 1003, value: self.ext_opt_sint32, encoding: .signed) @@ -2669,10 +2808,18 @@ extension _AllTypes : Codable { self.map_string_string = try container.decodeProtoMap([String : String].self, firstOfKeys: "mapStringString", "map_string_string") self.map_string_message = try container.decodeProtoMap([String : AllTypes.NestedMessage].self, firstOfKeys: "mapStringMessage", "map_string_message") self.map_string_enum = try container.decodeProtoMap([String : AllTypes.NestedEnum].self, firstOfKeys: "mapStringEnum", "map_string_enum") - self.primitive_fixed32s = try container.decodeProtoArray(UInt32.self, firstOfKeys: "primitiveFixed32s", "primitive_fixed32s") - self.primitive_fixed64s = try container.decodeProtoArray(UInt64.self, firstOfKeys: "primitiveFixed64s", "primitive_fixed64s") - self.primitive_floats = try container.decodeProtoArray(Float.self, firstOfKeys: "primitiveFloats", "primitive_floats") - self.primitive_doubles = try container.decodeProtoArray(Double.self, firstOfKeys: "primitiveDoubles", "primitive_doubles") + self.array_int32 = try container.decodeProtoArray(Int32.self, firstOfKeys: "arrayInt32", "array_int32") + self.array_uint32 = try container.decodeProtoArray(UInt32.self, firstOfKeys: "arrayUint32", "array_uint32") + self.array_sint32 = try container.decodeProtoArray(Int32.self, firstOfKeys: "arraySint32", "array_sint32") + self.array_fixed32 = try container.decodeProtoArray(UInt32.self, firstOfKeys: "arrayFixed32", "array_fixed32") + self.array_sfixed32 = try container.decodeProtoArray(Int32.self, firstOfKeys: "arraySfixed32", "array_sfixed32") + self.array_int64 = try container.decodeProtoArray(Int64.self, firstOfKeys: "arrayInt64", "array_int64") + self.array_uint64 = try container.decodeProtoArray(UInt64.self, firstOfKeys: "arrayUint64", "array_uint64") + self.array_sint64 = try container.decodeProtoArray(Int64.self, firstOfKeys: "arraySint64", "array_sint64") + self.array_fixed64 = try container.decodeProtoArray(UInt64.self, firstOfKeys: "arrayFixed64", "array_fixed64") + self.array_sfixed64 = try container.decodeProtoArray(Int64.self, firstOfKeys: "arraySfixed64", "array_sfixed64") + self.array_float = try container.decodeProtoArray(Float.self, firstOfKeys: "arrayFloat", "array_float") + self.array_double = try container.decodeProtoArray(Double.self, firstOfKeys: "arrayDouble", "array_double") self.ext_opt_int32 = try container.decodeIfPresent(Int32.self, firstOfKeys: "extOptInt32", "ext_opt_int32") self.ext_opt_uint32 = try container.decodeIfPresent(UInt32.self, firstOfKeys: "extOptUint32", "ext_opt_uint32") self.ext_opt_sint32 = try container.decodeIfPresent(Int32.self, firstOfKeys: "extOptSint32", "ext_opt_sint32") @@ -2915,17 +3062,41 @@ extension _AllTypes : Codable { if includeDefaults || !self.map_string_enum.isEmpty { try container.encodeProtoMap(self.map_string_enum, forKey: preferCamelCase ? "mapStringEnum" : "map_string_enum") } - if includeDefaults || !self.primitive_fixed32s.isEmpty { - try container.encodeProtoArray(self.primitive_fixed32s, forKey: preferCamelCase ? "primitiveFixed32s" : "primitive_fixed32s") + if includeDefaults || !self.array_int32.isEmpty { + try container.encodeProtoArray(self.array_int32, forKey: preferCamelCase ? "arrayInt32" : "array_int32") + } + if includeDefaults || !self.array_uint32.isEmpty { + try container.encodeProtoArray(self.array_uint32, forKey: preferCamelCase ? "arrayUint32" : "array_uint32") + } + if includeDefaults || !self.array_sint32.isEmpty { + try container.encodeProtoArray(self.array_sint32, forKey: preferCamelCase ? "arraySint32" : "array_sint32") + } + if includeDefaults || !self.array_fixed32.isEmpty { + try container.encodeProtoArray(self.array_fixed32, forKey: preferCamelCase ? "arrayFixed32" : "array_fixed32") + } + if includeDefaults || !self.array_sfixed32.isEmpty { + try container.encodeProtoArray(self.array_sfixed32, forKey: preferCamelCase ? "arraySfixed32" : "array_sfixed32") + } + if includeDefaults || !self.array_int64.isEmpty { + try container.encodeProtoArray(self.array_int64, forKey: preferCamelCase ? "arrayInt64" : "array_int64") + } + if includeDefaults || !self.array_uint64.isEmpty { + try container.encodeProtoArray(self.array_uint64, forKey: preferCamelCase ? "arrayUint64" : "array_uint64") + } + if includeDefaults || !self.array_sint64.isEmpty { + try container.encodeProtoArray(self.array_sint64, forKey: preferCamelCase ? "arraySint64" : "array_sint64") + } + if includeDefaults || !self.array_fixed64.isEmpty { + try container.encodeProtoArray(self.array_fixed64, forKey: preferCamelCase ? "arrayFixed64" : "array_fixed64") } - if includeDefaults || !self.primitive_fixed64s.isEmpty { - try container.encodeProtoArray(self.primitive_fixed64s, forKey: preferCamelCase ? "primitiveFixed64s" : "primitive_fixed64s") + if includeDefaults || !self.array_sfixed64.isEmpty { + try container.encodeProtoArray(self.array_sfixed64, forKey: preferCamelCase ? "arraySfixed64" : "array_sfixed64") } - if includeDefaults || !self.primitive_floats.isEmpty { - try container.encodeProtoArray(self.primitive_floats, forKey: preferCamelCase ? "primitiveFloats" : "primitive_floats") + if includeDefaults || !self.array_float.isEmpty { + try container.encodeProtoArray(self.array_float, forKey: preferCamelCase ? "arrayFloat" : "array_float") } - if includeDefaults || !self.primitive_doubles.isEmpty { - try container.encodeProtoArray(self.primitive_doubles, forKey: preferCamelCase ? "primitiveDoubles" : "primitive_doubles") + if includeDefaults || !self.array_double.isEmpty { + try container.encodeProtoArray(self.array_double, forKey: preferCamelCase ? "arrayDouble" : "array_double") } try container.encodeIfPresent(self.ext_opt_int32, forKey: preferCamelCase ? "extOptInt32" : "ext_opt_int32") try container.encodeIfPresent(self.ext_opt_uint32, forKey: preferCamelCase ? "extOptUint32" : "ext_opt_uint32") diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt index b05bc71f83..439d4a4d4e 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt @@ -98,7 +98,7 @@ class TestAllTypes { ext_opt_bool = true, ext_rep_bool = list(true, numRepeated), ext_pack_bool = list(true, numRepeated), - primitive_floats = list(122.0f, numRepeated).toFloatArray(), + array_float = list(122.0f, numRepeated).toFloatArray(), ) } diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt index ff3f0304f6..79e8d5ef38 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt @@ -36,9 +36,9 @@ object TestAllTypesData { "e{a=999}], pack_int32=[111, 111], pack_uint32=[112, 112], pack_sint32=[113, 113], pack_fix" + "ed32=[114, 114], pack_sfixed32=[115, 115], pack_int64=[116, 116], pack_uint64=[117, 117], " + "pack_sint64=[118, 118], pack_fixed64=[119, 119], pack_sfixed64=[120, 120], pack_bool=[true" + - ", true], pack_float=[122.0, 122.0], pack_double=[123.0, 123.0], pack_nested_enum=[A, A], p" + - "rimitive_floats=[122.0, 122.0], ext_opt_bool=true, ext_rep_bool=[true, true], ext_pack_boo" + - "l=[true, true]}" + ", true], pack_float=[122.0, 122.0], pack_double=[123.0, 123.0], pack_nested_enum=[A, A], a" + + "rray_float=[122.0, 122.0], ext_opt_bool=true, ext_rep_bool=[true, true], ext_pack_bool=[tr" + + "ue, true]}" ) val expectedOutput = ( "" + @@ -261,7 +261,7 @@ object TestAllTypesData { "02" + // length = 2 "01" + // value = 1 "01" + // value = 1 - "da25" + // tag 603, type = 2 + "a226" + // tag 612, type = 2 "08" + // length = 8 "0000f442" + // value = 122.0F "0000f442" + // value = 122.0F @@ -502,9 +502,9 @@ object TestAllTypesData { "01" + // value = 1 "e013" + // tag = 316, type = 0 "01" + // value = 1 - "dd25" + // tag 603, type = 5 + "a526" + // tag 612, type = 5 "0000f442" + // value = 122.0F - "dd25" + // tag 603, type = 5 + "a526" + // tag 612, type = 5 "0000f442" + // value = 122.0F // extension diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index eaa5c8f623..f227b715cd 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -344,28 +344,76 @@ public class AllTypes( map_string_enum: Map = emptyMap(), @field:WireField( tag = 601, - adapter = "com.squareup.wire.ProtoAdapter#FIXED32", + adapter = "com.squareup.wire.ProtoAdapter#INT32", label = WireField.Label.PACKED, ) - public val primitive_fixed32s: FloatArray = FloatArray(0), + public val array_int32: FloatArray = FloatArray(0), @field:WireField( tag = 602, - adapter = "com.squareup.wire.ProtoAdapter#FIXED64", + adapter = "com.squareup.wire.ProtoAdapter#UINT32", label = WireField.Label.PACKED, ) - public val primitive_fixed64s: FloatArray = FloatArray(0), + public val array_uint32: FloatArray = FloatArray(0), @field:WireField( tag = 603, - adapter = "com.squareup.wire.ProtoAdapter#FLOAT", + adapter = "com.squareup.wire.ProtoAdapter#SINT32", label = WireField.Label.PACKED, ) - public val primitive_floats: FloatArray = FloatArray(0), + public val array_sint32: FloatArray = FloatArray(0), @field:WireField( tag = 604, + adapter = "com.squareup.wire.ProtoAdapter#FIXED32", + label = WireField.Label.PACKED, + ) + public val array_fixed32: FloatArray = FloatArray(0), + @field:WireField( + tag = 605, + adapter = "com.squareup.wire.ProtoAdapter#SFIXED32", + label = WireField.Label.PACKED, + ) + public val array_sfixed32: FloatArray = FloatArray(0), + @field:WireField( + tag = 606, + adapter = "com.squareup.wire.ProtoAdapter#INT64", + label = WireField.Label.PACKED, + ) + public val array_int64: FloatArray = FloatArray(0), + @field:WireField( + tag = 607, + adapter = "com.squareup.wire.ProtoAdapter#UINT64", + label = WireField.Label.PACKED, + ) + public val array_uint64: FloatArray = FloatArray(0), + @field:WireField( + tag = 608, + adapter = "com.squareup.wire.ProtoAdapter#SINT64", + label = WireField.Label.PACKED, + ) + public val array_sint64: FloatArray = FloatArray(0), + @field:WireField( + tag = 609, + adapter = "com.squareup.wire.ProtoAdapter#FIXED64", + label = WireField.Label.PACKED, + ) + public val array_fixed64: FloatArray = FloatArray(0), + @field:WireField( + tag = 610, + adapter = "com.squareup.wire.ProtoAdapter#SFIXED64", + label = WireField.Label.PACKED, + ) + public val array_sfixed64: FloatArray = FloatArray(0), + @field:WireField( + tag = 612, + adapter = "com.squareup.wire.ProtoAdapter#FLOAT", + label = WireField.Label.PACKED, + ) + public val array_float: FloatArray = FloatArray(0), + @field:WireField( + tag = 613, adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", label = WireField.Label.PACKED, ) - public val primitive_doubles: FloatArray = FloatArray(0), + public val array_double: FloatArray = FloatArray(0), /** * Extension source: all_types.proto */ @@ -1194,10 +1242,18 @@ public class AllTypes( if (map_string_string != other.map_string_string) return false if (map_string_message != other.map_string_message) return false if (map_string_enum != other.map_string_enum) return false - if (!primitive_fixed32s.contentEquals(other.primitive_fixed32s)) return false - if (!primitive_fixed64s.contentEquals(other.primitive_fixed64s)) return false - if (!primitive_floats.contentEquals(other.primitive_floats)) return false - if (!primitive_doubles.contentEquals(other.primitive_doubles)) return false + if (!array_int32.contentEquals(other.array_int32)) return false + if (!array_uint32.contentEquals(other.array_uint32)) return false + if (!array_sint32.contentEquals(other.array_sint32)) return false + if (!array_fixed32.contentEquals(other.array_fixed32)) return false + if (!array_sfixed32.contentEquals(other.array_sfixed32)) return false + if (!array_int64.contentEquals(other.array_int64)) return false + if (!array_uint64.contentEquals(other.array_uint64)) return false + if (!array_sint64.contentEquals(other.array_sint64)) return false + if (!array_fixed64.contentEquals(other.array_fixed64)) return false + if (!array_sfixed64.contentEquals(other.array_sfixed64)) return false + if (!array_float.contentEquals(other.array_float)) return false + if (!array_double.contentEquals(other.array_double)) return false if (ext_opt_int32 != other.ext_opt_int32) return false if (ext_opt_uint32 != other.ext_opt_uint32) return false if (ext_opt_sint32 != other.ext_opt_sint32) return false @@ -1338,10 +1394,18 @@ public class AllTypes( result = result * 37 + map_string_string.hashCode() result = result * 37 + map_string_message.hashCode() result = result * 37 + map_string_enum.hashCode() - result = result * 37 + primitive_fixed32s.contentHashCode() - result = result * 37 + primitive_fixed64s.contentHashCode() - result = result * 37 + primitive_floats.contentHashCode() - result = result * 37 + primitive_doubles.contentHashCode() + result = result * 37 + array_int32.contentHashCode() + result = result * 37 + array_uint32.contentHashCode() + result = result * 37 + array_sint32.contentHashCode() + result = result * 37 + array_fixed32.contentHashCode() + result = result * 37 + array_sfixed32.contentHashCode() + result = result * 37 + array_int64.contentHashCode() + result = result * 37 + array_uint64.contentHashCode() + result = result * 37 + array_sint64.contentHashCode() + result = result * 37 + array_fixed64.contentHashCode() + result = result * 37 + array_sfixed64.contentHashCode() + result = result * 37 + array_float.contentHashCode() + result = result * 37 + array_double.contentHashCode() result = result * 37 + (ext_opt_int32?.hashCode() ?: 0) result = result * 37 + (ext_opt_uint32?.hashCode() ?: 0) result = result * 37 + (ext_opt_sint32?.hashCode() ?: 0) @@ -1482,10 +1546,20 @@ public class AllTypes( if (map_string_string.isNotEmpty()) result += """map_string_string=$map_string_string""" if (map_string_message.isNotEmpty()) result += """map_string_message=$map_string_message""" if (map_string_enum.isNotEmpty()) result += """map_string_enum=$map_string_enum""" - if (primitive_fixed32s.isNotEmpty()) result += """primitive_fixed32s=$primitive_fixed32s""" - if (primitive_fixed64s.isNotEmpty()) result += """primitive_fixed64s=$primitive_fixed64s""" - if (primitive_floats.isNotEmpty()) result += """primitive_floats=$primitive_floats""" - if (primitive_doubles.isNotEmpty()) result += """primitive_doubles=$primitive_doubles""" + if (array_int32.isNotEmpty()) result += """array_int32=${array_int32.contentToString()}""" + if (array_uint32.isNotEmpty()) result += """array_uint32=${array_uint32.contentToString()}""" + if (array_sint32.isNotEmpty()) result += """array_sint32=${array_sint32.contentToString()}""" + if (array_fixed32.isNotEmpty()) result += """array_fixed32=${array_fixed32.contentToString()}""" + if (array_sfixed32.isNotEmpty()) result += + """array_sfixed32=${array_sfixed32.contentToString()}""" + if (array_int64.isNotEmpty()) result += """array_int64=${array_int64.contentToString()}""" + if (array_uint64.isNotEmpty()) result += """array_uint64=${array_uint64.contentToString()}""" + if (array_sint64.isNotEmpty()) result += """array_sint64=${array_sint64.contentToString()}""" + if (array_fixed64.isNotEmpty()) result += """array_fixed64=${array_fixed64.contentToString()}""" + if (array_sfixed64.isNotEmpty()) result += + """array_sfixed64=${array_sfixed64.contentToString()}""" + if (array_float.isNotEmpty()) result += """array_float=${array_float.contentToString()}""" + if (array_double.isNotEmpty()) result += """array_double=${array_double.contentToString()}""" if (ext_opt_int32 != null) result += """ext_opt_int32=$ext_opt_int32""" if (ext_opt_uint32 != null) result += """ext_opt_uint32=$ext_opt_uint32""" if (ext_opt_sint32 != null) result += """ext_opt_sint32=$ext_opt_sint32""" @@ -1626,10 +1700,18 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, - primitive_fixed32s: FloatArray = this.primitive_fixed32s, - primitive_fixed64s: FloatArray = this.primitive_fixed64s, - primitive_floats: FloatArray = this.primitive_floats, - primitive_doubles: FloatArray = this.primitive_doubles, + array_int32: FloatArray = this.array_int32, + array_uint32: FloatArray = this.array_uint32, + array_sint32: FloatArray = this.array_sint32, + array_fixed32: FloatArray = this.array_fixed32, + array_sfixed32: FloatArray = this.array_sfixed32, + array_int64: FloatArray = this.array_int64, + array_uint64: FloatArray = this.array_uint64, + array_sint64: FloatArray = this.array_sint64, + array_fixed64: FloatArray = this.array_fixed64, + array_sfixed64: FloatArray = this.array_sfixed64, + array_float: FloatArray = this.array_float, + array_double: FloatArray = this.array_double, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -1692,8 +1774,9 @@ public class AllTypes( default_fixed32, default_sfixed32, default_int64, default_uint64, default_sint64, default_fixed64, default_sfixed64, default_bool, default_float, default_double, default_string, default_bytes, default_nested_enum, map_int32_int32, map_string_string, - map_string_message, map_string_enum, primitive_fixed32s, primitive_fixed64s, primitive_floats, - primitive_doubles, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, ext_opt_fixed32, + map_string_message, map_string_enum, array_int32, array_uint32, array_sint32, array_fixed32, + array_sfixed32, array_int64, array_uint64, array_sint64, array_fixed64, array_sfixed64, + array_float, array_double, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, ext_opt_fixed32, ext_opt_sfixed32, ext_opt_int64, ext_opt_uint64, ext_opt_sint64, ext_opt_fixed64, ext_opt_sfixed64, ext_opt_bool, ext_opt_float, ext_opt_double, ext_opt_string, ext_opt_bytes, ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, ext_rep_uint32, ext_rep_sint32, @@ -1849,10 +1932,18 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(601, value.primitive_fixed32s) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(602, value.primitive_fixed64s) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(603, value.primitive_floats) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(604, value.primitive_doubles) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(601, value.array_int32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(602, value.array_uint32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(603, value.array_sint32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(604, value.array_fixed32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(605, value.array_sfixed32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(606, value.array_int64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(607, value.array_uint64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(608, value.array_sint64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(609, value.array_fixed64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(610, value.array_sfixed64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(612, value.array_float) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(613, value.array_double) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -1991,10 +2082,18 @@ public class AllTypes( map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) @@ -2096,10 +2195,18 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -2273,10 +2380,18 @@ public class AllTypes( val map_string_string = mutableMapOf() val map_string_message = mutableMapOf() val map_string_enum = mutableMapOf() - var primitive_fixed32s: FloatArrayList? = null - var primitive_fixed64s: FloatArrayList? = null - var primitive_floats: FloatArrayList? = null - var primitive_doubles: FloatArrayList? = null + var array_int32: FloatArrayList? = null + var array_uint32: FloatArrayList? = null + var array_sint32: FloatArrayList? = null + var array_fixed32: FloatArrayList? = null + var array_sfixed32: FloatArrayList? = null + var array_int64: FloatArrayList? = null + var array_uint64: FloatArrayList? = null + var array_sint64: FloatArrayList? = null + var array_fixed64: FloatArrayList? = null + var array_sfixed64: FloatArrayList? = null + var array_float: FloatArrayList? = null + var array_double: FloatArrayList? = null var ext_opt_int32: Int? = null var ext_opt_uint32: Int? = null var ext_opt_sint32: Int? = null @@ -2550,44 +2665,124 @@ public class AllTypes( 503 -> map_string_message.putAll(map_string_messageAdapter.decode(reader)) 504 -> map_string_enum.putAll(map_string_enumAdapter.decode(reader)) 601 -> { - if (primitive_fixed32s == null) { - val minimumByteSize = 4 + if (array_int32 == null) { + val minimumByteSize = 1 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_fixed32s = FloatArrayList(initialCapacity) + array_int32 = FloatArrayList(initialCapacity) } - primitive_fixed32s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_int32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 602 -> { - if (primitive_fixed64s == null) { - val minimumByteSize = 8 + if (array_uint32 == null) { + val minimumByteSize = 1 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_fixed64s = FloatArrayList(initialCapacity) + array_uint32 = FloatArrayList(initialCapacity) } - primitive_fixed64s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_uint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 603 -> { - if (primitive_floats == null) { - val minimumByteSize = 4 + if (array_sint32 == null) { + val minimumByteSize = 1 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_floats = FloatArrayList(initialCapacity) + array_sint32 = FloatArrayList(initialCapacity) } - primitive_floats!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 604 -> { - if (primitive_doubles == null) { + if (array_fixed32 == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_fixed32 = FloatArrayList(initialCapacity) + } + array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 605 -> { + if (array_sfixed32 == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_sfixed32 = FloatArrayList(initialCapacity) + } + array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 606 -> { + if (array_int64 == null) { + val minimumByteSize = 1 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_int64 = FloatArrayList(initialCapacity) + } + array_int64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 607 -> { + if (array_uint64 == null) { + val minimumByteSize = 1 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_uint64 = FloatArrayList(initialCapacity) + } + array_uint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 608 -> { + if (array_sint64 == null) { + val minimumByteSize = 1 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_sint64 = FloatArrayList(initialCapacity) + } + array_sint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 609 -> { + if (array_fixed64 == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_fixed64 = FloatArrayList(initialCapacity) + } + array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 610 -> { + if (array_sfixed64 == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_sfixed64 = FloatArrayList(initialCapacity) + } + array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 612 -> { + if (array_float == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_float = FloatArrayList(initialCapacity) + } + array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 613 -> { + if (array_double == null) { val minimumByteSize = 8 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_doubles = FloatArrayList(initialCapacity) + array_double = FloatArrayList(initialCapacity) } - primitive_doubles!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_double!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) @@ -2857,10 +3052,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - primitive_fixed32s = primitive_fixed32s?.getTruncatedArray() ?: FloatArray(0), - primitive_fixed64s = primitive_fixed64s?.getTruncatedArray() ?: FloatArray(0), - primitive_floats = primitive_floats?.getTruncatedArray() ?: FloatArray(0), - primitive_doubles = primitive_doubles?.getTruncatedArray() ?: FloatArray(0), + array_int32 = array_int32?.getTruncatedArray() ?: FloatArray(0), + array_uint32 = array_uint32?.getTruncatedArray() ?: FloatArray(0), + array_sint32 = array_sint32?.getTruncatedArray() ?: FloatArray(0), + array_fixed32 = array_fixed32?.getTruncatedArray() ?: FloatArray(0), + array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: FloatArray(0), + array_int64 = array_int64?.getTruncatedArray() ?: FloatArray(0), + array_uint64 = array_uint64?.getTruncatedArray() ?: FloatArray(0), + array_sint64 = array_sint64?.getTruncatedArray() ?: FloatArray(0), + array_fixed64 = array_fixed64?.getTruncatedArray() ?: FloatArray(0), + array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: FloatArray(0), + array_float = array_float?.getTruncatedArray() ?: FloatArray(0), + array_double = array_double?.getTruncatedArray() ?: FloatArray(0), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, diff --git a/wire-tests/src/commonTest/proto/kotlin/all_types.proto b/wire-tests/src/commonTest/proto/kotlin/all_types.proto index 448af6e705..14f2f095be 100644 --- a/wire-tests/src/commonTest/proto/kotlin/all_types.proto +++ b/wire-tests/src/commonTest/proto/kotlin/all_types.proto @@ -124,10 +124,18 @@ message AllTypes { map map_string_message = 503; map map_string_enum = 504; - repeated fixed32 primitive_fixed32s = 601 [packed = true, wire.use_primitive_array = true]; - repeated fixed64 primitive_fixed64s = 602 [packed = true, wire.use_primitive_array = true]; - repeated float primitive_floats = 603 [packed = true, wire.use_primitive_array = true]; - repeated double primitive_doubles = 604 [packed = true, wire.use_primitive_array = true]; + repeated int32 array_int32 = 601 [packed = true, wire.use_array = true]; + repeated uint32 array_uint32 = 602 [packed = true, wire.use_array = true]; + repeated sint32 array_sint32 = 603 [packed = true, wire.use_array = true]; + repeated fixed32 array_fixed32 = 604 [packed = true, wire.use_array = true]; + repeated sfixed32 array_sfixed32 = 605 [packed = true, wire.use_array = true]; + repeated int64 array_int64 = 606 [packed = true, wire.use_array = true]; + repeated uint64 array_uint64 = 607 [packed = true, wire.use_array = true]; + repeated sint64 array_sint64 = 608 [packed = true, wire.use_array = true]; + repeated fixed64 array_fixed64 = 609 [packed = true, wire.use_array = true]; + repeated sfixed64 array_sfixed64 = 610 [packed = true, wire.use_array = true]; + repeated float array_float = 612 [packed = true, wire.use_array = true]; + repeated double array_double = 613 [packed = true, wire.use_array = true]; } extend AllTypes { diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 60e361c9b3..8102ce5c46 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -391,32 +391,88 @@ public class AllTypes( map_string_enum: Map = emptyMap(), @field:WireField( tag = 601, - adapter = "com.squareup.wire.ProtoAdapter#FIXED32", + adapter = "com.squareup.wire.ProtoAdapter#INT32", label = WireField.Label.PACKED, ) @JvmField - public val primitive_fixed32s: FloatArray = FloatArray(0), + public val array_int32: FloatArray = FloatArray(0), @field:WireField( tag = 602, - adapter = "com.squareup.wire.ProtoAdapter#FIXED64", + adapter = "com.squareup.wire.ProtoAdapter#UINT32", label = WireField.Label.PACKED, ) @JvmField - public val primitive_fixed64s: FloatArray = FloatArray(0), + public val array_uint32: FloatArray = FloatArray(0), @field:WireField( tag = 603, - adapter = "com.squareup.wire.ProtoAdapter#FLOAT", + adapter = "com.squareup.wire.ProtoAdapter#SINT32", label = WireField.Label.PACKED, ) @JvmField - public val primitive_floats: FloatArray = FloatArray(0), + public val array_sint32: FloatArray = FloatArray(0), @field:WireField( tag = 604, + adapter = "com.squareup.wire.ProtoAdapter#FIXED32", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_fixed32: FloatArray = FloatArray(0), + @field:WireField( + tag = 605, + adapter = "com.squareup.wire.ProtoAdapter#SFIXED32", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_sfixed32: FloatArray = FloatArray(0), + @field:WireField( + tag = 606, + adapter = "com.squareup.wire.ProtoAdapter#INT64", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_int64: FloatArray = FloatArray(0), + @field:WireField( + tag = 607, + adapter = "com.squareup.wire.ProtoAdapter#UINT64", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_uint64: FloatArray = FloatArray(0), + @field:WireField( + tag = 608, + adapter = "com.squareup.wire.ProtoAdapter#SINT64", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_sint64: FloatArray = FloatArray(0), + @field:WireField( + tag = 609, + adapter = "com.squareup.wire.ProtoAdapter#FIXED64", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_fixed64: FloatArray = FloatArray(0), + @field:WireField( + tag = 610, + adapter = "com.squareup.wire.ProtoAdapter#SFIXED64", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_sfixed64: FloatArray = FloatArray(0), + @field:WireField( + tag = 612, + adapter = "com.squareup.wire.ProtoAdapter#FLOAT", + label = WireField.Label.PACKED, + ) + @JvmField + public val array_float: FloatArray = FloatArray(0), + @field:WireField( + tag = 613, adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", label = WireField.Label.PACKED, ) @JvmField - public val primitive_doubles: FloatArray = FloatArray(0), + public val array_double: FloatArray = FloatArray(0), /** * Extension source: all_types.proto */ @@ -1319,10 +1375,18 @@ public class AllTypes( builder.map_string_string = map_string_string builder.map_string_message = map_string_message builder.map_string_enum = map_string_enum - builder.primitive_fixed32s = primitive_fixed32s - builder.primitive_fixed64s = primitive_fixed64s - builder.primitive_floats = primitive_floats - builder.primitive_doubles = primitive_doubles + builder.array_int32 = array_int32 + builder.array_uint32 = array_uint32 + builder.array_sint32 = array_sint32 + builder.array_fixed32 = array_fixed32 + builder.array_sfixed32 = array_sfixed32 + builder.array_int64 = array_int64 + builder.array_uint64 = array_uint64 + builder.array_sint64 = array_sint64 + builder.array_fixed64 = array_fixed64 + builder.array_sfixed64 = array_sfixed64 + builder.array_float = array_float + builder.array_double = array_double builder.ext_opt_int32 = ext_opt_int32 builder.ext_opt_uint32 = ext_opt_uint32 builder.ext_opt_sint32 = ext_opt_sint32 @@ -1464,10 +1528,18 @@ public class AllTypes( if (map_string_string != other.map_string_string) return false if (map_string_message != other.map_string_message) return false if (map_string_enum != other.map_string_enum) return false - if (!primitive_fixed32s.contentEquals(other.primitive_fixed32s)) return false - if (!primitive_fixed64s.contentEquals(other.primitive_fixed64s)) return false - if (!primitive_floats.contentEquals(other.primitive_floats)) return false - if (!primitive_doubles.contentEquals(other.primitive_doubles)) return false + if (!array_int32.contentEquals(other.array_int32)) return false + if (!array_uint32.contentEquals(other.array_uint32)) return false + if (!array_sint32.contentEquals(other.array_sint32)) return false + if (!array_fixed32.contentEquals(other.array_fixed32)) return false + if (!array_sfixed32.contentEquals(other.array_sfixed32)) return false + if (!array_int64.contentEquals(other.array_int64)) return false + if (!array_uint64.contentEquals(other.array_uint64)) return false + if (!array_sint64.contentEquals(other.array_sint64)) return false + if (!array_fixed64.contentEquals(other.array_fixed64)) return false + if (!array_sfixed64.contentEquals(other.array_sfixed64)) return false + if (!array_float.contentEquals(other.array_float)) return false + if (!array_double.contentEquals(other.array_double)) return false if (ext_opt_int32 != other.ext_opt_int32) return false if (ext_opt_uint32 != other.ext_opt_uint32) return false if (ext_opt_sint32 != other.ext_opt_sint32) return false @@ -1608,10 +1680,18 @@ public class AllTypes( result = result * 37 + map_string_string.hashCode() result = result * 37 + map_string_message.hashCode() result = result * 37 + map_string_enum.hashCode() - result = result * 37 + primitive_fixed32s.contentHashCode() - result = result * 37 + primitive_fixed64s.contentHashCode() - result = result * 37 + primitive_floats.contentHashCode() - result = result * 37 + primitive_doubles.contentHashCode() + result = result * 37 + array_int32.contentHashCode() + result = result * 37 + array_uint32.contentHashCode() + result = result * 37 + array_sint32.contentHashCode() + result = result * 37 + array_fixed32.contentHashCode() + result = result * 37 + array_sfixed32.contentHashCode() + result = result * 37 + array_int64.contentHashCode() + result = result * 37 + array_uint64.contentHashCode() + result = result * 37 + array_sint64.contentHashCode() + result = result * 37 + array_fixed64.contentHashCode() + result = result * 37 + array_sfixed64.contentHashCode() + result = result * 37 + array_float.contentHashCode() + result = result * 37 + array_double.contentHashCode() result = result * 37 + (ext_opt_int32?.hashCode() ?: 0) result = result * 37 + (ext_opt_uint32?.hashCode() ?: 0) result = result * 37 + (ext_opt_sint32?.hashCode() ?: 0) @@ -1752,10 +1832,20 @@ public class AllTypes( if (map_string_string.isNotEmpty()) result += """map_string_string=$map_string_string""" if (map_string_message.isNotEmpty()) result += """map_string_message=$map_string_message""" if (map_string_enum.isNotEmpty()) result += """map_string_enum=$map_string_enum""" - if (primitive_fixed32s.isNotEmpty()) result += """primitive_fixed32s=$primitive_fixed32s""" - if (primitive_fixed64s.isNotEmpty()) result += """primitive_fixed64s=$primitive_fixed64s""" - if (primitive_floats.isNotEmpty()) result += """primitive_floats=$primitive_floats""" - if (primitive_doubles.isNotEmpty()) result += """primitive_doubles=$primitive_doubles""" + if (array_int32.isNotEmpty()) result += """array_int32=${array_int32.contentToString()}""" + if (array_uint32.isNotEmpty()) result += """array_uint32=${array_uint32.contentToString()}""" + if (array_sint32.isNotEmpty()) result += """array_sint32=${array_sint32.contentToString()}""" + if (array_fixed32.isNotEmpty()) result += """array_fixed32=${array_fixed32.contentToString()}""" + if (array_sfixed32.isNotEmpty()) result += + """array_sfixed32=${array_sfixed32.contentToString()}""" + if (array_int64.isNotEmpty()) result += """array_int64=${array_int64.contentToString()}""" + if (array_uint64.isNotEmpty()) result += """array_uint64=${array_uint64.contentToString()}""" + if (array_sint64.isNotEmpty()) result += """array_sint64=${array_sint64.contentToString()}""" + if (array_fixed64.isNotEmpty()) result += """array_fixed64=${array_fixed64.contentToString()}""" + if (array_sfixed64.isNotEmpty()) result += + """array_sfixed64=${array_sfixed64.contentToString()}""" + if (array_float.isNotEmpty()) result += """array_float=${array_float.contentToString()}""" + if (array_double.isNotEmpty()) result += """array_double=${array_double.contentToString()}""" if (ext_opt_int32 != null) result += """ext_opt_int32=$ext_opt_int32""" if (ext_opt_uint32 != null) result += """ext_opt_uint32=$ext_opt_uint32""" if (ext_opt_sint32 != null) result += """ext_opt_sint32=$ext_opt_sint32""" @@ -1896,10 +1986,18 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, - primitive_fixed32s: FloatArray = this.primitive_fixed32s, - primitive_fixed64s: FloatArray = this.primitive_fixed64s, - primitive_floats: FloatArray = this.primitive_floats, - primitive_doubles: FloatArray = this.primitive_doubles, + array_int32: FloatArray = this.array_int32, + array_uint32: FloatArray = this.array_uint32, + array_sint32: FloatArray = this.array_sint32, + array_fixed32: FloatArray = this.array_fixed32, + array_sfixed32: FloatArray = this.array_sfixed32, + array_int64: FloatArray = this.array_int64, + array_uint64: FloatArray = this.array_uint64, + array_sint64: FloatArray = this.array_sint64, + array_fixed64: FloatArray = this.array_fixed64, + array_sfixed64: FloatArray = this.array_sfixed64, + array_float: FloatArray = this.array_float, + array_double: FloatArray = this.array_double, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -1962,8 +2060,9 @@ public class AllTypes( default_fixed32, default_sfixed32, default_int64, default_uint64, default_sint64, default_fixed64, default_sfixed64, default_bool, default_float, default_double, default_string, default_bytes, default_nested_enum, map_int32_int32, map_string_string, - map_string_message, map_string_enum, primitive_fixed32s, primitive_fixed64s, primitive_floats, - primitive_doubles, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, ext_opt_fixed32, + map_string_message, map_string_enum, array_int32, array_uint32, array_sint32, array_fixed32, + array_sfixed32, array_int64, array_uint64, array_sint64, array_fixed64, array_sfixed64, + array_float, array_double, ext_opt_int32, ext_opt_uint32, ext_opt_sint32, ext_opt_fixed32, ext_opt_sfixed32, ext_opt_int64, ext_opt_uint64, ext_opt_sint64, ext_opt_fixed64, ext_opt_sfixed64, ext_opt_bool, ext_opt_float, ext_opt_double, ext_opt_string, ext_opt_bytes, ext_opt_nested_enum, ext_opt_nested_message, ext_rep_int32, ext_rep_uint32, ext_rep_sint32, @@ -2231,16 +2330,40 @@ public class AllTypes( public var map_string_enum: Map = emptyMap() @JvmField - public var primitive_fixed32s: FloatArray = FloatArray(0) + public var array_int32: FloatArray = FloatArray(0) @JvmField - public var primitive_fixed64s: FloatArray = FloatArray(0) + public var array_uint32: FloatArray = FloatArray(0) @JvmField - public var primitive_floats: FloatArray = FloatArray(0) + public var array_sint32: FloatArray = FloatArray(0) @JvmField - public var primitive_doubles: FloatArray = FloatArray(0) + public var array_fixed32: FloatArray = FloatArray(0) + + @JvmField + public var array_sfixed32: FloatArray = FloatArray(0) + + @JvmField + public var array_int64: FloatArray = FloatArray(0) + + @JvmField + public var array_uint64: FloatArray = FloatArray(0) + + @JvmField + public var array_sint64: FloatArray = FloatArray(0) + + @JvmField + public var array_fixed64: FloatArray = FloatArray(0) + + @JvmField + public var array_sfixed64: FloatArray = FloatArray(0) + + @JvmField + public var array_float: FloatArray = FloatArray(0) + + @JvmField + public var array_double: FloatArray = FloatArray(0) @JvmField public var ext_opt_int32: Int? = null @@ -2842,23 +2965,63 @@ public class AllTypes( return this } - public fun primitive_fixed32s(primitive_fixed32s: FloatArray): Builder { - this.primitive_fixed32s = primitive_fixed32s + public fun array_int32(array_int32: FloatArray): Builder { + this.array_int32 = array_int32 + return this + } + + public fun array_uint32(array_uint32: FloatArray): Builder { + this.array_uint32 = array_uint32 + return this + } + + public fun array_sint32(array_sint32: FloatArray): Builder { + this.array_sint32 = array_sint32 + return this + } + + public fun array_fixed32(array_fixed32: FloatArray): Builder { + this.array_fixed32 = array_fixed32 + return this + } + + public fun array_sfixed32(array_sfixed32: FloatArray): Builder { + this.array_sfixed32 = array_sfixed32 + return this + } + + public fun array_int64(array_int64: FloatArray): Builder { + this.array_int64 = array_int64 + return this + } + + public fun array_uint64(array_uint64: FloatArray): Builder { + this.array_uint64 = array_uint64 return this } - public fun primitive_fixed64s(primitive_fixed64s: FloatArray): Builder { - this.primitive_fixed64s = primitive_fixed64s + public fun array_sint64(array_sint64: FloatArray): Builder { + this.array_sint64 = array_sint64 return this } - public fun primitive_floats(primitive_floats: FloatArray): Builder { - this.primitive_floats = primitive_floats + public fun array_fixed64(array_fixed64: FloatArray): Builder { + this.array_fixed64 = array_fixed64 return this } - public fun primitive_doubles(primitive_doubles: FloatArray): Builder { - this.primitive_doubles = primitive_doubles + public fun array_sfixed64(array_sfixed64: FloatArray): Builder { + this.array_sfixed64 = array_sfixed64 + return this + } + + public fun array_float(array_float: FloatArray): Builder { + this.array_float = array_float + return this + } + + public fun array_double(array_double: FloatArray): Builder { + this.array_double = array_double return this } @@ -3221,10 +3384,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - primitive_fixed32s = primitive_fixed32s, - primitive_fixed64s = primitive_fixed64s, - primitive_floats = primitive_floats, - primitive_doubles = primitive_doubles, + array_int32 = array_int32, + array_uint32 = array_uint32, + array_sint32 = array_sint32, + array_fixed32 = array_fixed32, + array_sfixed32 = array_sfixed32, + array_int64 = array_int64, + array_uint64 = array_uint64, + array_sint64 = array_sint64, + array_fixed64 = array_fixed64, + array_sfixed64 = array_sfixed64, + array_float = array_float, + array_double = array_double, ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, @@ -3422,10 +3593,18 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(601, value.primitive_fixed32s) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(602, value.primitive_fixed64s) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(603, value.primitive_floats) - size += ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodedSizeWithTag(604, value.primitive_doubles) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(601, value.array_int32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(602, value.array_uint32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(603, value.array_sint32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(604, value.array_fixed32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(605, value.array_sfixed32) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(606, value.array_int64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(607, value.array_uint64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(608, value.array_sint64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(609, value.array_fixed64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(610, value.array_sfixed64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(612, value.array_float) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(613, value.array_double) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -3564,10 +3743,18 @@ public class AllTypes( map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) @@ -3669,10 +3856,18 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 604, value.primitive_doubles) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 603, value.primitive_floats) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 602, value.primitive_fixed64s) - ProtoAdapter.FLOAT_PRIMITIVE_ARRAY.encodeWithTag(writer, 601, value.primitive_fixed32s) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -3846,10 +4041,18 @@ public class AllTypes( val map_string_string = mutableMapOf() val map_string_message = mutableMapOf() val map_string_enum = mutableMapOf() - var primitive_fixed32s: FloatArrayList? = null - var primitive_fixed64s: FloatArrayList? = null - var primitive_floats: FloatArrayList? = null - var primitive_doubles: FloatArrayList? = null + var array_int32: FloatArrayList? = null + var array_uint32: FloatArrayList? = null + var array_sint32: FloatArrayList? = null + var array_fixed32: FloatArrayList? = null + var array_sfixed32: FloatArrayList? = null + var array_int64: FloatArrayList? = null + var array_uint64: FloatArrayList? = null + var array_sint64: FloatArrayList? = null + var array_fixed64: FloatArrayList? = null + var array_sfixed64: FloatArrayList? = null + var array_float: FloatArrayList? = null + var array_double: FloatArrayList? = null var ext_opt_int32: Int? = null var ext_opt_uint32: Int? = null var ext_opt_sint32: Int? = null @@ -4123,44 +4326,124 @@ public class AllTypes( 503 -> map_string_message.putAll(map_string_messageAdapter.decode(reader)) 504 -> map_string_enum.putAll(map_string_enumAdapter.decode(reader)) 601 -> { - if (primitive_fixed32s == null) { - val minimumByteSize = 4 + if (array_int32 == null) { + val minimumByteSize = 1 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_fixed32s = FloatArrayList(initialCapacity) + array_int32 = FloatArrayList(initialCapacity) } - primitive_fixed32s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_int32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 602 -> { - if (primitive_fixed64s == null) { - val minimumByteSize = 8 + if (array_uint32 == null) { + val minimumByteSize = 1 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_fixed64s = FloatArrayList(initialCapacity) + array_uint32 = FloatArrayList(initialCapacity) } - primitive_fixed64s!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_uint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 603 -> { - if (primitive_floats == null) { - val minimumByteSize = 4 + if (array_sint32 == null) { + val minimumByteSize = 1 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_floats = FloatArrayList(initialCapacity) + array_sint32 = FloatArrayList(initialCapacity) } - primitive_floats!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 604 -> { - if (primitive_doubles == null) { + if (array_fixed32 == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_fixed32 = FloatArrayList(initialCapacity) + } + array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 605 -> { + if (array_sfixed32 == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_sfixed32 = FloatArrayList(initialCapacity) + } + array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 606 -> { + if (array_int64 == null) { + val minimumByteSize = 1 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_int64 = FloatArrayList(initialCapacity) + } + array_int64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 607 -> { + if (array_uint64 == null) { + val minimumByteSize = 1 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_uint64 = FloatArrayList(initialCapacity) + } + array_uint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 608 -> { + if (array_sint64 == null) { + val minimumByteSize = 1 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_sint64 = FloatArrayList(initialCapacity) + } + array_sint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 609 -> { + if (array_fixed64 == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_fixed64 = FloatArrayList(initialCapacity) + } + array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 610 -> { + if (array_sfixed64 == null) { + val minimumByteSize = 8 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_sfixed64 = FloatArrayList(initialCapacity) + } + array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 612 -> { + if (array_float == null) { + val minimumByteSize = 4 + val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + array_float = FloatArrayList(initialCapacity) + } + array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + } + 613 -> { + if (array_double == null) { val minimumByteSize = 8 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - primitive_doubles = FloatArrayList(initialCapacity) + array_double = FloatArrayList(initialCapacity) } - primitive_doubles!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_double!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) @@ -4430,10 +4713,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - primitive_fixed32s = primitive_fixed32s?.getTruncatedArray() ?: FloatArray(0), - primitive_fixed64s = primitive_fixed64s?.getTruncatedArray() ?: FloatArray(0), - primitive_floats = primitive_floats?.getTruncatedArray() ?: FloatArray(0), - primitive_doubles = primitive_doubles?.getTruncatedArray() ?: FloatArray(0), + array_int32 = array_int32?.getTruncatedArray() ?: FloatArray(0), + array_uint32 = array_uint32?.getTruncatedArray() ?: FloatArray(0), + array_sint32 = array_sint32?.getTruncatedArray() ?: FloatArray(0), + array_fixed32 = array_fixed32?.getTruncatedArray() ?: FloatArray(0), + array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: FloatArray(0), + array_int64 = array_int64?.getTruncatedArray() ?: FloatArray(0), + array_uint64 = array_uint64?.getTruncatedArray() ?: FloatArray(0), + array_sint64 = array_sint64?.getTruncatedArray() ?: FloatArray(0), + array_fixed64 = array_fixed64?.getTruncatedArray() ?: FloatArray(0), + array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: FloatArray(0), + array_float = array_float?.getTruncatedArray() ?: FloatArray(0), + array_double = array_double?.getTruncatedArray() ?: FloatArray(0), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, From 9b32281cc7387e345c989706113831c3751f9f41 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Fri, 14 Apr 2023 10:24:12 -0400 Subject: [PATCH 08/17] Finally get tests passing --- .../squareup/wire/kotlin/KotlinGenerator.kt | 55 +++- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 120 ++++++++ .../kotlin/com/squareup/wire/ProtoAdapter.kt | 11 + .../kotlin/com/squareup/wire/ProtoAdapter.kt | 10 + .../kotlin/com/squareup/wire/ProtoAdapter.kt | 10 + .../src/main/swift/AllTypes.swift | 8 +- .../kotlin/com/squareup/wire/TestAllTypes.kt | 11 + .../com/squareup/wire/TestAllTypesData.kt | 106 ++++++- .../wire/protos/kotlin/alltypes/AllTypes.kt | 218 ++++++++------- .../commonTest/proto/kotlin/all_types.proto | 4 +- .../squareup/wire/ProtoAdapterIdentityTest.kt | 6 + .../squareup/wire/ProtoAdapterTypeUrlTest.kt | 4 +- .../wire/protos/kotlin/alltypes/AllTypes.kt | 262 +++++++++--------- 13 files changed, 568 insertions(+), 257 deletions(-) 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 6c2f0e4906..b50939ce36 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 @@ -52,6 +52,7 @@ 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.DoubleArrayList import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding import com.squareup.wire.FloatArrayList @@ -59,6 +60,8 @@ import com.squareup.wire.GrpcCall import com.squareup.wire.GrpcClient import com.squareup.wire.GrpcMethod import com.squareup.wire.GrpcStreamingCall +import com.squareup.wire.IntArrayList +import com.squareup.wire.LongArrayList import com.squareup.wire.Message import com.squareup.wire.MessageSink import com.squareup.wire.MessageSource @@ -157,6 +160,39 @@ class KotlinGenerator private constructor( get() = type.typeName private val Service.serviceName get() = type.typeName + private val Field.primitiveArrayForType + get() = when (type!!.typeName) { + LONG -> LongArray::class + INT -> IntArray::class + FLOAT -> FloatArray::class + DOUBLE -> DoubleArray::class + else -> throw IllegalArgumentException("No Array type for $type") + } + private val Field.arrayListForType + get() = when (type!!.typeName) { + LONG -> LongArrayList::class + INT -> IntArrayList::class + FLOAT -> FloatArrayList::class + DOUBLE -> DoubleArrayList::class + else -> throw IllegalArgumentException("No ArrayList type for $type") + } + + private val Field.arrayAdapterForType + get() = when (type!!) { + ProtoType.INT32 -> CodeBlock.of("%T.INT32_ARRAY", ProtoAdapter::class) + ProtoType.UINT32 -> CodeBlock.of("%T.UINT32_ARRAY", ProtoAdapter::class) + ProtoType.SINT32 -> CodeBlock.of("%T.SINT32_ARRAY", ProtoAdapter::class) + ProtoType.FIXED32 -> CodeBlock.of("%T.FIXED32_ARRAY", ProtoAdapter::class) + ProtoType.SFIXED32 -> CodeBlock.of("%T.SFIXED32_ARRAY", ProtoAdapter::class) + ProtoType.INT64 -> CodeBlock.of("%T.INT64_ARRAY", ProtoAdapter::class) + ProtoType.UINT64 -> CodeBlock.of("%T.UINT64_ARRAY", ProtoAdapter::class) + ProtoType.SINT64 -> CodeBlock.of("%T.SINT64_ARRAY", ProtoAdapter::class) + ProtoType.FIXED64 -> CodeBlock.of("%T.FIXED64_ARRAY", ProtoAdapter::class) + ProtoType.SFIXED64 -> CodeBlock.of("%T.SFIXED64_ARRAY", ProtoAdapter::class) + ProtoType.FLOAT -> CodeBlock.of("%T.FLOAT_ARRAY", ProtoAdapter::class) + ProtoType.DOUBLE -> CodeBlock.of("%T.DOUBLE_ARRAY", ProtoAdapter::class) + else -> throw IllegalArgumentException("No Array adapter for $type") + } /** Returns the full name of the class generated for [type]. */ fun generatedTypeName(type: Type) = type.typeName as ClassName @@ -1536,7 +1572,7 @@ class KotlinGenerator private constructor( private fun adapterFor(field: Field) = buildCodeBlock { if (field.useArray) { - add("%T.FLOAT_ARRAY", ProtoAdapter::class) + add(field.arrayAdapterForType) } else { add("%L", field.getAdapterName()) if (field.isPacked) { @@ -1643,7 +1679,7 @@ class KotlinGenerator private constructor( if (fieldOrOneOf.isPacked && fieldOrOneOf.isScalar) { if (fieldOrOneOf.useArray) { - addStatement("%1L = %1L?.getTruncatedArray() ?: FloatArray(0),", fieldName) + addStatement("%1L = %1L?.getTruncatedArray() ?: %2T(0),", fieldName, fieldOrOneOf.primitiveArrayForType) } else { addStatement("%1L = %1L ?: listOf(),", fieldName) } @@ -1735,7 +1771,7 @@ class KotlinGenerator private constructor( return CodeBlock.of( when { field.isPacked && field.isScalar -> { - val type = if (field.useArray) "FloatArrayList" else "ArrayList" + val type = if (field.useArray) field.arrayListForType.simpleName else "ArrayList" buildCodeBlock { beginControlFlow("if (%L == null)", fieldName) addStatement("val minimumByteSize = ${field.getMinimumByteSize()}") @@ -1824,7 +1860,7 @@ class KotlinGenerator private constructor( private fun Field.redact(fieldName: String): CodeBlock? { if (isRedacted) { return when { - useArray -> CodeBlock.of("FloatArray(0)") + useArray -> CodeBlock.of("%T(0)", primitiveArrayForType) isRepeated -> CodeBlock.of("emptyList()") isMap -> CodeBlock.of("emptyMap()") encodeMode!! == EncodeMode.NULL_IF_ABSENT -> CodeBlock.of("null") @@ -1854,9 +1890,6 @@ class KotlinGenerator private constructor( } private fun Field.getAdapterName(nameDelimiter: Char = '.'): CodeBlock { - if (useArray) { - return CodeBlock.of("%T.FLOAT", ProtoAdapter::class) - } return if (type!!.isMap) { CodeBlock.of("%N", "${name}Adapter") } else { @@ -2218,7 +2251,7 @@ class KotlinGenerator private constructor( } private fun Field.getDeclaration(allocatedName: String) = when { - isPacked && isScalar && useArray -> CodeBlock.of("var %N: %T? = null", allocatedName, FloatArrayList::class) + useArray -> CodeBlock.of("var %N: %T? = null", allocatedName, arrayListForType) isPacked && isScalar -> CodeBlock.of("var %N: MutableList<%T>? = null", allocatedName, type!!.typeName) isRepeated -> CodeBlock.of("val $allocatedName = mutableListOf<%T>()", type!!.typeName) isMap -> CodeBlock.of( @@ -2252,7 +2285,7 @@ class KotlinGenerator private constructor( EncodeMode.REPEATED -> List::class.asClassName().parameterizedBy(baseClass) EncodeMode.PACKED -> { if (useArray) { - FloatArray::class.asTypeName() + primitiveArrayForType.asTypeName() } else { List::class.asTypeName().parameterizedBy(baseClass) } @@ -2276,7 +2309,7 @@ class KotlinGenerator private constructor( Map::class.asTypeName().parameterizedBy(keyType.typeName, valueType.typeName) EncodeMode.PACKED -> { when { - useArray -> FloatArray::class.asTypeName() + useArray -> primitiveArrayForType.asTypeName() else -> List::class.asTypeName().parameterizedBy(type.typeName) } } @@ -2301,7 +2334,7 @@ class KotlinGenerator private constructor( EncodeMode.REPEATED -> CodeBlock.of("emptyList()") EncodeMode.PACKED -> { if (useArray) { - CodeBlock.of("FloatArray(0)") + CodeBlock.of("%T(0)", primitiveArrayForType) } else { CodeBlock.of("emptyList()") } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt index db9d3cf16f..8f3eb8cd51 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -185,10 +185,15 @@ expect abstract class ProtoAdapter( @JvmField val BOOL: ProtoAdapter @JvmField val INT32: ProtoAdapter + @JvmField val INT32_ARRAY: ProtoAdapter @JvmField val UINT32: ProtoAdapter + @JvmField val UINT32_ARRAY: ProtoAdapter @JvmField val SINT32: ProtoAdapter + @JvmField val SINT32_ARRAY: ProtoAdapter @JvmField val FIXED32: ProtoAdapter + @JvmField val FIXED32_ARRAY: ProtoAdapter @JvmField val SFIXED32: ProtoAdapter + @JvmField val SFIXED32_ARRAY: ProtoAdapter @JvmField val INT64: ProtoAdapter @JvmField val INT64_ARRAY: ProtoAdapter /** @@ -196,12 +201,17 @@ expect abstract class ProtoAdapter( * in JSON. */ @JvmField val UINT64: ProtoAdapter + @JvmField val UINT64_ARRAY: ProtoAdapter @JvmField val SINT64: ProtoAdapter + @JvmField val SINT64_ARRAY: ProtoAdapter @JvmField val FIXED64: ProtoAdapter + @JvmField val FIXED64_ARRAY: ProtoAdapter @JvmField val SFIXED64: ProtoAdapter + @JvmField val SFIXED64_ARRAY: ProtoAdapter @JvmField val FLOAT: ProtoAdapter @JvmField val FLOAT_ARRAY: ProtoAdapter @JvmField val DOUBLE: ProtoAdapter + @JvmField val DOUBLE_ARRAY: ProtoAdapter @JvmField val BYTES: ProtoAdapter @JvmField val STRING: ProtoAdapter @JvmField val DURATION: ProtoAdapter @@ -441,6 +451,61 @@ internal class RepeatedProtoAdapter( override fun redact(value: List): List = emptyList() } +class DoubleArrayProtoAdapter( + private val originalAdapter: ProtoAdapter, +) : ProtoAdapter( + LENGTH_DELIMITED, + LongArray::class, + null, + originalAdapter.syntax, + DoubleArray(0), +) { + @Throws(IOException::class) + override fun encodeWithTag(writer: ProtoWriter, tag: Int, value: DoubleArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + @Throws(IOException::class) + override fun encodeWithTag(writer: ReverseProtoWriter, tag: Int, value: DoubleArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + override fun encodedSize(value: DoubleArray): Int { + var size = 0 + for (i in 0 until value.size) { + size += originalAdapter.encodedSize(value[i]) + } + return size + } + + override fun encodedSizeWithTag(tag: Int, value: DoubleArray?): Int { + return if (value == null || value.isEmpty()) 0 else super.encodedSizeWithTag(tag, value) + } + + @Throws(IOException::class) + override fun encode(writer: ProtoWriter, value: DoubleArray) { + for (i in 0 until value.size) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun encode(writer: ReverseProtoWriter, value: DoubleArray) { + for (i in (value.size - 1) downTo 0) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): DoubleArray = DoubleArray(1) { originalAdapter.decode(reader) } + + override fun redact(value: DoubleArray): DoubleArray = DoubleArray(0) +} + class LongArrayProtoAdapter( private val originalAdapter: ProtoAdapter, ) : ProtoAdapter( @@ -551,6 +616,61 @@ class FloatArrayProtoAdapter( override fun redact(value: FloatArray): FloatArray = FloatArray(0) } +class IntArrayProtoAdapter( + private val originalAdapter: ProtoAdapter, +) : ProtoAdapter( + LENGTH_DELIMITED, + IntArray::class, + null, + originalAdapter.syntax, + IntArray(0), +) { + @Throws(IOException::class) + override fun encodeWithTag(writer: ProtoWriter, tag: Int, value: IntArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + @Throws(IOException::class) + override fun encodeWithTag(writer: ReverseProtoWriter, tag: Int, value: IntArray?) { + if (value != null && value.isNotEmpty()) { + super.encodeWithTag(writer, tag, value) + } + } + + override fun encodedSize(value: IntArray): Int { + var size = 0 + for (i in 0 until value.size) { + size += originalAdapter.encodedSize(value[i]) + } + return size + } + + override fun encodedSizeWithTag(tag: Int, value: IntArray?): Int { + return if (value == null || value.isEmpty()) 0 else super.encodedSizeWithTag(tag, value) + } + + @Throws(IOException::class) + override fun encode(writer: ProtoWriter, value: IntArray) { + for (i in 0 until value.size) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun encode(writer: ReverseProtoWriter, value: IntArray) { + for (i in (value.size - 1) downTo 0) { + originalAdapter.encode(writer, value[i]) + } + } + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): IntArray = IntArray(1) { originalAdapter.decode(reader) } + + override fun redact(value: IntArray): IntArray = IntArray(0) +} + internal class MapProtoAdapter internal constructor( keyAdapter: ProtoAdapter, valueAdapter: ProtoAdapter diff --git a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 758c350a2f..c0cbd9a101 100644 --- a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -160,11 +160,17 @@ actual abstract class ProtoAdapter actual constructor( } actual val BOOL: ProtoAdapter = commonBool() + actual val INT32: ProtoAdapter = commonInt32() + actual val INT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(INT32) actual val UINT32: ProtoAdapter = commonUint32() + actual val UINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(UINT32) actual val SINT32: ProtoAdapter = commonSint32() + actual val SINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SINT32) actual val FIXED32: ProtoAdapter = commonFixed32() + actual val FIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(FIXED32) actual val SFIXED32: ProtoAdapter = commonSfixed32() + actual val SFIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SFIXED32) actual val INT64: ProtoAdapter = commonInt64() actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** @@ -172,12 +178,17 @@ actual abstract class ProtoAdapter actual constructor( * in JSON. */ actual val UINT64: ProtoAdapter = commonUint64() + actual val UINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(UINT64) actual val SINT64: ProtoAdapter = commonSint64() + actual val SINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SINT64) actual val FIXED64: ProtoAdapter = commonFixed64() + actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) actual val SFIXED64: ProtoAdapter = commonSfixed64() + actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) actual val FLOAT: ProtoAdapter = commonFloat() actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() + actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() actual val DURATION: ProtoAdapter = commonDuration() diff --git a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt index cf3b46fcb1..3f998f9d97 100644 --- a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -285,19 +285,29 @@ actual abstract class ProtoAdapter actual constructor( @JvmField actual val BOOL: ProtoAdapter = commonBool() @JvmField actual val INT32: ProtoAdapter = commonInt32() + @JvmField actual val INT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(INT32) @JvmField actual val UINT32: ProtoAdapter = commonUint32() + @JvmField actual val UINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(UINT32) @JvmField actual val SINT32: ProtoAdapter = commonSint32() + @JvmField actual val SINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SINT32) @JvmField actual val FIXED32: ProtoAdapter = commonFixed32() + @JvmField actual val FIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(FIXED32) @JvmField actual val SFIXED32: ProtoAdapter = commonSfixed32() + @JvmField actual val SFIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SFIXED32) @JvmField actual val INT64: ProtoAdapter = commonInt64() @JvmField actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) @JvmField actual val UINT64: ProtoAdapter = commonUint64() + @JvmField actual val UINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(UINT64) @JvmField actual val SINT64: ProtoAdapter = commonSint64() + @JvmField actual val SINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SINT64) @JvmField actual val FIXED64: ProtoAdapter = commonFixed64() + @JvmField actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) @JvmField actual val SFIXED64: ProtoAdapter = commonSfixed64() + @JvmField actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) @JvmField actual val FLOAT: ProtoAdapter = commonFloat() @JvmField actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) @JvmField actual val DOUBLE: ProtoAdapter = commonDouble() + @JvmField actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) @JvmField actual val BYTES: ProtoAdapter = commonBytes() @JvmField actual val STRING: ProtoAdapter = commonString() @JvmField actual val EMPTY: ProtoAdapter = commonEmpty() diff --git a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 460078f395..6b0d71b4e5 100644 --- a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -161,10 +161,15 @@ actual abstract class ProtoAdapter actual constructor( actual val BOOL: ProtoAdapter = commonBool() actual val INT32: ProtoAdapter = commonInt32() + actual val INT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(INT32) actual val UINT32: ProtoAdapter = commonUint32() + actual val UINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(UINT32) actual val SINT32: ProtoAdapter = commonSint32() + actual val SINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SINT32) actual val FIXED32: ProtoAdapter = commonFixed32() + actual val FIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(FIXED32) actual val SFIXED32: ProtoAdapter = commonSfixed32() + actual val SFIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SFIXED32) actual val INT64: ProtoAdapter = commonInt64() actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** @@ -172,12 +177,17 @@ actual abstract class ProtoAdapter actual constructor( * in JSON. */ actual val UINT64: ProtoAdapter = commonUint64() + actual val UINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(UINT64) actual val SINT64: ProtoAdapter = commonSint64() + actual val SINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SINT64) actual val FIXED64: ProtoAdapter = commonFixed64() + actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) actual val SFIXED64: ProtoAdapter = commonSfixed64() + actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) actual val FLOAT: ProtoAdapter = commonFloat() actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() + actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() actual val DURATION: ProtoAdapter = commonDuration() diff --git a/wire-tests-swift/src/main/swift/AllTypes.swift b/wire-tests-swift/src/main/swift/AllTypes.swift index 8b2d3a7cc5..38e6dc2b79 100644 --- a/wire-tests-swift/src/main/swift/AllTypes.swift +++ b/wire-tests-swift/src/main/swift/AllTypes.swift @@ -2353,8 +2353,8 @@ extension _AllTypes : Proto2Codable { case 608: try reader.decode(into: &array_sint64, encoding: .signed) case 609: try reader.decode(into: &array_fixed64, encoding: .fixed) case 610: try reader.decode(into: &array_sfixed64) - case 612: try reader.decode(into: &array_float) - case 613: try reader.decode(into: &array_double) + case 611: try reader.decode(into: &array_float) + case 612: try reader.decode(into: &array_double) case 1001: ext_opt_int32 = try reader.decode(Int32.self) case 1002: ext_opt_uint32 = try reader.decode(UInt32.self) case 1003: ext_opt_sint32 = try reader.decode(Int32.self, encoding: .signed) @@ -2651,8 +2651,8 @@ extension _AllTypes : Proto2Codable { try writer.encode(tag: 608, value: self.array_sint64, encoding: .signed, packed: true) try writer.encode(tag: 609, value: self.array_fixed64, encoding: .fixed, packed: true) try writer.encode(tag: 610, value: self.array_sfixed64, packed: true) - try writer.encode(tag: 612, value: self.array_float, packed: true) - try writer.encode(tag: 613, value: self.array_double, packed: true) + try writer.encode(tag: 611, value: self.array_float, packed: true) + try writer.encode(tag: 612, value: self.array_double, packed: true) try writer.encode(tag: 1001, value: self.ext_opt_int32) try writer.encode(tag: 1002, value: self.ext_opt_uint32) try writer.encode(tag: 1003, value: self.ext_opt_sint32, encoding: .signed) diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt index 439d4a4d4e..8ad9bf688a 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypes.kt @@ -98,7 +98,18 @@ class TestAllTypes { ext_opt_bool = true, ext_rep_bool = list(true, numRepeated), ext_pack_bool = list(true, numRepeated), + array_int32 = list(111, numRepeated).toIntArray(), + array_uint32 = list(112, numRepeated).toIntArray(), + array_sint32 = list(113, numRepeated).toIntArray(), + array_fixed32 = list(114, numRepeated).toIntArray(), + array_sfixed32 = list(115, numRepeated).toIntArray(), + array_int64 = list(116L, numRepeated).toLongArray(), + array_uint64 = list(117L, numRepeated).toLongArray(), + array_sint64 = list(118L, numRepeated).toLongArray(), + array_fixed64 = list(119L, numRepeated).toLongArray(), + array_sfixed64 = list(120L, numRepeated).toLongArray(), array_float = list(122.0f, numRepeated).toFloatArray(), + array_double = list(123.0, numRepeated).toDoubleArray(), ) } diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt index 79e8d5ef38..4b6b5cba14 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/TestAllTypesData.kt @@ -37,8 +37,11 @@ object TestAllTypesData { "ed32=[114, 114], pack_sfixed32=[115, 115], pack_int64=[116, 116], pack_uint64=[117, 117], " + "pack_sint64=[118, 118], pack_fixed64=[119, 119], pack_sfixed64=[120, 120], pack_bool=[true" + ", true], pack_float=[122.0, 122.0], pack_double=[123.0, 123.0], pack_nested_enum=[A, A], a" + - "rray_float=[122.0, 122.0], ext_opt_bool=true, ext_rep_bool=[true, true], ext_pack_bool=[tr" + - "ue, true]}" + "rray_int32=[111, 111], array_uint32=[112, 112], array_sint32=[113, 113], array_fixed32=[11" + + "4, 114], array_sfixed32=[115, 115], array_int64=[116, 116], array_uint64=[117, 117], array" + + "_sint64=[118, 118], array_fixed64=[119, 119], array_sfixed64=[120, 120], array_float=[122." + + "0, 122.0], array_double=[123.0, 123.0], ext_opt_bool=true, ext_rep_bool=[true, true], ext_" + + "pack_bool=[true, true]}" ) val expectedOutput = ( "" + @@ -261,10 +264,56 @@ object TestAllTypesData { "02" + // length = 2 "01" + // value = 1 "01" + // value = 1 - "a226" + // tag 612, type = 2 + + // arrays + "ca25" + // tag = 601, type = 2 + "02" + // length = 2 + "6f" + // value = 111 + "6f" + // value = 111 + "d225" + // tag = 602, type = 2 + "02" + // length = 2 + "70" + // value = 112 + "70" + // value = 112 + "da25" + // tag = 603, type = 2 + "04" + // length = 4 + "e201" + // value = 226 (=113 zig-zag) + "e201" + // value = 226 (=113 zig-zag) + "e225" + // tag = 604, type = 2 + "08" + // length = 8 + "72000000" + // value = 114 (fixed32) + "72000000" + // value = 114 (fixed32) + "ea25" + // tag = 605, type = 2 + "08" + // length = 8 + "73000000" + // value = 115 (sfixed32) + "73000000" + // value = 115 (sfixed32) + "f225" + // tag = 606, type = 2 + "02" + // length = 2 + "74" + // value = 116 + "74" + // value = 116 + "fa25" + // tag = 607, type = 2 + "02" + // length = 2 + "75" + // value = 117 + "75" + // value = 117 + "8226" + // tag = 608, type = 2 + "04" + // length = 4 + "ec01" + // value = 236 (=118 zigzag) + "ec01" + // value = 236 (=118 zigzag) + "8a26" + // tag = 609, type = 2 + "10" + // length = 16 + "7700000000000000" + // value = 119 + "7700000000000000" + // value = 119 + "9226" + // tag = 610, type = 2 + "10" + // length = 16 + "7800000000000000" + // value = 120 + "7800000000000000" + // value = 120 + "9a26" + // tag = 611, type = 2 "08" + // length = 8 "0000f442" + // value = 122.0F "0000f442" + // value = 122.0F + "a226" + // tag = 612, type = 2 + "10" + // length = 16 + "0000000000c05e40" + // value = 123.0 + "0000000000c05e40" + // value = 123.0 // extensions @@ -502,10 +551,57 @@ object TestAllTypesData { "01" + // value = 1 "e013" + // tag = 316, type = 0 "01" + // value = 1 - "a526" + // tag 612, type = 5 + + // arrays + + "c825" + // tag = 601, type = 0 + "6f" + // value = 111 + "c825" + // tag = 601, type = 0 + "6f" + // value = 111 + "d025" + // tag = 602, type = 0 + "70" + // value = 112 + "d025" + // tag = 602, type = 0 + "70" + // value = 112 + "d825" + // tag = 603, type = 0 + "e201" + // value = 226 (=113 zig-zag) + "d825" + // tag = 603, type = 0 + "e201" + // value = 226 (=113 zig-zag) + "e525" + // tag = 604, type = 5 + "72000000" + // value = 114 (fixed32) + "e525" + // tag = 604, type = 5 + "72000000" + // value = 114 (fixed32) + "ed25" + // tag = 605, type = 5 + "73000000" + // value = 115 (sfixed32) + "ed25" + // tag = 605, type = 5 + "73000000" + // value = 115 (sfixed32) + "f025" + // tag = 606, type = 0 + "74" + // value = 116 + "f025" + // tag = 606, type = 0 + "74" + // value = 116 + "f825" + // tag = 607, type = 0 + "75" + // value = 117 + "f825" + // tag = 607, type = 0 + "75" + // value = 117 + "8026" + // tag = 608, type = 0 + "ec01" + // value = 236 (=118 zigzag) + "8026" + // tag = 608, type = 0 + "ec01" + // value = 236 (=118 zigzag) + "8926" + // tag = 609, type = 1 + "7700000000000000" + // value = 119 + "8926" + // tag = 609, type = 1 + "7700000000000000" + // value = 119 + "9126" + // tag = 610, type = 1 + "7800000000000000" + // value = 120 + "9126" + // tag = 610, type = 1 + "7800000000000000" + // value = 120 + "9d26" + // tag = 611, type = 5 "0000f442" + // value = 122.0F - "a526" + // tag 612, type = 5 + "9d26" + // tag = 611, type = 5 "0000f442" + // value = 122.0F + "a126" + // tag = 612, type = 1 + "0000000000c05e40" + // value = 123.0 + "a126" + // tag = 612, type = 1 + "0000000000c05e40" + // value = 123.0 // extension diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index f227b715cd..7511c4b848 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -2,9 +2,12 @@ // Source: squareup.protos.kotlin.alltypes.AllTypes in all_types.proto package com.squareup.wire.protos.kotlin.alltypes +import com.squareup.wire.DoubleArrayList import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding import com.squareup.wire.FloatArrayList +import com.squareup.wire.IntArrayList +import com.squareup.wire.LongArrayList import com.squareup.wire.Message import com.squareup.wire.ProtoAdapter import com.squareup.wire.ProtoReader @@ -24,10 +27,13 @@ import kotlin.Boolean import kotlin.Deprecated import kotlin.DeprecationLevel import kotlin.Double +import kotlin.DoubleArray import kotlin.Float import kotlin.FloatArray import kotlin.Int +import kotlin.IntArray import kotlin.Long +import kotlin.LongArray import kotlin.Nothing import kotlin.String import kotlin.Unit @@ -347,73 +353,73 @@ public class AllTypes( adapter = "com.squareup.wire.ProtoAdapter#INT32", label = WireField.Label.PACKED, ) - public val array_int32: FloatArray = FloatArray(0), + public val array_int32: IntArray = IntArray(0), @field:WireField( tag = 602, adapter = "com.squareup.wire.ProtoAdapter#UINT32", label = WireField.Label.PACKED, ) - public val array_uint32: FloatArray = FloatArray(0), + public val array_uint32: IntArray = IntArray(0), @field:WireField( tag = 603, adapter = "com.squareup.wire.ProtoAdapter#SINT32", label = WireField.Label.PACKED, ) - public val array_sint32: FloatArray = FloatArray(0), + public val array_sint32: IntArray = IntArray(0), @field:WireField( tag = 604, adapter = "com.squareup.wire.ProtoAdapter#FIXED32", label = WireField.Label.PACKED, ) - public val array_fixed32: FloatArray = FloatArray(0), + public val array_fixed32: IntArray = IntArray(0), @field:WireField( tag = 605, adapter = "com.squareup.wire.ProtoAdapter#SFIXED32", label = WireField.Label.PACKED, ) - public val array_sfixed32: FloatArray = FloatArray(0), + public val array_sfixed32: IntArray = IntArray(0), @field:WireField( tag = 606, adapter = "com.squareup.wire.ProtoAdapter#INT64", label = WireField.Label.PACKED, ) - public val array_int64: FloatArray = FloatArray(0), + public val array_int64: LongArray = LongArray(0), @field:WireField( tag = 607, adapter = "com.squareup.wire.ProtoAdapter#UINT64", label = WireField.Label.PACKED, ) - public val array_uint64: FloatArray = FloatArray(0), + public val array_uint64: LongArray = LongArray(0), @field:WireField( tag = 608, adapter = "com.squareup.wire.ProtoAdapter#SINT64", label = WireField.Label.PACKED, ) - public val array_sint64: FloatArray = FloatArray(0), + public val array_sint64: LongArray = LongArray(0), @field:WireField( tag = 609, adapter = "com.squareup.wire.ProtoAdapter#FIXED64", label = WireField.Label.PACKED, ) - public val array_fixed64: FloatArray = FloatArray(0), + public val array_fixed64: LongArray = LongArray(0), @field:WireField( tag = 610, adapter = "com.squareup.wire.ProtoAdapter#SFIXED64", label = WireField.Label.PACKED, ) - public val array_sfixed64: FloatArray = FloatArray(0), + public val array_sfixed64: LongArray = LongArray(0), @field:WireField( - tag = 612, + tag = 611, adapter = "com.squareup.wire.ProtoAdapter#FLOAT", label = WireField.Label.PACKED, ) public val array_float: FloatArray = FloatArray(0), @field:WireField( - tag = 613, + tag = 612, adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", label = WireField.Label.PACKED, ) - public val array_double: FloatArray = FloatArray(0), + public val array_double: DoubleArray = DoubleArray(0), /** * Extension source: all_types.proto */ @@ -1700,18 +1706,18 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, - array_int32: FloatArray = this.array_int32, - array_uint32: FloatArray = this.array_uint32, - array_sint32: FloatArray = this.array_sint32, - array_fixed32: FloatArray = this.array_fixed32, - array_sfixed32: FloatArray = this.array_sfixed32, - array_int64: FloatArray = this.array_int64, - array_uint64: FloatArray = this.array_uint64, - array_sint64: FloatArray = this.array_sint64, - array_fixed64: FloatArray = this.array_fixed64, - array_sfixed64: FloatArray = this.array_sfixed64, + array_int32: IntArray = this.array_int32, + array_uint32: IntArray = this.array_uint32, + array_sint32: IntArray = this.array_sint32, + array_fixed32: IntArray = this.array_fixed32, + array_sfixed32: IntArray = this.array_sfixed32, + array_int64: LongArray = this.array_int64, + array_uint64: LongArray = this.array_uint64, + array_sint64: LongArray = this.array_sint64, + array_fixed64: LongArray = this.array_fixed64, + array_sfixed64: LongArray = this.array_sfixed64, array_float: FloatArray = this.array_float, - array_double: FloatArray = this.array_double, + array_double: DoubleArray = this.array_double, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -1932,18 +1938,18 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(601, value.array_int32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(602, value.array_uint32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(603, value.array_sint32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(604, value.array_fixed32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(605, value.array_sfixed32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(606, value.array_int64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(607, value.array_uint64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(608, value.array_sint64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(609, value.array_fixed64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(610, value.array_sfixed64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(612, value.array_float) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(613, value.array_double) + size += ProtoAdapter.INT32_ARRAY.encodedSizeWithTag(601, value.array_int32) + size += ProtoAdapter.UINT32_ARRAY.encodedSizeWithTag(602, value.array_uint32) + size += ProtoAdapter.SINT32_ARRAY.encodedSizeWithTag(603, value.array_sint32) + size += ProtoAdapter.FIXED32_ARRAY.encodedSizeWithTag(604, value.array_fixed32) + size += ProtoAdapter.SFIXED32_ARRAY.encodedSizeWithTag(605, value.array_sfixed32) + size += ProtoAdapter.INT64_ARRAY.encodedSizeWithTag(606, value.array_int64) + size += ProtoAdapter.UINT64_ARRAY.encodedSizeWithTag(607, value.array_uint64) + size += ProtoAdapter.SINT64_ARRAY.encodedSizeWithTag(608, value.array_sint64) + size += ProtoAdapter.FIXED64_ARRAY.encodedSizeWithTag(609, value.array_fixed64) + size += ProtoAdapter.SFIXED64_ARRAY.encodedSizeWithTag(610, value.array_sfixed64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(611, value.array_float) + size += ProtoAdapter.DOUBLE_ARRAY.encodedSizeWithTag(612, value.array_double) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -2082,18 +2088,18 @@ public class AllTypes( map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) + ProtoAdapter.INT32_ARRAY.encodeWithTag(writer, 601, value.array_int32) + ProtoAdapter.UINT32_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.SINT32_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.FIXED32_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.SFIXED32_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.INT64_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.UINT64_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.SINT64_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.FIXED64_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.SFIXED64_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 611, value.array_float) + ProtoAdapter.DOUBLE_ARRAY.encodeWithTag(writer, 612, value.array_double) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) @@ -2195,18 +2201,18 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) + ProtoAdapter.DOUBLE_ARRAY.encodeWithTag(writer, 612, value.array_double) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 611, value.array_float) + ProtoAdapter.SFIXED64_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FIXED64_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.SINT64_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.UINT64_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.INT64_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.SFIXED32_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.FIXED32_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.SINT32_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.UINT32_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.INT32_ARRAY.encodeWithTag(writer, 601, value.array_int32) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -2380,18 +2386,18 @@ public class AllTypes( val map_string_string = mutableMapOf() val map_string_message = mutableMapOf() val map_string_enum = mutableMapOf() - var array_int32: FloatArrayList? = null - var array_uint32: FloatArrayList? = null - var array_sint32: FloatArrayList? = null - var array_fixed32: FloatArrayList? = null - var array_sfixed32: FloatArrayList? = null - var array_int64: FloatArrayList? = null - var array_uint64: FloatArrayList? = null - var array_sint64: FloatArrayList? = null - var array_fixed64: FloatArrayList? = null - var array_sfixed64: FloatArrayList? = null + var array_int32: IntArrayList? = null + var array_uint32: IntArrayList? = null + var array_sint32: IntArrayList? = null + var array_fixed32: IntArrayList? = null + var array_sfixed32: IntArrayList? = null + var array_int64: LongArrayList? = null + var array_uint64: LongArrayList? = null + var array_sint64: LongArrayList? = null + var array_fixed64: LongArrayList? = null + var array_sfixed64: LongArrayList? = null var array_float: FloatArrayList? = null - var array_double: FloatArrayList? = null + var array_double: DoubleArrayList? = null var ext_opt_int32: Int? = null var ext_opt_uint32: Int? = null var ext_opt_sint32: Int? = null @@ -2670,9 +2676,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_int32 = FloatArrayList(initialCapacity) + array_int32 = IntArrayList(initialCapacity) } - array_int32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decode(reader)) } 602 -> { if (array_uint32 == null) { @@ -2680,9 +2686,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_uint32 = FloatArrayList(initialCapacity) + array_uint32 = IntArrayList(initialCapacity) } - array_uint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decode(reader)) } 603 -> { if (array_sint32 == null) { @@ -2690,9 +2696,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sint32 = FloatArrayList(initialCapacity) + array_sint32 = IntArrayList(initialCapacity) } - array_sint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decode(reader)) } 604 -> { if (array_fixed32 == null) { @@ -2700,9 +2706,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_fixed32 = FloatArrayList(initialCapacity) + array_fixed32 = IntArrayList(initialCapacity) } - array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decode(reader)) } 605 -> { if (array_sfixed32 == null) { @@ -2710,9 +2716,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sfixed32 = FloatArrayList(initialCapacity) + array_sfixed32 = IntArrayList(initialCapacity) } - array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decode(reader)) } 606 -> { if (array_int64 == null) { @@ -2720,9 +2726,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_int64 = FloatArrayList(initialCapacity) + array_int64 = LongArrayList(initialCapacity) } - array_int64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decode(reader)) } 607 -> { if (array_uint64 == null) { @@ -2730,9 +2736,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_uint64 = FloatArrayList(initialCapacity) + array_uint64 = LongArrayList(initialCapacity) } - array_uint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decode(reader)) } 608 -> { if (array_sint64 == null) { @@ -2740,9 +2746,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sint64 = FloatArrayList(initialCapacity) + array_sint64 = LongArrayList(initialCapacity) } - array_sint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decode(reader)) } 609 -> { if (array_fixed64 == null) { @@ -2750,9 +2756,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_fixed64 = FloatArrayList(initialCapacity) + array_fixed64 = LongArrayList(initialCapacity) } - array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decode(reader)) } 610 -> { if (array_sfixed64 == null) { @@ -2760,11 +2766,11 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sfixed64 = FloatArrayList(initialCapacity) + array_sfixed64 = LongArrayList(initialCapacity) } - array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decode(reader)) } - 612 -> { + 611 -> { if (array_float == null) { val minimumByteSize = 4 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) @@ -2774,15 +2780,15 @@ public class AllTypes( } array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } - 613 -> { + 612 -> { if (array_double == null) { val minimumByteSize = 8 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_double = FloatArrayList(initialCapacity) + array_double = DoubleArrayList(initialCapacity) } - array_double!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decode(reader)) } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) @@ -3052,18 +3058,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - array_int32 = array_int32?.getTruncatedArray() ?: FloatArray(0), - array_uint32 = array_uint32?.getTruncatedArray() ?: FloatArray(0), - array_sint32 = array_sint32?.getTruncatedArray() ?: FloatArray(0), - array_fixed32 = array_fixed32?.getTruncatedArray() ?: FloatArray(0), - array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: FloatArray(0), - array_int64 = array_int64?.getTruncatedArray() ?: FloatArray(0), - array_uint64 = array_uint64?.getTruncatedArray() ?: FloatArray(0), - array_sint64 = array_sint64?.getTruncatedArray() ?: FloatArray(0), - array_fixed64 = array_fixed64?.getTruncatedArray() ?: FloatArray(0), - array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: FloatArray(0), + array_int32 = array_int32?.getTruncatedArray() ?: IntArray(0), + array_uint32 = array_uint32?.getTruncatedArray() ?: IntArray(0), + array_sint32 = array_sint32?.getTruncatedArray() ?: IntArray(0), + array_fixed32 = array_fixed32?.getTruncatedArray() ?: IntArray(0), + array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: IntArray(0), + array_int64 = array_int64?.getTruncatedArray() ?: LongArray(0), + array_uint64 = array_uint64?.getTruncatedArray() ?: LongArray(0), + array_sint64 = array_sint64?.getTruncatedArray() ?: LongArray(0), + array_fixed64 = array_fixed64?.getTruncatedArray() ?: LongArray(0), + array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: LongArray(0), array_float = array_float?.getTruncatedArray() ?: FloatArray(0), - array_double = array_double?.getTruncatedArray() ?: FloatArray(0), + array_double = array_double?.getTruncatedArray() ?: DoubleArray(0), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, diff --git a/wire-tests/src/commonTest/proto/kotlin/all_types.proto b/wire-tests/src/commonTest/proto/kotlin/all_types.proto index 14f2f095be..39fef3881a 100644 --- a/wire-tests/src/commonTest/proto/kotlin/all_types.proto +++ b/wire-tests/src/commonTest/proto/kotlin/all_types.proto @@ -134,8 +134,8 @@ message AllTypes { repeated sint64 array_sint64 = 608 [packed = true, wire.use_array = true]; repeated fixed64 array_fixed64 = 609 [packed = true, wire.use_array = true]; repeated sfixed64 array_sfixed64 = 610 [packed = true, wire.use_array = true]; - repeated float array_float = 612 [packed = true, wire.use_array = true]; - repeated double array_double = 613 [packed = true, wire.use_array = true]; + repeated float array_float = 611 [packed = true, wire.use_array = true]; + repeated double array_double = 612 [packed = true, wire.use_array = true]; } extend AllTypes { diff --git a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt index 7eca5e2fa0..57d7c2d93a 100644 --- a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt +++ b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterIdentityTest.kt @@ -36,12 +36,18 @@ class ProtoAdapterIdentityTest { // StructNull's in Kotlin is in Java. assertThat(protoAdapter.identity).isNull() } + protoAdapter.type == DoubleArray::class -> { + assertThat(protoAdapter.identity).isEqualTo(doubleArrayOf()) + } protoAdapter.type == FloatArray::class -> { assertThat(protoAdapter.identity).isEqualTo(floatArrayOf()) } protoAdapter.type == LongArray::class -> { assertThat(protoAdapter.identity).isEqualTo(longArrayOf()) } + protoAdapter.type == IntArray::class -> { + assertThat(protoAdapter.identity).isEqualTo(intArrayOf()) + } protoAdapter.type.isPrimitive && protoAdapter.syntax === Syntax.PROTO_2 -> { // All other primitive types are numbers and must have 0 as their identity value. assertThat((protoAdapter.identity as Number).toDouble()).isEqualTo(0.0) diff --git a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt index 6a3cc9fe48..a191db2309 100644 --- a/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt +++ b/wire-tests/src/jvmJavaTest/kotlin/com/squareup/wire/ProtoAdapterTypeUrlTest.kt @@ -37,8 +37,10 @@ class ProtoAdapterTypeUrlTest { protoAdapter.type.isPrimitive || protoAdapter.type == String::class || protoAdapter.type == ByteString::class || + protoAdapter.type == DoubleArray::class || protoAdapter.type == FloatArray::class || - protoAdapter.type == LongArray::class + protoAdapter.type == LongArray::class || + protoAdapter.type == IntArray::class ) -> { // Scalar types don't have a type URL. assertThat(protoAdapter.typeUrl).isNull() diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 8102ce5c46..45341fbe04 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -2,9 +2,12 @@ // Source: squareup.protos.kotlin.alltypes.AllTypes in all_types.proto package com.squareup.wire.protos.kotlin.alltypes +import com.squareup.wire.DoubleArrayList import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding import com.squareup.wire.FloatArrayList +import com.squareup.wire.IntArrayList +import com.squareup.wire.LongArrayList import com.squareup.wire.Message import com.squareup.wire.ProtoAdapter import com.squareup.wire.ProtoReader @@ -22,10 +25,13 @@ import com.squareup.wire.`internal`.sanitize import kotlin.Any import kotlin.Boolean import kotlin.Double +import kotlin.DoubleArray import kotlin.Float import kotlin.FloatArray import kotlin.Int +import kotlin.IntArray import kotlin.Long +import kotlin.LongArray import kotlin.String import kotlin.Unit import kotlin.collections.List @@ -395,84 +401,84 @@ public class AllTypes( label = WireField.Label.PACKED, ) @JvmField - public val array_int32: FloatArray = FloatArray(0), + public val array_int32: IntArray = IntArray(0), @field:WireField( tag = 602, adapter = "com.squareup.wire.ProtoAdapter#UINT32", label = WireField.Label.PACKED, ) @JvmField - public val array_uint32: FloatArray = FloatArray(0), + public val array_uint32: IntArray = IntArray(0), @field:WireField( tag = 603, adapter = "com.squareup.wire.ProtoAdapter#SINT32", label = WireField.Label.PACKED, ) @JvmField - public val array_sint32: FloatArray = FloatArray(0), + public val array_sint32: IntArray = IntArray(0), @field:WireField( tag = 604, adapter = "com.squareup.wire.ProtoAdapter#FIXED32", label = WireField.Label.PACKED, ) @JvmField - public val array_fixed32: FloatArray = FloatArray(0), + public val array_fixed32: IntArray = IntArray(0), @field:WireField( tag = 605, adapter = "com.squareup.wire.ProtoAdapter#SFIXED32", label = WireField.Label.PACKED, ) @JvmField - public val array_sfixed32: FloatArray = FloatArray(0), + public val array_sfixed32: IntArray = IntArray(0), @field:WireField( tag = 606, adapter = "com.squareup.wire.ProtoAdapter#INT64", label = WireField.Label.PACKED, ) @JvmField - public val array_int64: FloatArray = FloatArray(0), + public val array_int64: LongArray = LongArray(0), @field:WireField( tag = 607, adapter = "com.squareup.wire.ProtoAdapter#UINT64", label = WireField.Label.PACKED, ) @JvmField - public val array_uint64: FloatArray = FloatArray(0), + public val array_uint64: LongArray = LongArray(0), @field:WireField( tag = 608, adapter = "com.squareup.wire.ProtoAdapter#SINT64", label = WireField.Label.PACKED, ) @JvmField - public val array_sint64: FloatArray = FloatArray(0), + public val array_sint64: LongArray = LongArray(0), @field:WireField( tag = 609, adapter = "com.squareup.wire.ProtoAdapter#FIXED64", label = WireField.Label.PACKED, ) @JvmField - public val array_fixed64: FloatArray = FloatArray(0), + public val array_fixed64: LongArray = LongArray(0), @field:WireField( tag = 610, adapter = "com.squareup.wire.ProtoAdapter#SFIXED64", label = WireField.Label.PACKED, ) @JvmField - public val array_sfixed64: FloatArray = FloatArray(0), + public val array_sfixed64: LongArray = LongArray(0), @field:WireField( - tag = 612, + tag = 611, adapter = "com.squareup.wire.ProtoAdapter#FLOAT", label = WireField.Label.PACKED, ) @JvmField public val array_float: FloatArray = FloatArray(0), @field:WireField( - tag = 613, + tag = 612, adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", label = WireField.Label.PACKED, ) @JvmField - public val array_double: FloatArray = FloatArray(0), + public val array_double: DoubleArray = DoubleArray(0), /** * Extension source: all_types.proto */ @@ -1986,18 +1992,18 @@ public class AllTypes( map_string_string: Map = this.map_string_string, map_string_message: Map = this.map_string_message, map_string_enum: Map = this.map_string_enum, - array_int32: FloatArray = this.array_int32, - array_uint32: FloatArray = this.array_uint32, - array_sint32: FloatArray = this.array_sint32, - array_fixed32: FloatArray = this.array_fixed32, - array_sfixed32: FloatArray = this.array_sfixed32, - array_int64: FloatArray = this.array_int64, - array_uint64: FloatArray = this.array_uint64, - array_sint64: FloatArray = this.array_sint64, - array_fixed64: FloatArray = this.array_fixed64, - array_sfixed64: FloatArray = this.array_sfixed64, + array_int32: IntArray = this.array_int32, + array_uint32: IntArray = this.array_uint32, + array_sint32: IntArray = this.array_sint32, + array_fixed32: IntArray = this.array_fixed32, + array_sfixed32: IntArray = this.array_sfixed32, + array_int64: LongArray = this.array_int64, + array_uint64: LongArray = this.array_uint64, + array_sint64: LongArray = this.array_sint64, + array_fixed64: LongArray = this.array_fixed64, + array_sfixed64: LongArray = this.array_sfixed64, array_float: FloatArray = this.array_float, - array_double: FloatArray = this.array_double, + array_double: DoubleArray = this.array_double, ext_opt_int32: Int? = this.ext_opt_int32, ext_opt_uint32: Int? = this.ext_opt_uint32, ext_opt_sint32: Int? = this.ext_opt_sint32, @@ -2330,40 +2336,40 @@ public class AllTypes( public var map_string_enum: Map = emptyMap() @JvmField - public var array_int32: FloatArray = FloatArray(0) + public var array_int32: IntArray = IntArray(0) @JvmField - public var array_uint32: FloatArray = FloatArray(0) + public var array_uint32: IntArray = IntArray(0) @JvmField - public var array_sint32: FloatArray = FloatArray(0) + public var array_sint32: IntArray = IntArray(0) @JvmField - public var array_fixed32: FloatArray = FloatArray(0) + public var array_fixed32: IntArray = IntArray(0) @JvmField - public var array_sfixed32: FloatArray = FloatArray(0) + public var array_sfixed32: IntArray = IntArray(0) @JvmField - public var array_int64: FloatArray = FloatArray(0) + public var array_int64: LongArray = LongArray(0) @JvmField - public var array_uint64: FloatArray = FloatArray(0) + public var array_uint64: LongArray = LongArray(0) @JvmField - public var array_sint64: FloatArray = FloatArray(0) + public var array_sint64: LongArray = LongArray(0) @JvmField - public var array_fixed64: FloatArray = FloatArray(0) + public var array_fixed64: LongArray = LongArray(0) @JvmField - public var array_sfixed64: FloatArray = FloatArray(0) + public var array_sfixed64: LongArray = LongArray(0) @JvmField public var array_float: FloatArray = FloatArray(0) @JvmField - public var array_double: FloatArray = FloatArray(0) + public var array_double: DoubleArray = DoubleArray(0) @JvmField public var ext_opt_int32: Int? = null @@ -2965,52 +2971,52 @@ public class AllTypes( return this } - public fun array_int32(array_int32: FloatArray): Builder { + public fun array_int32(array_int32: IntArray): Builder { this.array_int32 = array_int32 return this } - public fun array_uint32(array_uint32: FloatArray): Builder { + public fun array_uint32(array_uint32: IntArray): Builder { this.array_uint32 = array_uint32 return this } - public fun array_sint32(array_sint32: FloatArray): Builder { + public fun array_sint32(array_sint32: IntArray): Builder { this.array_sint32 = array_sint32 return this } - public fun array_fixed32(array_fixed32: FloatArray): Builder { + public fun array_fixed32(array_fixed32: IntArray): Builder { this.array_fixed32 = array_fixed32 return this } - public fun array_sfixed32(array_sfixed32: FloatArray): Builder { + public fun array_sfixed32(array_sfixed32: IntArray): Builder { this.array_sfixed32 = array_sfixed32 return this } - public fun array_int64(array_int64: FloatArray): Builder { + public fun array_int64(array_int64: LongArray): Builder { this.array_int64 = array_int64 return this } - public fun array_uint64(array_uint64: FloatArray): Builder { + public fun array_uint64(array_uint64: LongArray): Builder { this.array_uint64 = array_uint64 return this } - public fun array_sint64(array_sint64: FloatArray): Builder { + public fun array_sint64(array_sint64: LongArray): Builder { this.array_sint64 = array_sint64 return this } - public fun array_fixed64(array_fixed64: FloatArray): Builder { + public fun array_fixed64(array_fixed64: LongArray): Builder { this.array_fixed64 = array_fixed64 return this } - public fun array_sfixed64(array_sfixed64: FloatArray): Builder { + public fun array_sfixed64(array_sfixed64: LongArray): Builder { this.array_sfixed64 = array_sfixed64 return this } @@ -3020,7 +3026,7 @@ public class AllTypes( return this } - public fun array_double(array_double: FloatArray): Builder { + public fun array_double(array_double: DoubleArray): Builder { this.array_double = array_double return this } @@ -3593,18 +3599,18 @@ public class AllTypes( size += map_string_stringAdapter.encodedSizeWithTag(502, value.map_string_string) size += map_string_messageAdapter.encodedSizeWithTag(503, value.map_string_message) size += map_string_enumAdapter.encodedSizeWithTag(504, value.map_string_enum) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(601, value.array_int32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(602, value.array_uint32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(603, value.array_sint32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(604, value.array_fixed32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(605, value.array_sfixed32) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(606, value.array_int64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(607, value.array_uint64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(608, value.array_sint64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(609, value.array_fixed64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(610, value.array_sfixed64) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(612, value.array_float) - size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(613, value.array_double) + size += ProtoAdapter.INT32_ARRAY.encodedSizeWithTag(601, value.array_int32) + size += ProtoAdapter.UINT32_ARRAY.encodedSizeWithTag(602, value.array_uint32) + size += ProtoAdapter.SINT32_ARRAY.encodedSizeWithTag(603, value.array_sint32) + size += ProtoAdapter.FIXED32_ARRAY.encodedSizeWithTag(604, value.array_fixed32) + size += ProtoAdapter.SFIXED32_ARRAY.encodedSizeWithTag(605, value.array_sfixed32) + size += ProtoAdapter.INT64_ARRAY.encodedSizeWithTag(606, value.array_int64) + size += ProtoAdapter.UINT64_ARRAY.encodedSizeWithTag(607, value.array_uint64) + size += ProtoAdapter.SINT64_ARRAY.encodedSizeWithTag(608, value.array_sint64) + size += ProtoAdapter.FIXED64_ARRAY.encodedSizeWithTag(609, value.array_fixed64) + size += ProtoAdapter.SFIXED64_ARRAY.encodedSizeWithTag(610, value.array_sfixed64) + size += ProtoAdapter.FLOAT_ARRAY.encodedSizeWithTag(611, value.array_float) + size += ProtoAdapter.DOUBLE_ARRAY.encodedSizeWithTag(612, value.array_double) size += ProtoAdapter.INT32.encodedSizeWithTag(1001, value.ext_opt_int32) size += ProtoAdapter.UINT32.encodedSizeWithTag(1002, value.ext_opt_uint32) size += ProtoAdapter.SINT32.encodedSizeWithTag(1003, value.ext_opt_sint32) @@ -3743,18 +3749,18 @@ public class AllTypes( map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) + ProtoAdapter.INT32_ARRAY.encodeWithTag(writer, 601, value.array_int32) + ProtoAdapter.UINT32_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.SINT32_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.FIXED32_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.SFIXED32_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.INT64_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.UINT64_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.SINT64_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.FIXED64_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.SFIXED64_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 611, value.array_float) + ProtoAdapter.DOUBLE_ARRAY.encodeWithTag(writer, 612, value.array_double) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) @@ -3856,18 +3862,18 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 613, value.array_double) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 612, value.array_float) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 608, value.array_sint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 607, value.array_uint64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 606, value.array_int64) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 603, value.array_sint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 602, value.array_uint32) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 601, value.array_int32) + ProtoAdapter.DOUBLE_ARRAY.encodeWithTag(writer, 612, value.array_double) + ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 611, value.array_float) + ProtoAdapter.SFIXED64_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) + ProtoAdapter.FIXED64_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) + ProtoAdapter.SINT64_ARRAY.encodeWithTag(writer, 608, value.array_sint64) + ProtoAdapter.UINT64_ARRAY.encodeWithTag(writer, 607, value.array_uint64) + ProtoAdapter.INT64_ARRAY.encodeWithTag(writer, 606, value.array_int64) + ProtoAdapter.SFIXED32_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) + ProtoAdapter.FIXED32_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) + ProtoAdapter.SINT32_ARRAY.encodeWithTag(writer, 603, value.array_sint32) + ProtoAdapter.UINT32_ARRAY.encodeWithTag(writer, 602, value.array_uint32) + ProtoAdapter.INT32_ARRAY.encodeWithTag(writer, 601, value.array_int32) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -4041,18 +4047,18 @@ public class AllTypes( val map_string_string = mutableMapOf() val map_string_message = mutableMapOf() val map_string_enum = mutableMapOf() - var array_int32: FloatArrayList? = null - var array_uint32: FloatArrayList? = null - var array_sint32: FloatArrayList? = null - var array_fixed32: FloatArrayList? = null - var array_sfixed32: FloatArrayList? = null - var array_int64: FloatArrayList? = null - var array_uint64: FloatArrayList? = null - var array_sint64: FloatArrayList? = null - var array_fixed64: FloatArrayList? = null - var array_sfixed64: FloatArrayList? = null + var array_int32: IntArrayList? = null + var array_uint32: IntArrayList? = null + var array_sint32: IntArrayList? = null + var array_fixed32: IntArrayList? = null + var array_sfixed32: IntArrayList? = null + var array_int64: LongArrayList? = null + var array_uint64: LongArrayList? = null + var array_sint64: LongArrayList? = null + var array_fixed64: LongArrayList? = null + var array_sfixed64: LongArrayList? = null var array_float: FloatArrayList? = null - var array_double: FloatArrayList? = null + var array_double: DoubleArrayList? = null var ext_opt_int32: Int? = null var ext_opt_uint32: Int? = null var ext_opt_sint32: Int? = null @@ -4331,9 +4337,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_int32 = FloatArrayList(initialCapacity) + array_int32 = IntArrayList(initialCapacity) } - array_int32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decode(reader)) } 602 -> { if (array_uint32 == null) { @@ -4341,9 +4347,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_uint32 = FloatArrayList(initialCapacity) + array_uint32 = IntArrayList(initialCapacity) } - array_uint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decode(reader)) } 603 -> { if (array_sint32 == null) { @@ -4351,9 +4357,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sint32 = FloatArrayList(initialCapacity) + array_sint32 = IntArrayList(initialCapacity) } - array_sint32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decode(reader)) } 604 -> { if (array_fixed32 == null) { @@ -4361,9 +4367,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_fixed32 = FloatArrayList(initialCapacity) + array_fixed32 = IntArrayList(initialCapacity) } - array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decode(reader)) } 605 -> { if (array_sfixed32 == null) { @@ -4371,9 +4377,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sfixed32 = FloatArrayList(initialCapacity) + array_sfixed32 = IntArrayList(initialCapacity) } - array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decode(reader)) } 606 -> { if (array_int64 == null) { @@ -4381,9 +4387,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_int64 = FloatArrayList(initialCapacity) + array_int64 = LongArrayList(initialCapacity) } - array_int64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decode(reader)) } 607 -> { if (array_uint64 == null) { @@ -4391,9 +4397,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_uint64 = FloatArrayList(initialCapacity) + array_uint64 = LongArrayList(initialCapacity) } - array_uint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decode(reader)) } 608 -> { if (array_sint64 == null) { @@ -4401,9 +4407,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sint64 = FloatArrayList(initialCapacity) + array_sint64 = LongArrayList(initialCapacity) } - array_sint64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decode(reader)) } 609 -> { if (array_fixed64 == null) { @@ -4411,9 +4417,9 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_fixed64 = FloatArrayList(initialCapacity) + array_fixed64 = LongArrayList(initialCapacity) } - array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decode(reader)) } 610 -> { if (array_sfixed64 == null) { @@ -4421,11 +4427,11 @@ public class AllTypes( val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_sfixed64 = FloatArrayList(initialCapacity) + array_sfixed64 = LongArrayList(initialCapacity) } - array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decode(reader)) } - 612 -> { + 611 -> { if (array_float == null) { val minimumByteSize = 4 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) @@ -4435,15 +4441,15 @@ public class AllTypes( } array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) } - 613 -> { + 612 -> { if (array_double == null) { val minimumByteSize = 8 val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) .coerceAtMost(Int.MAX_VALUE.toLong()) .toInt() - array_double = FloatArrayList(initialCapacity) + array_double = DoubleArrayList(initialCapacity) } - array_double!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decode(reader)) } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) @@ -4713,18 +4719,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - array_int32 = array_int32?.getTruncatedArray() ?: FloatArray(0), - array_uint32 = array_uint32?.getTruncatedArray() ?: FloatArray(0), - array_sint32 = array_sint32?.getTruncatedArray() ?: FloatArray(0), - array_fixed32 = array_fixed32?.getTruncatedArray() ?: FloatArray(0), - array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: FloatArray(0), - array_int64 = array_int64?.getTruncatedArray() ?: FloatArray(0), - array_uint64 = array_uint64?.getTruncatedArray() ?: FloatArray(0), - array_sint64 = array_sint64?.getTruncatedArray() ?: FloatArray(0), - array_fixed64 = array_fixed64?.getTruncatedArray() ?: FloatArray(0), - array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: FloatArray(0), + array_int32 = array_int32?.getTruncatedArray() ?: IntArray(0), + array_uint32 = array_uint32?.getTruncatedArray() ?: IntArray(0), + array_sint32 = array_sint32?.getTruncatedArray() ?: IntArray(0), + array_fixed32 = array_fixed32?.getTruncatedArray() ?: IntArray(0), + array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: IntArray(0), + array_int64 = array_int64?.getTruncatedArray() ?: LongArray(0), + array_uint64 = array_uint64?.getTruncatedArray() ?: LongArray(0), + array_sint64 = array_sint64?.getTruncatedArray() ?: LongArray(0), + array_fixed64 = array_fixed64?.getTruncatedArray() ?: LongArray(0), + array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: LongArray(0), array_float = array_float?.getTruncatedArray() ?: FloatArray(0), - array_double = array_double?.getTruncatedArray() ?: FloatArray(0), + array_double = array_double?.getTruncatedArray() ?: DoubleArray(0), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, From 96a95e591fcd1d2b004b8c696d7f674a12c227b4 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Fri, 14 Apr 2023 11:01:03 -0400 Subject: [PATCH 09/17] Fix up my new test --- .../test/java/com/squareup/wire/kotlin/KotlinGeneratorTest.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 c8faa5e2bd..9ed8c80633 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 @@ -1528,7 +1528,8 @@ class KotlinGeneratorTest { ) } val code = KotlinWithProfilesGenerator(schema).generateKotlin("proto_package.Person") - assertContains(code, "foobarbazz") + assertContains(code, "public val name: FloatArray = FloatArray(0)") + assertContains(code, "ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 1, value.name)") } @Test fun documentationEscapesBrackets() { From 7e5a415f082e0a07178fc17c2fe363013259b2b2 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Fri, 14 Apr 2023 11:38:39 -0400 Subject: [PATCH 10/17] Add linker validation --- .../kotlin/com/squareup/wire/schema/Field.kt | 6 +++ .../com/squareup/wire/schema/SchemaTest.kt | 38 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt index 86943b8a73..e149ba95b3 100644 --- a/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt +++ b/wire-schema/src/commonMain/kotlin/com/squareup/wire/schema/Field.kt @@ -144,6 +144,12 @@ data class Field( if (isPacked && !isPackable(linker, type!!)) { linker.errors += "packed=true not permitted on $type" } + if (useArray && !isPacked) { + linker.errors += "wire.use_array=true only permitted on packed fields" + } + if (useArray && type?.isScalar != true) { + linker.errors += "wire.use_array=true only permitted on scalar fields" + } if (isExtension) { if (isRequired) { linker.errors += "extension fields cannot be required" diff --git a/wire-schema/src/jvmTest/kotlin/com/squareup/wire/schema/SchemaTest.kt b/wire-schema/src/jvmTest/kotlin/com/squareup/wire/schema/SchemaTest.kt index 324b102703..7b891e48ea 100644 --- a/wire-schema/src/jvmTest/kotlin/com/squareup/wire/schema/SchemaTest.kt +++ b/wire-schema/src/jvmTest/kotlin/com/squareup/wire/schema/SchemaTest.kt @@ -296,6 +296,44 @@ class SchemaTest { } } + @Test + fun fieldUsesUseArrayButShouldntBe() { + try { + buildSchema { + add( + "message.proto".toPath(), + """ + |import "wire/extensions.proto"; + | + |message Message { + | repeated bytes a = 1 [wire.use_array=true]; + | repeated Message b = 2 [wire.use_array=true]; + | repeated float c = 3 [wire.use_array=true]; + |} + """.trimMargin() + ) + } + fail() + } catch (expected: SchemaException) { + assertThat(expected).hasMessage( + """ + |wire.use_array=true only permitted on packed fields + | for field a (/source/message.proto:4:3) + | in message Message (/source/message.proto:3:1) + |wire.use_array=true only permitted on packed fields + | for field b (/source/message.proto:5:3) + | in message Message (/source/message.proto:3:1) + |wire.use_array=true only permitted on scalar fields + | for field b (/source/message.proto:5:3) + | in message Message (/source/message.proto:3:1) + |wire.use_array=true only permitted on packed fields + | for field c (/source/message.proto:6:3) + | in message Message (/source/message.proto:3:1) + """.trimMargin() + ) + } + } + @Test fun fieldIsDeprecated() { val schema = buildSchema { From 2f78652fa35c4bc58ba191eb98682027b9cb43c3 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Fri, 14 Apr 2023 17:23:45 -0400 Subject: [PATCH 11/17] Play around with FloatProtoAdapter --- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 39 ++++++++++++++----- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 2 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 2 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 2 +- 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 8f3eb8cd51..ab036453b1 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -208,7 +208,7 @@ expect abstract class ProtoAdapter( @JvmField val FIXED64_ARRAY: ProtoAdapter @JvmField val SFIXED64: ProtoAdapter @JvmField val SFIXED64_ARRAY: ProtoAdapter - @JvmField val FLOAT: ProtoAdapter + @JvmField val FLOAT: FloatProtoAdapter @JvmField val FLOAT_ARRAY: ProtoAdapter @JvmField val DOUBLE: ProtoAdapter @JvmField val DOUBLE_ARRAY: ProtoAdapter @@ -562,7 +562,7 @@ class LongArrayProtoAdapter( } class FloatArrayProtoAdapter( - private val originalAdapter: ProtoAdapter, + private val originalAdapter: FloatProtoAdapter, ) : ProtoAdapter( LENGTH_DELIMITED, LongArray::class, @@ -599,19 +599,19 @@ class FloatArrayProtoAdapter( @Throws(IOException::class) override fun encode(writer: ProtoWriter, value: FloatArray) { for (i in 0 until value.size) { - originalAdapter.encode(writer, value[i]) + originalAdapter.encodePrimitive(writer, value[i]) } } @Throws(IOException::class) override fun encode(writer: ReverseProtoWriter, value: FloatArray) { for (i in (value.size - 1) downTo 0) { - originalAdapter.encode(writer, value[i]) + originalAdapter.encodePrimitive(writer, value[i]) } } @Throws(IOException::class) - override fun decode(reader: ProtoReader): FloatArray = FloatArray(1) { originalAdapter.decode(reader) } + override fun decode(reader: ProtoReader): FloatArray = FloatArray(1) { originalAdapter.decodePrimitive(reader) } override fun redact(value: FloatArray): FloatArray = FloatArray(0) } @@ -1027,34 +1027,53 @@ internal fun commonFixed64(): ProtoAdapter = object : ProtoAdapter( override fun redact(value: Long): Long = throw UnsupportedOperationException() } -internal fun commonSfixed64() = commonFixed64() -internal fun commonFloat(): ProtoAdapter = object : ProtoAdapter( +class FloatProtoAdapter : ProtoAdapter( FieldEncoding.FIXED32, Float::class, null, Syntax.PROTO_2, 0.0f ) { - override fun encodedSize(value: Float): Int = FIXED_32_SIZE + @Throws(IOException::class) + fun encodePrimitive(writer: ProtoWriter, value: Float) { + writer.writeFixed32(value.toBits()) + } @Throws(IOException::class) override fun encode(writer: ProtoWriter, value: Float) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + fun encodePrimitive(writer: ReverseProtoWriter, value: Float) { writer.writeFixed32(value.toBits()) } @Throws(IOException::class) override fun encode(writer: ReverseProtoWriter, value: Float) { - writer.writeFixed32(value.toBits()) + encodePrimitive(writer, value) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Float { + fun decodePrimitive(reader: ProtoReader): Float { return Float.fromBits(reader.readFixed32()) } + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Float { + return decodePrimitive(reader) + } + + fun encodedSizePrimitive(): Int = FIXED_32_SIZE + + override fun encodedSize(value: Float): Int = FIXED_32_SIZE + override fun redact(value: Float): Float = throw UnsupportedOperationException() } +internal fun commonSfixed64() = commonFixed64() +internal fun commonFloat(): FloatProtoAdapter = FloatProtoAdapter() + internal fun commonDouble(): ProtoAdapter = object : ProtoAdapter( FieldEncoding.FIXED64, Double::class, diff --git a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt index c0cbd9a101..9bba896c4b 100644 --- a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -185,7 +185,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) actual val SFIXED64: ProtoAdapter = commonSfixed64() actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) - actual val FLOAT: ProtoAdapter = commonFloat() + actual val FLOAT: FloatProtoAdapter = commonFloat() actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) diff --git a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 3f998f9d97..0fd3f6efe0 100644 --- a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -304,7 +304,7 @@ actual abstract class ProtoAdapter actual constructor( @JvmField actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) @JvmField actual val SFIXED64: ProtoAdapter = commonSfixed64() @JvmField actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) - @JvmField actual val FLOAT: ProtoAdapter = commonFloat() + @JvmField actual val FLOAT: FloatProtoAdapter = commonFloat() @JvmField actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) @JvmField actual val DOUBLE: ProtoAdapter = commonDouble() @JvmField actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) diff --git a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 6b0d71b4e5..cddcbd4603 100644 --- a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -184,7 +184,7 @@ actual abstract class ProtoAdapter actual constructor( actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) actual val SFIXED64: ProtoAdapter = commonSfixed64() actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) - actual val FLOAT: ProtoAdapter = commonFloat() + actual val FLOAT: FloatProtoAdapter = commonFloat() actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) actual val DOUBLE: ProtoAdapter = commonDouble() actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) From 041b3e302bc2d57f04ce03ba98da1a0b17efcf04 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Mon, 17 Apr 2023 15:59:37 -0400 Subject: [PATCH 12/17] Changes after chatting w/ Jesse --- .../squareup/wire/kotlin/KotlinGenerator.kt | 55 ++-- .../wire/kotlin/KotlinGeneratorTest.kt | 8 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 247 +++++++++++------- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 22 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 22 +- .../kotlin/com/squareup/wire/ProtoAdapter.kt | 22 +- .../wire/protos/kotlin/alltypes/AllTypes.kt | 180 +++++++++---- .../wire/protos/kotlin/alltypes/AllTypes.kt | 204 ++++++++++----- 8 files changed, 499 insertions(+), 261 deletions(-) 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 b50939ce36..9e6daedae3 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 @@ -160,7 +160,7 @@ class KotlinGenerator private constructor( get() = type.typeName private val Service.serviceName get() = type.typeName - private val Field.primitiveArrayForType + private val Field.primitiveArrayClassForType get() = when (type!!.typeName) { LONG -> LongArray::class INT -> IntArray::class @@ -168,7 +168,15 @@ class KotlinGenerator private constructor( DOUBLE -> DoubleArray::class else -> throw IllegalArgumentException("No Array type for $type") } - private val Field.arrayListForType + private val Field.emptyPrimitiveArrayForType + get() = when (type!!.typeName) { + LONG -> CodeBlock.of("longArrayOf()") + INT -> CodeBlock.of("intArrayOf()") + FLOAT -> CodeBlock.of("floatArrayOf()") + DOUBLE -> CodeBlock.of("doubleArrayOf()") + else -> throw IllegalArgumentException("No Array type for $type") + } + private val Field.arrayListClassForType get() = when (type!!.typeName) { LONG -> LongArrayList::class INT -> IntArrayList::class @@ -1594,12 +1602,23 @@ class KotlinGenerator private constructor( if (field.encodeMode == EncodeMode.OMIT_IDENTITY) { add(fieldEqualsIdentityBlock(field, fieldName)) } - addStatement( - "%L.encodeWithTag(writer, %L, value.%L)", - adapterFor(field), - field.tag, - fieldName - ) + if (field.useArray && reverse) { + beginControlFlow("if (value.%L.isNotEmpty())", field.name) + addStatement("val byteCountBefore%L = writer.byteCount", field.tag) + beginControlFlow("for (i in (value.%L.size - 1) downTo 0)", field.name) + addStatement("%L.encodePrimitive(writer, value.%L[i])", field.getAdapterName(), field.name) + endControlFlow() + addStatement("writer.writeVarint32(writer.byteCount - byteCountBefore${field.tag})") + addStatement("writer.writeTag(%L, FieldEncoding.LENGTH_DELIMITED)", field.tag) + endControlFlow() + } else { + addStatement( + "%L.encodeWithTag(writer, %L, value.%L)", + adapterFor(field), + field.tag, + fieldName + ) + } } } for (boxOneOf in message.boxOneOfs()) { @@ -1679,7 +1698,7 @@ class KotlinGenerator private constructor( if (fieldOrOneOf.isPacked && fieldOrOneOf.isScalar) { if (fieldOrOneOf.useArray) { - addStatement("%1L = %1L?.getTruncatedArray() ?: %2T(0),", fieldName, fieldOrOneOf.primitiveArrayForType) + addStatement("%1L = %1L?.getTruncatedArray() ?: %2L,", fieldName, fieldOrOneOf.emptyPrimitiveArrayForType) } else { addStatement("%1L = %1L ?: listOf(),", fieldName) } @@ -1767,11 +1786,15 @@ class KotlinGenerator private constructor( } private fun decodeAndAssign(field: Field, fieldName: String, adapterName: CodeBlock): CodeBlock { - val decode = CodeBlock.of("%L.decode(reader)", adapterName) + val decode = CodeBlock.of( + "%L.%L(reader)", + adapterName, + if (field.useArray) "decodePrimitive" else "decode", + ) return CodeBlock.of( when { field.isPacked && field.isScalar -> { - val type = if (field.useArray) field.arrayListForType.simpleName else "ArrayList" + val type = if (field.useArray) field.arrayListClassForType.simpleName else "ArrayList" buildCodeBlock { beginControlFlow("if (%L == null)", fieldName) addStatement("val minimumByteSize = ${field.getMinimumByteSize()}") @@ -1860,7 +1883,7 @@ class KotlinGenerator private constructor( private fun Field.redact(fieldName: String): CodeBlock? { if (isRedacted) { return when { - useArray -> CodeBlock.of("%T(0)", primitiveArrayForType) + useArray -> emptyPrimitiveArrayForType isRepeated -> CodeBlock.of("emptyList()") isMap -> CodeBlock.of("emptyMap()") encodeMode!! == EncodeMode.NULL_IF_ABSENT -> CodeBlock.of("null") @@ -2251,7 +2274,7 @@ class KotlinGenerator private constructor( } private fun Field.getDeclaration(allocatedName: String) = when { - useArray -> CodeBlock.of("var %N: %T? = null", allocatedName, arrayListForType) + useArray -> CodeBlock.of("var %N: %T? = null", allocatedName, arrayListClassForType) isPacked && isScalar -> CodeBlock.of("var %N: MutableList<%T>? = null", allocatedName, type!!.typeName) isRepeated -> CodeBlock.of("val $allocatedName = mutableListOf<%T>()", type!!.typeName) isMap -> CodeBlock.of( @@ -2285,7 +2308,7 @@ class KotlinGenerator private constructor( EncodeMode.REPEATED -> List::class.asClassName().parameterizedBy(baseClass) EncodeMode.PACKED -> { if (useArray) { - primitiveArrayForType.asTypeName() + primitiveArrayClassForType.asTypeName() } else { List::class.asTypeName().parameterizedBy(baseClass) } @@ -2309,7 +2332,7 @@ class KotlinGenerator private constructor( Map::class.asTypeName().parameterizedBy(keyType.typeName, valueType.typeName) EncodeMode.PACKED -> { when { - useArray -> primitiveArrayForType.asTypeName() + useArray -> primitiveArrayClassForType.asTypeName() else -> List::class.asTypeName().parameterizedBy(type.typeName) } } @@ -2334,7 +2357,7 @@ class KotlinGenerator private constructor( EncodeMode.REPEATED -> CodeBlock.of("emptyList()") EncodeMode.PACKED -> { if (useArray) { - CodeBlock.of("%T(0)", primitiveArrayForType) + emptyPrimitiveArrayForType } else { CodeBlock.of("emptyList()") } 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 9ed8c80633..dbf2d5eeea 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 @@ -1513,7 +1513,7 @@ class KotlinGeneratorTest { assertTrue(code.contains("import wire_package.Person")) } - @Test fun jeffTest() { + @Test fun useArrayUsesTheCorrectType() { val schema = buildSchema { add( "proto_package/person.proto".toPath(), @@ -1522,14 +1522,14 @@ class KotlinGeneratorTest { |import "wire/extensions.proto"; | |message Person { - | repeated float name = 1 [packed = true, wire.use_array = true]; + | repeated float info = 1 [packed = true, wire.use_array = true]; |} |""".trimMargin() ) } val code = KotlinWithProfilesGenerator(schema).generateKotlin("proto_package.Person") - assertContains(code, "public val name: FloatArray = FloatArray(0)") - assertContains(code, "ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 1, value.name)") + assertContains(code, "public val info: FloatArray = floatArrayOf()") + assertContains(code, "ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 1, value.info)") } @Test fun documentationEscapesBrackets() { diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt index ab036453b1..05089c58a7 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -184,33 +184,33 @@ expect abstract class ProtoAdapter( ): ProtoAdapter> @JvmField val BOOL: ProtoAdapter - @JvmField val INT32: ProtoAdapter + @JvmField val INT32: IntProtoAdapter @JvmField val INT32_ARRAY: ProtoAdapter - @JvmField val UINT32: ProtoAdapter + @JvmField val UINT32: IntProtoAdapter @JvmField val UINT32_ARRAY: ProtoAdapter - @JvmField val SINT32: ProtoAdapter + @JvmField val SINT32: IntProtoAdapter @JvmField val SINT32_ARRAY: ProtoAdapter - @JvmField val FIXED32: ProtoAdapter + @JvmField val FIXED32: IntProtoAdapter @JvmField val FIXED32_ARRAY: ProtoAdapter - @JvmField val SFIXED32: ProtoAdapter + @JvmField val SFIXED32: IntProtoAdapter @JvmField val SFIXED32_ARRAY: ProtoAdapter - @JvmField val INT64: ProtoAdapter + @JvmField val INT64: LongProtoAdapter @JvmField val INT64_ARRAY: ProtoAdapter /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. */ - @JvmField val UINT64: ProtoAdapter + @JvmField val UINT64: LongProtoAdapter @JvmField val UINT64_ARRAY: ProtoAdapter - @JvmField val SINT64: ProtoAdapter + @JvmField val SINT64: LongProtoAdapter @JvmField val SINT64_ARRAY: ProtoAdapter - @JvmField val FIXED64: ProtoAdapter + @JvmField val FIXED64: LongProtoAdapter @JvmField val FIXED64_ARRAY: ProtoAdapter - @JvmField val SFIXED64: ProtoAdapter + @JvmField val SFIXED64: LongProtoAdapter @JvmField val SFIXED64_ARRAY: ProtoAdapter @JvmField val FLOAT: FloatProtoAdapter @JvmField val FLOAT_ARRAY: ProtoAdapter - @JvmField val DOUBLE: ProtoAdapter + @JvmField val DOUBLE: DoubleProtoAdapter @JvmField val DOUBLE_ARRAY: ProtoAdapter @JvmField val BYTES: ProtoAdapter @JvmField val STRING: ProtoAdapter @@ -451,7 +451,7 @@ internal class RepeatedProtoAdapter( override fun redact(value: List): List = emptyList() } -class DoubleArrayProtoAdapter( +internal class DoubleArrayProtoAdapter( private val originalAdapter: ProtoAdapter, ) : ProtoAdapter( LENGTH_DELIMITED, @@ -501,12 +501,12 @@ class DoubleArrayProtoAdapter( } @Throws(IOException::class) - override fun decode(reader: ProtoReader): DoubleArray = DoubleArray(1) { originalAdapter.decode(reader) } + override fun decode(reader: ProtoReader): DoubleArray = doubleArrayOf(originalAdapter.decode(reader)) - override fun redact(value: DoubleArray): DoubleArray = DoubleArray(0) + override fun redact(value: DoubleArray): DoubleArray = doubleArrayOf() } -class LongArrayProtoAdapter( +internal class LongArrayProtoAdapter( private val originalAdapter: ProtoAdapter, ) : ProtoAdapter( LENGTH_DELIMITED, @@ -556,12 +556,12 @@ class LongArrayProtoAdapter( } @Throws(IOException::class) - override fun decode(reader: ProtoReader): LongArray = LongArray(1) { originalAdapter.decode(reader) } + override fun decode(reader: ProtoReader): LongArray = longArrayOf(originalAdapter.decode(reader)) - override fun redact(value: LongArray): LongArray = LongArray(0) + override fun redact(value: LongArray): LongArray = longArrayOf() } -class FloatArrayProtoAdapter( +internal class FloatArrayProtoAdapter( private val originalAdapter: FloatProtoAdapter, ) : ProtoAdapter( LENGTH_DELIMITED, @@ -599,7 +599,7 @@ class FloatArrayProtoAdapter( @Throws(IOException::class) override fun encode(writer: ProtoWriter, value: FloatArray) { for (i in 0 until value.size) { - originalAdapter.encodePrimitive(writer, value[i]) + originalAdapter.encode(writer, value[i]) } } @@ -611,12 +611,12 @@ class FloatArrayProtoAdapter( } @Throws(IOException::class) - override fun decode(reader: ProtoReader): FloatArray = FloatArray(1) { originalAdapter.decodePrimitive(reader) } + override fun decode(reader: ProtoReader): FloatArray = floatArrayOf(originalAdapter.decodePrimitive(reader)) - override fun redact(value: FloatArray): FloatArray = FloatArray(0) + override fun redact(value: FloatArray): FloatArray = floatArrayOf() } -class IntArrayProtoAdapter( +internal class IntArrayProtoAdapter( private val originalAdapter: ProtoAdapter, ) : ProtoAdapter( LENGTH_DELIMITED, @@ -666,9 +666,9 @@ class IntArrayProtoAdapter( } @Throws(IOException::class) - override fun decode(reader: ProtoReader): IntArray = IntArray(1) { originalAdapter.decode(reader) } + override fun decode(reader: ProtoReader): IntArray = intArrayOf(originalAdapter.decode(reader)) - override fun redact(value: IntArray): IntArray = IntArray(0) + override fun redact(value: IntArray): IntArray = intArrayOf() } internal class MapProtoAdapter internal constructor( @@ -822,13 +822,21 @@ internal fun commonBool(): ProtoAdapter = object : ProtoAdapter = object : ProtoAdapter( - VARINT, +abstract class IntProtoAdapter(fieldEncoding: FieldEncoding) : ProtoAdapter( + fieldEncoding, Int::class, null, Syntax.PROTO_2, - 0 + 0, ) { + @Throws(IOException::class) + abstract fun encodePrimitive(writer: ReverseProtoWriter, value: Int) + + @Throws(IOException::class) + abstract fun decodePrimitive(reader: ProtoReader): Int +} + +internal fun commonInt32(): IntProtoAdapter = object : IntProtoAdapter(VARINT) { override fun encodedSize(value: Int): Int = int32Size(value) @Throws(IOException::class) @@ -837,23 +845,25 @@ internal fun commonInt32(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Int) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Int) { writer.writeSignedVarint32(value) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Int = reader.readVarint32() + override fun encode(writer: ReverseProtoWriter, value: Int) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Int = reader.readVarint32() + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Int = decodePrimitive(reader) override fun redact(value: Int): Int = throw UnsupportedOperationException() } -internal fun commonUint32(): ProtoAdapter = object : ProtoAdapter( - VARINT, - Int::class, - null, - Syntax.PROTO_2, - 0 -) { +internal fun commonUint32(): IntProtoAdapter = object : IntProtoAdapter(VARINT) { override fun encodedSize(value: Int): Int = varint32Size(value) @Throws(IOException::class) @@ -862,23 +872,25 @@ internal fun commonUint32(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Int) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Int) { writer.writeVarint32(value) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Int = reader.readVarint32() + override fun encode(writer: ReverseProtoWriter, value: Int) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Int = reader.readVarint32() + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Int = decodePrimitive(reader) override fun redact(value: Int): Int = throw UnsupportedOperationException() } -internal fun commonSint32(): ProtoAdapter = object : ProtoAdapter( - VARINT, - Int::class, - null, - Syntax.PROTO_2, - 0 -) { +internal fun commonSint32(): IntProtoAdapter = object : IntProtoAdapter(VARINT) { override fun encodedSize(value: Int): Int = varint32Size(encodeZigZag32(value)) @Throws(IOException::class) @@ -887,23 +899,25 @@ internal fun commonSint32(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Int) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Int) { writer.writeVarint32(encodeZigZag32(value)) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Int = decodeZigZag32(reader.readVarint32()) + override fun encode(writer: ReverseProtoWriter, value: Int) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Int = decodeZigZag32(reader.readVarint32()) + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Int = decodePrimitive(reader) override fun redact(value: Int): Int = throw UnsupportedOperationException() } -internal fun commonFixed32(): ProtoAdapter = object : ProtoAdapter( - FieldEncoding.FIXED32, - Int::class, - null, - Syntax.PROTO_2, - 0 -) { +internal fun commonFixed32(): IntProtoAdapter = object : IntProtoAdapter(FieldEncoding.FIXED32) { override fun encodedSize(value: Int): Int = FIXED_32_SIZE @Throws(IOException::class) @@ -912,24 +926,40 @@ internal fun commonFixed32(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Int) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Int) { writer.writeFixed32(value) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Int = reader.readFixed32() + override fun encode(writer: ReverseProtoWriter, value: Int) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Int = reader.readFixed32() + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Int = decodePrimitive(reader) override fun redact(value: Int): Int = throw UnsupportedOperationException() } -internal fun commonSfixed32() = commonFixed32() -internal fun commonInt64(): ProtoAdapter = object : ProtoAdapter( - VARINT, +abstract class LongProtoAdapter(fieldEncoding: FieldEncoding) : ProtoAdapter( + fieldEncoding, Long::class, null, Syntax.PROTO_2, - 0L + 0L, ) { + @Throws(IOException::class) + abstract fun encodePrimitive(writer: ReverseProtoWriter, value: Long) + + @Throws(IOException::class) + abstract fun decodePrimitive(reader: ProtoReader): Long +} + +internal fun commonSfixed32() = commonFixed32() +internal fun commonInt64(): LongProtoAdapter = object : LongProtoAdapter(VARINT) { override fun encodedSize(value: Long): Int = varint64Size(value) @Throws(IOException::class) @@ -938,12 +968,20 @@ internal fun commonInt64(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Long) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Long) { writer.writeVarint64(value) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Long = reader.readVarint64() + override fun encode(writer: ReverseProtoWriter, value: Long) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Long = reader.readVarint64() + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Long = decodePrimitive(reader) override fun redact(value: Long): Long = throw UnsupportedOperationException() } @@ -952,13 +990,7 @@ internal fun commonInt64(): ProtoAdapter = object : ProtoAdapter( * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. */ -internal fun commonUint64(): ProtoAdapter = object : ProtoAdapter( - VARINT, - Long::class, - null, - Syntax.PROTO_2, - 0L -) { +internal fun commonUint64(): LongProtoAdapter = object : LongProtoAdapter(VARINT) { override fun encodedSize(value: Long): Int = varint64Size(value) @Throws(IOException::class) @@ -967,23 +999,25 @@ internal fun commonUint64(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Long) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Long) { writer.writeVarint64(value) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Long = reader.readVarint64() + override fun encode(writer: ReverseProtoWriter, value: Long) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Long = reader.readVarint64() + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Long = decodePrimitive(reader) override fun redact(value: Long): Long = throw UnsupportedOperationException() } -internal fun commonSint64(): ProtoAdapter = object : ProtoAdapter( - VARINT, - Long::class, - null, - Syntax.PROTO_2, - 0L -) { +internal fun commonSint64(): LongProtoAdapter = object: LongProtoAdapter(VARINT) { override fun encodedSize(value: Long): Int = varint64Size(encodeZigZag64(value)) @Throws(IOException::class) @@ -992,23 +1026,25 @@ internal fun commonSint64(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Long) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Long) { writer.writeVarint64(encodeZigZag64(value)) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Long = decodeZigZag64(reader.readVarint64()) + override fun encode(writer: ReverseProtoWriter, value: Long) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Long = decodeZigZag64(reader.readVarint64()) + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Long = decodePrimitive(reader) override fun redact(value: Long): Long = throw UnsupportedOperationException() } -internal fun commonFixed64(): ProtoAdapter = object : ProtoAdapter( - FieldEncoding.FIXED64, - Long::class, - null, - Syntax.PROTO_2, - 0L -) { +internal fun commonFixed64(): LongProtoAdapter = object : LongProtoAdapter(FieldEncoding.FIXED64) { override fun encodedSize(value: Long): Int = FIXED_64_SIZE @Throws(IOException::class) @@ -1017,12 +1053,20 @@ internal fun commonFixed64(): ProtoAdapter = object : ProtoAdapter( } @Throws(IOException::class) - override fun encode(writer: ReverseProtoWriter, value: Long) { + override fun encodePrimitive(writer: ReverseProtoWriter, value: Long) { writer.writeFixed64(value) } @Throws(IOException::class) - override fun decode(reader: ProtoReader): Long = reader.readFixed64() + override fun encode(writer: ReverseProtoWriter, value: Long) { + encodePrimitive(writer, value) + } + + @Throws(IOException::class) + override fun decodePrimitive(reader: ProtoReader): Long = reader.readFixed64() + + @Throws(IOException::class) + override fun decode(reader: ProtoReader): Long = decodePrimitive(reader) override fun redact(value: Long): Long = throw UnsupportedOperationException() } @@ -1034,14 +1078,9 @@ class FloatProtoAdapter : ProtoAdapter( Syntax.PROTO_2, 0.0f ) { - @Throws(IOException::class) - fun encodePrimitive(writer: ProtoWriter, value: Float) { - writer.writeFixed32(value.toBits()) - } - @Throws(IOException::class) override fun encode(writer: ProtoWriter, value: Float) { - encodePrimitive(writer, value) + writer.writeFixed32(value.toBits()) } @Throws(IOException::class) @@ -1064,8 +1103,6 @@ class FloatProtoAdapter : ProtoAdapter( return decodePrimitive(reader) } - fun encodedSizePrimitive(): Int = FIXED_32_SIZE - override fun encodedSize(value: Float): Int = FIXED_32_SIZE override fun redact(value: Float): Float = throw UnsupportedOperationException() @@ -1074,7 +1111,7 @@ class FloatProtoAdapter : ProtoAdapter( internal fun commonSfixed64() = commonFixed64() internal fun commonFloat(): FloatProtoAdapter = FloatProtoAdapter() -internal fun commonDouble(): ProtoAdapter = object : ProtoAdapter( +class DoubleProtoAdapter : ProtoAdapter( FieldEncoding.FIXED64, Double::class, null, @@ -1089,18 +1126,28 @@ internal fun commonDouble(): ProtoAdapter = object : ProtoAdapter = object : ProtoAdapter( LENGTH_DELIMITED, String::class, diff --git a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 9bba896c4b..1f03b372c1 100644 --- a/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jsMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -161,33 +161,33 @@ actual abstract class ProtoAdapter actual constructor( actual val BOOL: ProtoAdapter = commonBool() - actual val INT32: ProtoAdapter = commonInt32() + actual val INT32: IntProtoAdapter = commonInt32() actual val INT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(INT32) - actual val UINT32: ProtoAdapter = commonUint32() + actual val UINT32: IntProtoAdapter = commonUint32() actual val UINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(UINT32) - actual val SINT32: ProtoAdapter = commonSint32() + actual val SINT32: IntProtoAdapter = commonSint32() actual val SINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SINT32) - actual val FIXED32: ProtoAdapter = commonFixed32() + actual val FIXED32: IntProtoAdapter = commonFixed32() actual val FIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(FIXED32) - actual val SFIXED32: ProtoAdapter = commonSfixed32() + actual val SFIXED32: IntProtoAdapter = commonSfixed32() actual val SFIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SFIXED32) - actual val INT64: ProtoAdapter = commonInt64() + actual val INT64: LongProtoAdapter = commonInt64() actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. */ - actual val UINT64: ProtoAdapter = commonUint64() + actual val UINT64: LongProtoAdapter= commonUint64() actual val UINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(UINT64) - actual val SINT64: ProtoAdapter = commonSint64() + actual val SINT64: LongProtoAdapter = commonSint64() actual val SINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SINT64) - actual val FIXED64: ProtoAdapter = commonFixed64() + actual val FIXED64: LongProtoAdapter = commonFixed64() actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) - actual val SFIXED64: ProtoAdapter = commonSfixed64() + actual val SFIXED64: LongProtoAdapter = commonSfixed64() actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) actual val FLOAT: FloatProtoAdapter = commonFloat() actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) - actual val DOUBLE: ProtoAdapter = commonDouble() + actual val DOUBLE: DoubleProtoAdapter = commonDouble() actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() diff --git a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt index 0fd3f6efe0..7dcff13fea 100644 --- a/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/jvmMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -284,29 +284,29 @@ actual abstract class ProtoAdapter actual constructor( } @JvmField actual val BOOL: ProtoAdapter = commonBool() - @JvmField actual val INT32: ProtoAdapter = commonInt32() + @JvmField actual val INT32: IntProtoAdapter = commonInt32() @JvmField actual val INT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(INT32) - @JvmField actual val UINT32: ProtoAdapter = commonUint32() + @JvmField actual val UINT32: IntProtoAdapter = commonUint32() @JvmField actual val UINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(UINT32) - @JvmField actual val SINT32: ProtoAdapter = commonSint32() + @JvmField actual val SINT32: IntProtoAdapter = commonSint32() @JvmField actual val SINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SINT32) - @JvmField actual val FIXED32: ProtoAdapter = commonFixed32() + @JvmField actual val FIXED32: IntProtoAdapter = commonFixed32() @JvmField actual val FIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(FIXED32) - @JvmField actual val SFIXED32: ProtoAdapter = commonSfixed32() + @JvmField actual val SFIXED32: IntProtoAdapter = commonSfixed32() @JvmField actual val SFIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SFIXED32) - @JvmField actual val INT64: ProtoAdapter = commonInt64() + @JvmField actual val INT64: LongProtoAdapter = commonInt64() @JvmField actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) - @JvmField actual val UINT64: ProtoAdapter = commonUint64() + @JvmField actual val UINT64: LongProtoAdapter = commonUint64() @JvmField actual val UINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(UINT64) - @JvmField actual val SINT64: ProtoAdapter = commonSint64() + @JvmField actual val SINT64: LongProtoAdapter = commonSint64() @JvmField actual val SINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SINT64) - @JvmField actual val FIXED64: ProtoAdapter = commonFixed64() + @JvmField actual val FIXED64: LongProtoAdapter = commonFixed64() @JvmField actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) - @JvmField actual val SFIXED64: ProtoAdapter = commonSfixed64() + @JvmField actual val SFIXED64: LongProtoAdapter = commonSfixed64() @JvmField actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) @JvmField actual val FLOAT: FloatProtoAdapter = commonFloat() @JvmField actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) - @JvmField actual val DOUBLE: ProtoAdapter = commonDouble() + @JvmField actual val DOUBLE: DoubleProtoAdapter = commonDouble() @JvmField actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) @JvmField actual val BYTES: ProtoAdapter = commonBytes() @JvmField actual val STRING: ProtoAdapter = commonString() diff --git a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt index cddcbd4603..1b7ee3b725 100644 --- a/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt +++ b/wire-runtime/src/nativeMain/kotlin/com/squareup/wire/ProtoAdapter.kt @@ -160,33 +160,33 @@ actual abstract class ProtoAdapter actual constructor( } actual val BOOL: ProtoAdapter = commonBool() - actual val INT32: ProtoAdapter = commonInt32() + actual val INT32: IntProtoAdapter = commonInt32() actual val INT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(INT32) - actual val UINT32: ProtoAdapter = commonUint32() + actual val UINT32: IntProtoAdapter = commonUint32() actual val UINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(UINT32) - actual val SINT32: ProtoAdapter = commonSint32() + actual val SINT32: IntProtoAdapter = commonSint32() actual val SINT32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SINT32) - actual val FIXED32: ProtoAdapter = commonFixed32() + actual val FIXED32: IntProtoAdapter = commonFixed32() actual val FIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(FIXED32) - actual val SFIXED32: ProtoAdapter = commonSfixed32() + actual val SFIXED32: IntProtoAdapter = commonSfixed32() actual val SFIXED32_ARRAY: ProtoAdapter = IntArrayProtoAdapter(SFIXED32) - actual val INT64: ProtoAdapter = commonInt64() + actual val INT64: LongProtoAdapter = commonInt64() actual val INT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(INT64) /** * Like INT64, but negative longs are interpreted as large positive values, and encoded that way * in JSON. */ - actual val UINT64: ProtoAdapter = commonUint64() + actual val UINT64: LongProtoAdapter = commonUint64() actual val UINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(UINT64) - actual val SINT64: ProtoAdapter = commonSint64() + actual val SINT64: LongProtoAdapter = commonSint64() actual val SINT64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SINT64) - actual val FIXED64: ProtoAdapter = commonFixed64() + actual val FIXED64: LongProtoAdapter = commonFixed64() actual val FIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(FIXED64) - actual val SFIXED64: ProtoAdapter = commonSfixed64() + actual val SFIXED64: LongProtoAdapter = commonSfixed64() actual val SFIXED64_ARRAY: ProtoAdapter = LongArrayProtoAdapter(SFIXED64) actual val FLOAT: FloatProtoAdapter = commonFloat() actual val FLOAT_ARRAY: ProtoAdapter = FloatArrayProtoAdapter(FLOAT) - actual val DOUBLE: ProtoAdapter = commonDouble() + actual val DOUBLE: DoubleProtoAdapter = commonDouble() actual val DOUBLE_ARRAY: ProtoAdapter = DoubleArrayProtoAdapter(DOUBLE) actual val BYTES: ProtoAdapter = commonBytes() actual val STRING: ProtoAdapter = commonString() diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 7511c4b848..682b72d3e1 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -353,73 +353,73 @@ public class AllTypes( adapter = "com.squareup.wire.ProtoAdapter#INT32", label = WireField.Label.PACKED, ) - public val array_int32: IntArray = IntArray(0), + public val array_int32: IntArray = intArrayOf(), @field:WireField( tag = 602, adapter = "com.squareup.wire.ProtoAdapter#UINT32", label = WireField.Label.PACKED, ) - public val array_uint32: IntArray = IntArray(0), + public val array_uint32: IntArray = intArrayOf(), @field:WireField( tag = 603, adapter = "com.squareup.wire.ProtoAdapter#SINT32", label = WireField.Label.PACKED, ) - public val array_sint32: IntArray = IntArray(0), + public val array_sint32: IntArray = intArrayOf(), @field:WireField( tag = 604, adapter = "com.squareup.wire.ProtoAdapter#FIXED32", label = WireField.Label.PACKED, ) - public val array_fixed32: IntArray = IntArray(0), + public val array_fixed32: IntArray = intArrayOf(), @field:WireField( tag = 605, adapter = "com.squareup.wire.ProtoAdapter#SFIXED32", label = WireField.Label.PACKED, ) - public val array_sfixed32: IntArray = IntArray(0), + public val array_sfixed32: IntArray = intArrayOf(), @field:WireField( tag = 606, adapter = "com.squareup.wire.ProtoAdapter#INT64", label = WireField.Label.PACKED, ) - public val array_int64: LongArray = LongArray(0), + public val array_int64: LongArray = longArrayOf(), @field:WireField( tag = 607, adapter = "com.squareup.wire.ProtoAdapter#UINT64", label = WireField.Label.PACKED, ) - public val array_uint64: LongArray = LongArray(0), + public val array_uint64: LongArray = longArrayOf(), @field:WireField( tag = 608, adapter = "com.squareup.wire.ProtoAdapter#SINT64", label = WireField.Label.PACKED, ) - public val array_sint64: LongArray = LongArray(0), + public val array_sint64: LongArray = longArrayOf(), @field:WireField( tag = 609, adapter = "com.squareup.wire.ProtoAdapter#FIXED64", label = WireField.Label.PACKED, ) - public val array_fixed64: LongArray = LongArray(0), + public val array_fixed64: LongArray = longArrayOf(), @field:WireField( tag = 610, adapter = "com.squareup.wire.ProtoAdapter#SFIXED64", label = WireField.Label.PACKED, ) - public val array_sfixed64: LongArray = LongArray(0), + public val array_sfixed64: LongArray = longArrayOf(), @field:WireField( tag = 611, adapter = "com.squareup.wire.ProtoAdapter#FLOAT", label = WireField.Label.PACKED, ) - public val array_float: FloatArray = FloatArray(0), + public val array_float: FloatArray = floatArrayOf(), @field:WireField( tag = 612, adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", label = WireField.Label.PACKED, ) - public val array_double: DoubleArray = DoubleArray(0), + public val array_double: DoubleArray = doubleArrayOf(), /** * Extension source: all_types.proto */ @@ -2201,18 +2201,102 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - ProtoAdapter.DOUBLE_ARRAY.encodeWithTag(writer, 612, value.array_double) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 611, value.array_float) - ProtoAdapter.SFIXED64_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) - ProtoAdapter.FIXED64_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) - ProtoAdapter.SINT64_ARRAY.encodeWithTag(writer, 608, value.array_sint64) - ProtoAdapter.UINT64_ARRAY.encodeWithTag(writer, 607, value.array_uint64) - ProtoAdapter.INT64_ARRAY.encodeWithTag(writer, 606, value.array_int64) - ProtoAdapter.SFIXED32_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) - ProtoAdapter.FIXED32_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) - ProtoAdapter.SINT32_ARRAY.encodeWithTag(writer, 603, value.array_sint32) - ProtoAdapter.UINT32_ARRAY.encodeWithTag(writer, 602, value.array_uint32) - ProtoAdapter.INT32_ARRAY.encodeWithTag(writer, 601, value.array_int32) + if (value.array_double.isNotEmpty()) { + val byteCountBefore612 = writer.byteCount + for (i in (value.array_double.size - 1) downTo 0) { + ProtoAdapter.DOUBLE.encodePrimitive(writer, value.array_double[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore612) + writer.writeTag(612, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_float.isNotEmpty()) { + val byteCountBefore611 = writer.byteCount + for (i in (value.array_float.size - 1) downTo 0) { + ProtoAdapter.FLOAT.encodePrimitive(writer, value.array_float[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore611) + writer.writeTag(611, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sfixed64.isNotEmpty()) { + val byteCountBefore610 = writer.byteCount + for (i in (value.array_sfixed64.size - 1) downTo 0) { + ProtoAdapter.SFIXED64.encodePrimitive(writer, value.array_sfixed64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore610) + writer.writeTag(610, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_fixed64.isNotEmpty()) { + val byteCountBefore609 = writer.byteCount + for (i in (value.array_fixed64.size - 1) downTo 0) { + ProtoAdapter.FIXED64.encodePrimitive(writer, value.array_fixed64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore609) + writer.writeTag(609, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sint64.isNotEmpty()) { + val byteCountBefore608 = writer.byteCount + for (i in (value.array_sint64.size - 1) downTo 0) { + ProtoAdapter.SINT64.encodePrimitive(writer, value.array_sint64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore608) + writer.writeTag(608, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_uint64.isNotEmpty()) { + val byteCountBefore607 = writer.byteCount + for (i in (value.array_uint64.size - 1) downTo 0) { + ProtoAdapter.UINT64.encodePrimitive(writer, value.array_uint64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore607) + writer.writeTag(607, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_int64.isNotEmpty()) { + val byteCountBefore606 = writer.byteCount + for (i in (value.array_int64.size - 1) downTo 0) { + ProtoAdapter.INT64.encodePrimitive(writer, value.array_int64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore606) + writer.writeTag(606, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sfixed32.isNotEmpty()) { + val byteCountBefore605 = writer.byteCount + for (i in (value.array_sfixed32.size - 1) downTo 0) { + ProtoAdapter.SFIXED32.encodePrimitive(writer, value.array_sfixed32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore605) + writer.writeTag(605, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_fixed32.isNotEmpty()) { + val byteCountBefore604 = writer.byteCount + for (i in (value.array_fixed32.size - 1) downTo 0) { + ProtoAdapter.FIXED32.encodePrimitive(writer, value.array_fixed32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore604) + writer.writeTag(604, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sint32.isNotEmpty()) { + val byteCountBefore603 = writer.byteCount + for (i in (value.array_sint32.size - 1) downTo 0) { + ProtoAdapter.SINT32.encodePrimitive(writer, value.array_sint32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore603) + writer.writeTag(603, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_uint32.isNotEmpty()) { + val byteCountBefore602 = writer.byteCount + for (i in (value.array_uint32.size - 1) downTo 0) { + ProtoAdapter.UINT32.encodePrimitive(writer, value.array_uint32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore602) + writer.writeTag(602, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_int32.isNotEmpty()) { + val byteCountBefore601 = writer.byteCount + for (i in (value.array_int32.size - 1) downTo 0) { + ProtoAdapter.INT32.encodePrimitive(writer, value.array_int32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore601) + writer.writeTag(601, FieldEncoding.LENGTH_DELIMITED) + } map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -2678,7 +2762,7 @@ public class AllTypes( .toInt() array_int32 = IntArrayList(initialCapacity) } - array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decode(reader)) + array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decodePrimitive(reader)) } 602 -> { if (array_uint32 == null) { @@ -2688,7 +2772,7 @@ public class AllTypes( .toInt() array_uint32 = IntArrayList(initialCapacity) } - array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decode(reader)) + array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decodePrimitive(reader)) } 603 -> { if (array_sint32 == null) { @@ -2698,7 +2782,7 @@ public class AllTypes( .toInt() array_sint32 = IntArrayList(initialCapacity) } - array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decode(reader)) + array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decodePrimitive(reader)) } 604 -> { if (array_fixed32 == null) { @@ -2708,7 +2792,7 @@ public class AllTypes( .toInt() array_fixed32 = IntArrayList(initialCapacity) } - array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decode(reader)) + array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decodePrimitive(reader)) } 605 -> { if (array_sfixed32 == null) { @@ -2718,7 +2802,7 @@ public class AllTypes( .toInt() array_sfixed32 = IntArrayList(initialCapacity) } - array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decode(reader)) + array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decodePrimitive(reader)) } 606 -> { if (array_int64 == null) { @@ -2728,7 +2812,7 @@ public class AllTypes( .toInt() array_int64 = LongArrayList(initialCapacity) } - array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decode(reader)) + array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decodePrimitive(reader)) } 607 -> { if (array_uint64 == null) { @@ -2738,7 +2822,7 @@ public class AllTypes( .toInt() array_uint64 = LongArrayList(initialCapacity) } - array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decode(reader)) + array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decodePrimitive(reader)) } 608 -> { if (array_sint64 == null) { @@ -2748,7 +2832,7 @@ public class AllTypes( .toInt() array_sint64 = LongArrayList(initialCapacity) } - array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decode(reader)) + array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decodePrimitive(reader)) } 609 -> { if (array_fixed64 == null) { @@ -2758,7 +2842,7 @@ public class AllTypes( .toInt() array_fixed64 = LongArrayList(initialCapacity) } - array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decode(reader)) + array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decodePrimitive(reader)) } 610 -> { if (array_sfixed64 == null) { @@ -2768,7 +2852,7 @@ public class AllTypes( .toInt() array_sfixed64 = LongArrayList(initialCapacity) } - array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decode(reader)) + array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decodePrimitive(reader)) } 611 -> { if (array_float == null) { @@ -2778,7 +2862,7 @@ public class AllTypes( .toInt() array_float = FloatArrayList(initialCapacity) } - array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decodePrimitive(reader)) } 612 -> { if (array_double == null) { @@ -2788,7 +2872,7 @@ public class AllTypes( .toInt() array_double = DoubleArrayList(initialCapacity) } - array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decode(reader)) + array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decodePrimitive(reader)) } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) @@ -3058,18 +3142,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - array_int32 = array_int32?.getTruncatedArray() ?: IntArray(0), - array_uint32 = array_uint32?.getTruncatedArray() ?: IntArray(0), - array_sint32 = array_sint32?.getTruncatedArray() ?: IntArray(0), - array_fixed32 = array_fixed32?.getTruncatedArray() ?: IntArray(0), - array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: IntArray(0), - array_int64 = array_int64?.getTruncatedArray() ?: LongArray(0), - array_uint64 = array_uint64?.getTruncatedArray() ?: LongArray(0), - array_sint64 = array_sint64?.getTruncatedArray() ?: LongArray(0), - array_fixed64 = array_fixed64?.getTruncatedArray() ?: LongArray(0), - array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: LongArray(0), - array_float = array_float?.getTruncatedArray() ?: FloatArray(0), - array_double = array_double?.getTruncatedArray() ?: DoubleArray(0), + array_int32 = array_int32?.getTruncatedArray() ?: intArrayOf(), + array_uint32 = array_uint32?.getTruncatedArray() ?: intArrayOf(), + array_sint32 = array_sint32?.getTruncatedArray() ?: intArrayOf(), + array_fixed32 = array_fixed32?.getTruncatedArray() ?: intArrayOf(), + array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: intArrayOf(), + array_int64 = array_int64?.getTruncatedArray() ?: longArrayOf(), + array_uint64 = array_uint64?.getTruncatedArray() ?: longArrayOf(), + array_sint64 = array_sint64?.getTruncatedArray() ?: longArrayOf(), + array_fixed64 = array_fixed64?.getTruncatedArray() ?: longArrayOf(), + array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: longArrayOf(), + array_float = array_float?.getTruncatedArray() ?: floatArrayOf(), + array_double = array_double?.getTruncatedArray() ?: doubleArrayOf(), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 45341fbe04..979842dc3c 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -401,84 +401,84 @@ public class AllTypes( label = WireField.Label.PACKED, ) @JvmField - public val array_int32: IntArray = IntArray(0), + public val array_int32: IntArray = intArrayOf(), @field:WireField( tag = 602, adapter = "com.squareup.wire.ProtoAdapter#UINT32", label = WireField.Label.PACKED, ) @JvmField - public val array_uint32: IntArray = IntArray(0), + public val array_uint32: IntArray = intArrayOf(), @field:WireField( tag = 603, adapter = "com.squareup.wire.ProtoAdapter#SINT32", label = WireField.Label.PACKED, ) @JvmField - public val array_sint32: IntArray = IntArray(0), + public val array_sint32: IntArray = intArrayOf(), @field:WireField( tag = 604, adapter = "com.squareup.wire.ProtoAdapter#FIXED32", label = WireField.Label.PACKED, ) @JvmField - public val array_fixed32: IntArray = IntArray(0), + public val array_fixed32: IntArray = intArrayOf(), @field:WireField( tag = 605, adapter = "com.squareup.wire.ProtoAdapter#SFIXED32", label = WireField.Label.PACKED, ) @JvmField - public val array_sfixed32: IntArray = IntArray(0), + public val array_sfixed32: IntArray = intArrayOf(), @field:WireField( tag = 606, adapter = "com.squareup.wire.ProtoAdapter#INT64", label = WireField.Label.PACKED, ) @JvmField - public val array_int64: LongArray = LongArray(0), + public val array_int64: LongArray = longArrayOf(), @field:WireField( tag = 607, adapter = "com.squareup.wire.ProtoAdapter#UINT64", label = WireField.Label.PACKED, ) @JvmField - public val array_uint64: LongArray = LongArray(0), + public val array_uint64: LongArray = longArrayOf(), @field:WireField( tag = 608, adapter = "com.squareup.wire.ProtoAdapter#SINT64", label = WireField.Label.PACKED, ) @JvmField - public val array_sint64: LongArray = LongArray(0), + public val array_sint64: LongArray = longArrayOf(), @field:WireField( tag = 609, adapter = "com.squareup.wire.ProtoAdapter#FIXED64", label = WireField.Label.PACKED, ) @JvmField - public val array_fixed64: LongArray = LongArray(0), + public val array_fixed64: LongArray = longArrayOf(), @field:WireField( tag = 610, adapter = "com.squareup.wire.ProtoAdapter#SFIXED64", label = WireField.Label.PACKED, ) @JvmField - public val array_sfixed64: LongArray = LongArray(0), + public val array_sfixed64: LongArray = longArrayOf(), @field:WireField( tag = 611, adapter = "com.squareup.wire.ProtoAdapter#FLOAT", label = WireField.Label.PACKED, ) @JvmField - public val array_float: FloatArray = FloatArray(0), + public val array_float: FloatArray = floatArrayOf(), @field:WireField( tag = 612, adapter = "com.squareup.wire.ProtoAdapter#DOUBLE", label = WireField.Label.PACKED, ) @JvmField - public val array_double: DoubleArray = DoubleArray(0), + public val array_double: DoubleArray = doubleArrayOf(), /** * Extension source: all_types.proto */ @@ -2336,40 +2336,40 @@ public class AllTypes( public var map_string_enum: Map = emptyMap() @JvmField - public var array_int32: IntArray = IntArray(0) + public var array_int32: IntArray = intArrayOf() @JvmField - public var array_uint32: IntArray = IntArray(0) + public var array_uint32: IntArray = intArrayOf() @JvmField - public var array_sint32: IntArray = IntArray(0) + public var array_sint32: IntArray = intArrayOf() @JvmField - public var array_fixed32: IntArray = IntArray(0) + public var array_fixed32: IntArray = intArrayOf() @JvmField - public var array_sfixed32: IntArray = IntArray(0) + public var array_sfixed32: IntArray = intArrayOf() @JvmField - public var array_int64: LongArray = LongArray(0) + public var array_int64: LongArray = longArrayOf() @JvmField - public var array_uint64: LongArray = LongArray(0) + public var array_uint64: LongArray = longArrayOf() @JvmField - public var array_sint64: LongArray = LongArray(0) + public var array_sint64: LongArray = longArrayOf() @JvmField - public var array_fixed64: LongArray = LongArray(0) + public var array_fixed64: LongArray = longArrayOf() @JvmField - public var array_sfixed64: LongArray = LongArray(0) + public var array_sfixed64: LongArray = longArrayOf() @JvmField - public var array_float: FloatArray = FloatArray(0) + public var array_float: FloatArray = floatArrayOf() @JvmField - public var array_double: DoubleArray = DoubleArray(0) + public var array_double: DoubleArray = doubleArrayOf() @JvmField public var ext_opt_int32: Int? = null @@ -3862,18 +3862,102 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - ProtoAdapter.DOUBLE_ARRAY.encodeWithTag(writer, 612, value.array_double) - ProtoAdapter.FLOAT_ARRAY.encodeWithTag(writer, 611, value.array_float) - ProtoAdapter.SFIXED64_ARRAY.encodeWithTag(writer, 610, value.array_sfixed64) - ProtoAdapter.FIXED64_ARRAY.encodeWithTag(writer, 609, value.array_fixed64) - ProtoAdapter.SINT64_ARRAY.encodeWithTag(writer, 608, value.array_sint64) - ProtoAdapter.UINT64_ARRAY.encodeWithTag(writer, 607, value.array_uint64) - ProtoAdapter.INT64_ARRAY.encodeWithTag(writer, 606, value.array_int64) - ProtoAdapter.SFIXED32_ARRAY.encodeWithTag(writer, 605, value.array_sfixed32) - ProtoAdapter.FIXED32_ARRAY.encodeWithTag(writer, 604, value.array_fixed32) - ProtoAdapter.SINT32_ARRAY.encodeWithTag(writer, 603, value.array_sint32) - ProtoAdapter.UINT32_ARRAY.encodeWithTag(writer, 602, value.array_uint32) - ProtoAdapter.INT32_ARRAY.encodeWithTag(writer, 601, value.array_int32) + if (value.array_double.isNotEmpty()) { + val byteCountBefore612 = writer.byteCount + for (i in (value.array_double.size - 1) downTo 0) { + ProtoAdapter.DOUBLE.encodePrimitive(writer, value.array_double[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore612) + writer.writeTag(612, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_float.isNotEmpty()) { + val byteCountBefore611 = writer.byteCount + for (i in (value.array_float.size - 1) downTo 0) { + ProtoAdapter.FLOAT.encodePrimitive(writer, value.array_float[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore611) + writer.writeTag(611, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sfixed64.isNotEmpty()) { + val byteCountBefore610 = writer.byteCount + for (i in (value.array_sfixed64.size - 1) downTo 0) { + ProtoAdapter.SFIXED64.encodePrimitive(writer, value.array_sfixed64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore610) + writer.writeTag(610, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_fixed64.isNotEmpty()) { + val byteCountBefore609 = writer.byteCount + for (i in (value.array_fixed64.size - 1) downTo 0) { + ProtoAdapter.FIXED64.encodePrimitive(writer, value.array_fixed64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore609) + writer.writeTag(609, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sint64.isNotEmpty()) { + val byteCountBefore608 = writer.byteCount + for (i in (value.array_sint64.size - 1) downTo 0) { + ProtoAdapter.SINT64.encodePrimitive(writer, value.array_sint64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore608) + writer.writeTag(608, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_uint64.isNotEmpty()) { + val byteCountBefore607 = writer.byteCount + for (i in (value.array_uint64.size - 1) downTo 0) { + ProtoAdapter.UINT64.encodePrimitive(writer, value.array_uint64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore607) + writer.writeTag(607, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_int64.isNotEmpty()) { + val byteCountBefore606 = writer.byteCount + for (i in (value.array_int64.size - 1) downTo 0) { + ProtoAdapter.INT64.encodePrimitive(writer, value.array_int64[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore606) + writer.writeTag(606, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sfixed32.isNotEmpty()) { + val byteCountBefore605 = writer.byteCount + for (i in (value.array_sfixed32.size - 1) downTo 0) { + ProtoAdapter.SFIXED32.encodePrimitive(writer, value.array_sfixed32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore605) + writer.writeTag(605, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_fixed32.isNotEmpty()) { + val byteCountBefore604 = writer.byteCount + for (i in (value.array_fixed32.size - 1) downTo 0) { + ProtoAdapter.FIXED32.encodePrimitive(writer, value.array_fixed32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore604) + writer.writeTag(604, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_sint32.isNotEmpty()) { + val byteCountBefore603 = writer.byteCount + for (i in (value.array_sint32.size - 1) downTo 0) { + ProtoAdapter.SINT32.encodePrimitive(writer, value.array_sint32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore603) + writer.writeTag(603, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_uint32.isNotEmpty()) { + val byteCountBefore602 = writer.byteCount + for (i in (value.array_uint32.size - 1) downTo 0) { + ProtoAdapter.UINT32.encodePrimitive(writer, value.array_uint32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore602) + writer.writeTag(602, FieldEncoding.LENGTH_DELIMITED) + } + if (value.array_int32.isNotEmpty()) { + val byteCountBefore601 = writer.byteCount + for (i in (value.array_int32.size - 1) downTo 0) { + ProtoAdapter.INT32.encodePrimitive(writer, value.array_int32[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore601) + writer.writeTag(601, FieldEncoding.LENGTH_DELIMITED) + } map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) @@ -4339,7 +4423,7 @@ public class AllTypes( .toInt() array_int32 = IntArrayList(initialCapacity) } - array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decode(reader)) + array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decodePrimitive(reader)) } 602 -> { if (array_uint32 == null) { @@ -4349,7 +4433,7 @@ public class AllTypes( .toInt() array_uint32 = IntArrayList(initialCapacity) } - array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decode(reader)) + array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decodePrimitive(reader)) } 603 -> { if (array_sint32 == null) { @@ -4359,7 +4443,7 @@ public class AllTypes( .toInt() array_sint32 = IntArrayList(initialCapacity) } - array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decode(reader)) + array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decodePrimitive(reader)) } 604 -> { if (array_fixed32 == null) { @@ -4369,7 +4453,7 @@ public class AllTypes( .toInt() array_fixed32 = IntArrayList(initialCapacity) } - array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decode(reader)) + array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decodePrimitive(reader)) } 605 -> { if (array_sfixed32 == null) { @@ -4379,7 +4463,7 @@ public class AllTypes( .toInt() array_sfixed32 = IntArrayList(initialCapacity) } - array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decode(reader)) + array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decodePrimitive(reader)) } 606 -> { if (array_int64 == null) { @@ -4389,7 +4473,7 @@ public class AllTypes( .toInt() array_int64 = LongArrayList(initialCapacity) } - array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decode(reader)) + array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decodePrimitive(reader)) } 607 -> { if (array_uint64 == null) { @@ -4399,7 +4483,7 @@ public class AllTypes( .toInt() array_uint64 = LongArrayList(initialCapacity) } - array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decode(reader)) + array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decodePrimitive(reader)) } 608 -> { if (array_sint64 == null) { @@ -4409,7 +4493,7 @@ public class AllTypes( .toInt() array_sint64 = LongArrayList(initialCapacity) } - array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decode(reader)) + array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decodePrimitive(reader)) } 609 -> { if (array_fixed64 == null) { @@ -4419,7 +4503,7 @@ public class AllTypes( .toInt() array_fixed64 = LongArrayList(initialCapacity) } - array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decode(reader)) + array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decodePrimitive(reader)) } 610 -> { if (array_sfixed64 == null) { @@ -4429,7 +4513,7 @@ public class AllTypes( .toInt() array_sfixed64 = LongArrayList(initialCapacity) } - array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decode(reader)) + array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decodePrimitive(reader)) } 611 -> { if (array_float == null) { @@ -4439,7 +4523,7 @@ public class AllTypes( .toInt() array_float = FloatArrayList(initialCapacity) } - array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decode(reader)) + array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decodePrimitive(reader)) } 612 -> { if (array_double == null) { @@ -4449,7 +4533,7 @@ public class AllTypes( .toInt() array_double = DoubleArrayList(initialCapacity) } - array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decode(reader)) + array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decodePrimitive(reader)) } 1001 -> ext_opt_int32 = ProtoAdapter.INT32.decode(reader) 1002 -> ext_opt_uint32 = ProtoAdapter.UINT32.decode(reader) @@ -4719,18 +4803,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - array_int32 = array_int32?.getTruncatedArray() ?: IntArray(0), - array_uint32 = array_uint32?.getTruncatedArray() ?: IntArray(0), - array_sint32 = array_sint32?.getTruncatedArray() ?: IntArray(0), - array_fixed32 = array_fixed32?.getTruncatedArray() ?: IntArray(0), - array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: IntArray(0), - array_int64 = array_int64?.getTruncatedArray() ?: LongArray(0), - array_uint64 = array_uint64?.getTruncatedArray() ?: LongArray(0), - array_sint64 = array_sint64?.getTruncatedArray() ?: LongArray(0), - array_fixed64 = array_fixed64?.getTruncatedArray() ?: LongArray(0), - array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: LongArray(0), - array_float = array_float?.getTruncatedArray() ?: FloatArray(0), - array_double = array_double?.getTruncatedArray() ?: DoubleArray(0), + array_int32 = array_int32?.getTruncatedArray() ?: intArrayOf(), + array_uint32 = array_uint32?.getTruncatedArray() ?: intArrayOf(), + array_sint32 = array_sint32?.getTruncatedArray() ?: intArrayOf(), + array_fixed32 = array_fixed32?.getTruncatedArray() ?: intArrayOf(), + array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: intArrayOf(), + array_int64 = array_int64?.getTruncatedArray() ?: longArrayOf(), + array_uint64 = array_uint64?.getTruncatedArray() ?: longArrayOf(), + array_sint64 = array_sint64?.getTruncatedArray() ?: longArrayOf(), + array_fixed64 = array_fixed64?.getTruncatedArray() ?: longArrayOf(), + array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: longArrayOf(), + array_float = array_float?.getTruncatedArray() ?: floatArrayOf(), + array_double = array_double?.getTruncatedArray() ?: doubleArrayOf(), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, From 09ff0cf3ab539003dc452655b80e6559440dd757 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Mon, 17 Apr 2023 18:25:21 -0400 Subject: [PATCH 13/17] Reduce boilerplate --- .../squareup/wire/kotlin/KotlinGenerator.kt | 16 +-- .../com/squareup/wire/internal/Internal.kt | 70 +++++++++++ .../wire/protos/kotlin/alltypes/AllTypes.kt | 109 +++--------------- .../wire/protos/kotlin/alltypes/AllTypes.kt | 109 +++--------------- 4 files changed, 104 insertions(+), 200 deletions(-) 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 9e6daedae3..b141bb76f8 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 @@ -1603,14 +1603,14 @@ class KotlinGenerator private constructor( add(fieldEqualsIdentityBlock(field, fieldName)) } if (field.useArray && reverse) { - beginControlFlow("if (value.%L.isNotEmpty())", field.name) - addStatement("val byteCountBefore%L = writer.byteCount", field.tag) - beginControlFlow("for (i in (value.%L.size - 1) downTo 0)", field.name) - addStatement("%L.encodePrimitive(writer, value.%L[i])", field.getAdapterName(), field.name) - endControlFlow() - addStatement("writer.writeVarint32(writer.byteCount - byteCountBefore${field.tag})") - addStatement("writer.writeTag(%L, FieldEncoding.LENGTH_DELIMITED)", field.tag) - endControlFlow() + val encodeArray = MemberName("com.squareup.wire.internal", "encodeArray") + addStatement( + "%M(value.%L, %L, writer, %L)", + encodeArray, + fieldName, + field.getAdapterName(), + field.tag, + ) } else { addStatement( "%L.encodeWithTag(writer, %L, value.%L)", diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/Internal.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/Internal.kt index 20bb1f19f5..672f17db7b 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/Internal.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/Internal.kt @@ -18,7 +18,13 @@ package com.squareup.wire.internal +import com.squareup.wire.DoubleProtoAdapter +import com.squareup.wire.FieldEncoding +import com.squareup.wire.FloatProtoAdapter +import com.squareup.wire.IntProtoAdapter +import com.squareup.wire.LongProtoAdapter import com.squareup.wire.ProtoAdapter +import com.squareup.wire.ReverseProtoWriter import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName @@ -223,3 +229,67 @@ fun boxedOneOfKeyFieldName(oneOfName: String, fieldName: String): String { fun boxedOneOfKeysFieldName(oneOfName: String): String { return "${oneOfName}_keys".uppercase() } + +fun encodeArray( + array: LongArray, + protoAdapter: LongProtoAdapter, + writer: ReverseProtoWriter, + tag: Int, +) { + if (array.isNotEmpty()) { + val byteCountBefore = writer.byteCount + for (i in (array.size - 1) downTo 0) { + protoAdapter.encodePrimitive(writer, array[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore) + writer.writeTag(tag, FieldEncoding.LENGTH_DELIMITED) + } +} + +fun encodeArray( + array: IntArray, + protoAdapter: IntProtoAdapter, + writer: ReverseProtoWriter, + tag: Int, +) { + if (array.isNotEmpty()) { + val byteCountBefore = writer.byteCount + for (i in (array.size - 1) downTo 0) { + protoAdapter.encodePrimitive(writer, array[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore) + writer.writeTag(tag, FieldEncoding.LENGTH_DELIMITED) + } +} + +fun encodeArray( + array: DoubleArray, + protoAdapter: DoubleProtoAdapter, + writer: ReverseProtoWriter, + tag: Int, +) { + if (array.isNotEmpty()) { + val byteCountBefore = writer.byteCount + for (i in (array.size - 1) downTo 0) { + protoAdapter.encodePrimitive(writer, array[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore) + writer.writeTag(tag, FieldEncoding.LENGTH_DELIMITED) + } +} + +fun encodeArray( + array: FloatArray, + protoAdapter: FloatProtoAdapter, + writer: ReverseProtoWriter, + tag: Int, +) { + if (array.isNotEmpty()) { + val byteCountBefore = writer.byteCount + for (i in (array.size - 1) downTo 0) { + protoAdapter.encodePrimitive(writer, array[i]) + } + writer.writeVarint32(writer.byteCount - byteCountBefore) + writer.writeTag(tag, FieldEncoding.LENGTH_DELIMITED) + } +} diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 682b72d3e1..0905bdaf4c 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -17,6 +17,7 @@ import com.squareup.wire.Syntax import com.squareup.wire.Syntax.PROTO_2 import com.squareup.wire.WireEnum import com.squareup.wire.WireField +import com.squareup.wire.`internal`.encodeArray import com.squareup.wire.`internal`.immutableCopyOf import com.squareup.wire.`internal`.missingRequiredFields import com.squareup.wire.`internal`.redactElements @@ -2201,102 +2202,18 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - if (value.array_double.isNotEmpty()) { - val byteCountBefore612 = writer.byteCount - for (i in (value.array_double.size - 1) downTo 0) { - ProtoAdapter.DOUBLE.encodePrimitive(writer, value.array_double[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore612) - writer.writeTag(612, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_float.isNotEmpty()) { - val byteCountBefore611 = writer.byteCount - for (i in (value.array_float.size - 1) downTo 0) { - ProtoAdapter.FLOAT.encodePrimitive(writer, value.array_float[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore611) - writer.writeTag(611, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sfixed64.isNotEmpty()) { - val byteCountBefore610 = writer.byteCount - for (i in (value.array_sfixed64.size - 1) downTo 0) { - ProtoAdapter.SFIXED64.encodePrimitive(writer, value.array_sfixed64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore610) - writer.writeTag(610, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_fixed64.isNotEmpty()) { - val byteCountBefore609 = writer.byteCount - for (i in (value.array_fixed64.size - 1) downTo 0) { - ProtoAdapter.FIXED64.encodePrimitive(writer, value.array_fixed64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore609) - writer.writeTag(609, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sint64.isNotEmpty()) { - val byteCountBefore608 = writer.byteCount - for (i in (value.array_sint64.size - 1) downTo 0) { - ProtoAdapter.SINT64.encodePrimitive(writer, value.array_sint64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore608) - writer.writeTag(608, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_uint64.isNotEmpty()) { - val byteCountBefore607 = writer.byteCount - for (i in (value.array_uint64.size - 1) downTo 0) { - ProtoAdapter.UINT64.encodePrimitive(writer, value.array_uint64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore607) - writer.writeTag(607, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_int64.isNotEmpty()) { - val byteCountBefore606 = writer.byteCount - for (i in (value.array_int64.size - 1) downTo 0) { - ProtoAdapter.INT64.encodePrimitive(writer, value.array_int64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore606) - writer.writeTag(606, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sfixed32.isNotEmpty()) { - val byteCountBefore605 = writer.byteCount - for (i in (value.array_sfixed32.size - 1) downTo 0) { - ProtoAdapter.SFIXED32.encodePrimitive(writer, value.array_sfixed32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore605) - writer.writeTag(605, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_fixed32.isNotEmpty()) { - val byteCountBefore604 = writer.byteCount - for (i in (value.array_fixed32.size - 1) downTo 0) { - ProtoAdapter.FIXED32.encodePrimitive(writer, value.array_fixed32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore604) - writer.writeTag(604, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sint32.isNotEmpty()) { - val byteCountBefore603 = writer.byteCount - for (i in (value.array_sint32.size - 1) downTo 0) { - ProtoAdapter.SINT32.encodePrimitive(writer, value.array_sint32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore603) - writer.writeTag(603, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_uint32.isNotEmpty()) { - val byteCountBefore602 = writer.byteCount - for (i in (value.array_uint32.size - 1) downTo 0) { - ProtoAdapter.UINT32.encodePrimitive(writer, value.array_uint32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore602) - writer.writeTag(602, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_int32.isNotEmpty()) { - val byteCountBefore601 = writer.byteCount - for (i in (value.array_int32.size - 1) downTo 0) { - ProtoAdapter.INT32.encodePrimitive(writer, value.array_int32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore601) - writer.writeTag(601, FieldEncoding.LENGTH_DELIMITED) - } + encodeArray(value.array_double, ProtoAdapter.DOUBLE, writer, 612) + encodeArray(value.array_float, ProtoAdapter.FLOAT, writer, 611) + encodeArray(value.array_sfixed64, ProtoAdapter.SFIXED64, writer, 610) + encodeArray(value.array_fixed64, ProtoAdapter.FIXED64, writer, 609) + encodeArray(value.array_sint64, ProtoAdapter.SINT64, writer, 608) + encodeArray(value.array_uint64, ProtoAdapter.UINT64, writer, 607) + encodeArray(value.array_int64, ProtoAdapter.INT64, writer, 606) + encodeArray(value.array_sfixed32, ProtoAdapter.SFIXED32, writer, 605) + encodeArray(value.array_fixed32, ProtoAdapter.FIXED32, writer, 604) + encodeArray(value.array_sint32, ProtoAdapter.SINT32, writer, 603) + encodeArray(value.array_uint32, ProtoAdapter.UINT32, writer, 602) + encodeArray(value.array_int32, ProtoAdapter.INT32, writer, 601) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 979842dc3c..1fa5e44a89 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -18,6 +18,7 @@ import com.squareup.wire.Syntax.PROTO_2 import com.squareup.wire.WireEnum import com.squareup.wire.WireField import com.squareup.wire.`internal`.checkElementsNotNull +import com.squareup.wire.`internal`.encodeArray import com.squareup.wire.`internal`.immutableCopyOf import com.squareup.wire.`internal`.missingRequiredFields import com.squareup.wire.`internal`.redactElements @@ -3862,102 +3863,18 @@ public class AllTypes( ProtoAdapter.SINT32.encodeWithTag(writer, 1003, value.ext_opt_sint32) ProtoAdapter.UINT32.encodeWithTag(writer, 1002, value.ext_opt_uint32) ProtoAdapter.INT32.encodeWithTag(writer, 1001, value.ext_opt_int32) - if (value.array_double.isNotEmpty()) { - val byteCountBefore612 = writer.byteCount - for (i in (value.array_double.size - 1) downTo 0) { - ProtoAdapter.DOUBLE.encodePrimitive(writer, value.array_double[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore612) - writer.writeTag(612, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_float.isNotEmpty()) { - val byteCountBefore611 = writer.byteCount - for (i in (value.array_float.size - 1) downTo 0) { - ProtoAdapter.FLOAT.encodePrimitive(writer, value.array_float[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore611) - writer.writeTag(611, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sfixed64.isNotEmpty()) { - val byteCountBefore610 = writer.byteCount - for (i in (value.array_sfixed64.size - 1) downTo 0) { - ProtoAdapter.SFIXED64.encodePrimitive(writer, value.array_sfixed64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore610) - writer.writeTag(610, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_fixed64.isNotEmpty()) { - val byteCountBefore609 = writer.byteCount - for (i in (value.array_fixed64.size - 1) downTo 0) { - ProtoAdapter.FIXED64.encodePrimitive(writer, value.array_fixed64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore609) - writer.writeTag(609, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sint64.isNotEmpty()) { - val byteCountBefore608 = writer.byteCount - for (i in (value.array_sint64.size - 1) downTo 0) { - ProtoAdapter.SINT64.encodePrimitive(writer, value.array_sint64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore608) - writer.writeTag(608, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_uint64.isNotEmpty()) { - val byteCountBefore607 = writer.byteCount - for (i in (value.array_uint64.size - 1) downTo 0) { - ProtoAdapter.UINT64.encodePrimitive(writer, value.array_uint64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore607) - writer.writeTag(607, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_int64.isNotEmpty()) { - val byteCountBefore606 = writer.byteCount - for (i in (value.array_int64.size - 1) downTo 0) { - ProtoAdapter.INT64.encodePrimitive(writer, value.array_int64[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore606) - writer.writeTag(606, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sfixed32.isNotEmpty()) { - val byteCountBefore605 = writer.byteCount - for (i in (value.array_sfixed32.size - 1) downTo 0) { - ProtoAdapter.SFIXED32.encodePrimitive(writer, value.array_sfixed32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore605) - writer.writeTag(605, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_fixed32.isNotEmpty()) { - val byteCountBefore604 = writer.byteCount - for (i in (value.array_fixed32.size - 1) downTo 0) { - ProtoAdapter.FIXED32.encodePrimitive(writer, value.array_fixed32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore604) - writer.writeTag(604, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_sint32.isNotEmpty()) { - val byteCountBefore603 = writer.byteCount - for (i in (value.array_sint32.size - 1) downTo 0) { - ProtoAdapter.SINT32.encodePrimitive(writer, value.array_sint32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore603) - writer.writeTag(603, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_uint32.isNotEmpty()) { - val byteCountBefore602 = writer.byteCount - for (i in (value.array_uint32.size - 1) downTo 0) { - ProtoAdapter.UINT32.encodePrimitive(writer, value.array_uint32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore602) - writer.writeTag(602, FieldEncoding.LENGTH_DELIMITED) - } - if (value.array_int32.isNotEmpty()) { - val byteCountBefore601 = writer.byteCount - for (i in (value.array_int32.size - 1) downTo 0) { - ProtoAdapter.INT32.encodePrimitive(writer, value.array_int32[i]) - } - writer.writeVarint32(writer.byteCount - byteCountBefore601) - writer.writeTag(601, FieldEncoding.LENGTH_DELIMITED) - } + encodeArray(value.array_double, ProtoAdapter.DOUBLE, writer, 612) + encodeArray(value.array_float, ProtoAdapter.FLOAT, writer, 611) + encodeArray(value.array_sfixed64, ProtoAdapter.SFIXED64, writer, 610) + encodeArray(value.array_fixed64, ProtoAdapter.FIXED64, writer, 609) + encodeArray(value.array_sint64, ProtoAdapter.SINT64, writer, 608) + encodeArray(value.array_uint64, ProtoAdapter.UINT64, writer, 607) + encodeArray(value.array_int64, ProtoAdapter.INT64, writer, 606) + encodeArray(value.array_sfixed32, ProtoAdapter.SFIXED32, writer, 605) + encodeArray(value.array_fixed32, ProtoAdapter.FIXED32, writer, 604) + encodeArray(value.array_sint32, ProtoAdapter.SINT32, writer, 603) + encodeArray(value.array_uint32, ProtoAdapter.UINT32, writer, 602) + encodeArray(value.array_int32, ProtoAdapter.INT32, writer, 601) map_string_enumAdapter.encodeWithTag(writer, 504, value.map_string_enum) map_string_messageAdapter.encodeWithTag(writer, 503, value.map_string_message) map_string_stringAdapter.encodeWithTag(writer, 502, value.map_string_string) From 4ea62e70eb8b4a779fa79f36dd58399cc53bb10f Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Mon, 17 Apr 2023 18:32:07 -0400 Subject: [PATCH 14/17] Delegate ArrayList toString methods to the underlying arrays and remove tests --- .../kotlin/com/squareup/wire/DoubleArrayList.kt | 3 +-- .../kotlin/com/squareup/wire/FloatArrayList.kt | 3 +-- .../kotlin/com/squareup/wire/IntArrayList.kt | 3 +-- .../kotlin/com/squareup/wire/LongArrayList.kt | 3 +-- .../{DoubleArrayList.kt => DoubleArrayListTest.kt} | 10 ---------- .../kotlin/com/squareup/wire/FloatArrayListTest.kt | 10 ---------- .../kotlin/com/squareup/wire/IntArrayListTest.kt | 10 ---------- .../wire/{LongArrayList.kt => LongArrayListTest.kt} | 10 ---------- 8 files changed, 4 insertions(+), 48 deletions(-) rename wire-runtime/src/commonTest/kotlin/com/squareup/wire/{DoubleArrayList.kt => DoubleArrayListTest.kt} (67%) rename wire-runtime/src/commonTest/kotlin/com/squareup/wire/{LongArrayList.kt => LongArrayListTest.kt} (68%) diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt index 79ca785cdc..dd3e17680d 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt @@ -38,6 +38,5 @@ class DoubleArrayList(initialCapacity: Int) { } } - override fun toString(): String = - (0 until size).joinToString(", ", "[", "]") { data[it].toString() } + override fun toString(): String = data.copyOf(size).contentToString() } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt index 0126a13eb2..b9f3ad1517 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt @@ -39,6 +39,5 @@ class FloatArrayList(initialCapacity: Int) { } - override fun toString(): String = - (0 until size).joinToString(", ", "[", "]") { data[it].toString() } + override fun toString(): String = data.copyOf(size).contentToString() } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt index 53689dc65f..b3e5ef2683 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt @@ -38,6 +38,5 @@ class IntArrayList(initialCapacity: Int) { } } - override fun toString(): String = - (0 until size).joinToString(", ", "[", "]") { data[it].toString() } + override fun toString(): String = data.copyOf(size).contentToString() } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt index 0e9bafa05f..1603fc7cae 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt @@ -38,6 +38,5 @@ class LongArrayList(initialCapacity: Int) { } } - override fun toString(): String = - (0 until size).joinToString(", ", "[", "]") { data[it].toString() } + override fun toString(): String = data.copyOf(size).contentToString() } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayList.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayListTest.kt similarity index 67% rename from wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayList.kt rename to wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayListTest.kt index f070de3403..14f7a07671 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayList.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayListTest.kt @@ -17,14 +17,4 @@ class DoubleArrayListTest { assertEquals(array[i], (i + 1).toDouble()) } } - - @Test - fun toStringIsReasonable() { - val arrayList = DoubleArrayList(0) - arrayList.add(1.0) - arrayList.add(2.0) - arrayList.add(3.0) - - assertEquals(arrayList.toString(), "[1.0, 2.0, 3.0]") - } } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt index a19d4cfec1..5f4954de5f 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt @@ -17,14 +17,4 @@ class FloatArrayListTest { assertEquals(array[i], (i + 1).toFloat()) } } - - @Test - fun toStringIsReasonable() { - val arrayList = FloatArrayList(0) - arrayList.add(1f) - arrayList.add(2f) - arrayList.add(3f) - - assertEquals(arrayList.toString(), "[1.0, 2.0, 3.0]") - } } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt index bf945ad7f3..a0cb25b687 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt @@ -17,14 +17,4 @@ class IntArrayListTest { assertEquals(array[i], i + 1) } } - - @Test - fun toStringIsReasonable() { - val arrayList = IntArrayList(0) - arrayList.add(1) - arrayList.add(2) - arrayList.add(3) - - assertEquals(arrayList.toString(), "[1, 2, 3]") - } } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayList.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayListTest.kt similarity index 68% rename from wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayList.kt rename to wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayListTest.kt index 74b56786c2..33aefce37b 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayList.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayListTest.kt @@ -17,14 +17,4 @@ class LongArrayListTest { assertEquals(array[i], (i + 1).toLong()) } } - - @Test - fun toStringIsReasonable() { - val arrayList = LongArrayList(0) - arrayList.add(1L) - arrayList.add(2L) - arrayList.add(3L) - - assertEquals(arrayList.toString(), "[1, 2, 3]") - } } From 721369632ad086c243aedfd0b79375e019b7212b Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Mon, 17 Apr 2023 20:17:44 -0400 Subject: [PATCH 15/17] Update use_array tag to 1180 --- wire-schema/src/jvmMain/resources/wire/extensions.proto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wire-schema/src/jvmMain/resources/wire/extensions.proto b/wire-schema/src/jvmMain/resources/wire/extensions.proto index 9f29b275c7..4b6871700e 100644 --- a/wire-schema/src/jvmMain/resources/wire/extensions.proto +++ b/wire-schema/src/jvmMain/resources/wire/extensions.proto @@ -40,7 +40,7 @@ extend google.protobuf.FieldOptions { * Uses an Array at runtime to hold the values, to avoid autoboxing on the JVM. Will use the appropriate * array type, for example `repeated float` would be represented as a FloatArray. */ - optional string use_array = 1078; + optional string use_array = 1180; } extend google.protobuf.EnumValueOptions { From be6175ed5abde4e4fea8cd9f662b1589f6c6d0f0 Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Tue, 18 Apr 2023 09:16:53 -0400 Subject: [PATCH 16/17] Move ArrayList classes to internal package --- .../java/com/squareup/wire/kotlin/KotlinGenerator.kt | 8 ++++---- .../squareup/wire/{ => internal}/DoubleArrayList.kt | 2 +- .../squareup/wire/{ => internal}/FloatArrayList.kt | 2 +- .../com/squareup/wire/{ => internal}/IntArrayList.kt | 2 +- .../squareup/wire/{ => internal}/LongArrayList.kt | 2 +- .../wire/{ => internal}/DoubleArrayListTest.kt | 12 +++++++++++- .../wire/{ => internal}/FloatArrayListTest.kt | 12 +++++++++++- .../squareup/wire/{ => internal}/IntArrayListTest.kt | 12 +++++++++++- .../wire/{ => internal}/LongArrayListTest.kt | 12 +++++++++++- .../squareup/wire/protos/kotlin/alltypes/AllTypes.kt | 8 ++++---- .../squareup/wire/protos/kotlin/alltypes/AllTypes.kt | 8 ++++---- 11 files changed, 60 insertions(+), 20 deletions(-) rename wire-runtime/src/commonMain/kotlin/com/squareup/wire/{ => internal}/DoubleArrayList.kt (97%) rename wire-runtime/src/commonMain/kotlin/com/squareup/wire/{ => internal}/FloatArrayList.kt (97%) rename wire-runtime/src/commonMain/kotlin/com/squareup/wire/{ => internal}/IntArrayList.kt (97%) rename wire-runtime/src/commonMain/kotlin/com/squareup/wire/{ => internal}/LongArrayList.kt (97%) rename wire-runtime/src/commonTest/kotlin/com/squareup/wire/{ => internal}/DoubleArrayListTest.kt (58%) rename wire-runtime/src/commonTest/kotlin/com/squareup/wire/{ => internal}/FloatArrayListTest.kt (59%) rename wire-runtime/src/commonTest/kotlin/com/squareup/wire/{ => internal}/IntArrayListTest.kt (58%) rename wire-runtime/src/commonTest/kotlin/com/squareup/wire/{ => internal}/LongArrayListTest.kt (59%) 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 b141bb76f8..469fee0ba9 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 @@ -52,16 +52,16 @@ 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.DoubleArrayList +import com.squareup.wire.internal.DoubleArrayList import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding -import com.squareup.wire.FloatArrayList +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.IntArrayList -import com.squareup.wire.LongArrayList +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 diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/DoubleArrayList.kt similarity index 97% rename from wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt rename to wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/DoubleArrayList.kt index dd3e17680d..1e458a9238 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/DoubleArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/DoubleArrayList.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal /** * Inspired by org.jetbrains.kotlin.utils.IntArrayList diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/FloatArrayList.kt similarity index 97% rename from wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt rename to wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/FloatArrayList.kt index b9f3ad1517..60863e611b 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/FloatArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/FloatArrayList.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal /** * Inspired by org.jetbrains.kotlin.utils.IntArrayList diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/IntArrayList.kt similarity index 97% rename from wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt rename to wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/IntArrayList.kt index b3e5ef2683..5e37bef0e6 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/IntArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/IntArrayList.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal /** * Inspired by org.jetbrains.kotlin.utils.IntArrayList diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/LongArrayList.kt similarity index 97% rename from wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt rename to wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/LongArrayList.kt index 1603fc7cae..737747a633 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/LongArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/LongArrayList.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal /** * Inspired by org.jetbrains.kotlin.utils.IntArrayList diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/DoubleArrayListTest.kt similarity index 58% rename from wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayListTest.kt rename to wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/DoubleArrayListTest.kt index 14f7a07671..35c7cebc22 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/DoubleArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/DoubleArrayListTest.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal import kotlin.test.Test import kotlin.test.assertEquals @@ -17,4 +17,14 @@ class DoubleArrayListTest { assertEquals(array[i], (i + 1).toDouble()) } } + + @Test + fun getStringDelegatesToTheUnderlyingArray() { + val arrayList = DoubleArrayList(0) + arrayList.add(1.0) + arrayList.add(2.0) + arrayList.add(3.0) + + assertEquals(arrayList.toString(), doubleArrayOf(1.0, 2.0, 3.0).contentToString()) + } } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/FloatArrayListTest.kt similarity index 59% rename from wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt rename to wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/FloatArrayListTest.kt index 5f4954de5f..95f7fcdd15 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/FloatArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/FloatArrayListTest.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal import kotlin.test.Test import kotlin.test.assertEquals @@ -17,4 +17,14 @@ class FloatArrayListTest { assertEquals(array[i], (i + 1).toFloat()) } } + + @Test + fun getStringDelegatesToTheUnderlyingArray() { + val arrayList = FloatArrayList(0) + arrayList.add(1f) + arrayList.add(2f) + arrayList.add(3f) + + assertEquals(arrayList.toString(), floatArrayOf(1f, 2f, 3f).contentToString()) + } } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/IntArrayListTest.kt similarity index 58% rename from wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt rename to wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/IntArrayListTest.kt index a0cb25b687..4589d9143b 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/IntArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/IntArrayListTest.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal import kotlin.test.Test import kotlin.test.assertEquals @@ -17,4 +17,14 @@ class IntArrayListTest { assertEquals(array[i], i + 1) } } + + @Test + fun getStringDelegatesToTheUnderlyingArray() { + val arrayList = IntArrayList(0) + arrayList.add(1) + arrayList.add(2) + arrayList.add(3) + + assertEquals(arrayList.toString(), intArrayOf(1, 2, 3).contentToString()) + } } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/LongArrayListTest.kt similarity index 59% rename from wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayListTest.kt rename to wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/LongArrayListTest.kt index 33aefce37b..445e53c07b 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/LongArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/LongArrayListTest.kt @@ -1,4 +1,4 @@ -package com.squareup.wire +package com.squareup.wire.internal import kotlin.test.Test import kotlin.test.assertEquals @@ -17,4 +17,14 @@ class LongArrayListTest { assertEquals(array[i], (i + 1).toLong()) } } + + @Test + fun getStringDelegatesToTheUnderlyingArray() { + val arrayList = LongArrayList(0) + arrayList.add(1L) + arrayList.add(2L) + arrayList.add(3L) + + assertEquals(arrayList.toString(), longArrayOf(1L, 2L, 3L).contentToString()) + } } diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 0905bdaf4c..10f8cc8a83 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -2,12 +2,8 @@ // Source: squareup.protos.kotlin.alltypes.AllTypes in all_types.proto package com.squareup.wire.protos.kotlin.alltypes -import com.squareup.wire.DoubleArrayList import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding -import com.squareup.wire.FloatArrayList -import com.squareup.wire.IntArrayList -import com.squareup.wire.LongArrayList import com.squareup.wire.Message import com.squareup.wire.ProtoAdapter import com.squareup.wire.ProtoReader @@ -17,6 +13,10 @@ import com.squareup.wire.Syntax import com.squareup.wire.Syntax.PROTO_2 import com.squareup.wire.WireEnum import com.squareup.wire.WireField +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`.encodeArray import com.squareup.wire.`internal`.immutableCopyOf import com.squareup.wire.`internal`.missingRequiredFields diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 1fa5e44a89..4e88e740c9 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -2,12 +2,8 @@ // Source: squareup.protos.kotlin.alltypes.AllTypes in all_types.proto package com.squareup.wire.protos.kotlin.alltypes -import com.squareup.wire.DoubleArrayList import com.squareup.wire.EnumAdapter import com.squareup.wire.FieldEncoding -import com.squareup.wire.FloatArrayList -import com.squareup.wire.IntArrayList -import com.squareup.wire.LongArrayList import com.squareup.wire.Message import com.squareup.wire.ProtoAdapter import com.squareup.wire.ProtoReader @@ -17,6 +13,10 @@ import com.squareup.wire.Syntax import com.squareup.wire.Syntax.PROTO_2 import com.squareup.wire.WireEnum import com.squareup.wire.WireField +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`.checkElementsNotNull import com.squareup.wire.`internal`.encodeArray import com.squareup.wire.`internal`.immutableCopyOf From 69cb53c4ba8cf83be3877353ab5916752b590e5c Mon Sep 17 00:00:00 2001 From: Jeff Gulbronson Date: Mon, 24 Apr 2023 09:03:21 -0400 Subject: [PATCH 17/17] Updates after Jesse's review - Rename `getTruncatedArray()` to `toArray()` - Add copyright headers - Add helper `forDecoding()` method on companion objects --- .../squareup/wire/kotlin/KotlinGenerator.kt | 18 +++- .../squareup/wire/internal/DoubleArrayList.kt | 20 +++- .../squareup/wire/internal/FloatArrayList.kt | 20 +++- .../squareup/wire/internal/IntArrayList.kt | 20 +++- .../squareup/wire/internal/LongArrayList.kt | 22 ++++- .../wire/internal/DoubleArrayListTest.kt | 17 +++- .../wire/internal/FloatArrayListTest.kt | 17 +++- .../wire/internal/IntArrayListTest.kt | 17 +++- .../wire/internal/LongArrayListTest.kt | 17 +++- .../wire/protos/kotlin/alltypes/AllTypes.kt | 96 +++++-------------- .../wire/protos/kotlin/alltypes/AllTypes.kt | 96 +++++-------------- 11 files changed, 196 insertions(+), 164 deletions(-) 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 469fee0ba9..833303477b 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 @@ -1698,7 +1698,7 @@ class KotlinGenerator private constructor( if (fieldOrOneOf.isPacked && fieldOrOneOf.isScalar) { if (fieldOrOneOf.useArray) { - addStatement("%1L = %1L?.getTruncatedArray() ?: %2L,", fieldName, fieldOrOneOf.emptyPrimitiveArrayForType) + addStatement("%1L = %1L?.toArray() ?: %2L,", fieldName, fieldOrOneOf.emptyPrimitiveArrayForType) } else { addStatement("%1L = %1L ?: listOf(),", fieldName) } @@ -1793,15 +1793,27 @@ class KotlinGenerator private constructor( ) return CodeBlock.of( when { + field.useArray -> { + buildCodeBlock { + beginControlFlow("if (%L == null)", fieldName) + addStatement( + "%L = %L.forDecoding(reader.nextFieldMinLengthInBytes(), %L)", + fieldName, + field.arrayListClassForType.simpleName, + field.getMinimumByteSize(), + ) + endControlFlow() + addStatement("%1L!!.add(%2L)", field, decode) + }.toString() + } field.isPacked && field.isScalar -> { - val type = if (field.useArray) field.arrayListClassForType.simpleName else "ArrayList" buildCodeBlock { beginControlFlow("if (%L == null)", fieldName) addStatement("val minimumByteSize = ${field.getMinimumByteSize()}") addStatement("val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize)") addStatement("⇥.coerceAtMost(Int.MAX_VALUE.toLong())") addStatement(".toInt()") - addStatement("⇤%L = %L(initialCapacity)", fieldName, type) + addStatement("⇤%L = %L(initialCapacity)", fieldName, ArrayList::class.simpleName) endControlFlow() addStatement("%1L!!.add(%2L)", field, decode) }.toString() diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/DoubleArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/DoubleArrayList.kt index 1e458a9238..3c207c8242 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/DoubleArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/DoubleArrayList.kt @@ -1,8 +1,10 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license. + */ package com.squareup.wire.internal /** - * Inspired by org.jetbrains.kotlin.utils.IntArrayList - * * Offers a nice wrapper around DoubleArray, that handles resizing the underlying array as needed and * provides a trimToSize() method to truncate the underlying array to the current number of elements. */ @@ -18,7 +20,7 @@ class DoubleArrayList(initialCapacity: Int) { * elements have been added to the list, otherwise the next call to add() will cause the * array to be resized. */ - fun getTruncatedArray(): DoubleArray { + fun toArray(): DoubleArray { if (size < data.size) { data = data.copyOf(size) } @@ -39,4 +41,16 @@ class DoubleArrayList(initialCapacity: Int) { } override fun toString(): String = data.copyOf(size).contentToString() + + companion object { + fun forDecoding( + minLengthInBytes: Long, + minimumElementByteSize: Long, + ) : DoubleArrayList { + val minElements = (minLengthInBytes / minimumElementByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + return DoubleArrayList(minElements) + } + } } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/FloatArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/FloatArrayList.kt index 60863e611b..4682946e13 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/FloatArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/FloatArrayList.kt @@ -1,8 +1,10 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license. + */ package com.squareup.wire.internal /** - * Inspired by org.jetbrains.kotlin.utils.IntArrayList - * * Offers a nice wrapper around FloatArray, that handles resizing the underlying array as needed and * provides a trimToSize() method to truncate the underlying array to the current number of elements. */ @@ -18,7 +20,7 @@ class FloatArrayList(initialCapacity: Int) { * elements have been added to the list, otherwise the next call to add() will cause the * array to be resized. */ - fun getTruncatedArray(): FloatArray { + fun toArray(): FloatArray { if (size < data.size) { data = data.copyOf(size) } @@ -40,4 +42,16 @@ class FloatArrayList(initialCapacity: Int) { override fun toString(): String = data.copyOf(size).contentToString() + + companion object { + fun forDecoding( + minLengthInBytes: Long, + minimumElementByteSize: Long, + ) : FloatArrayList { + val minElements = (minLengthInBytes / minimumElementByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + return FloatArrayList(minElements) + } + } } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/IntArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/IntArrayList.kt index 5e37bef0e6..250be444ff 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/IntArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/IntArrayList.kt @@ -1,8 +1,10 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license. + */ package com.squareup.wire.internal /** - * Inspired by org.jetbrains.kotlin.utils.IntArrayList - * * Offers a nice wrapper around IntArray, that handles resizing the underlying array as needed and * provides a trimToSize() method to truncate the underlying array to the current number of elements. */ @@ -18,7 +20,7 @@ class IntArrayList(initialCapacity: Int) { * elements have been added to the list, otherwise the next call to add() will cause the * array to be resized. */ - fun getTruncatedArray(): IntArray { + fun toArray(): IntArray { if (size < data.size) { data = data.copyOf(size) } @@ -39,4 +41,16 @@ class IntArrayList(initialCapacity: Int) { } override fun toString(): String = data.copyOf(size).contentToString() + + companion object { + fun forDecoding( + minLengthInBytes: Long, + minimumElementByteSize: Long, + ) : IntArrayList { + val minElements = (minLengthInBytes / minimumElementByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + return IntArrayList(minElements) + } + } } diff --git a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/LongArrayList.kt b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/LongArrayList.kt index 737747a633..c55c76a687 100644 --- a/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/LongArrayList.kt +++ b/wire-runtime/src/commonMain/kotlin/com/squareup/wire/internal/LongArrayList.kt @@ -1,8 +1,10 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license. + */ package com.squareup.wire.internal /** - * Inspired by org.jetbrains.kotlin.utils.IntArrayList - * * Offers a nice wrapper around LongArray, that handles resizing the underlying array as needed and * provides a trimToSize() method to truncate the underlying array to the current number of elements. */ @@ -18,7 +20,7 @@ class LongArrayList(initialCapacity: Int) { * elements have been added to the list, otherwise the next call to add() will cause the * array to be resized. */ - fun getTruncatedArray(): LongArray { + fun toArray(): LongArray { if (size < data.size) { data = data.copyOf(size) } @@ -30,7 +32,7 @@ class LongArrayList(initialCapacity: Int) { data[size++] = long } - fun isNotEmpty() : Boolean = size > 0 + fun isNotEmpty(): Boolean = size > 0 private fun ensureCapacity(minCapacity: Int) { if (minCapacity > data.size) { @@ -39,4 +41,16 @@ class LongArrayList(initialCapacity: Int) { } override fun toString(): String = data.copyOf(size).contentToString() + + companion object { + fun forDecoding( + minLengthInBytes: Long, + minimumElementByteSize: Long, + ) : LongArrayList { + val minElements = (minLengthInBytes / minimumElementByteSize) + .coerceAtMost(Int.MAX_VALUE.toLong()) + .toInt() + return LongArrayList(minElements) + } + } } diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/DoubleArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/DoubleArrayListTest.kt index 35c7cebc22..33bc95e60b 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/DoubleArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/DoubleArrayListTest.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2023 Square Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.squareup.wire.internal import kotlin.test.Test @@ -11,7 +26,7 @@ class DoubleArrayListTest { arrayList.add(2.0) arrayList.add(3.0) - val array = arrayList.getTruncatedArray() + val array = arrayList.toArray() assertEquals(array.size, 3) for (i in 0..2) { assertEquals(array[i], (i + 1).toDouble()) diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/FloatArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/FloatArrayListTest.kt index 95f7fcdd15..911717789b 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/FloatArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/FloatArrayListTest.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2023 Square Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.squareup.wire.internal import kotlin.test.Test @@ -11,7 +26,7 @@ class FloatArrayListTest { arrayList.add(2f) arrayList.add(3f) - val array = arrayList.getTruncatedArray() + val array = arrayList.toArray() assertEquals(array.size, 3) for (i in 0..2) { assertEquals(array[i], (i + 1).toFloat()) diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/IntArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/IntArrayListTest.kt index 4589d9143b..e41a7878e2 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/IntArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/IntArrayListTest.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2023 Square Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.squareup.wire.internal import kotlin.test.Test @@ -11,7 +26,7 @@ class IntArrayListTest { arrayList.add(2) arrayList.add(3) - val array = arrayList.getTruncatedArray() + val array = arrayList.toArray() assertEquals(array.size, 3) for (i in 0..2) { assertEquals(array[i], i + 1) diff --git a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/LongArrayListTest.kt b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/LongArrayListTest.kt index 445e53c07b..da11d801b6 100644 --- a/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/LongArrayListTest.kt +++ b/wire-runtime/src/commonTest/kotlin/com/squareup/wire/internal/LongArrayListTest.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2023 Square Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.squareup.wire.internal import kotlin.test.Test @@ -11,7 +26,7 @@ class LongArrayListTest { arrayList.add(2L) arrayList.add(3L) - val array = arrayList.getTruncatedArray() + val array = arrayList.toArray() assertEquals(array.size, 3) for (i in 0..2) { assertEquals(array[i], (i + 1).toLong()) diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 10f8cc8a83..af1c6d5f65 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -2673,121 +2673,73 @@ public class AllTypes( 504 -> map_string_enum.putAll(map_string_enumAdapter.decode(reader)) 601 -> { if (array_int32 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_int32 = IntArrayList(initialCapacity) + array_int32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decodePrimitive(reader)) } 602 -> { if (array_uint32 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_uint32 = IntArrayList(initialCapacity) + array_uint32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decodePrimitive(reader)) } 603 -> { if (array_sint32 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sint32 = IntArrayList(initialCapacity) + array_sint32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decodePrimitive(reader)) } 604 -> { if (array_fixed32 == null) { - val minimumByteSize = 4 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_fixed32 = IntArrayList(initialCapacity) + array_fixed32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 4) } array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decodePrimitive(reader)) } 605 -> { if (array_sfixed32 == null) { - val minimumByteSize = 4 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sfixed32 = IntArrayList(initialCapacity) + array_sfixed32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 4) } array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decodePrimitive(reader)) } 606 -> { if (array_int64 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_int64 = LongArrayList(initialCapacity) + array_int64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decodePrimitive(reader)) } 607 -> { if (array_uint64 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_uint64 = LongArrayList(initialCapacity) + array_uint64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decodePrimitive(reader)) } 608 -> { if (array_sint64 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sint64 = LongArrayList(initialCapacity) + array_sint64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decodePrimitive(reader)) } 609 -> { if (array_fixed64 == null) { - val minimumByteSize = 8 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_fixed64 = LongArrayList(initialCapacity) + array_fixed64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 8) } array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decodePrimitive(reader)) } 610 -> { if (array_sfixed64 == null) { - val minimumByteSize = 8 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sfixed64 = LongArrayList(initialCapacity) + array_sfixed64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 8) } array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decodePrimitive(reader)) } 611 -> { if (array_float == null) { - val minimumByteSize = 4 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_float = FloatArrayList(initialCapacity) + array_float = FloatArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 4) } array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decodePrimitive(reader)) } 612 -> { if (array_double == null) { - val minimumByteSize = 8 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_double = DoubleArrayList(initialCapacity) + array_double = DoubleArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 8) } array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decodePrimitive(reader)) } @@ -3059,18 +3011,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - array_int32 = array_int32?.getTruncatedArray() ?: intArrayOf(), - array_uint32 = array_uint32?.getTruncatedArray() ?: intArrayOf(), - array_sint32 = array_sint32?.getTruncatedArray() ?: intArrayOf(), - array_fixed32 = array_fixed32?.getTruncatedArray() ?: intArrayOf(), - array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: intArrayOf(), - array_int64 = array_int64?.getTruncatedArray() ?: longArrayOf(), - array_uint64 = array_uint64?.getTruncatedArray() ?: longArrayOf(), - array_sint64 = array_sint64?.getTruncatedArray() ?: longArrayOf(), - array_fixed64 = array_fixed64?.getTruncatedArray() ?: longArrayOf(), - array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: longArrayOf(), - array_float = array_float?.getTruncatedArray() ?: floatArrayOf(), - array_double = array_double?.getTruncatedArray() ?: doubleArrayOf(), + array_int32 = array_int32?.toArray() ?: intArrayOf(), + array_uint32 = array_uint32?.toArray() ?: intArrayOf(), + array_sint32 = array_sint32?.toArray() ?: intArrayOf(), + array_fixed32 = array_fixed32?.toArray() ?: intArrayOf(), + array_sfixed32 = array_sfixed32?.toArray() ?: intArrayOf(), + array_int64 = array_int64?.toArray() ?: longArrayOf(), + array_uint64 = array_uint64?.toArray() ?: longArrayOf(), + array_sint64 = array_sint64?.toArray() ?: longArrayOf(), + array_fixed64 = array_fixed64?.toArray() ?: longArrayOf(), + array_sfixed64 = array_sfixed64?.toArray() ?: longArrayOf(), + array_float = array_float?.toArray() ?: floatArrayOf(), + array_double = array_double?.toArray() ?: doubleArrayOf(), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32, diff --git a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt index 4e88e740c9..a787501ee0 100644 --- a/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt +++ b/wire-tests/src/jvmKotlinInteropTest/proto-kotlin/com/squareup/wire/protos/kotlin/alltypes/AllTypes.kt @@ -4334,121 +4334,73 @@ public class AllTypes( 504 -> map_string_enum.putAll(map_string_enumAdapter.decode(reader)) 601 -> { if (array_int32 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_int32 = IntArrayList(initialCapacity) + array_int32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_int32!!.add(com.squareup.wire.ProtoAdapter.INT32.decodePrimitive(reader)) } 602 -> { if (array_uint32 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_uint32 = IntArrayList(initialCapacity) + array_uint32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_uint32!!.add(com.squareup.wire.ProtoAdapter.UINT32.decodePrimitive(reader)) } 603 -> { if (array_sint32 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sint32 = IntArrayList(initialCapacity) + array_sint32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_sint32!!.add(com.squareup.wire.ProtoAdapter.SINT32.decodePrimitive(reader)) } 604 -> { if (array_fixed32 == null) { - val minimumByteSize = 4 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_fixed32 = IntArrayList(initialCapacity) + array_fixed32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 4) } array_fixed32!!.add(com.squareup.wire.ProtoAdapter.FIXED32.decodePrimitive(reader)) } 605 -> { if (array_sfixed32 == null) { - val minimumByteSize = 4 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sfixed32 = IntArrayList(initialCapacity) + array_sfixed32 = IntArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 4) } array_sfixed32!!.add(com.squareup.wire.ProtoAdapter.SFIXED32.decodePrimitive(reader)) } 606 -> { if (array_int64 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_int64 = LongArrayList(initialCapacity) + array_int64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_int64!!.add(com.squareup.wire.ProtoAdapter.INT64.decodePrimitive(reader)) } 607 -> { if (array_uint64 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_uint64 = LongArrayList(initialCapacity) + array_uint64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_uint64!!.add(com.squareup.wire.ProtoAdapter.UINT64.decodePrimitive(reader)) } 608 -> { if (array_sint64 == null) { - val minimumByteSize = 1 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sint64 = LongArrayList(initialCapacity) + array_sint64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 1) } array_sint64!!.add(com.squareup.wire.ProtoAdapter.SINT64.decodePrimitive(reader)) } 609 -> { if (array_fixed64 == null) { - val minimumByteSize = 8 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_fixed64 = LongArrayList(initialCapacity) + array_fixed64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 8) } array_fixed64!!.add(com.squareup.wire.ProtoAdapter.FIXED64.decodePrimitive(reader)) } 610 -> { if (array_sfixed64 == null) { - val minimumByteSize = 8 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_sfixed64 = LongArrayList(initialCapacity) + array_sfixed64 = LongArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 8) } array_sfixed64!!.add(com.squareup.wire.ProtoAdapter.SFIXED64.decodePrimitive(reader)) } 611 -> { if (array_float == null) { - val minimumByteSize = 4 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_float = FloatArrayList(initialCapacity) + array_float = FloatArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 4) } array_float!!.add(com.squareup.wire.ProtoAdapter.FLOAT.decodePrimitive(reader)) } 612 -> { if (array_double == null) { - val minimumByteSize = 8 - val initialCapacity = (reader.nextFieldMinLengthInBytes() / minimumByteSize) - .coerceAtMost(Int.MAX_VALUE.toLong()) - .toInt() - array_double = DoubleArrayList(initialCapacity) + array_double = DoubleArrayList.forDecoding(reader.nextFieldMinLengthInBytes(), 8) } array_double!!.add(com.squareup.wire.ProtoAdapter.DOUBLE.decodePrimitive(reader)) } @@ -4720,18 +4672,18 @@ public class AllTypes( map_string_string = map_string_string, map_string_message = map_string_message, map_string_enum = map_string_enum, - array_int32 = array_int32?.getTruncatedArray() ?: intArrayOf(), - array_uint32 = array_uint32?.getTruncatedArray() ?: intArrayOf(), - array_sint32 = array_sint32?.getTruncatedArray() ?: intArrayOf(), - array_fixed32 = array_fixed32?.getTruncatedArray() ?: intArrayOf(), - array_sfixed32 = array_sfixed32?.getTruncatedArray() ?: intArrayOf(), - array_int64 = array_int64?.getTruncatedArray() ?: longArrayOf(), - array_uint64 = array_uint64?.getTruncatedArray() ?: longArrayOf(), - array_sint64 = array_sint64?.getTruncatedArray() ?: longArrayOf(), - array_fixed64 = array_fixed64?.getTruncatedArray() ?: longArrayOf(), - array_sfixed64 = array_sfixed64?.getTruncatedArray() ?: longArrayOf(), - array_float = array_float?.getTruncatedArray() ?: floatArrayOf(), - array_double = array_double?.getTruncatedArray() ?: doubleArrayOf(), + array_int32 = array_int32?.toArray() ?: intArrayOf(), + array_uint32 = array_uint32?.toArray() ?: intArrayOf(), + array_sint32 = array_sint32?.toArray() ?: intArrayOf(), + array_fixed32 = array_fixed32?.toArray() ?: intArrayOf(), + array_sfixed32 = array_sfixed32?.toArray() ?: intArrayOf(), + array_int64 = array_int64?.toArray() ?: longArrayOf(), + array_uint64 = array_uint64?.toArray() ?: longArrayOf(), + array_sint64 = array_sint64?.toArray() ?: longArrayOf(), + array_fixed64 = array_fixed64?.toArray() ?: longArrayOf(), + array_sfixed64 = array_sfixed64?.toArray() ?: longArrayOf(), + array_float = array_float?.toArray() ?: floatArrayOf(), + array_double = array_double?.toArray() ?: doubleArrayOf(), ext_opt_int32 = ext_opt_int32, ext_opt_uint32 = ext_opt_uint32, ext_opt_sint32 = ext_opt_sint32,