Skip to content

Commit 585a43e

Browse files
committed
grpc-pb: Support nested message
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 9c58222 commit 585a43e

File tree

6 files changed

+112
-60
lines changed

6 files changed

+112
-60
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import kotlin.reflect.KProperty
1212
@InternalRpcApi
1313
public abstract class InternalMessage(fieldsWithPresence: Int) {
1414
public val presenceMask: BitSet = BitSet(fieldsWithPresence)
15+
16+
@Suppress("PropertyName")
1517
public abstract val _size: Int
1618
}
1719

@@ -32,13 +34,12 @@ public class MsgFieldDelegate<T : Any>(
3234
error("Property ${property.name} not initialized")
3335
}
3436
}
35-
@Suppress("UNCHECKED_CAST")
3637
return _value as T
3738
}
3839

39-
override operator fun setValue(thisRef: InternalMessage, property: KProperty<*>, new: T) {
40+
override operator fun setValue(thisRef: InternalMessage, property: KProperty<*>, value: T) {
4041
presenceIdx?.let { thisRef.presenceMask[it] = true }
41-
_value = new
42+
_value = value
4243
valueSet = true
4344
}
4445
}

grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/pb/ProtosTest.kt

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ import OuterInternal
1111
import invoke
1212
import kotlinx.io.Buffer
1313
import kotlinx.rpc.grpc.internal.MessageCodec
14-
import kotlinx.rpc.grpc.test.*
14+
import kotlinx.rpc.grpc.test.MyEnum
15+
import kotlinx.rpc.grpc.test.UsingEnum
16+
import kotlinx.rpc.grpc.test.UsingEnumInternal
1517
import kotlinx.rpc.grpc.test.common.*
18+
import kotlinx.rpc.grpc.test.invoke
19+
import test.nested.*
20+
import test.recursive.Recursive
21+
import test.recursive.RecursiveInternal
22+
import test.recursive.RecursiveReq
23+
import test.recursive.invoke
1624
import kotlin.test.Test
1725
import kotlin.test.assertEquals
1826
import kotlin.test.assertFailsWith
@@ -100,18 +108,18 @@ class ProtosTest {
100108
encoder.flush()
101109

102110
val decodedMsg = UsingEnumInternal.CODEC.decode(buffer)
103-
assertEquals(Enum.UNRECOGNIZED(50), decodedMsg.enum)
111+
assertEquals(MyEnum.UNRECOGNIZED(50), decodedMsg.enum)
104112
}
105113

106114
@Test
107115
fun testEnumAlias() {
108116
val msg = UsingEnum {
109-
enum = Enum.ONE_SECOND
117+
enum = MyEnum.ONE_SECOND
110118
}
111119

112120
val decodedMsg = encodeDecode(msg, UsingEnumInternal.CODEC)
113-
assertEquals(Enum.ONE, decodedMsg.enum)
114-
assertEquals(Enum.ONE_SECOND, decodedMsg.enum)
121+
assertEquals(MyEnum.ONE, decodedMsg.enum)
122+
assertEquals(MyEnum.ONE_SECOND, decodedMsg.enum)
115123
}
116124

117125
@Test
@@ -124,7 +132,7 @@ class ProtosTest {
124132
assertEquals(0, buffer.size)
125133

126134
val decoded = UsingEnumInternal.CODEC.decode(buffer)
127-
assertEquals(Enum.ZERO, decoded.enum)
135+
assertEquals(MyEnum.ZERO, decoded.enum)
128136
}
129137

130138
@Test
@@ -208,4 +216,45 @@ class ProtosTest {
208216
assertEquals(null, decoded.rec.rec.rec.rec.num)
209217
}
210218

