Skip to content

Commit

Permalink
Fix issue with messages with no fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
TimOrtel committed Mar 7, 2023
1 parent 516ba88 commit 1d3652d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 10 deletions.
12 changes: 12 additions & 0 deletions buildSrc/src/main/kotlin/TestServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ object TestServer {
server = NettyServerBuilder
.forPort(17888)
.addService(object : TestServiceGrpcKt.TestServiceCoroutineImplBase() {
override suspend fun emptyRpc(request: EmptyMessage): EmptyMessage {
return request
}

override suspend fun simpleRpc(request: SimpleMessage): SimpleMessage {
return request
}
Expand All @@ -27,6 +31,14 @@ object TestServer {
return request
}

override fun emptyStream(request: EmptyMessage): Flow<EmptyMessage> {
return flow {
emit(request)
emit(request)
emit(request)
}
}

override fun simpleStreamingRpc(request: SimpleMessage): Flow<SimpleMessage> {
return flow {
emit(request)
Expand Down
7 changes: 7 additions & 0 deletions grpc-mp-test/src/commonMain/proto/basicMessages.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ package io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages;

option java_multiple_files = true;

message EmptyMessage {

}

message SimpleMessage {
string field1 = 1;
}
Expand Down Expand Up @@ -112,10 +116,13 @@ message OneOfMessage {
}

service TestService {
rpc emptyRpc (EmptyMessage) returns (EmptyMessage);

rpc simpleRpc (SimpleMessage) returns (SimpleMessage);
rpc scalarRpc (ScalarTypes) returns (ScalarTypes);
rpc everythingRpc (MessageWithEverything) returns (MessageWithEverything);

rpc emptyStream (EmptyMessage) returns (stream EmptyMessage);
rpc simpleStreamingRpc (SimpleMessage) returns (stream SimpleMessage);
rpc everythingStreamingRpc (MessageWithEverything) returns (stream MessageWithEverything);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package io.github.timortel.kotlin_multiplatform_grpc_plugin.test

import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.kmEmptyMessage
import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.kmSimpleMessage
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class EqTest {

@Test
fun emptyMessageEquals() {
assertEquals(kmEmptyMessage { }, kmEmptyMessage { })
}

@Test
fun scalarMessageEquals() {
assertEquals(createScalarMessage(), createScalarMessage())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package io.github.timortel.kotlin_multiplatform_grpc_plugin.test

import io.github.timortel.kotlin_multiplatform_grpc_lib.KMChannel
import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.KMMessageWithEverything
import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.KMSimpleMessage
import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.KMTestServiceStub
import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.kmSimpleMessage
import io.github.timortel.kotlin_multiplatform_grpc_plugin.test.basic_messages.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.toCollection
Expand All @@ -22,6 +19,17 @@ class RpcTest {

private val stub = KMTestServiceStub(channel)

@Test
fun testEmpty() {
runTest {
val message = kmEmptyMessage { }
val response = stub
.emptyRpc(message)

assertEquals(message, response)
}
}

@Test
fun testSimple() {
runTest {
Expand Down Expand Up @@ -55,6 +63,17 @@ class RpcTest {
}
}

@Test
fun testStreamEmpty() {
runTest {
val message = kmEmptyMessage { }
val flow: Flow<KMEmptyMessage> = stub
.emptyStream(message)

assertEquals(listOf(message, message, message), flow.toList())
}
}

@Test
fun testStreamSimple() {
runTest {
Expand Down
2 changes: 1 addition & 1 deletion grpc-multiplatform-lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "io.github.timortel"
version = "0.2.1"
version = "0.3.0"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package io.github.timortel.kotlin_multiplatform_grpc_lib

import io.github.timortel.kotlin_multiplatform_grpc_lib.util.TimeUnit

/**
* Wrapps around the GRPC-Channel
*/
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "io.github.timortel"
version = "0.2.1"
version = "0.3.0"

java {
withSourcesJar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ abstract class IosJvmProtoFileWriteBase(private val protoFile: ProtoFile) :
FunSpec
.builder(Const.Message.Companion.IOS.DataDeserializationFunction.NAME)
.addModifiers(KModifier.OVERRIDE)
.addParameter(Const.Message.Companion.IOS.DataDeserializationFunction.DATA_PARAM, serializedDataType)
.addParameter(
Const.Message.Companion.IOS.DataDeserializationFunction.DATA_PARAM,
serializedDataType
)
.addCode(
deserializeFunctionCode
)
Expand Down Expand Up @@ -234,6 +237,11 @@ abstract class IosJvmProtoFileWriteBase(private val protoFile: ProtoFile) :
Const.Message.OneOf.IosJvm.REQUIRED_SIZE_PROPERTY_NAME
)
}

// Fallback for messages without any fields.
if (onlyNonOneOfAttributes.isEmpty() && message.oneOfs.isEmpty()) {
add("0")
}
}
.build()
}
Expand Down
6 changes: 5 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](http://www.apache.org/licenses/LICENSE-2.0)
![version](https://img.shields.io/badge/version-0.2.0-blue)
![version](https://img.shields.io/badge/version-0.3.0-blue)

![badge][badge-android]
![badge][badge-jvm]
Expand All @@ -9,6 +9,10 @@
# gRPC Kotlin Multiplatform
This projects implements client-side gRPC for Android, JVM, iOS and the web.

**⚠️ Warning: This project is still under development and does not support all gRPC features!**

**⚠️ Warning: The implementation for javascript is currently not covered by unit tests!**

## Table of contents
- [Features](#features)
- [Usage](#usage)
Expand Down

0 comments on commit 1d3652d

Please sign in to comment.