Skip to content

Commit d5e9f00

Browse files
committed
grpc-native: Move internal decode/encoding into grpc.pb package and move common proto generation to commonTest
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 0090141 commit d5e9f00

File tree

23 files changed

+102
-201
lines changed

23 files changed

+102
-201
lines changed

gradle-plugin/src/main/kotlin/kotlinx/rpc/proto/DefaultProtoSourceSet.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ internal fun Project.createProtoExtensions() {
100100
findOrCreateAndConfigure("jvmTest", null)
101101

102102
sourceSets.configureEach {
103-
if (name == "jvmMain" || name == "jvmTest" || name == "nativeTest") {
103+
if (name == "jvmMain" || name == "jvmTest" || name == "nativeTest" || name == "commonTest") {
104104
findOrCreateAndConfigure(name, this)
105105
}
106106
}

grpc/grpc-core/build.gradle.kts

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

55
import kotlinx.rpc.buf.tasks.BufGenerateTask
66
import kotlinx.rpc.proto.kotlinMultiplatform
7-
import org.gradle.kotlin.dsl.withType
87
import org.gradle.internal.extensions.stdlib.capitalized
98
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
109
import org.jetbrains.kotlin.gradle.tasks.CInteropProcess
@@ -123,8 +122,8 @@ kotlin {
123122
)
124123
}
125124

126-
val libUpbTask = "cinterop${libprotowire.name.capitalized()}${it.targetName.capitalized()}"
127-
tasks.named(libUpbTask, CInteropProcess::class) {
125+
val libProtowireTask = "cinterop${libprotowire.name.capitalized()}${it.targetName.capitalized()}"
126+
tasks.named(libProtowireTask, CInteropProcess::class) {
128127
dependsOn(buildGrpcppCLib)
129128
}
130129

@@ -139,8 +138,15 @@ protoSourceSets {
139138
exclude("exclude/**")
140139
}
141140
}
141+
142+
configureEach {
143+
proto {
144+
exclude("exclude/**")
145+
}
146+
}
142147
}
143148

