From 5a612b5d9a376a2dd2a21eb1f23b7ebeb97ccff6 Mon Sep 17 00:00:00 2001 From: Egor Andreevici Date: Sun, 15 Sep 2019 09:32:40 -0400 Subject: [PATCH 1/4] Copy UnknownFieldsTest into commonTest --- gen-tests.gradle | 3 +- .../com/squareup/wire/UnknownFieldsTest.kt | 107 ++++++++++ .../kotlin/unknownfields/NestedVersionOne.kt | 98 +++++++++ .../kotlin/unknownfields/NestedVersionTwo.kt | 173 ++++++++++++++++ .../protos/kotlin/unknownfields/VersionOne.kt | 115 +++++++++++ .../protos/kotlin/unknownfields/VersionTwo.kt | 188 ++++++++++++++++++ .../proto/kotlin/unknown_fields.proto | 50 +++++ 7 files changed, 733 insertions(+), 1 deletion(-) create mode 100644 wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt create mode 100644 wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt create mode 100644 wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt create mode 100644 wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt create mode 100644 wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt create mode 100644 wire-tests/src/commonTest/proto/kotlin/unknown_fields.proto diff --git a/gen-tests.gradle b/gen-tests.gradle index e03841b6b4..66230cad90 100644 --- a/gen-tests.gradle +++ b/gen-tests.gradle @@ -167,7 +167,8 @@ task generateKotlinTests(type: JavaExec) { 'no_fields.proto', 'to_string.proto', 'simple_message.proto', - 'external_message.proto' + 'external_message.proto', + 'unknown_fields.proto', ] } diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt new file mode 100644 index 0000000000..d2ffb6b449 --- /dev/null +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt @@ -0,0 +1,107 @@ +/* + * Copyright 2013 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 + +import com.squareup.wire.protos.kotlin.unknownfields.NestedVersionOne +import com.squareup.wire.protos.kotlin.unknownfields.NestedVersionTwo +import com.squareup.wire.protos.kotlin.unknownfields.VersionOne +import com.squareup.wire.protos.kotlin.unknownfields.VersionTwo +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +class UnknownFieldsTest { + private val v1Adapter = VersionOne.ADAPTER + private val v2Adapter = VersionTwo.ADAPTER + + @Test + fun testUnknownFields() { + val v1_obj = NestedVersionOne(i = 111) + val v2_obj = NestedVersionTwo( + i = 111, + v2_i = 12345, + v2_s = "222", + v2_f32 = 67890, + v2_f64 = 98765L, + v2_rs = listOf("1", "2") + ) + + val v2 = VersionTwo( + i = 111, + v2_i = 12345, + v2_s = "222", + v2_f32 = 67890, + v2_f64 = 98765L, + v2_rs = listOf("1", "2"), + obj = v2_obj + ) + assertEquals(111, v2.i) + assertEquals(v2.obj!!.copy(), v2.obj) + // Check v.2 fields + assertEquals(12345, v2.v2_i) + assertEquals("222", v2.v2_s) + assertEquals(67890, v2.v2_f32) + assertEquals(98765L, v2.v2_f64) + assertEquals(listOf("1", "2"), v2.v2_rs) + // Serialized + val v2Bytes = v2Adapter.encode(v2) + + // Parse + val v1 = v1Adapter.decode(v2Bytes) + // v.1 fields are visible, v.2 fields are in unknownFieldSet + assertEquals(111, v1.i) + assertEquals(v1_obj, v1.obj!!.withoutUnknownFields()) + // Serialized output should still contain the v.2 fields + val v1Bytes = v1Adapter.encode(v1) + + // Unknown fields participate in equals() and hashCode() + val v1Simple = VersionOne(i = 111, obj = v1_obj) + assertNotEquals(v1Simple, v1) + assertNotEquals(v1Simple.hashCode(), v1.hashCode()) + assertNotEquals(v1Adapter.encode(v1Simple), v1Adapter.encode(v1)) + + // Unknown fields can be removed for equals() and hashCode(); + val v1Known = v1.withoutUnknownFields().copy(obj = v1.obj.withoutUnknownFields()) + assertEquals(v1Simple, v1Known) + assertEquals(v1Simple.hashCode(), v1Known.hashCode()) + assertEquals(v1Adapter.encode(v1Simple), v1Adapter.encode(v1Known)) + + // Re-parse + val v2B = v2Adapter.decode(v1Bytes) + assertEquals(111, v2B.i) + assertEquals(12345, v2B.v2_i) + assertEquals("222", v2B.v2_s) + assertEquals(67890, v2B.v2_f32) + assertEquals(98765L, v2B.v2_f64) + assertEquals(listOf("1", "2"), v2B.v2_rs) + assertEquals(v2_obj, v2B.obj) + + // "Modify" v1 via a merged builder, serialize, and re-parse + val v1Modified = v1.copy(i = 777, obj = v1_obj.copy(i = 777)) + assertEquals(777, v1Modified.i) + assertEquals(v1_obj.copy(i = 777), v1Modified.obj) + val v1ModifiedBytes = v1Adapter.encode(v1Modified) + + val v2C = v2Adapter.decode(v1ModifiedBytes) + assertEquals(777, v2C.i) + assertEquals(12345, v2C.v2_i) + assertEquals("222", v2C.v2_s) + assertEquals(67890, v2C.v2_f32) + assertEquals(98765L, v2C.v2_f64) + assertEquals(NestedVersionTwo(i = 777), v2C.obj) + assertEquals(listOf("1", "2"), v2C.v2_rs) + } +} diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt new file mode 100644 index 0000000000..7bb4a2415c --- /dev/null +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt @@ -0,0 +1,98 @@ +// Code generated by Wire protocol buffer compiler, do not edit. +// Source file: unknown_fields.proto +package com.squareup.wire.protos.kotlin.unknownfields + +import com.squareup.wire.FieldEncoding +import com.squareup.wire.Message +import com.squareup.wire.ProtoAdapter +import com.squareup.wire.ProtoReader +import com.squareup.wire.ProtoWriter +import com.squareup.wire.WireField +import kotlin.Any +import kotlin.AssertionError +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.DeprecationLevel +import kotlin.Int +import kotlin.Nothing +import kotlin.String +import kotlin.hashCode +import kotlin.jvm.JvmField +import okio.ByteString + +class NestedVersionOne( + @field:WireField( + tag = 1, + adapter = "com.squareup.wire.ProtoAdapter#INT32" + ) + val i: Int? = null, + unknownFields: ByteString = ByteString.EMPTY +) : Message(ADAPTER, unknownFields) { + @Deprecated( + message = "Shouldn't be used in Kotlin", + level = DeprecationLevel.HIDDEN + ) + override fun newBuilder(): Nothing { + throw AssertionError() + } + + override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other !is NestedVersionOne) return false + return unknownFields == other.unknownFields + && i == other.i + } + + override fun hashCode(): Int { + var result = super.hashCode + if (result == 0) { + result = i.hashCode() + super.hashCode = result + } + return result + } + + override fun toString(): String { + val result = mutableListOf() + if (i != null) result += """i=$i""" + return result.joinToString(prefix = "NestedVersionOne{", separator = ", ", postfix = "}") + } + + fun copy(i: Int? = this.i, unknownFields: ByteString = this.unknownFields): NestedVersionOne = + NestedVersionOne(i, unknownFields) + + companion object { + @JvmField + val ADAPTER: ProtoAdapter = object : ProtoAdapter( + FieldEncoding.LENGTH_DELIMITED, + NestedVersionOne::class + ) { + override fun encodedSize(value: NestedVersionOne): Int = + ProtoAdapter.INT32.encodedSizeWithTag(1, value.i) + + value.unknownFields.size + + override fun encode(writer: ProtoWriter, value: NestedVersionOne) { + ProtoAdapter.INT32.encodeWithTag(writer, 1, value.i) + writer.writeBytes(value.unknownFields) + } + + override fun decode(reader: ProtoReader): NestedVersionOne { + var i: Int? = null + val unknownFields = reader.forEachTag { tag -> + when (tag) { + 1 -> i = ProtoAdapter.INT32.decode(reader) + else -> reader.readUnknownField(tag) + } + } + return NestedVersionOne( + i = i, + unknownFields = unknownFields + ) + } + + override fun redact(value: NestedVersionOne): NestedVersionOne = value.copy( + unknownFields = ByteString.EMPTY + ) + } + } +} diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt new file mode 100644 index 0000000000..2e4fe669da --- /dev/null +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt @@ -0,0 +1,173 @@ +// Code generated by Wire protocol buffer compiler, do not edit. +// Source file: unknown_fields.proto +package com.squareup.wire.protos.kotlin.unknownfields + +import com.squareup.wire.FieldEncoding +import com.squareup.wire.Message +import com.squareup.wire.ProtoAdapter +import com.squareup.wire.ProtoReader +import com.squareup.wire.ProtoWriter +import com.squareup.wire.WireField +import kotlin.Any +import kotlin.AssertionError +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.DeprecationLevel +import kotlin.Int +import kotlin.Long +import kotlin.Nothing +import kotlin.String +import kotlin.collections.List +import kotlin.hashCode +import kotlin.jvm.JvmField +import okio.ByteString + +class NestedVersionTwo( + @field:WireField( + tag = 1, + adapter = "com.squareup.wire.ProtoAdapter#INT32" + ) + val i: Int? = null, + @field:WireField( + tag = 2, + adapter = "com.squareup.wire.ProtoAdapter#INT32" + ) + val v2_i: Int? = null, + @field:WireField( + tag = 3, + adapter = "com.squareup.wire.ProtoAdapter#STRING" + ) + val v2_s: String? = null, + @field:WireField( + tag = 4, + adapter = "com.squareup.wire.ProtoAdapter#FIXED32" + ) + val v2_f32: Int? = null, + @field:WireField( + tag = 5, + adapter = "com.squareup.wire.ProtoAdapter#FIXED64" + ) + val v2_f64: Long? = null, + @field:WireField( + tag = 6, + adapter = "com.squareup.wire.ProtoAdapter#STRING", + label = WireField.Label.REPEATED + ) + val v2_rs: List = emptyList(), + unknownFields: ByteString = ByteString.EMPTY +) : Message(ADAPTER, unknownFields) { + @Deprecated( + message = "Shouldn't be used in Kotlin", + level = DeprecationLevel.HIDDEN + ) + override fun newBuilder(): Nothing { + throw AssertionError() + } + + override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other !is NestedVersionTwo) return false + return unknownFields == other.unknownFields + && i == other.i + && v2_i == other.v2_i + && v2_s == other.v2_s + && v2_f32 == other.v2_f32 + && v2_f64 == other.v2_f64 + && v2_rs == other.v2_rs + } + + override fun hashCode(): Int { + var result = super.hashCode + if (result == 0) { + result = i.hashCode() + result = result * 37 + v2_i.hashCode() + result = result * 37 + v2_s.hashCode() + result = result * 37 + v2_f32.hashCode() + result = result * 37 + v2_f64.hashCode() + result = result * 37 + v2_rs.hashCode() + super.hashCode = result + } + return result + } + + override fun toString(): String { + val result = mutableListOf() + if (i != null) result += """i=$i""" + if (v2_i != null) result += """v2_i=$v2_i""" + if (v2_s != null) result += """v2_s=$v2_s""" + if (v2_f32 != null) result += """v2_f32=$v2_f32""" + if (v2_f64 != null) result += """v2_f64=$v2_f64""" + if (v2_rs.isNotEmpty()) result += """v2_rs=$v2_rs""" + return result.joinToString(prefix = "NestedVersionTwo{", separator = ", ", postfix = "}") + } + + fun copy( + i: Int? = this.i, + v2_i: Int? = this.v2_i, + v2_s: String? = this.v2_s, + v2_f32: Int? = this.v2_f32, + v2_f64: Long? = this.v2_f64, + v2_rs: List = this.v2_rs, + unknownFields: ByteString = this.unknownFields + ): NestedVersionTwo = NestedVersionTwo(i, v2_i, v2_s, v2_f32, v2_f64, v2_rs, unknownFields) + + companion object { + @JvmField + val ADAPTER: ProtoAdapter = object : ProtoAdapter( + FieldEncoding.LENGTH_DELIMITED, + NestedVersionTwo::class + ) { + override fun encodedSize(value: NestedVersionTwo): Int = + ProtoAdapter.INT32.encodedSizeWithTag(1, value.i) + + ProtoAdapter.INT32.encodedSizeWithTag(2, value.v2_i) + + ProtoAdapter.STRING.encodedSizeWithTag(3, value.v2_s) + + ProtoAdapter.FIXED32.encodedSizeWithTag(4, value.v2_f32) + + ProtoAdapter.FIXED64.encodedSizeWithTag(5, value.v2_f64) + + ProtoAdapter.STRING.asRepeated().encodedSizeWithTag(6, value.v2_rs) + + value.unknownFields.size + + override fun encode(writer: ProtoWriter, value: NestedVersionTwo) { + ProtoAdapter.INT32.encodeWithTag(writer, 1, value.i) + ProtoAdapter.INT32.encodeWithTag(writer, 2, value.v2_i) + ProtoAdapter.STRING.encodeWithTag(writer, 3, value.v2_s) + ProtoAdapter.FIXED32.encodeWithTag(writer, 4, value.v2_f32) + ProtoAdapter.FIXED64.encodeWithTag(writer, 5, value.v2_f64) + ProtoAdapter.STRING.asRepeated().encodeWithTag(writer, 6, value.v2_rs) + writer.writeBytes(value.unknownFields) + } + + override fun decode(reader: ProtoReader): NestedVersionTwo { + var i: Int? = null + var v2_i: Int? = null + var v2_s: String? = null + var v2_f32: Int? = null + var v2_f64: Long? = null + val v2_rs = mutableListOf() + val unknownFields = reader.forEachTag { tag -> + when (tag) { + 1 -> i = ProtoAdapter.INT32.decode(reader) + 2 -> v2_i = ProtoAdapter.INT32.decode(reader) + 3 -> v2_s = ProtoAdapter.STRING.decode(reader) + 4 -> v2_f32 = ProtoAdapter.FIXED32.decode(reader) + 5 -> v2_f64 = ProtoAdapter.FIXED64.decode(reader) + 6 -> v2_rs.add(ProtoAdapter.STRING.decode(reader)) + else -> reader.readUnknownField(tag) + } + } + return NestedVersionTwo( + i = i, + v2_i = v2_i, + v2_s = v2_s, + v2_f32 = v2_f32, + v2_f64 = v2_f64, + v2_rs = v2_rs, + unknownFields = unknownFields + ) + } + + override fun redact(value: NestedVersionTwo): NestedVersionTwo = value.copy( + unknownFields = ByteString.EMPTY + ) + } + } +} diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt new file mode 100644 index 0000000000..5af705e7c4 --- /dev/null +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt @@ -0,0 +1,115 @@ +// Code generated by Wire protocol buffer compiler, do not edit. +// Source file: unknown_fields.proto +package com.squareup.wire.protos.kotlin.unknownfields + +import com.squareup.wire.FieldEncoding +import com.squareup.wire.Message +import com.squareup.wire.ProtoAdapter +import com.squareup.wire.ProtoReader +import com.squareup.wire.ProtoWriter +import com.squareup.wire.WireField +import kotlin.Any +import kotlin.AssertionError +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.DeprecationLevel +import kotlin.Int +import kotlin.Nothing +import kotlin.String +import kotlin.hashCode +import kotlin.jvm.JvmField +import okio.ByteString + +class VersionOne( + @field:WireField( + tag = 1, + adapter = "com.squareup.wire.ProtoAdapter#INT32" + ) + val i: Int? = null, + @field:WireField( + tag = 7, + adapter = "com.squareup.wire.protos.kotlin.unknownfields.NestedVersionOne#ADAPTER" + ) + val obj: NestedVersionOne? = null, + unknownFields: ByteString = ByteString.EMPTY +) : Message(ADAPTER, unknownFields) { + @Deprecated( + message = "Shouldn't be used in Kotlin", + level = DeprecationLevel.HIDDEN + ) + override fun newBuilder(): Nothing { + throw AssertionError() + } + + override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other !is VersionOne) return false + return unknownFields == other.unknownFields + && i == other.i + && obj == other.obj + } + + override fun hashCode(): Int { + var result = super.hashCode + if (result == 0) { + result = i.hashCode() + result = result * 37 + obj.hashCode() + super.hashCode = result + } + return result + } + + override fun toString(): String { + val result = mutableListOf() + if (i != null) result += """i=$i""" + if (obj != null) result += """obj=$obj""" + return result.joinToString(prefix = "VersionOne{", separator = ", ", postfix = "}") + } + + fun copy( + i: Int? = this.i, + obj: NestedVersionOne? = this.obj, + unknownFields: ByteString = this.unknownFields + ): VersionOne = VersionOne(i, obj, unknownFields) + + companion object { + @JvmField + val ADAPTER: ProtoAdapter = object : ProtoAdapter( + FieldEncoding.LENGTH_DELIMITED, + VersionOne::class + ) { + override fun encodedSize(value: VersionOne): Int = + ProtoAdapter.INT32.encodedSizeWithTag(1, value.i) + + NestedVersionOne.ADAPTER.encodedSizeWithTag(7, value.obj) + + value.unknownFields.size + + override fun encode(writer: ProtoWriter, value: VersionOne) { + ProtoAdapter.INT32.encodeWithTag(writer, 1, value.i) + NestedVersionOne.ADAPTER.encodeWithTag(writer, 7, value.obj) + writer.writeBytes(value.unknownFields) + } + + override fun decode(reader: ProtoReader): VersionOne { + var i: Int? = null + var obj: NestedVersionOne? = null + val unknownFields = reader.forEachTag { tag -> + when (tag) { + 1 -> i = ProtoAdapter.INT32.decode(reader) + 7 -> obj = NestedVersionOne.ADAPTER.decode(reader) + else -> reader.readUnknownField(tag) + } + } + return VersionOne( + i = i, + obj = obj, + unknownFields = unknownFields + ) + } + + override fun redact(value: VersionOne): VersionOne = value.copy( + obj = value.obj?.let(NestedVersionOne.ADAPTER::redact), + unknownFields = ByteString.EMPTY + ) + } + } +} diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt new file mode 100644 index 0000000000..0ca5d1dac5 --- /dev/null +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt @@ -0,0 +1,188 @@ +// Code generated by Wire protocol buffer compiler, do not edit. +// Source file: unknown_fields.proto +package com.squareup.wire.protos.kotlin.unknownfields + +import com.squareup.wire.FieldEncoding +import com.squareup.wire.Message +import com.squareup.wire.ProtoAdapter +import com.squareup.wire.ProtoReader +import com.squareup.wire.ProtoWriter +import com.squareup.wire.WireField +import kotlin.Any +import kotlin.AssertionError +import kotlin.Boolean +import kotlin.Deprecated +import kotlin.DeprecationLevel +import kotlin.Int +import kotlin.Long +import kotlin.Nothing +import kotlin.String +import kotlin.collections.List +import kotlin.hashCode +import kotlin.jvm.JvmField +import okio.ByteString + +class VersionTwo( + @field:WireField( + tag = 1, + adapter = "com.squareup.wire.ProtoAdapter#INT32" + ) + val i: Int? = null, + @field:WireField( + tag = 2, + adapter = "com.squareup.wire.ProtoAdapter#INT32" + ) + val v2_i: Int? = null, + @field:WireField( + tag = 3, + adapter = "com.squareup.wire.ProtoAdapter#STRING" + ) + val v2_s: String? = null, + @field:WireField( + tag = 4, + adapter = "com.squareup.wire.ProtoAdapter#FIXED32" + ) + val v2_f32: Int? = null, + @field:WireField( + tag = 5, + adapter = "com.squareup.wire.ProtoAdapter#FIXED64" + ) + val v2_f64: Long? = null, + @field:WireField( + tag = 6, + adapter = "com.squareup.wire.ProtoAdapter#STRING", + label = WireField.Label.REPEATED + ) + val v2_rs: List = emptyList(), + @field:WireField( + tag = 7, + adapter = "com.squareup.wire.protos.kotlin.unknownfields.NestedVersionTwo#ADAPTER" + ) + val obj: NestedVersionTwo? = null, + unknownFields: ByteString = ByteString.EMPTY +) : Message(ADAPTER, unknownFields) { + @Deprecated( + message = "Shouldn't be used in Kotlin", + level = DeprecationLevel.HIDDEN + ) + override fun newBuilder(): Nothing { + throw AssertionError() + } + + override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other !is VersionTwo) return false + return unknownFields == other.unknownFields + && i == other.i + && v2_i == other.v2_i + && v2_s == other.v2_s + && v2_f32 == other.v2_f32 + && v2_f64 == other.v2_f64 + && v2_rs == other.v2_rs + && obj == other.obj + } + + override fun hashCode(): Int { + var result = super.hashCode + if (result == 0) { + result = i.hashCode() + result = result * 37 + v2_i.hashCode() + result = result * 37 + v2_s.hashCode() + result = result * 37 + v2_f32.hashCode() + result = result * 37 + v2_f64.hashCode() + result = result * 37 + v2_rs.hashCode() + result = result * 37 + obj.hashCode() + super.hashCode = result + } + return result + } + + override fun toString(): String { + val result = mutableListOf() + if (i != null) result += """i=$i""" + if (v2_i != null) result += """v2_i=$v2_i""" + if (v2_s != null) result += """v2_s=$v2_s""" + if (v2_f32 != null) result += """v2_f32=$v2_f32""" + if (v2_f64 != null) result += """v2_f64=$v2_f64""" + if (v2_rs.isNotEmpty()) result += """v2_rs=$v2_rs""" + if (obj != null) result += """obj=$obj""" + return result.joinToString(prefix = "VersionTwo{", separator = ", ", postfix = "}") + } + + fun copy( + i: Int? = this.i, + v2_i: Int? = this.v2_i, + v2_s: String? = this.v2_s, + v2_f32: Int? = this.v2_f32, + v2_f64: Long? = this.v2_f64, + v2_rs: List = this.v2_rs, + obj: NestedVersionTwo? = this.obj, + unknownFields: ByteString = this.unknownFields + ): VersionTwo = VersionTwo(i, v2_i, v2_s, v2_f32, v2_f64, v2_rs, obj, unknownFields) + + companion object { + @JvmField + val ADAPTER: ProtoAdapter = object : ProtoAdapter( + FieldEncoding.LENGTH_DELIMITED, + VersionTwo::class + ) { + override fun encodedSize(value: VersionTwo): Int = + ProtoAdapter.INT32.encodedSizeWithTag(1, value.i) + + ProtoAdapter.INT32.encodedSizeWithTag(2, value.v2_i) + + ProtoAdapter.STRING.encodedSizeWithTag(3, value.v2_s) + + ProtoAdapter.FIXED32.encodedSizeWithTag(4, value.v2_f32) + + ProtoAdapter.FIXED64.encodedSizeWithTag(5, value.v2_f64) + + ProtoAdapter.STRING.asRepeated().encodedSizeWithTag(6, value.v2_rs) + + NestedVersionTwo.ADAPTER.encodedSizeWithTag(7, value.obj) + + value.unknownFields.size + + override fun encode(writer: ProtoWriter, value: VersionTwo) { + ProtoAdapter.INT32.encodeWithTag(writer, 1, value.i) + ProtoAdapter.INT32.encodeWithTag(writer, 2, value.v2_i) + ProtoAdapter.STRING.encodeWithTag(writer, 3, value.v2_s) + ProtoAdapter.FIXED32.encodeWithTag(writer, 4, value.v2_f32) + ProtoAdapter.FIXED64.encodeWithTag(writer, 5, value.v2_f64) + ProtoAdapter.STRING.asRepeated().encodeWithTag(writer, 6, value.v2_rs) + NestedVersionTwo.ADAPTER.encodeWithTag(writer, 7, value.obj) + writer.writeBytes(value.unknownFields) + } + + override fun decode(reader: ProtoReader): VersionTwo { + var i: Int? = null + var v2_i: Int? = null + var v2_s: String? = null + var v2_f32: Int? = null + var v2_f64: Long? = null + val v2_rs = mutableListOf() + var obj: NestedVersionTwo? = null + val unknownFields = reader.forEachTag { tag -> + when (tag) { + 1 -> i = ProtoAdapter.INT32.decode(reader) + 2 -> v2_i = ProtoAdapter.INT32.decode(reader) + 3 -> v2_s = ProtoAdapter.STRING.decode(reader) + 4 -> v2_f32 = ProtoAdapter.FIXED32.decode(reader) + 5 -> v2_f64 = ProtoAdapter.FIXED64.decode(reader) + 6 -> v2_rs.add(ProtoAdapter.STRING.decode(reader)) + 7 -> obj = NestedVersionTwo.ADAPTER.decode(reader) + else -> reader.readUnknownField(tag) + } + } + return VersionTwo( + i = i, + v2_i = v2_i, + v2_s = v2_s, + v2_f32 = v2_f32, + v2_f64 = v2_f64, + v2_rs = v2_rs, + obj = obj, + unknownFields = unknownFields + ) + } + + override fun redact(value: VersionTwo): VersionTwo = value.copy( + obj = value.obj?.let(NestedVersionTwo.ADAPTER::redact), + unknownFields = ByteString.EMPTY + ) + } + } +} diff --git a/wire-tests/src/commonTest/proto/kotlin/unknown_fields.proto b/wire-tests/src/commonTest/proto/kotlin/unknown_fields.proto new file mode 100644 index 0000000000..c8884499db --- /dev/null +++ b/wire-tests/src/commonTest/proto/kotlin/unknown_fields.proto @@ -0,0 +1,50 @@ +/* + * Copyright 2013 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 squareup.protos.kotlin.unknownfields; + +option java_package = "com.squareup.wire.protos.kotlin.unknownfields"; + +message VersionOne { + optional int32 i = 1; + + optional NestedVersionOne obj = 7; +} + +message NestedVersionOne { + optional int32 i = 1; +} + +message VersionTwo { + optional int32 i = 1; + + optional int32 v2_i = 2; + optional string v2_s = 3; + optional fixed32 v2_f32 = 4; + optional fixed64 v2_f64 = 5; + repeated string v2_rs = 6; + + optional NestedVersionTwo obj = 7; +} + +message NestedVersionTwo { + optional int32 i = 1; + + optional int32 v2_i = 2; + optional string v2_s = 3; + optional fixed32 v2_f32 = 4; + optional fixed64 v2_f64 = 5; + repeated string v2_rs = 6; +} From 0e725eb8f927555ff4f2b05cb1cf5c9a3896a8a3 Mon Sep 17 00:00:00 2001 From: Egor Andreevici Date: Sun, 15 Sep 2019 15:35:39 -0400 Subject: [PATCH 2/4] Fix asserts, use copy() instead of withoutUnknownFields() --- .../kotlin/com/squareup/wire/Asserts.kt | 23 +++++++++++++++++++ .../com/squareup/wire/UnknownFieldsTest.kt | 12 ++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/Asserts.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/Asserts.kt index 976fbe8010..7ade0123b1 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/Asserts.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/Asserts.kt @@ -26,3 +26,26 @@ fun assertFloatEquals(expected: Float, actual: Float, delta: Float = 0.01f) { " but was <$diff>.") } } + +fun assertArrayEquals(expected: ByteArray, actual: ByteArray) { + if (expected === actual) return + if (actual.size != expected.size) { + fail("Expected array of length <${expected.size}> but was <${actual.size}>.") + } + for (i in expected.indices) { + if (actual[i] != expected[i]) { + fail("Expected element at position <$i> to be <${expected[i]}> but was <${actual[i]}>.") + } + } +} + +fun assertArrayNotEquals(expected: ByteArray, actual: ByteArray) { + if (expected === actual) { + fail("Expected $actual to not be equal to $expected.") + } + if (actual.size != expected.size) return + for (i in expected.indices) { + if (actual[i] != expected[i]) return + } + fail("Expected ${actual.contentToString()} to not be equal to ${expected.contentToString()}.") +} diff --git a/wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt b/wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt index d2ffb6b449..475248bdc4 100644 --- a/wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt +++ b/wire-tests/src/commonTest/kotlin/com/squareup/wire/UnknownFieldsTest.kt @@ -19,6 +19,7 @@ import com.squareup.wire.protos.kotlin.unknownfields.NestedVersionOne import com.squareup.wire.protos.kotlin.unknownfields.NestedVersionTwo import com.squareup.wire.protos.kotlin.unknownfields.VersionOne import com.squareup.wire.protos.kotlin.unknownfields.VersionTwo +import okio.ByteString import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotEquals @@ -63,7 +64,7 @@ class UnknownFieldsTest { val v1 = v1Adapter.decode(v2Bytes) // v.1 fields are visible, v.2 fields are in unknownFieldSet assertEquals(111, v1.i) - assertEquals(v1_obj, v1.obj!!.withoutUnknownFields()) + assertEquals(v1_obj, v1.obj!!.copy(unknownFields = ByteString.EMPTY)) // Serialized output should still contain the v.2 fields val v1Bytes = v1Adapter.encode(v1) @@ -71,13 +72,16 @@ class UnknownFieldsTest { val v1Simple = VersionOne(i = 111, obj = v1_obj) assertNotEquals(v1Simple, v1) assertNotEquals(v1Simple.hashCode(), v1.hashCode()) - assertNotEquals(v1Adapter.encode(v1Simple), v1Adapter.encode(v1)) + assertArrayNotEquals(v1Adapter.encode(v1Simple), v1Adapter.encode(v1)) // Unknown fields can be removed for equals() and hashCode(); - val v1Known = v1.withoutUnknownFields().copy(obj = v1.obj.withoutUnknownFields()) + val v1Known = v1.copy( + obj = v1.obj.copy(unknownFields = ByteString.EMPTY), + unknownFields = ByteString.EMPTY + ) assertEquals(v1Simple, v1Known) assertEquals(v1Simple.hashCode(), v1Known.hashCode()) - assertEquals(v1Adapter.encode(v1Simple), v1Adapter.encode(v1Known)) + assertArrayEquals(v1Adapter.encode(v1Simple), v1Adapter.encode(v1Known)) // Re-parse val v2B = v2Adapter.decode(v1Bytes) From 34b0361382d4fe0646a971617e7a84ec2aa9fdd1 Mon Sep 17 00:00:00 2001 From: Egor Andreevici Date: Mon, 16 Sep 2019 10:37:21 -0400 Subject: [PATCH 3/4] Regenerate test protos --- .../wire/protos/kotlin/unknownfields/NestedVersionOne.kt | 3 ++- .../wire/protos/kotlin/unknownfields/NestedVersionTwo.kt | 3 ++- .../squareup/wire/protos/kotlin/unknownfields/VersionOne.kt | 3 ++- .../squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt index 7bb4a2415c..fe301d70da 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionOne.kt @@ -46,7 +46,8 @@ class NestedVersionOne( override fun hashCode(): Int { var result = super.hashCode if (result == 0) { - result = i.hashCode() + result = unknownFields.hashCode() + result = result * 37 + i.hashCode() super.hashCode = result } return result diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt index 2e4fe669da..f7be828ed0 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/NestedVersionTwo.kt @@ -79,7 +79,8 @@ class NestedVersionTwo( override fun hashCode(): Int { var result = super.hashCode if (result == 0) { - result = i.hashCode() + result = unknownFields.hashCode() + result = result * 37 + i.hashCode() result = result * 37 + v2_i.hashCode() result = result * 37 + v2_s.hashCode() result = result * 37 + v2_f32.hashCode() diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt index 5af705e7c4..06c8f0eea6 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionOne.kt @@ -52,7 +52,8 @@ class VersionOne( override fun hashCode(): Int { var result = super.hashCode if (result == 0) { - result = i.hashCode() + result = unknownFields.hashCode() + result = result * 37 + i.hashCode() result = result * 37 + obj.hashCode() super.hashCode = result } diff --git a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt index 0ca5d1dac5..9cf194e09d 100644 --- a/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt +++ b/wire-tests/src/commonTest/proto-kotlin/com/squareup/wire/protos/kotlin/unknownfields/VersionTwo.kt @@ -85,7 +85,8 @@ class VersionTwo( override fun hashCode(): Int { var result = super.hashCode if (result == 0) { - result = i.hashCode() + result = unknownFields.hashCode() + result = result * 37 + i.hashCode() result = result * 37 + v2_i.hashCode() result = result * 37 + v2_s.hashCode() result = result * 37 + v2_f32.hashCode() From c801aa9f93d5bd0f4694d65449791b5f7aa1d730 Mon Sep 17 00:00:00 2001 From: Egor Andreevici Date: Mon, 16 Sep 2019 16:02:46 -0400 Subject: [PATCH 4/4] Bump to Okio snapshot --- build.gradle | 3 ++- .../src/test/projects/java-project-java-protos/build.gradle | 1 + .../src/test/projects/java-project-kotlin-protos/build.gradle | 1 + .../src/test/projects/kotlin-project-java-protos/build.gradle | 1 + .../test/projects/kotlin-project-kotlin-protos/build.gradle | 1 + 5 files changed, 6 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 4135ab4692..1d678d12aa 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { 'kotlinpoet': '1.3.0', 'jsr305': '3.0.2', 'kotlin': '1.3.50', - 'okio': '2.4.0', + 'okio': '2.5.0-SNAPSHOT', 'okhttp': '4.1.0', 'moshi': '1.6.0', 'protobuf': '0.8.10', @@ -118,6 +118,7 @@ allprojects { repositories { mavenCentral() google() + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } } diff --git a/wire-gradle-plugin/src/test/projects/java-project-java-protos/build.gradle b/wire-gradle-plugin/src/test/projects/java-project-java-protos/build.gradle index 066dfa789c..b79d5e0e30 100644 --- a/wire-gradle-plugin/src/test/projects/java-project-java-protos/build.gradle +++ b/wire-gradle-plugin/src/test/projects/java-project-java-protos/build.gradle @@ -10,6 +10,7 @@ repositories { url "file://${projectDir.absolutePath}/../../../../../build/localMaven" } mavenCentral() + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { diff --git a/wire-gradle-plugin/src/test/projects/java-project-kotlin-protos/build.gradle b/wire-gradle-plugin/src/test/projects/java-project-kotlin-protos/build.gradle index d6732621b3..58b5470dab 100644 --- a/wire-gradle-plugin/src/test/projects/java-project-kotlin-protos/build.gradle +++ b/wire-gradle-plugin/src/test/projects/java-project-kotlin-protos/build.gradle @@ -11,6 +11,7 @@ repositories { url "file://${projectDir.absolutePath}/../../../../../build/localMaven" } mavenCentral() + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { diff --git a/wire-gradle-plugin/src/test/projects/kotlin-project-java-protos/build.gradle b/wire-gradle-plugin/src/test/projects/kotlin-project-java-protos/build.gradle index e42e186eb5..89f8765eef 100644 --- a/wire-gradle-plugin/src/test/projects/kotlin-project-java-protos/build.gradle +++ b/wire-gradle-plugin/src/test/projects/kotlin-project-java-protos/build.gradle @@ -11,6 +11,7 @@ repositories { url "file://${projectDir.absolutePath}/../../../../../build/localMaven" } mavenCentral() + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { diff --git a/wire-gradle-plugin/src/test/projects/kotlin-project-kotlin-protos/build.gradle b/wire-gradle-plugin/src/test/projects/kotlin-project-kotlin-protos/build.gradle index ba67f3e98d..b44d3c954b 100644 --- a/wire-gradle-plugin/src/test/projects/kotlin-project-kotlin-protos/build.gradle +++ b/wire-gradle-plugin/src/test/projects/kotlin-project-kotlin-protos/build.gradle @@ -11,6 +11,7 @@ repositories { url "file://${projectDir.absolutePath}/../../../../../build/localMaven" } mavenCentral() + maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies {