Skip to content

Commit 4e545e0

Browse files
committed
grpc-native: Support for repeated fields in Message generation
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 49db8e1 commit 4e545e0

File tree

23 files changed

+423
-125
lines changed

23 files changed

+423
-125
lines changed

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/pb/WireSize.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ package kotlinx.rpc.grpc.pb
66

77
internal object WireSize
88

9-
internal expect fun WireSize.int32(value: Int): UInt
10-
internal expect fun WireSize.int64(value: Long): UInt
11-
internal expect fun WireSize.uInt32(value: UInt): UInt
12-
internal expect fun WireSize.uInt64(value: ULong): UInt
13-
internal expect fun WireSize.sInt32(value: Int): UInt
14-
internal expect fun WireSize.sInt64(value: Long): UInt
9+
internal expect fun WireSize.int32(value: Int): Int
10+
internal expect fun WireSize.int64(value: Long): Int
11+
internal expect fun WireSize.uInt32(value: UInt): Int
12+
internal expect fun WireSize.uInt64(value: ULong): Int
13+
internal expect fun WireSize.sInt32(value: Int): Int
14+
internal expect fun WireSize.sInt64(value: Long): Int
1515

1616
internal fun WireSize.bool(value: Boolean) = int32(if (value) 1 else 0)
1717
internal fun WireSize.enum(value: Int) = int32(value)

grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/internal/WireCodecTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,13 +734,13 @@ class WireCodecTest {
734734

735735
private fun <T> runPackedVarTest(
736736
list: List<T>,
737-
sizeFn: (List<T>) -> UInt,
737+
sizeFn: (List<T>) -> Int,
738738
write: WireEncoder.(Int, List<T>, Int) -> Boolean,
739739
read: WireDecoder.() -> List<T>?,
740740
) {
741741
val buf = Buffer()
742742
with(WireEncoder(buf)) {
743-
assertTrue(write(1, list, sizeFn(list).toInt()))
743+
assertTrue(write(1, list, sizeFn(list)))
744744
flush()
745745
}
746746
WireDecoder(buf).use { dec ->

grpc/grpc-core/src/jsMain/kotlin/kotlinx/rpc/grpc/pb/WireSize.js.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@
44

55
package kotlinx.rpc.grpc.pb
66

7-
internal actual fun WireSize.int32(value: Int): UInt {
7+
internal actual fun WireSize.int32(value: Int): Int {
88
TODO("Not yet implemented")
99
}
1010

11-
internal actual fun WireSize.int64(value: Long): UInt {
11+
internal actual fun WireSize.int64(value: Long): Int {
1212
TODO("Not yet implemented")
1313
}
1414

15-
internal actual fun WireSize.uInt32(value: UInt): UInt {
15+
internal actual fun WireSize.uInt32(value: UInt): Int {
1616
TODO("Not yet implemented")
1717
}
1818

19-
internal actual fun WireSize.uInt64(value: ULong): UInt {
19+
internal actual fun WireSize.uInt64(value: ULong): Int {
2020
TODO("Not yet implemented")
2121
}
2222

23-
internal actual fun WireSize.sInt32(value: Int): UInt {
23+
internal actual fun WireSize.sInt32(value: Int): Int {
2424
TODO("Not yet implemented")
2525
}
2626

27-
internal actual fun WireSize.sInt64(value: Long): UInt {
27+
internal actual fun WireSize.sInt64(value: Long): Int {
2828
TODO("Not yet implemented")
2929
}

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/pb/WireSize.native.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ package kotlinx.rpc.grpc.pb
99
import kotlinx.cinterop.ExperimentalForeignApi
1010
import libprotowire.*
1111

12-
internal actual fun WireSize.int32(value: Int) = pw_size_int32(value)
13-
internal actual fun WireSize.int64(value: Long) = pw_size_int64(value)
14-
internal actual fun WireSize.uInt32(value: UInt) = pw_size_uint32(value)
15-
internal actual fun WireSize.uInt64(value: ULong) = pw_size_uint64(value)
16-
internal actual fun WireSize.sInt32(value: Int) = pw_size_sint32(value)
17-
internal actual fun WireSize.sInt64(value: Long) = pw_size_sint64(value)
12+
internal actual fun WireSize.int32(value: Int) = pw_size_int32(value).toInt()
13+
internal actual fun WireSize.int64(value: Long) = pw_size_int64(value).toInt()
14+
internal actual fun WireSize.uInt32(value: UInt) = pw_size_uint32(value).toInt()
15+
internal actual fun WireSize.uInt64(value: ULong) = pw_size_uint64(value).toInt()
16+
internal actual fun WireSize.sInt32(value: Int) = pw_size_sint32(value).toInt()
17+
internal actual fun WireSize.sInt64(value: Long) = pw_size_sint64(value).toInt()
1818

grpc/grpc-core/src/nativeTest/kotlin/kotlinx/rpc/grpc/pb/TestAllPrimitive.kt

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.grpc.pb
6+
7+
import kotlinx.io.Buffer
8+
import kotlinx.rpc.grpc.test.*
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
12+
class TestProtos {
13+
14+
private fun <T : Any> decodeEncode(
15+
msg: T,
16+
enc: T.(WireEncoder) -> Unit,
17+
dec: (WireDecoder) -> T?
18+
): T? {
19+
val buffer = Buffer()
20+
val encoder = WireEncoder(buffer)
21+
22+
msg.enc(encoder)
23+
encoder.flush()
24+
25+
return WireDecoder(buffer).use {
26+
dec(it)
27+
}
28+
}
29+
30+
31+
@Test
32+
fun testAllPrimitiveProto() {
33+
val msg = AllPrimitives {
34+
double = 3.0
35+
}
36+
37+
val decoded = decodeEncode(msg, { encodeWith(it) }, AllPrimitives::decodeWith)
38+
39+
assertEquals(msg.double, decoded?.double)
40+
}
41+
42+
@Test
43+
fun testRepeatedProto() {
44+
val msg = Repeated {
45+
listFixed32 = listOf(1, 2, 3).map { it.toUInt() }
46+
listInt32 = listOf(4, 5, 6)
47+
listString = listOf("a", "b", "c")
48+
}
49+
50+
val decoded = decodeEncode(msg, { encodeWith(it) }, Repeated::decodeWith)
51+
52+
assertEquals(msg.listInt32, decoded?.listInt32)
53+
assertEquals(msg.listFixed32, decoded?.listFixed32)
54+
assertEquals(msg.listString, decoded?.listString)
55+
}
56+
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
syntax = "proto3";
2+
3+
package kotlinx.rpc.grpc.test;
4+
5+
message AllPrimitives {
6+
double double = 1;
7+
float float = 2;
8+
int32 int32 = 3;
9+
int64 int64 = 4;
10+
uint32 uint32 = 5;
11+
uint64 uint64 = 6;
12+
sint32 sint32 = 7;
13+
sint64 sint64 = 8;
14+
fixed32 fixed32 = 9;
15+
fixed64 fixed64 = 10;
16+
sfixed32 sfixed32 = 11;
17+
sfixed64 sfixed64 = 12;
18+
bool bool = 13;
19+
string string = 14;
20+
bytes bytes = 15;
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto3";
2+
3+
package kotlinx.rpc.grpc.test;
4+
5+
enum Enum {
6+
option allow_alias = true;
7+
ZERO = 0;
8+
ONE = 1;
9+
ONE_SECOND = 1;
10+
TWO = 2 [deprecated = true];
11+
THREE = 3;
12+
}
13+
14+
message UsingEnum {
15+
Enum enum = 1;
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
syntax = "proto3";
2+
3+
message Image {
4+
bytes data = 1;
5+
}
6+
7+
message RecogniseResult {
8+
int32 category = 1;
9+
}
10+
11+
service ImageRecognizer {
12+
rpc recognize(Image) returns (RecogniseResult);
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
syntax = "proto3";
2+
3+
package kotlinx.rpc.grpc.test;
4+
5+
message Nested {
6+
message Inner1 {
7+
message Inner11 {
8+
optional Nested.Inner2.Inner21 reference21 = 1;
9+
Nested.Inner1.Inner12 reference12 = 2;
10+
Nested.Inner2.NestedEnum enum = 3;
11+
}
12+
13+
message Inner12 {
14+
optional Inner12 recursion = 1;
15+
}
16+
17+
Inner11 inner11 = 1;
18+
Inner12 inner22 = 2;
19+
string string = 3;
20+
optional Inner1 inner1 = 4;
21+
}
22+
23+
message Inner2 {
24+
message Inner21 {
25+
Nested.Inner1.Inner11 reference11 = 1;
26+
Nested.Inner2.Inner22 reference22 = 2;
27+
}
28+
29+
message Inner22 {
30+
NestedEnum enum = 1;
31+
}
32+
33+
enum NestedEnum {
34+
ZERO = 0;
35+
}
36+
37+
Inner21 inner21 = 1;
38+
Inner22 inner22 = 2;
39+
string string = 3;
40+
}
41+
42+
Inner1 inner1 = 1;
43+
Inner2 inner2 = 2;
44+
string string = 3;
45+
Inner2.NestedEnum enum = 4;
46+
}

0 commit comments

Comments
 (0)