Skip to content

Commit 111f0f8

Browse files
committed
grpc-pb: Move checking
Signed-off-by: Johannes Zottele <[email protected]>
1 parent 2032642 commit 111f0f8

File tree

7 files changed

+36
-30
lines changed

7 files changed

+36
-30
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public interface WireDecoder : AutoCloseable {
9898
}
9999
}
100100

101+
public expect fun checkForPlatformDecodeException(block: () -> Unit)
102+
101103
/**
102104
* Creates a platform-specific [WireDecoder].
103105
*
@@ -108,4 +110,4 @@ public interface WireDecoder : AutoCloseable {
108110
*
109111
* @param source The buffer containing the encoded wire-format data.
110112
*/
111-
internal expect fun WireDecoder(source: Buffer): WireDecoder
113+
public expect fun WireDecoder(source: Buffer): WireDecoder

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
@@ -60,4 +60,4 @@ public interface WireEncoder {
6060
}
6161

6262

63-
internal expect fun WireEncoder(sink: Sink): WireEncoder
63+
public expect fun WireEncoder(sink: Sink): WireEncoder

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,79 @@ internal class WireDecoderJvm(source: Buffer) : WireDecoder {
1515
// there is no way to omit coping here
1616
internal val codedInputStream: CodedInputStream = CodedInputStream.newInstance(source.asInputStream())
1717

18-
override fun readTag(): KTag? = checked {
18+
override fun readTag(): KTag? {
1919
val tag = codedInputStream.readTag().toUInt()
2020
if (tag == 0u) {
2121
return null
2222
}
2323
return KTag.from(tag)
2424
}
2525

26-
override fun readBool(): Boolean = checked {
26+
override fun readBool(): Boolean {
2727
return codedInputStream.readBool()
2828
}
2929

30-
override fun readInt32(): Int = checked {
30+
override fun readInt32(): Int {
3131
return codedInputStream.readInt32()
3232
}
3333

34-
override fun readInt64(): Long = checked {
34+
override fun readInt64(): Long {
3535
return codedInputStream.readInt64()
3636
}
3737

38-
override fun readUInt32(): UInt = checked {
38+
override fun readUInt32(): UInt {
3939
// todo check java unsigned types
4040
return codedInputStream.readUInt32().toUInt()
4141
}
4242

43-
override fun readUInt64(): ULong = checked {
43+
override fun readUInt64(): ULong {
4444
// todo check java unsigned types
4545
return codedInputStream.readUInt64().toULong()
4646
}
4747

48-
override fun readSInt32(): Int = checked {
48+
override fun readSInt32(): Int {
4949
return codedInputStream.readSInt32()
5050
}
5151

52-
override fun readSInt64(): Long = checked {
52+
override fun readSInt64(): Long {
5353
return codedInputStream.readSInt64()
5454
}
5555

56-
override fun readFixed32(): UInt = checked {
56+
override fun readFixed32(): UInt {
5757
// todo check java unsigned types
5858
return codedInputStream.readFixed32().toUInt()
5959
}
6060

61-
override fun readFixed64(): ULong = checked {
61+
override fun readFixed64(): ULong {
6262
// todo check java unsigned types
6363
return codedInputStream.readFixed64().toULong()
6464
}
6565

66-
override fun readSFixed32(): Int = checked {
66+
override fun readSFixed32(): Int {
6767
return codedInputStream.readSFixed32()
6868
}
6969

70-
override fun readSFixed64(): Long = checked {
70+
override fun readSFixed64(): Long {
7171
return codedInputStream.readSFixed64()
7272
}
7373

74-
override fun readFloat(): Float = checked {
74+
override fun readFloat(): Float {
7575
return codedInputStream.readFloat()
7676
}
7777

78-
override fun readDouble(): Double = checked {
78+
override fun readDouble(): Double {
7979
return codedInputStream.readDouble()
8080
}
8181

82-
override fun readEnum(): Int = checked {
82+
override fun readEnum(): Int {
8383
return codedInputStream.readEnum()
8484
}
8585

86-
override fun readString(): String = checked {
86+
override fun readString(): String {
8787
return codedInputStream.readStringRequireUtf8()
8888
}
8989

90-
override fun readBytes(): ByteArray = checked {
90+
override fun readBytes(): ByteArray {
9191
return codedInputStream.readByteArray()
9292
}
9393

@@ -114,21 +114,19 @@ internal class WireDecoderJvm(source: Buffer) : WireDecoder {
114114
)
115115
}
116116

117-
internal actual fun WireDecoder(source: Buffer): WireDecoder {
118-
return WireDecoderJvm(source)
119-
}
120117

121-
/**
122-
* Turns a [InvalidProtocolBufferException] into our own [ProtobufDecodingException].
123-
*/
124-
private inline fun <reified T> checked(block: () -> T): T {
118+
public actual fun checkForPlatformDecodeException(block: () -> Unit) {
125119
try {
126120
return block()
127121
} catch (e: InvalidProtocolBufferException) {
128122
throw e.toDecodingException()
129123
}
130124
}
131125

126+
public actual fun WireDecoder(source: Buffer): WireDecoder {
127+
return WireDecoderJvm(source)
128+
}
129+
132130
private fun InvalidProtocolBufferException.toDecodingException(): ProtobufDecodingException {
133131
return ProtobufDecodingException(message ?: "Failed to decode protobuf message.", cause)
134132
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ private class WireEncoderJvm(sink: Sink) : WireEncoder {
198198
}
199199
}
200200

201-
internal actual fun WireEncoder(sink: Sink): WireEncoder {
201+
public actual fun WireEncoder(sink: Sink): WireEncoder {
202202
return WireEncoderJvm(sink)
203203
}
204204

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,8 @@ internal class WireDecoderNative(private val source: Buffer) : WireDecoder {
301301
}
302302
}
303303

304-
internal actual fun WireDecoder(source: Buffer): WireDecoder = WireDecoderNative(source)
304+
public actual fun WireDecoder(source: Buffer): WireDecoder = WireDecoderNative(source)
305+
306+
public actual fun checkForPlatformDecodeException(block: () -> Unit) {
307+
block()
308+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ internal class WireEncoderNative(private val sink: Sink) : WireEncoder {
178178
}
179179
}
180180

181-
internal actual fun WireEncoder(sink: Sink): WireEncoder = WireEncoderNative(sink)
181+
public actual fun WireEncoder(sink: Sink): WireEncoder = WireEncoderNative(sink)
182182

183183

184184
// the current implementation is slow, as it iterates through the list, to write each element individually,

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ class ModelToKotlinCommonGenerator(
252252
function("decode", modifiers = "override", args = "stream: $sourceFqName", returnType = msgFqName) {
253253
scope("$PB_PKG.WireDecoder(stream as $bufferFqName).use") {
254254
code("val msg = ${declaration.internalClassFullName()}()")
255-
code("${declaration.internalClassFullName()}.decodeWith(msg, it)")
255+
scope("$PB_PKG.checkForPlatformDecodeException", nlAfterClosed = false) {
256+
code("${declaration.internalClassFullName()}.decodeWith(msg, it)")
257+
}
256258
code("msg.checkRequiredFields()")
257259
code("return msg")
258260
}

0 commit comments

Comments
 (0)