Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -118,6 +118,7 @@ allprojects {
repositories {
mavenCentral()
google()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
}

Expand Down
3 changes: 2 additions & 1 deletion gen-tests.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {
url "file://${projectDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repositories {
url "file://${projectDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repositories {
url "file://${projectDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repositories {
url "file://${projectDir.absolutePath}/../../../../../build/localMaven"
}
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}

dependencies {
Expand Down
23 changes: 23 additions & 0 deletions wire-tests/src/commonTest/kotlin/com/squareup/wire/Asserts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()}.")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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 okio.ByteString
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!!.copy(unknownFields = ByteString.EMPTY))
// 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())
assertArrayNotEquals(v1Adapter.encode(v1Simple), v1Adapter.encode(v1))

// Unknown fields can be removed for equals() and hashCode();
val v1Known = v1.copy(
obj = v1.obj.copy(unknownFields = ByteString.EMPTY),
unknownFields = ByteString.EMPTY
)
assertEquals(v1Simple, v1Known)
assertEquals(v1Simple.hashCode(), v1Known.hashCode())
assertArrayEquals(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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// 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<NestedVersionOne, Nothing>(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 = unknownFields.hashCode()
result = result * 37 + i.hashCode()
super.hashCode = result
}
return result
}

override fun toString(): String {
val result = mutableListOf<String>()
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<NestedVersionOne> = object : ProtoAdapter<NestedVersionOne>(
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
)
}
}
}
Loading