149+
144150
rpc {
145151
grpc {
146152
val globalRootDir: String by extra
@@ -152,6 +158,15 @@ rpc {
152158
}
153159

154160
project.tasks.withType<BufGenerateTask>().configureEach {
161+
162+
// TODO: Remove this once we remove JVM generation
163+
// Set compile for common(native) option
164+
if (name.endsWith("CommonTest")) {
165+
protocPlugins.kotlinMultiplatform {
166+
options.set(options.getOrElse(emptyMap()) + mapOf("targetMode" to "common"))
167+
}
168+
}
169+
155170
if (name.endsWith("Test")) {
156171
dependsOn(gradle.includedBuild("protoc-gen").task(":jar"))
157172
}

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/WireDecoder.kt

Lines changed: 0 additions & 85 deletions
This file was deleted.

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/WireEncoder.kt

Lines changed: 0 additions & 54 deletions
This file was deleted.

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/pb/readPacked.kt renamed to grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/internal/readPacked.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package kotlinx.rpc.grpc.internal
66

7-
import kotlinx.io.Buffer
7+
import kotlinx.rpc.grpc.pb.WireDecoder
88

99
internal expect fun WireDecoder.pushLimit(byteLen: Int): Int
1010
internal expect fun WireDecoder.popLimit(limit: Int)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ package kotlinx.rpc.grpc.pb
77
import kotlinx.io.Buffer
88
import kotlinx.rpc.internal.utils.InternalRpcApi
99

10+
// TODO: Evaluate if this buffer size is suitable for all targets (KRPC-186)
11+
// maximum buffer size to allocate as contiguous memory in bytes
12+
internal const val MAX_PACKED_BULK_SIZE: Int = 1_000_000
13+
1014
/**
1115
* A platform-specific decoder for wire format data.
1216
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import kotlinx.rpc.internal.utils.InternalRpcApi
1919
@OptIn(ExperimentalUnsignedTypes::class)
2020
public interface WireEncoder {
2121
public fun flush()
22-
public fun writeBool(field: Int, value: Boolean): Boolean
22+
public fun writeBool(fieldNr: Int, value: Boolean): Boolean
2323
public fun writeInt32(fieldNr: Int, value: Int): Boolean
2424
public fun writeInt64(fieldNr: Int, value: Long): Boolean
2525
public fun writeUInt32(fieldNr: Int, value: UInt): Boolean

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

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

55
package kotlinx.rpc.grpc.pb
66

7-
internal object WireSize
7+
public object WireSize
88

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
9+
public expect fun WireSize.int32(value: Int): Int
10+
public expect fun WireSize.int64(value: Long): Int
11+
public expect fun WireSize.uInt32(value: UInt): Int
12+
public expect fun WireSize.uInt64(value: ULong): Int
13+
public expect fun WireSize.sInt32(value: Int): Int
14+
public expect fun WireSize.sInt64(value: Long): Int
1515

16-
internal fun WireSize.bool(value: Boolean) = int32(if (value) 1 else 0)
17-
internal fun WireSize.enum(value: Int) = int32(value)
18-
internal fun WireSize.packedInt32(value: List<Int>) = value.sumOf { int32(it) }
19-
internal fun WireSize.packedInt64(value: List<Long>) = value.sumOf { int64(it) }
20-
internal fun WireSize.packedUInt32(value: List<UInt>) = value.sumOf { uInt32(it) }
21-
internal fun WireSize.packedUInt64(value: List<ULong>) = value.sumOf { uInt64(it) }
22-
internal fun WireSize.packedSInt32(value: List<Int>) = value.sumOf { sInt32(it) }
23-
internal fun WireSize.packedSInt64(value: List<Long>) = value.sumOf { sInt64(it) }
24-
internal fun WireSize.packedEnum(value: List<Int>) = value.sumOf { enum(it) }
16+
public fun WireSize.bool(value: Boolean): Int = int32(if (value) 1 else 0)
17+
public fun WireSize.enum(value: Int): Int = int32(value)
18+
public fun WireSize.packedInt32(value: List<Int>): Int = value.sumOf { int32(it) }
19+
public fun WireSize.packedInt64(value: List<Long>): Int = value.sumOf { int64(it) }
20+
public fun WireSize.packedUInt32(value: List<UInt>): Int = value.sumOf { uInt32(it) }
21+
public fun WireSize.packedUInt64(value: List<ULong>): Int = value.sumOf { uInt64(it) }
22+
public fun WireSize.packedSInt32(value: List<Int>): Int = value.sumOf { sInt32(it) }
23+
public fun WireSize.packedSInt64(value: List<Long>): Int = value.sumOf { sInt64(it) }
24+
public fun WireSize.packedEnum(value: List<Int>): Int = value.sumOf { enum(it) }

grpc/grpc-core/src/nativeTest/kotlin/kotlinx/rpc/grpc/pb/TestProtos.kt renamed to grpc/grpc-core/src/commonTest/kotlin/kotlinx/rpc/grpc/pb/ProtosTest.kt

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

77
import kotlinx.io.Buffer
8-
import kotlinx.rpc.grpc.test.*
8+
import kotlinx.rpc.grpc.test.common.*
99
import kotlin.test.Test
1010
import kotlin.test.assertEquals
1111

@@ -30,7 +30,7 @@ class TestProtos {
3030

3131
@Test
3232
fun testAllPrimitiveProto() {
33-
val msg = AllPrimitives {
33+
val msg = AllPrimitivesCommon {
3434
int32 = 12
3535
int64 = 1234567890123456789L
3636
uint32 = 12345u
@@ -48,20 +48,20 @@ class TestProtos {
4848
bytes = byteArrayOf(1, 2, 3)
4949
}
5050

51-
val decoded = decodeEncode(msg, { encodeWith(it) }, AllPrimitives::decodeWith)
51+
val decoded = decodeEncode(msg, { encodeWith(it) }, AllPrimitivesCommon::decodeWith)
5252

5353
assertEquals(msg.double, decoded?.double)
5454
}
5555

5656
@Test
5757
fun testRepeatedProto() {
58-
val msg = Repeated {
58+
val msg = RepeatedCommon {
5959
listFixed32 = listOf(1, 2, 3).map { it.toUInt() }
6060
listInt32 = listOf(4, 5, 6)
6161
listString = listOf("a", "b", "c")
6262
}
6363

64-
val decoded = decodeEncode(msg, { encodeWith(it) }, Repeated::decodeWith)
64+
val decoded = decodeEncode(msg, { encodeWith(it) }, RepeatedCommon::decodeWith)
6565

6666
assertEquals(msg.listInt32, decoded?.listInt32)
6767
assertEquals(msg.listFixed32, decoded?.listFixed32)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5-
package kotlinx.rpc.grpc.internal
5+
package kotlinx.rpc.grpc.pb
66

77
import kotlinx.io.Buffer
8-
import kotlinx.rpc.grpc.pb.*
9-
import kotlin.experimental.ExperimentalNativeApi
108
import kotlin.test.*
119

1210
enum class TestPlatform {

0 commit comments

Comments
 (0)