Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into gradle-kotlin-bumps
Browse files Browse the repository at this point in the history
  • Loading branch information
ogolberg committed Mar 10, 2024
2 parents 3a1ec95 + d74dc53 commit 92099cd
Show file tree
Hide file tree
Showing 55 changed files with 688 additions and 422 deletions.
9 changes: 9 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ dependencies {
implementation(kotlin("gradle-plugin-api"))
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
}

sourceSets {
main {
java {
srcDir("../shared-src/codegen")
srcDir("../shared-src/gradle-plugin")
}
}
}
9 changes: 0 additions & 9 deletions buildSrc/src/main/kotlin/LocalProtoktBuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@

import com.google.protobuf.gradle.GenerateProtoTask
import org.gradle.api.Project
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.the
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.targets.js.dsl.KotlinJsTargetDsl
import protokt.v1.gradle.CODEGEN_NAME
Expand All @@ -37,13 +35,6 @@ fun Project.localProtokt() {
}
}

fun Project.includeBuildSrc(vararg filePatterns: String) {
the<SourceSetContainer>()["main"].java {
srcDir(rootProject.file("buildSrc/src/main/kotlin"))
filePatterns.forEach { include(it) }
}
}

fun Project.pureKotlin() {
tasks.withType<JavaCompile> {
enabled = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ allprojects {
"examples/protos/src/main/proto/io/grpc/examples/route_guide.proto",
"testing/conformance/driver/src/main/proto/conformance/conformance.proto",
"testing/conformance/driver/src/main/proto/proto3/test_messages_proto3.proto",
"testing/interop/src/main/proto/tutorial/addressbook.proto"
"testing/interop/src/main/proto/tutorial/addressbook.proto",
"testing/interop/src/main/proto/google/protobuf/unittest_import.proto",
"testing/interop/src/main/proto/google/protobuf/unittest_import_public.proto",
"testing/interop/src/main/proto/google/protobuf/unittest_proto3.proto",
).map(rootProject::file) +
"node_modules/**" +
"**/build/extracted-include-protos/**" +
Expand Down
3 changes: 1 addition & 2 deletions examples/grpc-java-lite/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ dependencies {
implementation(libs.jackson)

runtimeOnly(libs.protobuf.lite)

testImplementation(project(":protokt-util"))
}

sourceSets {
Expand All @@ -51,6 +49,7 @@ sourceSets {
test {
java {
srcDir(liteOptionTestSourceDir())
srcDir(rootProject.file("shared-src/lite-util"))
}
}
}
2 changes: 1 addition & 1 deletion examples/grpc-kotlin-lite/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ dependencies {

testImplementation(kotlin("test-junit"))
testImplementation(libs.grpc.testing)
testImplementation(project(":protokt-util"))
}

sourceSets {
Expand All @@ -60,6 +59,7 @@ sourceSets {
java {
srcDir("../grpc-kotlin/src/test/kotlin")
srcDir(liteOptionTestSourceDir())
srcDir(rootProject.file("shared-src/lite-util"))
}
}
}
3 changes: 2 additions & 1 deletion gradle-plugin-integration-test/jvm-lite/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dependencies {
testImplementation(kotlin("test-junit5"))
testImplementation(libs.junit.jupiter)
testImplementation(libs.protobuf.java)
testImplementation("com.toasttab.protokt:protokt-util:$version")
}

sourceSets {
Expand All @@ -53,6 +52,8 @@ sourceSets {
check(file(lite).exists())
srcDir(common)
srcDir(lite)

srcDir(rootProject.file("../shared-src/lite-util"))
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions protokt-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ tasks.withType<Test> {

sourceSets {
main {
java {
srcDir("../shared-src/codegen")
}
proto {
srcDir("../extensions/protokt-extensions-lite/src/main/proto")
}
Expand All @@ -93,8 +96,3 @@ buildConfig {
buildConfigField("String", "DEFAULT_PROTOBUF_VERSION", "\"${libs.versions.protobuf.java.get()}\"")
buildConfigField("String", "PROTOKT_VERSION", "\"$version\"")
}

includeBuildSrc(
"protokt/v1/gradle/ProtoktExtension.kt",
"**/*.java" // don't override the protobuf-gradle-plugin; todo: fix this function to not need this
)
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private class BuilderGenerator(
.mutable(true)
.handleDeprecation(it.deprecation)
.apply {
if (it.map) {
if (it.isMap) {
setter(
FunSpec.setterBuilder()
.addParameter("newValue", Map::class)
Expand All @@ -88,7 +88,7 @@ private class BuilderGenerator(
}
.initializer(
when {
it.map -> CodeBlock.of("emptyMap()")
it.isMap -> CodeBlock.of("emptyMap()")
it.repeated -> CodeBlock.of("emptyList()")
it.fieldType == FieldType.Message || it.wrapped || it.nullable -> CodeBlock.of("null")
else -> it.defaultValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ object CodeGenerator {

fun generate(contents: ProtoFileContents) =
contents.types.flatMap {
generate(
it,
Context(emptyList(), contents.info)
)
.map { type -> GeneratedType(it, type) }
generate(it, Context(emptyList(), contents.info)).map(::GeneratedType)
}

fun generate(type: TopLevelType, ctx: Context): Iterable<TypeSpec> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ import protokt.v1.KtMessageDeserializer
import protokt.v1.UnknownFieldSet
import protokt.v1.codegen.generate.CodeGenerator.Context
import protokt.v1.codegen.generate.Wrapper.interceptRead
import protokt.v1.codegen.generate.Wrapper.mapKeyConverter
import protokt.v1.codegen.generate.Wrapper.mapValueConverter
import protokt.v1.codegen.generate.Wrapper.wrapField
import protokt.v1.codegen.generate.Wrapper.wrapper
import protokt.v1.codegen.util.FieldType
import protokt.v1.codegen.util.FieldType.Enum
import protokt.v1.codegen.util.FieldType.SFixed32
Expand Down Expand Up @@ -83,7 +80,7 @@ private class DeserializerGenerator(
beginControlFlow("when (deserializer.readTag())")
val constructor =
buildCodeBlock {
add("0·->·return·%T(\n", msg.className)
add("0u·->·return·%T(\n", msg.className)
withIndent {
constructorLines(properties).forEach(::add)
}
Expand All @@ -92,7 +89,7 @@ private class DeserializerGenerator(
addStatement("%L", constructor)
deserializerInfo.forEach {
addStatement(
"%L -> %N = %L",
"%Lu -> %N = %L",
it.tag,
it.fieldName,
it.value
Expand Down Expand Up @@ -142,7 +139,7 @@ private class DeserializerGenerator(
}

private fun deserializeType(p: PropertyInfo) =
if (p.repeated || p.map) {
if (p.repeated || p.isMap) {
p.deserializeType as ParameterizedTypeName
ClassName(p.deserializeType.rawType.packageName, "Mutable" + p.deserializeType.rawType.simpleName)
.parameterizedBy(p.deserializeType.typeArguments)
Expand All @@ -156,7 +153,7 @@ private class DeserializerGenerator(
CodeBlock.of("%T.from(unknownFields)", UnknownFieldSet::class)

private class DeserializerInfo(
val tag: Int,
val tag: UInt,
val fieldName: String,
val value: CodeBlock
)
Expand Down Expand Up @@ -217,10 +214,10 @@ private class DeserializerGenerator(

fun deserialize(f: StandardField, ctx: Context, packed: Boolean = false): CodeBlock {
val read = CodeBlock.of("deserializer.%L", interceptRead(f, f.readFn()))
val wrappedRead = wrapper(f, ctx)?.let { wrapField(it, read) } ?: read
val wrappedRead = wrapField(f, ctx, read) ?: read

return when {
f.map -> deserializeMap(f, ctx, read)
f.isMap -> deserializeMap(f, read)
f.repeated ->
buildCodeBlock {
add("\n(%N ?: mutableListOf())", f.fieldName)
Expand All @@ -234,24 +231,14 @@ fun deserialize(f: StandardField, ctx: Context, packed: Boolean = false): CodeBl
}
}

private fun deserializeMap(f: StandardField, ctx: Context, read: CodeBlock): CodeBlock {
val key =
mapKeyConverter(f, ctx)
?.let { wrapField(it, CodeBlock.of("it.key")) }
?: CodeBlock.of("it.key")

val value =
mapValueConverter(f, ctx)
?.let { wrapField(it, CodeBlock.of("it.value")) }
?: CodeBlock.of("it.value")

private fun deserializeMap(f: StandardField, read: CodeBlock): CodeBlock {
return buildCodeBlock {
add("\n(%N ?: mutableMapOf())", f.fieldName)
beginControlFlow(".apply")
beginControlFlow("deserializer.readRepeated(false)")
add(read)
beginControlFlow(".let")
add("put(%L, %L)\n", key, value)
add("put(%L, %L)\n", CodeBlock.of("it.key"), CodeBlock.of("it.value"))
endControlFlow()
endControlFlow()
endControlFlowWithoutNewline()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import com.squareup.kotlinpoet.withIndent
import protokt.v1.codegen.util.FieldType

fun deserializeVarInitialState(p: PropertyInfo) =
if (
(p.repeated || p.wrapped || p.nullable || p.fieldType == FieldType.Message) &&
!p.mapEntry
) {
if (p.repeated || p.wrapped || p.nullable || p.fieldType == FieldType.Message) {
CodeBlock.of("null")
} else {
p.defaultValue
Expand All @@ -48,7 +45,7 @@ fun wrapDeserializedValueForConstructor(p: PropertyInfo) =
endControlFlowWithoutNewline()
}
} else {
if (p.map) {
if (p.isMap) {
CodeBlock.of("%M(%N)", unmodifiableMap, p.name)
} else if (p.repeated) {
CodeBlock.of("%M(%N)", unmodifiableList, p.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object Implements {
msg: Message
) =
msg.superInterface(ctx)
?.let { fieldName in ctx.info.context.classLookup.properties(it) }
?.let { fieldName in ctx.info.context.classLookup.properties(it.canonicalName) }
?: false

fun TypeSpec.Builder.handleSuperInterface(implements: ClassName?, v: OneofGeneratorInfo? = null) =
Expand All @@ -46,7 +46,6 @@ object Implements {
if (msg.options.protokt.implements.isNotEmpty()) {
if (msg.options.protokt.implements.delegates()) {
addSuperinterface(
// TODO: parameterize this by the ctx package?
ClassName.bestGuess(msg.options.protokt.implements.substringBefore(" by ")),
msg.options.protokt.implements.substringAfter(" by ")
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.squareup.kotlinpoet.asTypeName
import protokt.v1.Collections
import protokt.v1.SizeCodecs
import protokt.v1.codegen.generate.CodeGenerator.Context
import protokt.v1.codegen.util.inferClassName
import kotlin.reflect.KClass
import kotlin.reflect.KFunction1

Expand Down Expand Up @@ -85,14 +86,6 @@ fun CodeBlock.Builder.endControlFlowWithoutNewline() {
add("}")
}

fun inferClassName(className: String, ctx: Context) =
internal fun inferClassName(className: String, ctx: Context) =
inferClassName(className, ctx.info.kotlinPackage)

fun inferClassName(className: String, pkg: String): ClassName {
val inferred = ClassName.bestGuess(className)
return if (inferred.packageName == "") {
ClassName(pkg, className.split("."))
} else {
inferred
}
}
.let { (pkg, names) -> ClassName(pkg, names) }
Loading

0 comments on commit 92099cd

Please sign in to comment.