Skip to content

Commit 365872a

Browse files
committed
Protobuf conformance fixes (#454)
1 parent 4b7e035 commit 365872a

File tree

21 files changed

+823
-261
lines changed

21 files changed

+823
-261
lines changed

gradle-conventions/src/main/kotlin/conventions-protoc-gen.gradle.kts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,19 @@ dependencies {
1818
testImplementation(libs.kotlin.test)
1919
}
2020

21-
tasks.jar {
22-
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
23-
archiveClassifier = "all"
24-
25-
// Protoc plugins are all fat jars basically (the ones built on jvm)
26-
// be really careful of what you put in the classpath here
27-
from(
28-
configurations.runtimeClasspath.map { prop ->
29-
prop.map { if (it.isDirectory()) it else zipTree(it) }
30-
}
31-
)
21+
if (project.name != "common") {
22+
tasks.jar {
23+
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
24+
archiveClassifier = "all"
25+
26+
// Protoc plugins are all fat jars basically (the ones built on jvm)
27+
// be really careful of what you put in the classpath here
28+
from(
29+
configurations.runtimeClasspath.map { prop ->
30+
prop.map { if (it.isDirectory()) it else zipTree(it) }
31+
}
32+
)
33+
}
3234
}
3335

3436
kotlin {

gradle-conventions/src/main/kotlin/util/other/localProperties.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.gradle.kotlin.dsl.provideDelegate
1010
import java.util.Properties
1111
import java.util.concurrent.atomic.AtomicReference
1212
import kotlin.io.path.Path
13+
import kotlin.io.path.exists
1314
import kotlin.io.path.inputStream
1415

1516
private val ref = AtomicReference<Properties>()
@@ -19,7 +20,13 @@ fun Project.localProperties(): Properties {
1920
ref.compareAndSet(null, Properties().apply {
2021
val globalRootDir: String by extra
2122

22-
load(Path(globalRootDir, "local.properties").inputStream())
23+
val filepath = Path(globalRootDir, "local.properties")
24+
25+
if (!filepath.exists()) {
26+
return@apply
27+
}
28+
29+
load(filepath.inputStream())
2330
})
2431
}
2532

protobuf/protobuf-core/src/commonMain/generated-code/kotlin-multiplatform/com/google/protobuf/kotlin/_rpc_internal/Api.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public fun com.google.protobuf.kotlin.ApiInternal.encodeWith(encoder: kotlinx.rp
194194
}
195195
}
196196

197-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
197+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
198198
encoder.writeEnum(fieldNr = 7, value = syntax.number)
199199
}
200200
}
@@ -257,11 +257,11 @@ private fun com.google.protobuf.kotlin.ApiInternal.computeSize(): Int {
257257
}
258258

259259
if (methods.isNotEmpty()) {
260-
__result = methods.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
260+
__result += methods.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
261261
}
262262

263263
if (options.isNotEmpty()) {
264-
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
264+
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
265265
}
266266

267267
if (version.isNotEmpty()) {
@@ -273,10 +273,10 @@ private fun com.google.protobuf.kotlin.ApiInternal.computeSize(): Int {
273273
}
274274

275275
if (mixins.isNotEmpty()) {
276-
__result = mixins.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
276+
__result += mixins.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
277277
}
278278

279-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
279+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
280280
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(7, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
281281
}
282282

@@ -324,7 +324,7 @@ public fun com.google.protobuf.kotlin.MethodInternal.encodeWith(encoder: kotlinx
324324
}
325325
}
326326

327-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
327+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
328328
encoder.writeEnum(fieldNr = 7, value = syntax.number)
329329
}
330330
}
@@ -395,10 +395,10 @@ private fun com.google.protobuf.kotlin.MethodInternal.computeSize(): Int {
395395
}
396396

397397
if (options.isNotEmpty()) {
398-
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
398+
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
399399
}
400400

401-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
401+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
402402
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(7, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
403403
}
404404