219+
@Test
220+
fun testNested() {
221+
val inner = NestedOuter.Inner.SuperInner.DuperInner.EvenMoreInner.CantBelieveItsSoInner {
222+
num = 123456789
223+
}
224+
225+
val notInside = NotInside {
226+
num = -12
227+
}
228+
val outer = NestedOuter {
229+
deep = inner
230+
deepEnum =
231+
NestedOuter.Inner.SuperInner.DuperInner.EvenMoreInner.JustWayTooInner.JUST_WAY_TOO_INNER_UNSPECIFIED
232+
}
233+
234+
assertEquals(123456789, outer.deep.num)
235+
assertEquals(
236+
NestedOuter.Inner.SuperInner.DuperInner.EvenMoreInner.JustWayTooInner.JUST_WAY_TOO_INNER_UNSPECIFIED,
237+
outer.deepEnum
238+
)
239+
assertEquals(-12, notInside.num)
240+
241+
val decodedOuter = encodeDecode(outer, NestedOuterInternal.CODEC)
242+
assertEquals(123456789, decodedOuter.deep.num)
243+
assertEquals(
244+
NestedOuter.Inner.SuperInner.DuperInner.EvenMoreInner.JustWayTooInner.JUST_WAY_TOO_INNER_UNSPECIFIED,
245+
decodedOuter.deepEnum
246+
)
247+
assertEquals(-12, notInside.num)
248+
249+
val decodedNotInside = encodeDecode(notInside, NotInsideInternal.CODEC)
250+
assertEquals(-12, decodedNotInside.num)
251+
252+
val decodedInner = encodeDecode(
253+
inner,
254+
NestedOuterInternal.InnerInternal.SuperInnerInternal.DuperInnerInternal.EvenMoreInnerInternal.CantBelieveItsSoInnerInternal.CODEC
255+
)
256+
assertEquals(123456789, decodedInner.num)
257+
258+
}
259+
211260
}

grpc/grpc-core/src/commonTest/proto/enum.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ syntax = "proto3";
22

33
package kotlinx.rpc.grpc.test;
44

