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
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ import encodeWith
import invoke
import kotlinx.io.Buffer
import kotlinx.rpc.grpc.codec.MessageCodec
import kotlinx.rpc.grpc.test.MyEnum
import kotlinx.rpc.grpc.test.UsingEnum
import kotlinx.rpc.grpc.test.UsingEnumInternal
import kotlinx.rpc.grpc.test.*
import kotlinx.rpc.grpc.test.common.*
import kotlinx.rpc.grpc.test.invoke
import test.nested.*
import test.recursive.Recursive
import test.recursive.RecursiveInternal
Expand All @@ -31,7 +28,7 @@ class ProtosTest {

private fun <M> encodeDecode(
msg: M,
codec: MessageCodec<M>
codec: MessageCodec<M>,
): M {
val source = codec.encode(msg)
return codec.decode(source)
Expand Down Expand Up @@ -357,4 +354,38 @@ class ProtosTest {
assertEquals("fourth", decoded.other.arg3)
}


@Test
fun testMap() {
val msg = TestMap {
primitives = mapOf("one" to 1, "two" to 2, "three" to 3)
messages = mapOf(
1 to PresenceCheck { RequiredPresence = 1 },
2 to PresenceCheck { RequiredPresence = 2; OptionalPresence = 3F }
)
}

val decoded = encodeDecode(msg, TestMapInternal.CODEC)
assertEquals(msg.primitives, decoded.primitives)
assertEquals(msg.messages.size, decoded.messages.size)
for ((key, value) in msg.messages) {
assertEquals(value.RequiredPresence, decoded.messages[key]!!.RequiredPresence)
assertEquals(value.OptionalPresence, decoded.messages[key]!!.OptionalPresence)
}
}

@Test
fun testMapRequiredSubField() {
// we use the internal constructor to avoid a "missing required field" error during object construction
val missingRequiredMessage = PresenceCheckInternal()

assertFailsWith<IllegalStateException> {
val msg = TestMap {
messages = mapOf(
2 to missingRequiredMessage
)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ syntax = "proto3";

package kotlinx.rpc.grpc.test;

import "reference_package.proto";
import "presence_check.proto";

message TestMap {
map<string, int64> primitives = 1;
map<string, References> references = 2;
}
map<int32, kotlinx.rpc.grpc.test.common.PresenceCheck> messages = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ open class CodeGenerator(
suffix: String = "",
nlAfterClosed: Boolean = true,
openingBracket: Boolean = true,
paramDecl: String = "",
block: (CodeGenerator.() -> Unit)? = null,
) {
addLine(prefix)
scopeWithSuffix(suffix, openingBracket, nlAfterClosed, block)
scopeWithSuffix(suffix, openingBracket, nlAfterClosed, paramDecl, block)
}

internal fun ifBranch(
Expand Down Expand Up @@ -122,6 +123,7 @@ open class CodeGenerator(
suffix: String = "",
openingBracket: Boolean = true,
nlAfterClosed: Boolean = true,
paramDeclaration: String = "",
block: (CodeGenerator.() -> Unit)? = null,
) {
if (block == null) {
Expand All @@ -139,7 +141,7 @@ open class CodeGenerator(
}

if (openingBracket) {
append(" {")
append(" { $paramDeclaration")
}
newLine()
append(nested.build().trimEnd())
Expand Down
Loading
Loading