protobuf/protobuf-core/src/commonMain/generated-code/kotlin-multiplatform/com/google/protobuf/kotlin/_rpc_internal/FieldMask.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public fun com.google.protobuf.kotlin.FieldMaskInternal.Companion.decodeWith(msg
8080
private fun com.google.protobuf.kotlin.FieldMaskInternal.computeSize(): Int {
8181
var __result = 0
8282
if (paths.isNotEmpty()) {
83-
__result = paths.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
83+
__result += paths.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
8484
}
8585

8686
return __result

protobuf/protobuf-core/src/commonMain/generated-code/kotlin-multiplatform/com/google/protobuf/kotlin/_rpc_internal/Struct.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ public fun com.google.protobuf.kotlin.ListValueInternal.Companion.decodeWith(msg
372372
private fun com.google.protobuf.kotlin.ListValueInternal.computeSize(): Int {
373373
var __result = 0
374374
if (values.isNotEmpty()) {
375-
__result = values.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
375+
__result += values.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
376376
}
377377

378378
return __result

protobuf/protobuf-core/src/commonMain/generated-code/kotlin-multiplatform/com/google/protobuf/kotlin/_rpc_internal/Type.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public fun com.google.protobuf.kotlin.TypeInternal.encodeWith(encoder: kotlinx.r
284284
encoder.writeMessage(fieldNr = 5, value = sourceContext.asInternal()) { encodeWith(it) }
285285
}
286286

287-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
287+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
288288
encoder.writeEnum(fieldNr = 6, value = syntax.number)
289289
}
290290

@@ -350,22 +350,22 @@ private fun com.google.protobuf.kotlin.TypeInternal.computeSize(): Int {
350350
}
351351

352352
if (fields.isNotEmpty()) {
353-
__result = fields.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
353+
__result += fields.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
354354
}
355355

356356
if (oneofs.isNotEmpty()) {
357-
__result = oneofs.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
357+
__result += oneofs.sumOf { kotlinx.rpc.protobuf.internal.WireSize.string(it) + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
358358
}
359359

360360
if (options.isNotEmpty()) {
361-
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(4, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
361+
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(4, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
362362
}
363363

364364
if (presenceMask[0]) {
365365
__result += sourceContext.asInternal()._size.let { kotlinx.rpc.protobuf.internal.WireSize.tag(5, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) + kotlinx.rpc.protobuf.internal.WireSize.int32(it) + it }
366366
}
367367

368-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
368+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
369369
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(6, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
370370
}
371371

@@ -391,11 +391,11 @@ public fun com.google.protobuf.kotlin.FieldInternal.checkRequiredFields() {
391391

392392
@kotlinx.rpc.internal.utils.InternalRpcApi
393393
public fun com.google.protobuf.kotlin.FieldInternal.encodeWith(encoder: kotlinx.rpc.protobuf.internal.WireEncoder) {
394-
if (com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN != kind) {
394+
if (kind != com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN) {
395395
encoder.writeEnum(fieldNr = 1, value = kind.number)
396396
}
397397

398-
if (com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN != cardinality) {
398+
if (cardinality != com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN) {
399399
encoder.writeEnum(fieldNr = 2, value = cardinality.number)
400400
}
401401

@@ -491,11 +491,11 @@ public fun com.google.protobuf.kotlin.FieldInternal.Companion.decodeWith(msg: co
491491

492492
private fun com.google.protobuf.kotlin.FieldInternal.computeSize(): Int {
493493
var __result = 0
494-
if (com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN != kind) {
494+
if (kind != com.google.protobuf.kotlin.Field.Kind.TYPE_UNKNOWN) {
495495
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(1, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(kind.number))
496496
}
497497

498-
if (com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN != cardinality) {
498+
if (cardinality != com.google.protobuf.kotlin.Field.Cardinality.CARDINALITY_UNKNOWN) {
499499
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(cardinality.number))
500500
}
501501

@@ -520,7 +520,7 @@ private fun com.google.protobuf.kotlin.FieldInternal.computeSize(): Int {
520520
}
521521

522522
if (options.isNotEmpty()) {
523-
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(9, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
523+
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(9, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
524524
}
525525

526526
if (jsonName.isNotEmpty()) {
@@ -577,7 +577,7 @@ public fun com.google.protobuf.kotlin.EnumInternal.encodeWith(encoder: kotlinx.r
577577
encoder.writeMessage(fieldNr = 4, value = sourceContext.asInternal()) { encodeWith(it) }
578578
}
579579

580-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
580+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
581581
encoder.writeEnum(fieldNr = 5, value = syntax.number)
582582
}
583583

@@ -638,18 +638,18 @@ private fun com.google.protobuf.kotlin.EnumInternal.computeSize(): Int {
638638
}
639639

640640
if (enumvalue.isNotEmpty()) {
641-
__result = enumvalue.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
641+
__result += enumvalue.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(2, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
642642
}
643643

644644
if (options.isNotEmpty()) {
645-
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
645+
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
646646
}
647647

648648
if (presenceMask[0]) {
649649
__result += sourceContext.asInternal()._size.let { kotlinx.rpc.protobuf.internal.WireSize.tag(4, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) + kotlinx.rpc.protobuf.internal.WireSize.int32(it) + it }
650650
}
651651

652-
if (com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2 != syntax) {
652+
if (syntax != com.google.protobuf.kotlin.Syntax.SYNTAX_PROTO2) {
653653
__result += (kotlinx.rpc.protobuf.internal.WireSize.tag(5, kotlinx.rpc.protobuf.internal.WireType.VARINT) + kotlinx.rpc.protobuf.internal.WireSize.enum(syntax.number))
654654
}
655655

@@ -728,7 +728,7 @@ private fun com.google.protobuf.kotlin.EnumValueInternal.computeSize(): Int {
728728
}
729729

730730
if (options.isNotEmpty()) {
731-
__result = options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
731+
__result += options.sumOf { it.asInternal()._size + kotlinx.rpc.protobuf.internal.WireSize.tag(3, kotlinx.rpc.protobuf.internal.WireType.LENGTH_DELIMITED) }
732732
}
733733

734734
return __result

protobuf/protobuf-core/src/commonMain/kotlin/kotlinx/rpc/protobuf/internal/WireDecoder.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public interface WireDecoder : AutoCloseable {
8484
if (len < 0) throw ProtobufDecodingException.negativeSize()
8585
val limit = pushLimit(len)
8686
decoder(msg, this)
87+
if (bytesUntilLimit() != 0) {
88+
throw ProtobufDecodingException.truncatedMessage()
89+
}
90+
8791
popLimit(limit)
8892
}
8993

protobuf/protobuf-core/src/commonMain/kotlin/kotlinx/rpc/protobuf/internal/WireSize.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ public expect fun WireSize.sInt32(value: Int): Int
3535
public expect fun WireSize.sInt64(value: Long): Int
3636

3737
@InternalRpcApi
38-
public fun WireSize.float(value: Float): Int = 32
38+
public fun WireSize.float(value: Float): Int = 4
3939

4040
@InternalRpcApi
41-
public fun WireSize.double(value: Double): Int = 64
41+
public fun WireSize.double(value: Double): Int = 8
4242

4343
@InternalRpcApi
44-
public fun WireSize.fixed32(value: UInt): Int = 32
44+
public fun WireSize.fixed32(value: UInt): Int = 4
4545

4646
@InternalRpcApi
47-
public fun WireSize.fixed64(value: ULong): Int = 64
47+
public fun WireSize.fixed64(value: ULong): Int = 8
4848

4949
@InternalRpcApi
50-
public fun WireSize.sFixed32(value: Int): Int = 32
50+
public fun WireSize.sFixed32(value: Int): Int = 4
5151

5252
@InternalRpcApi
53-
public fun WireSize.sFixed64(value: Long): Int = 64
53+
public fun WireSize.sFixed64(value: Long): Int = 8
5454

5555
@InternalRpcApi
5656
public fun WireSize.bool(value: Boolean): Int = int32(if (value) 1 else 0)
@@ -86,22 +86,22 @@ public fun WireSize.packedSInt64(value: List<Long>): Int = value.sumOf { sInt64(
8686
public fun WireSize.packedEnum(value: List<Int>): Int = value.sumOf { enum(it) }
8787

8888
@InternalRpcApi
89-
public fun WireSize.packedFloat(value: List<Float>): Int = value.size * 32
89+
public fun WireSize.packedFloat(value: List<Float>): Int = value.size * 4
9090

9191
@InternalRpcApi
92-
public fun WireSize.packedDouble(value: List<Double>): Int = value.size * 64
92+
public fun WireSize.packedDouble(value: List<Double>): Int = value.size * 8
9393

9494
@InternalRpcApi
95-
public fun WireSize.packedFixed32(value: List<UInt>): Int = value.size * 32
95+
public fun WireSize.packedFixed32(value: List<UInt>): Int = value.size * 4
9696

9797
@InternalRpcApi
98-
public fun WireSize.packedFixed64(value: List<ULong>): Int = value.size * 64
98+
public fun WireSize.packedFixed64(value: List<ULong>): Int = value.size * 8
9999

100100
@InternalRpcApi
101-
public fun WireSize.packedSFixed32(value: List<Int>): Int = value.size * 32
101+
public fun WireSize.packedSFixed32(value: List<Int>): Int = value.size * 4
102102

103103
@InternalRpcApi
104-
public fun WireSize.packedSFixed64(value: List<Long>): Int = value.size * 64
104+
public fun WireSize.packedSFixed64(value: List<Long>): Int = value.size * 8
105105

106106
@InternalRpcApi
107107
public fun WireSize.packedBool(value: List<Boolean>): Int = packedSInt32(value.map { if (it) 1 else 0 })

protobuf/protobuf-core/src/commonTest/proto/all_primitives.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ message AllPrimitives {
1818
optional bool bool = 13;
1919
optional string string = 14;
2020
optional bytes bytes = 15;
21+
string requiredString = 16;
22+
bytes requiredBytes = 17;
2123
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
syntax = "proto2";
2+
3+
// https://protobuf.dev/programming-guides/proto2/#default
4+
message WithDefaults {
5+
optional double double = 1 [default = 2.0];
6+
optional float float = 2 [default = 2.0];
7+
optional int32 int32 = 3 [default = 2];
8+
optional int64 int64 = 4 [default = 2];
9+
optional uint32 uint32 = 5 [default = 2];
10+
optional uint64 uint64 = 6 [default = 2];
11+
optional sint32 sint32 = 7 [default = 2];
12+
optional sint64 sint64 = 8 [default = 2];
13+
optional fixed32 fixed32 = 9 [default = 2];
14+
optional fixed64 fixed64 = 10 [default = 2];
15+
optional sfixed32 sfixed32 = 11 [default = 2];
16+
optional sfixed64 sfixed64 = 12 [default = 2];
17+
optional bool bool = 13 [default = true];
18+
optional string string = 14 [default = "str"];
19+
optional bytes bytes = 15 [default = "bytes"];
20+
21+
optional Custom1 enum1 = 16;
22+
optional Custom2 enum2 = 17 [default = BAZ];
23+
24+
enum Custom1 {
25+
FOO = 1; // default
26+
}
27+
28+
enum Custom2 {
29+
BAR = 1; // default
30+
BAZ = 2; // default
31+
}
32+
}

0 commit comments

Comments
 (0)