5-
enum Enum {
5+
enum MyEnum {
66
option allow_alias = true;
77
ZERO = 0;
88
ONE = 1;
@@ -12,5 +12,5 @@ enum Enum {
1212
}
1313

1414
message UsingEnum {
15-
Enum enum = 1;
15+
MyEnum enum = 1;
1616
}

grpc/grpc-core/src/commonTest/proto/nested.proto

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@
77

88
syntax = "proto2";
99

10-
package kotlinx.rpc.grpc.test;
10+
package test.nested;
1111

12-
message Outer {
12+
message NestedOuter {
1313
message Inner {
14-
message InnerSubMsg {
15-
optional bool flag = 1;
16-
}
17-
18-
enum InnerEnum {
19-
INNER_ENUM_UNSPECIFIED = 0;
20-
INNER_ENUM_FOO = 1;
21-
}
14+
// message InnerSubMsg {
15+
// optional bool flag = 1;
16+
// }
17+
//
18+
// enum InnerEnum {
19+
// INNER_ENUM_UNSPECIFIED = 0;
20+
// INNER_ENUM_FOO = 1;
21+
// }
2222

23-
optional double double = 1;
24-
optional float float = 2;
25-
optional int32 int32 = 3;
26-
optional int64 int64 = 4;
27-
optional uint32 uint32 = 5;
28-
optional uint64 uint64 = 6;
29-
optional sint32 sint32 = 7;
30-
optional sint64 sint64 = 8;
31-
optional fixed32 fixed32 = 9;
32-
optional fixed64 fixed64 = 10;
33-
optional sfixed32 sfixed32 = 11;
34-
optional sfixed64 sfixed64 = 12;
35-
optional bool bool = 13;
36-
optional string string = 14;
37-
optional bytes bytes = 15;
38-
optional InnerSubMsg inner_submsg = 16;
39-
optional InnerEnum inner_enum = 17;
40-
repeated int32 repeated_int32 = 18 [packed = true];
41-
repeated InnerSubMsg repeated_inner_submsg = 19;
23+
// optional double double = 1;
24+
// optional float float = 2;
25+
// optional int32 int32 = 3;
26+
// optional int64 int64 = 4;
27+
// optional uint32 uint32 = 5;
28+
// optional uint64 uint64 = 6;
29+
// optional sint32 sint32 = 7;
30+
// optional sint64 sint64 = 8;
31+
// optional fixed32 fixed32 = 9;
32+
// optional fixed64 fixed64 = 10;
33+
// optional sfixed32 sfixed32 = 11;
34+
// optional sfixed64 sfixed64 = 12;
35+
// optional bool bool = 13;
36+
// optional string string = 14;
37+
// optional bytes bytes = 15;
38+
// optional InnerSubMsg inner_submsg = 16;
39+
// optional InnerEnum inner_enum = 17;
40+
// repeated int32 repeated_int32 = 18 [packed = true];
41+
// repeated InnerSubMsg repeated_inner_submsg = 19;
4242
// map<string, string> string_map = 20;
4343

4444
message SuperInner {
@@ -55,16 +55,19 @@ message Outer {
5555
}
5656
}
5757
}
58-
// optional Inner inner = 1;
59-
// optional .kotlinx.rpc.grpc.test.Outer.Inner.SuperInner.DuperInner.EvenMoreInner
60-
// .CantBelieveItsSoInner deep = 2;
61-
//
62-
// optional .kotlinx.rpc.grpc.test.Outer.Inner.SuperInner.DuperInner.EvenMoreInner.JustWayTooInner
63-
// deep_enum = 4;
58+
optional Inner inner = 1;
59+
optional .test.nested.NestedOuter.Inner.SuperInner.DuperInner.EvenMoreInner
60+
.CantBelieveItsSoInner deep = 2;
61+
optional .test.nested.NestedOuter.Inner.SuperInner.DuperInner.EvenMoreInner.JustWayTooInner
62+
deep_enum = 4;
6463

6564
optional NotInside notinside = 3;
6665
}
6766

67+
enum OutsideEnum {
68+
JUST_WAY_TOO_INNER_UNSPECIFIED = 0;
69+
}
70+
6871
message NotInside {
6972
optional int32 num = 1;
7073
}

grpc/grpc-core/src/commonTest/proto/recursive.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
syntax = "proto2";
22

3-
package kotlinx.rpc.grpc.test;
3+
package test.recursive;
44

55
message RecursiveReq {
66
required RecursiveReq rec = 1;

protoc-gen/src/main/kotlin/kotlinx/rpc/protobuf/ModelToKotlinCommonGenerator.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class ModelToKotlinCommonGenerator(
111111
messages.forEach { generateInternalMessage(it) }
112112

113113
// emit all required functions in the outer scope
114-
val allMsgs = messages + messages.flatMap(MessageDeclaration::nestedDeclarations)
114+
val allMsgs = messages + messages.flatMap(MessageDeclaration::allNestedRecursively)
115115
allMsgs.forEach {
116116
generateMessageConstructor(it)
117117
}
@@ -714,21 +714,20 @@ class ModelToKotlinCommonGenerator(
714714
superTypes = listOf("$className(number)"),
715715
)
716716

717-
if (declaration.aliases.isNotEmpty()) {
718-
newLine()
719-
720-
clazz("", modifiers = "companion", declarationType = DeclarationType.Object) {
721-
declaration.aliases.forEach { alias: EnumDeclaration.Alias ->
722-
code(
723-
"val ${alias.name.simpleName}: $className " +
724-
"get() = ${alias.original.name.simpleName}"
725-
)
726-
}
717+
newLine()
727718

728-
val entryNamesSorted = entriesSorted.joinToString(", ") { it.name.simpleName }
729-
code("val entries: List<$className> by lazy { listOf($entryNamesSorted) }")
719+
clazz("", modifiers = "companion", declarationType = DeclarationType.Object) {
720+
declaration.aliases.forEach { alias: EnumDeclaration.Alias ->
721+
code(
722+
"val ${alias.name.simpleName}: $className " +
723+
"get() = ${alias.original.name.simpleName}"
724+
)
730725
}
726+
727+
val entryNamesSorted = entriesSorted.joinToString(", ") { it.name.simpleName }
728+
code("val entries: List<$className> by lazy { listOf($entryNamesSorted) }")
731729
}
730+
732731
}
733732
}
734733

0 commit comments

Comments
 (0)