Skip to content

Commit 58500b3

Browse files
committed
grpc-pb: Support maps
Signed-off-by: Johannes Zottele <[email protected]>
1 parent aab38b9 commit 58500b3

File tree

7 files changed

+208
-71
lines changed

7 files changed

+208
-71
lines changed

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

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ import encodeWith
1414
import invoke
1515
import kotlinx.io.Buffer
1616
import kotlinx.rpc.grpc.codec.MessageCodec
17-
import kotlinx.rpc.grpc.test.MyEnum
18-
import kotlinx.rpc.grpc.test.UsingEnum
19-
import kotlinx.rpc.grpc.test.UsingEnumInternal
17+
import kotlinx.rpc.grpc.test.*
2018
import kotlinx.rpc.grpc.test.common.*
21-
import kotlinx.rpc.grpc.test.invoke
2219
import test.nested.*
2320
import test.recursive.Recursive
2421
import test.recursive.RecursiveInternal
@@ -357,4 +354,37 @@ class ProtosTest {
357354
assertEquals("fourth", decoded.other.arg3)
358355
}
359356

357+
358+
@Test
359+
fun testMap() {
360+
val msg = TestMap {
361+
primitives = mapOf("one" to 1, "two" to 2, "three" to 3)
362+
messages = mapOf(
363+
1 to PresenceCheck { RequiredPresence = 1 },
364+
2 to PresenceCheck { RequiredPresence = 2; OptionalPresence = 3F })
365+
}
366+
367+
val decoded = encodeDecode(msg, TestMapInternal.CODEC)
368+
assertEquals(msg.primitives, decoded.primitives)
369+
assertEquals(msg.messages.size, decoded.messages.size)
370+
for ((key, value) in msg.messages) {
371+
assertEquals(value.RequiredPresence, decoded.messages[key]!!.RequiredPresence)
372+
assertEquals(value.OptionalPresence, decoded.messages[key]!!.OptionalPresence)
373+
}
374+
}
375+
376+
@Test
377+
fun testMapRequiredSubField() {
378+
// we the use internal constructor to avoid crash during object construction
379+
val missingRequiredMessage = PresenceCheckInternal()
380+
381+
assertFailsWith<IllegalStateException> {
382+
val msg = TestMap {
383+
messages = mapOf(
384+
2 to missingRequiredMessage
385+
)
386+
}
387+
}
388+
}
389+
360390
}

grpc/grpc-core/src/commonTest/proto/exclude/test_map.proto renamed to grpc/grpc-core/src/commonTest/proto/test_map.proto

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

33
package kotlinx.rpc.grpc.test;
44

5-
import "reference_package.proto";
5+
import "presence_check.proto";
66

77
message TestMap {
88
map<string, int64> primitives = 1;
9-
map<string, References> references = 2;
10-
}
9+
map<int32, kotlinx.rpc.grpc.test.common.PresenceCheck> messages = 2;
10+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ open class CodeGenerator(
7272
suffix: String = "",
7373
nlAfterClosed: Boolean = true,
7474
openingBracket: Boolean = true,
75+
paramDecl: String = "",
7576
block: (CodeGenerator.() -> Unit)? = null,
7677
) {
7778
addLine(prefix)
78-
scopeWithSuffix(suffix, openingBracket, nlAfterClosed, block)
79+
scopeWithSuffix(suffix, openingBracket, nlAfterClosed, paramDecl, block)
7980
}
8081

8182
internal fun ifBranch(
@@ -122,6 +123,7 @@ open class CodeGenerator(
122123
suffix: String = "",
123124
openingBracket: Boolean = true,
124125
nlAfterClosed: Boolean = true,
126+
paramDecl: String = "",
125127
block: (CodeGenerator.() -> Unit)? = null,
126128
) {
127129
if (block == null) {
@@ -139,7 +141,7 @@ open class CodeGenerator(
139141
}
140142

141143
if (openingBracket) {
142-
append(" {")
144+
append(" { $paramDecl")
143145
}
144146
newLine()
145147
append(nested.build().trimEnd())

0 commit comments

Comments
 (0)