Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime polish #198

Merged
merged 37 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
9ea8e36
runtime polish
andrewparmet Sep 28, 2023
d32d6ee
more polish
andrewparmet Sep 29, 2023
e16b816
upgrades
andrewparmet Sep 29, 2023
c682497
fix code cleanup
andrewparmet Sep 29, 2023
9f63462
lint
andrewparmet Sep 29, 2023
9137dc5
configure js tests
andrewparmet Sep 29, 2023
7ee2a1d
remove unneeded lines
andrewparmet Sep 29, 2023
b495979
remove more unneeded lines
andrewparmet Sep 29, 2023
2ec260c
whoops
andrewparmet Sep 29, 2023
6a7359b
more cleanup
andrewparmet Sep 29, 2023
8fe4e32
compilation
andrewparmet Sep 29, 2023
de87193
fix recursion
andrewparmet Sep 29, 2023
acb8244
compilation
andrewparmet Sep 29, 2023
74c33e4
compilation
andrewparmet Sep 29, 2023
7a2cd55
compilation
andrewparmet Sep 29, 2023
b81a518
integration compilation
andrewparmet Sep 29, 2023
92e022a
a bit of cleanup
andrewparmet Sep 29, 2023
86dbca3
value classes, eh
andrewparmet Sep 29, 2023
a3b6d04
api dump
andrewparmet Sep 29, 2023
ad17060
less copying
andrewparmet Sep 29, 2023
2e68229
cleanup consts
andrewparmet Sep 29, 2023
5856773
more cleanup
andrewparmet Sep 29, 2023
9904eda
jvm name instead of mangled
andrewparmet Sep 29, 2023
7fa5dda
unused parameter
andrewparmet Sep 29, 2023
b92cf14
cleanup
andrewparmet Sep 29, 2023
6ac8610
api dump
andrewparmet Sep 29, 2023
a73f456
be selective with jvmstatic for now
andrewparmet Sep 30, 2023
a2c1127
back to deserializer constructor fns
andrewparmet Oct 1, 2023
58c8218
api dump
andrewparmet Oct 1, 2023
a1d1b06
lint
andrewparmet Oct 1, 2023
8ff1449
use builder instead of dsl
andrewparmet Oct 1, 2023
2290d04
fix tests
andrewparmet Oct 1, 2023
c9661a5
lint
andrewparmet Oct 1, 2023
0c00d68
bytes from message
andrewparmet Oct 1, 2023
9bd9cc4
update todo
andrewparmet Oct 1, 2023
eda0347
fix dsl
andrewparmet Oct 1, 2023
5e2ecd4
fix more
andrewparmet Oct 1, 2023
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 @@ -91,7 +91,7 @@ class FixtureGenerator(private val weight: Int) {
Boolean::class -> random.nextInt() % 2 == 0
Float::class -> random.nextFloat()
Double::class -> random.nextDouble()
Bytes::class -> Bytes(ByteArray(randomSize()).also { random.nextBytes(it) })
Bytes::class -> Bytes.from(ByteArray(randomSize()).also { random.nextBytes(it) })
String::class -> String(ByteArray(randomSize()).also { random.nextBytes(it) })
else -> throw IllegalArgumentException("unknown $cls")
}
Expand All @@ -102,9 +102,7 @@ class FixtureGenerator(private val weight: Int) {
BenchmarkDataset {
this.name = name
messageName = msg.qualifiedName!!
payload = (0..size).map {
Bytes(randomMessageValue(msg).serialize())
}
payload = (0..size).map { Bytes.from(randomMessageValue(msg)) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ kotlin {
}

js(IR) {
browser {}
nodejs {}
useCommonJs()
configureJsTests()
}

sourceSets {
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}

val jvmTest by getting {
dependencies {
implementation(libs.junit.jupiter)
Expand Down
5 changes: 3 additions & 2 deletions examples/grpc-node/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ fun GradleBuild.setUp(service: String, mode: String, vararg extra: Pair<String,
startParameter.projectProperties =
mapOf(
"service" to service,
"mode" to mode
) + extra.toMap()
"mode" to mode,
*extra
)
}

tasks.register<GradleBuild>("HelloWorldServer") {
Expand Down
302 changes: 156 additions & 146 deletions extensions/protokt-extensions-lite/api/protokt-extensions-lite.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,19 @@ public final class protokt/v1/InetSocketAddress : protokt/v1/AbstractKtMessage {
public static final field Deserializer Lprotokt/v1/InetSocketAddress$Deserializer;
public synthetic fun <init> (Ljava/net/InetAddress;ILprotokt/v1/UnknownFieldSet;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun copy (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
public static fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/InetSocketAddress;
public fun equals (Ljava/lang/Object;)Z
public final fun getAddress ()Ljava/net/InetAddress;
public fun getMessageSize ()I
public final fun getPort ()I
public final fun getUnknownFields ()Lprotokt/v1/UnknownFieldSet;
public fun hashCode ()I
public static final fun invoke (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
public fun serialize (Lprotokt/v1/KtMessageSerializer;)V
public fun toString ()Ljava/lang/String;
}

public final class protokt/v1/InetSocketAddress$Deserializer : protokt/v1/AbstractKtDeserializer {
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lcom/toasttab/protokt/rt/KtMessage;
public fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/InetSocketAddress;
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/KtMessage;
public final synthetic fun invoke (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
}

public final class protokt/v1/InetSocketAddress$InetSocketAddressDsl {
public final class protokt/v1/InetSocketAddress$Builder {
public fun <init> ()V
public final fun build ()Lprotokt/v1/InetSocketAddress;
public final fun getAddress ()Ljava/net/InetAddress;
Expand All @@ -90,6 +85,13 @@ public final class protokt/v1/InetSocketAddress$InetSocketAddressDsl {
public final fun setUnknownFields (Lprotokt/v1/UnknownFieldSet;)V
}

public final class protokt/v1/InetSocketAddress$Deserializer : protokt/v1/AbstractKtDeserializer {
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lcom/toasttab/protokt/rt/KtMessage;
public fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/InetSocketAddress;
public synthetic fun deserialize (Lprotokt/v1/KtMessageDeserializer;)Lprotokt/v1/KtMessage;
public final fun invoke (Lkotlin/jvm/functions/Function1;)Lprotokt/v1/InetSocketAddress;
}

public final class protokt/v1/InetSocketAddressConverter : protokt/v1/Converter {
public static final field INSTANCE Lprotokt/v1/InetSocketAddressConverter;
public fun getWrapped ()Lkotlin/reflect/KClass;
Expand All @@ -100,7 +102,3 @@ public final class protokt/v1/InetSocketAddressConverter : protokt/v1/Converter
public fun wrap (Lprotokt/v1/InetSocketAddress;)Ljava/net/InetSocketAddress;
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ object InetAddressBytesConverter : Converter<InetAddress, Bytes> {
}

override fun unwrap(wrapped: InetAddress): Bytes =
Bytes(wrapped.address)
Bytes.from(wrapped.address)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package protokt.v1

import com.google.auto.service.AutoService
import protokt.v1.JvmBytes.asReadOnlyBuffer
import protokt.v1.SizeCodecs.sizeOf
import java.nio.ByteBuffer
import java.util.UUID

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

override fun unwrap(wrapped: UUID): Bytes =
Bytes(
Bytes.from(
ByteBuffer.allocate(16)
.putLong(wrapped.mostSignificantBits)
.putLong(wrapped.leastSignificantBits)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package protokt.v1

import com.google.auto.service.AutoService
import protokt.v1.SizeCodecs.sizeOf
import protokt.v1.google.protobuf.BytesValue
import java.util.UUID

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

private val sizeOfProxy =
BytesValue { value = Bytes(ByteArray(16)) }
BytesValue { value = Bytes.from(ByteArray(16)) }

override fun sizeOf(wrapped: UUID) =
sizeOf(sizeOfProxy)
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ object IdConverter : Converter<Id, Bytes> {
Id(String(unwrapped.bytes))

override fun unwrap(wrapped: Id) =
Bytes(wrapped.value.toByteArray())
Bytes.from(wrapped.value.toByteArray())
}
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

[versions]
autoService = "1.0.1"
grpc-java = "1.56.1"
grpc-kotlin = "1.3.0"
grpc-java = "1.58.0"
grpc-kotlin = "1.4.0"
kotlinLogging = "5.1.0"
kotlinPoet = "1.14.2"
kotlinx-coroutines = "1.6.0"
kotlinx-serialization = "1.5.1"
ktlint = "0.50.0"
ktlint = "1.0.0"
protobuf-java = "3.21.7"
protobuf-js = "6.11.3"
protobufGradlePlugin = "0.9.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,64 +23,65 @@ import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.buildCodeBlock
import com.squareup.kotlinpoet.withIndent
import protokt.v1.KtDsl
import protokt.v1.KtBuilderDsl
import protokt.v1.UnknownFieldSet
import protokt.v1.codegen.generate.Deprecation.handleDeprecation
import protokt.v1.codegen.util.BUILDER
import protokt.v1.codegen.util.FieldType
import protokt.v1.codegen.util.Message

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

private class DslGenerator(
private class BuilderGenerator(
private val msg: Message,
private val properties: List<PropertyInfo>
) {
fun addDsl(builder: TypeSpec.Builder) {
fun addBuilder(builder: TypeSpec.Builder) {
builder.addFunction(
FunSpec.builder("copy")
.returns(msg.className)
.addParameter(
"dsl",
"builder",
LambdaTypeName.get(
msg.dslClassName,
msg.builderClassName,
emptyList(),
Unit::class.asTypeName()
)
)
.addCode(
buildCodeBlock {
beginControlFlow("return %T().apply", msg.dslClassName)
dslLines().forEach { add("%L\n", it) }
beginControlFlow("return %T().apply", msg.builderClassName)
builderLines().forEach { add("%L\n", it) }
addStatement("unknownFields = this@%L.unknownFields", msg.className.simpleName)
addStatement("dsl()")
addStatement("builder()")
endControlFlowWithoutNewline()
add(".build()")
}
)
.build()
)
builder.addType(
TypeSpec.classBuilder(msg.dslClassName)
.addAnnotation(KtDsl::class)
TypeSpec.classBuilder(msg.builderClassName)
.addAnnotation(KtBuilderDsl::class)
.addProperties(
properties.map {
PropertySpec.builder(it.name, it.dslPropertyType)
PropertySpec.builder(it.name, it.builderPropertyType)
.mutable(true)
.handleDeprecation(it.deprecation)
.apply {
if (it.map) {
setter(
FunSpec.setterBuilder()
.addParameter("newValue", Map::class)
.addCode("field = %M(newValue)", runtimeFunction("copyMap"))
.addCode("field = %M(newValue)", copyMap)
.build()
)
} else if (it.repeated) {
setter(
FunSpec.setterBuilder()
.addParameter("newValue", List::class)
.addCode("field = %M(newValue)", runtimeFunction("copyList"))
.addCode("field = %M(newValue)", copyList)
.build()
)
}
Expand Down Expand Up @@ -127,7 +128,7 @@ private class DslGenerator(
)
}

private fun dslLines() =
private fun builderLines() =
properties.map {
CodeBlock.of(
"%N = this@%L.%N",
Expand All @@ -137,3 +138,6 @@ private class DslGenerator(
)
}
}

val Message.builderClassName
get() = className.nestedClass(BUILDER)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@

package protokt.v1.codegen.generate

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.LambdaTypeName
import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.ParameterizedTypeName
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.TypeSpec
import com.squareup.kotlinpoet.asClassName
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.buildCodeBlock
import com.squareup.kotlinpoet.withIndent
Expand All @@ -45,6 +42,7 @@ import protokt.v1.codegen.util.FieldType.SInt32
import protokt.v1.codegen.util.FieldType.SInt64
import protokt.v1.codegen.util.FieldType.UInt32
import protokt.v1.codegen.util.FieldType.UInt64
import protokt.v1.codegen.util.KotlinPlugin
import protokt.v1.codegen.util.Message
import protokt.v1.codegen.util.Oneof
import protokt.v1.codegen.util.StandardField
Expand All @@ -70,6 +68,9 @@ private class DeserializerGenerator(
.addFunction(
buildFunSpec("deserialize") {
addModifiers(KModifier.OVERRIDE)
if (ctx.info.context.appliedKotlinPlugin != KotlinPlugin.JS) {
addAnnotation(JvmStatic::class) // can't put this here generally until JS code is actually common code in a multiplatform module
}
addParameter("deserializer", KtMessageDeserializer::class)
returns(msg.className)
if (properties.isNotEmpty()) {
Expand Down Expand Up @@ -109,35 +110,22 @@ private class DeserializerGenerator(
endControlFlow()
}
)
.apply {
msg.nestedTypes
.filterIsInstance<Message>()
.filterNot { it.mapEntry }
.forEach { addConstructorFunction(it, ::addFunction) }
}
.addFunction(
buildFunSpec("invoke") {
if (ctx.info.context.appliedKotlinPlugin != KotlinPlugin.JS) {
addAnnotation(JvmStatic::class) // todo: remove when JS code is common in multiplatform
}
addModifiers(KModifier.OPERATOR)
returns(msg.className)
addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.addMember("for ABI backwards compatibility only".embed())
.addMember(
CodeBlock.of(
"level = %M",
MemberName(DeprecationLevel::class.asClassName(), DeprecationLevel.HIDDEN.name)
)
)
.build()
)
addParameter(
"dsl",
LambdaTypeName.get(
msg.dslClassName,
msg.builderClassName,
emptyList(),
Unit::class.asTypeName()
)
)
addStatement("return %T().apply(dsl).build()", msg.dslClassName)
addStatement("return %T().apply(dsl).build()", msg.builderClassName)
build()
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ fun wrapDeserializedValueForConstructor(p: PropertyInfo) =
}
} else {
if (p.map) {
CodeBlock.of("%M(%N)", runtimeFunction("finishMap"), p.name)
CodeBlock.of("%M(%N)", unmodifiableMap, p.name)
} else if (p.repeated) {
CodeBlock.of("%M(%N)", runtimeFunction("finishList"), p.name)
CodeBlock.of("%M(%N)", unmodifiableList, p.name)
} else {
buildCodeBlock {
add("%N", p.name)
Expand Down
Loading