Skip to content

Commit 58a6a7b

Browse files
authored
runtime polish (#198)
1 parent 010802c commit 58a6a7b

File tree

80 files changed

+2427
-2358
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2427
-2358
lines changed

benchmarks/protokt-benchmarks/src/main/kotlin/protokt/v1/benchmarks/FixtureGenerator.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class FixtureGenerator(private val weight: Int) {
9191
Boolean::class -> random.nextInt() % 2 == 0
9292
Float::class -> random.nextFloat()
9393
Double::class -> random.nextDouble()
94-
Bytes::class -> Bytes(ByteArray(randomSize()).also { random.nextBytes(it) })
94+
Bytes::class -> Bytes.from(ByteArray(randomSize()).also { random.nextBytes(it) })
9595
String::class -> String(ByteArray(randomSize()).also { random.nextBytes(it) })
9696
else -> throw IllegalArgumentException("unknown $cls")
9797
}
@@ -102,9 +102,7 @@ class FixtureGenerator(private val weight: Int) {
102102
BenchmarkDataset {
103103
this.name = name
104104
messageName = msg.qualifiedName!!
105-
payload = (0..size).map {
106-
Bytes(randomMessageValue(msg).serialize())
107-
}
105+
payload = (0..size).map { Bytes.from(randomMessageValue(msg)) }
108106
}
109107
}
110108

buildSrc/src/main/kotlin/protokt.multiplatform-conventions.gradle.kts

+7-3
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ kotlin {
2424
}
2525

2626
js(IR) {
27-
browser {}
28-
nodejs {}
29-
useCommonJs()
27+
configureJsTests()
3028
}
3129

3230
sourceSets {
31+
val commonTest by getting {
32+
dependencies {
33+
implementation(kotlin("test"))
34+
}
35+
}
36+
3337
val jvmTest by getting {
3438
dependencies {
3539
implementation(libs.junit.jupiter)

examples/grpc-node/build.gradle.kts

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ fun GradleBuild.setUp(service: String, mode: String, vararg extra: Pair<String,
6969
startParameter.projectProperties =
7070
mapOf(
7171
"service" to service,
72-
"mode" to mode
73-
) + extra.toMap()
72+
"mode" to mode,
73+
*extra
74+
)
7475
}
7576

7677
tasks.register<GradleBuild>("HelloWorldServer") {

extensions/protokt-extensions-lite/api/protokt-extensions-lite.api

+156-146
Large diffs are not rendered by default.

extensions/protokt-jvm-extensions-lite/api/protokt-jvm-extensions-lite.api

+10-12
Original file line numberDiff line numberDiff line change
@@ -62,24 +62,19 @@ public final class protokt/v1/InetSocketAddress : protokt/v1/AbstractKtMessage {
6262
public static final field Deserializer Lprotokt/v1/InetSocketAddress$Deserializer;
6363
public synthetic fun <init> (Ljava/net/InetAddress;ILprotokt/v1/UnknownFieldSet;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
6464
public final fun copy (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
65+
public static fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/InetSocketAddress;
6566
public fun equals (Ljava/lang/Object;)Z
6667
public final fun getAddress ()Ljava/net/InetAddress;
6768
public fun getMessageSize ()I
6869
public final fun getPort ()I
6970
public final fun getUnknownFields ()Lprotokt/v1/UnknownFieldSet;
7071
public fun hashCode ()I
72+
public static final fun invoke (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
7173
public fun serialize (Lprotokt/v1/KtMessageSerializer;)V
7274
public fun toString ()Ljava/lang/String;
7375
}
7476

75-
public final class protokt/v1/InetSocketAddress$Deserializer : protokt/v1/AbstractKtDeserializer {
76-
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lcom/toasttab/protokt/rt/KtMessage;
77-
public fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/InetSocketAddress;
78-
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/KtMessage;
79-
public final synthetic fun invoke (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
80-
}
81-
82-
public final class protokt/v1/InetSocketAddress$InetSocketAddressDsl {
77+
public final class protokt/v1/InetSocketAddress$Builder {
8378
public fun <init> ()V
8479
public final fun build ()Lprotokt/v1/InetSocketAddress;
8580
public final fun getAddress ()Ljava/net/InetAddress;
@@ -90,6 +85,13 @@ public final class protokt/v1/InetSocketAddress$InetSocketAddressDsl {
9085
public final fun setUnknownFields (Lprotokt/v1/UnknownFieldSet;)V
9186
}
9287

88+
public final class protokt/v1/InetSocketAddress$Deserializer : protokt/v1/AbstractKtDeserializer {
89+
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lcom/toasttab/protokt/rt/KtMessage;
90+
public fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/InetSocketAddress;
91+
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/KtMessage;
92+
public final fun invoke (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
93+
}
94+
9395
public final class protokt/v1/InetSocketAddressConverter : protokt/v1/Converter {
9496
public static final field INSTANCE Lprotokt/v1/InetSocketAddressConverter;
9597
public fun getWrapped ()Lkotlin/reflect/KClass;
@@ -100,7 +102,3 @@ public final class protokt/v1/InetSocketAddressConverter : protokt/v1/Converter
100102
public fun wrap (Lprotokt/v1/InetSocketAddress;)Ljava/net/InetSocketAddress;
101103
}
102104

103-
public final class protokt/v1/Inet_socket_addressKt {
104-
public static final fun InetSocketAddress (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
105-
}
106-

extensions/protokt-jvm-extensions-simple/src/main/kotlin/protokt/v1/InetAddressBytesConverter.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ object InetAddressBytesConverter : Converter<InetAddress, Bytes> {
3232
}
3333

3434
override fun unwrap(wrapped: InetAddress): Bytes =
35-
Bytes(wrapped.address)
35+
Bytes.from(wrapped.address)
3636
}

extensions/protokt-jvm-extensions-simple/src/main/kotlin/protokt/v1/UuidBytesConverter.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package protokt.v1
1717

1818
import com.google.auto.service.AutoService
19+
import protokt.v1.JvmBytes.asReadOnlyBuffer
20+
import protokt.v1.SizeCodecs.sizeOf
1921
import java.nio.ByteBuffer
2022
import java.util.UUID
2123

@@ -41,7 +43,7 @@ object UuidBytesConverter : OptimizedSizeOfConverter<UUID, Bytes> {
4143
}
4244

4345
override fun unwrap(wrapped: UUID): Bytes =
44-
Bytes(
46+
Bytes.from(
4547
ByteBuffer.allocate(16)
4648
.putLong(wrapped.mostSignificantBits)
4749
.putLong(wrapped.leastSignificantBits)

extensions/protokt-jvm-extensions-simple/src/main/kotlin/protokt/v1/UuidBytesValueConverter.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package protokt.v1
1717

1818
import com.google.auto.service.AutoService
19+
import protokt.v1.SizeCodecs.sizeOf
1920
import protokt.v1.google.protobuf.BytesValue
2021
import java.util.UUID
2122

@@ -26,7 +27,7 @@ object UuidBytesValueConverter : OptimizedSizeOfConverter<UUID, BytesValue> {
2627
override val wrapped = BytesValue::class
2728

2829
private val sizeOfProxy =
29-
BytesValue { value = Bytes(ByteArray(16)) }
30+
BytesValue { value = Bytes.from(ByteArray(16)) }
3031

3132
override fun sizeOf(wrapped: UUID) =
3233
sizeOf(sizeOfProxy)

gradle-plugin-integration-test/semantic-build-versioning.gradle

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../semantic-build-versioning.gradle

gradle-plugin-integration-test/wrapper-types/src/main/kotlin/protokt/v1/testing/Id.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ object IdConverter : Converter<Id, Bytes> {
3131
Id(String(unwrapped.bytes))
3232

3333
override fun unwrap(wrapped: Id) =
34-
Bytes(wrapped.value.toByteArray())
34+
Bytes.from(wrapped.value.toByteArray())
3535
}

gradle/libs.versions.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313

1414
[versions]
1515
autoService = "1.0.1"
16-
grpc-java = "1.56.1"
17-
grpc-kotlin = "1.3.0"
16+
grpc-java = "1.58.0"
17+
grpc-kotlin = "1.4.0"
1818
kotlinLogging = "5.1.0"
1919
kotlinPoet = "1.14.2"
2020
kotlinx-coroutines = "1.6.0"
2121
kotlinx-serialization = "1.5.1"
22-
ktlint = "0.50.0"
22+
ktlint = "1.0.0"
2323
protobuf-java = "3.21.7"
2424
protobuf-js = "6.11.3"
2525
protobufGradlePlugin = "0.9.4"

protokt-codegen/src/main/kotlin/protokt/v1/codegen/generate/DslGenerator.kt renamed to protokt-codegen/src/main/kotlin/protokt/v1/codegen/generate/BuilderGenerator.kt

+20-16
Original file line numberDiff line numberDiff line change
@@ -23,64 +23,65 @@ import com.squareup.kotlinpoet.TypeSpec
2323
import com.squareup.kotlinpoet.asTypeName
2424
import com.squareup.kotlinpoet.buildCodeBlock
2525
import com.squareup.kotlinpoet.withIndent
26-
import protokt.v1.KtDsl
26+
import protokt.v1.KtBuilderDsl
2727
import protokt.v1.UnknownFieldSet
2828
import protokt.v1.codegen.generate.Deprecation.handleDeprecation
29+
import protokt.v1.codegen.util.BUILDER
2930
import protokt.v1.codegen.util.FieldType
3031
import protokt.v1.codegen.util.Message
3132

32-
internal fun TypeSpec.Builder.handleDsl(msg: Message, properties: List<PropertyInfo>) =
33-
apply { DslGenerator(msg, properties).addDsl(this) }
33+
internal fun TypeSpec.Builder.handleBuilder(msg: Message, properties: List<PropertyInfo>) =
34+
apply { BuilderGenerator(msg, properties).addBuilder(this) }
3435

35-
private class DslGenerator(
36+
private class BuilderGenerator(
3637
private val msg: Message,
3738
private val properties: List<PropertyInfo>
3839
) {
39-
fun addDsl(builder: TypeSpec.Builder) {
40+
fun addBuilder(builder: TypeSpec.Builder) {
4041
builder.addFunction(
4142
FunSpec.builder("copy")
4243
.returns(msg.className)
4344
.addParameter(
44-
"dsl",
45+
"builder",
4546
LambdaTypeName.get(
46-
msg.dslClassName,
47+
msg.builderClassName,
4748
emptyList(),
4849
Unit::class.asTypeName()
4950
)
5051
)
5152
.addCode(
5253
buildCodeBlock {
53-
beginControlFlow("return %T().apply", msg.dslClassName)
54-
dslLines().forEach { add("%L\n", it) }
54+
beginControlFlow("return %T().apply", msg.builderClassName)
55+
builderLines().forEach { add("%L\n", it) }
5556
addStatement("unknownFields = this@%L.unknownFields", msg.className.simpleName)
56-
addStatement("dsl()")
57+
addStatement("builder()")
5758
endControlFlowWithoutNewline()
5859
add(".build()")
5960
}
6061
)
6162
.build()
6263
)
6364
builder.addType(
64-
TypeSpec.classBuilder(msg.dslClassName)
65-
.addAnnotation(KtDsl::class)
65+
TypeSpec.classBuilder(msg.builderClassName)
66+
.addAnnotation(KtBuilderDsl::class)
6667
.addProperties(
6768
properties.map {
68-
PropertySpec.builder(it.name, it.dslPropertyType)
69+
PropertySpec.builder(it.name, it.builderPropertyType)
6970
.mutable(true)
7071
.handleDeprecation(it.deprecation)
7172
.apply {
7273
if (it.map) {
7374
setter(
7475
FunSpec.setterBuilder()
7576
.addParameter("newValue", Map::class)
76-
.addCode("field = %M(newValue)", runtimeFunction("copyMap"))
77+
.addCode("field = %M(newValue)", copyMap)
7778
.build()
7879
)
7980
} else if (it.repeated) {
8081
setter(
8182
FunSpec.setterBuilder()
8283
.addParameter("newValue", List::class)
83-
.addCode("field = %M(newValue)", runtimeFunction("copyList"))
84+
.addCode("field = %M(newValue)", copyList)
8485
.build()
8586
)
8687
}
@@ -127,7 +128,7 @@ private class DslGenerator(
127128
)
128129
}
129130

130-
private fun dslLines() =
131+
private fun builderLines() =
131132
properties.map {
132133
CodeBlock.of(
133134
"%N = this@%L.%N",
@@ -137,3 +138,6 @@ private class DslGenerator(
137138
)
138139
}
139140
}
141+
142+
val Message.builderClassName
143+
get() = className.nestedClass(BUILDER)

protokt-codegen/src/main/kotlin/protokt/v1/codegen/generate/ConstructorMethodSupport.kt

-40
This file was deleted.

protokt-codegen/src/main/kotlin/protokt/v1/codegen/generate/DeserializerGenerator.kt

+10-22
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@
1515

1616
package protokt.v1.codegen.generate
1717

18-
import com.squareup.kotlinpoet.AnnotationSpec
1918
import com.squareup.kotlinpoet.ClassName
2019
import com.squareup.kotlinpoet.CodeBlock
2120
import com.squareup.kotlinpoet.KModifier
2221
import com.squareup.kotlinpoet.LambdaTypeName
23-
import com.squareup.kotlinpoet.MemberName
2422
import com.squareup.kotlinpoet.ParameterizedTypeName
2523
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
2624
import com.squareup.kotlinpoet.TypeSpec
27-
import com.squareup.kotlinpoet.asClassName
2825
import com.squareup.kotlinpoet.asTypeName
2926
import com.squareup.kotlinpoet.buildCodeBlock
3027
import com.squareup.kotlinpoet.withIndent
@@ -45,6 +42,7 @@ import protokt.v1.codegen.util.FieldType.SInt32
4542
import protokt.v1.codegen.util.FieldType.SInt64
4643
import protokt.v1.codegen.util.FieldType.UInt32
4744
import protokt.v1.codegen.util.FieldType.UInt64
45+
import protokt.v1.codegen.util.KotlinPlugin
4846
import protokt.v1.codegen.util.Message
4947
import protokt.v1.codegen.util.Oneof
5048
import protokt.v1.codegen.util.StandardField
@@ -70,6 +68,9 @@ private class DeserializerGenerator(
7068
.addFunction(
7169
buildFunSpec("deserialize") {
7270
addModifiers(KModifier.OVERRIDE)
71+
if (ctx.info.context.appliedKotlinPlugin != KotlinPlugin.JS) {
72+
addAnnotation(JvmStatic::class) // can't put this here generally until JS code is actually common code in a multiplatform module
73+
}
7374
addParameter("deserializer", KtMessageDeserializer::class)
7475
returns(msg.className)
7576
if (properties.isNotEmpty()) {
@@ -109,35 +110,22 @@ private class DeserializerGenerator(
109110
endControlFlow()
110111
}
111112
)
112-
.apply {
113-
msg.nestedTypes
114-
.filterIsInstance<Message>()
115-
.filterNot { it.mapEntry }
116-
.forEach { addConstructorFunction(it, ::addFunction) }
117-
}
118113
.addFunction(
119114
buildFunSpec("invoke") {
115+
if (ctx.info.context.appliedKotlinPlugin != KotlinPlugin.JS) {
116+
addAnnotation(JvmStatic::class) // todo: remove when JS code is common in multiplatform
117+
}
118+
addModifiers(KModifier.OPERATOR)
120119
returns(msg.className)
121-
addAnnotation(
122-
AnnotationSpec.builder(Deprecated::class)
123-
.addMember("for ABI backwards compatibility only".embed())
124-
.addMember(
125-
CodeBlock.of(
126-
"level = %M",
127-
MemberName(DeprecationLevel::class.asClassName(), DeprecationLevel.HIDDEN.name)
128-
)
129-
)
130-
.build()
131-
)
132120
addParameter(
133121
"dsl",
134122
LambdaTypeName.get(
135-
msg.dslClassName,
123+
msg.builderClassName,
136124
emptyList(),
137125
Unit::class.asTypeName()
138126
)
139127
)
140-
addStatement("return %T().apply(dsl).build()", msg.dslClassName)
128+
addStatement("return %T().apply(dsl).build()", msg.builderClassName)
141129
build()
142130
}
143131
)

protokt-codegen/src/main/kotlin/protokt/v1/codegen/generate/DeserializerSupport.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ fun wrapDeserializedValueForConstructor(p: PropertyInfo) =
4646
}
4747
} else {
4848
if (p.map) {
49-
CodeBlock.of("%M(%N)", runtimeFunction("finishMap"), p.name)
49+
CodeBlock.of("%M(%N)", unmodifiableMap, p.name)
5050
} else if (p.repeated) {
51-
CodeBlock.of("%M(%N)", runtimeFunction("finishList"), p.name)
51+
CodeBlock.of("%M(%N)", unmodifiableList, p.name)
5252
} else {
5353
buildCodeBlock {
5454
add("%N", p.name)

0 commit comments

Comments
 (0)