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

Final updates to runtime and codegen fixes #352

Merged
merged 13 commits into from
Feb 27, 2025
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/RemotePublishing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ fun Project.enablePublishing(defaultJars: Boolean = true) {
configure(KotlinMultiplatform(JavadocJar.Empty()))
}
pluginManager.withPlugin(KotlinPlugins.JVM) {
configure(KotlinJvm(JavadocJar.Empty(), true))
configure(KotlinJvm(JavadocJar.Empty()))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the default value.

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ allprojects {
targetExclude("buildSrc/build/**")
licenseHeaderFile(
rootProject.file("gradle/license-header-c-style"),
"(package |@file|import |fun )|buildscript |plugins |subprojects |spotless "
"(package |@file|import |fun )|buildscript |plugins |subprojects |spotless |group ="
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Toast, Inc.
* Copyright (c) 2022 Toast, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,6 +13,4 @@
* limitations under the License.
*/

subprojects {
group = "${rootProject.group}.thirdparty"
}
group = "com.toasttab.protokt.thirdparty"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was getting clobbered by the common-conventions plugin - we've been publishing to the wrong group since that change went in.

Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@ import protokt.v1.codegen.generate.Nullability.nonNullPropName
import protokt.v1.codegen.util.Message
import protokt.v1.codegen.util.StandardField
import kotlin.reflect.KClass
import kotlin.reflect.KProperty

internal object Implements {
class OverriddenProperty(
val property: KProperty<*>
)

fun StandardField.overrides(
ctx: Context,
msg: Message
) =
msg.superInterface(ctx)
?.let { fieldName in ctx.info.context.classLookup.properties(it.canonicalName).map { p -> p.name } }
?: false
?.let {
ctx.info.context.classLookup
.properties(it.canonicalName)
.find { p -> p.name == fieldName }
?.let(::OverriddenProperty)
}

fun TypeSpec.Builder.handleSuperInterface(implements: ClassName?, v: OneofGeneratorInfo? = null) =
apply {
Expand All @@ -61,7 +70,7 @@ internal object Implements {
}

private fun TypeSpec.Builder.delegateProperties(msg: Message, ctx: Context, canonicalName: String, fieldName: String) {
val fieldsByName = msg.fields.filterIsInstance<StandardField>().associateBy { it.fieldName }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't exclude overrides that are oneofs.

val fieldsByName = msg.fields.associateBy { it.fieldName }

val interfaceFields =
ctx.info.context.classLookup
Expand All @@ -77,7 +86,7 @@ internal object Implements {
field.name,
(field.returnType.classifier as KClass<*>).asTypeName()
.let {
if (assumeNotNull) {
if (assumeNotNull && !field.returnType.isMarkedNullable) {
Copy link
Collaborator Author

@andrewparmet andrewparmet Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't delegate a field and mark it as non-nullable if the delegated field is nullable in the interface.

it
} else {
it.copy(nullable = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private class MessageGenerator(
)
}
initializer(property.name)
if (property.overrides) {
if (property.overrides != null) {
addModifiers(KModifier.OVERRIDE)
}
property.documentation?.let { addKdoc(formatDoc(it)) }
Expand All @@ -159,6 +159,9 @@ private class MessageGenerator(
.build()
)
}
if (property.overrides != null) {
addModifiers(KModifier.OVERRIDE)
}
initializer(property.name)
}.build(),
PropertySpec.builder(nonNullPropName(property.name), property.propertyType.copy(nullable = false)).apply {
Expand All @@ -167,16 +170,13 @@ private class MessageGenerator(
.addCode("return ${dereferenceNullableBackingProperty(property.name)}")
.build()
)
if (property.overrides) {
addModifiers(KModifier.OVERRIDE)
}
property.documentation?.let { addKdoc(formatDoc(it)) }
handleDeprecation(property.deprecation)
}.build()
)

private fun dereferenceNullableBackingProperty(propName: String) =
"requireNotNull($propName) { \"$propName is assumed non-null with (protokt.property).generate_non_null_accessor but was null\" }".bindSpaces()
"requireNotNull($propName) { \"$propName is assumed non-null with (protokt.v1.property).generate_non_null_accessor but was null\" }".bindSpaces()

private fun TypeSpec.Builder.handleMessageSize(propertySpecs: List<PropertySpec>) {
addProperty(generateMessageSize(msg, propertySpecs, ctx))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.squareup.kotlinpoet.TypeName
import com.squareup.kotlinpoet.asTypeName
import protokt.v1.codegen.generate.CodeGenerator.Context
import protokt.v1.codegen.generate.Deprecation.renderOptions
import protokt.v1.codegen.generate.Implements.OverriddenProperty
import protokt.v1.codegen.generate.Implements.overrides
import protokt.v1.codegen.generate.Nullability.deserializeType
import protokt.v1.codegen.generate.Nullability.dslPropertyType
Expand Down Expand Up @@ -146,7 +147,7 @@ internal class PropertyInfo(
val mapEntry: Message? = null,
val oneof: Boolean = false,
val wrapped: Boolean = false,
val overrides: Boolean = false,
val overrides: OverriddenProperty? = null,
val documentation: List<String>?,
val deprecation: Deprecation.RenderOptions? = null
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ private fun generateFdpObjectNames(files: List<FileDescriptorProto>): Map<String
Pair(
fdp.name,
fdp.fileOptions.protokt.fileDescriptorObjectName.takeIf { it.isNotEmpty() }
?: fdp.fileOptions.default.javaOuterClassname.takeIf { it.isNotEmpty() }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore all Java options.

?: (fdp.name.substringBefore(".proto").substringAfterLast('/') + "_file_descriptor")
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ import kotlin.Unit
* { "@type": "type.googleapis.com/google.protobuf.Duration", "value": "1.212s"
* }
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.Any")
class Any private constructor(
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import kotlin.collections.MutableList
* They are also sometimes simply referred to as "APIs" in other contexts, such as the name of this
* message itself. See https://cloud.google.com/apis/design/glossary for detailed terminology.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.Api")
class Api private constructor(
/**
Expand Down Expand Up @@ -281,6 +282,7 @@ class Api private constructor(
/**
* Method represents a method of an API interface.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.Method")
class Method private constructor(
/**
Expand Down Expand Up @@ -538,6 +540,7 @@ class Method private constructor(
* returns (Acl) { option (google.api.http).get = "/v2/acls/{resource=**}:getAcl"; }
* ... }
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.Mixin")
class Mixin private constructor(
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import kotlin.collections.MutableList
/**
* The version number of protocol compiler.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.compiler.Version")
class Version private constructor(
val major: Int?,
Expand Down Expand Up @@ -345,6 +346,7 @@ class CodeGeneratorRequest private constructor(
/**
* The plugin writes an encoded CodeGeneratorResponse to stdout.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.compiler.CodeGeneratorResponse")
class CodeGeneratorResponse private constructor(
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import kotlin.collections.MutableList
/**
* The protocol compiler can output a FileDescriptorSet containing the .proto files it parses.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.FileDescriptorSet")
class FileDescriptorSet private constructor(
val `file`: List<FileDescriptorProto>,
Expand Down Expand Up @@ -136,6 +137,7 @@ class FileDescriptorSet private constructor(
/**
* Describes a complete .proto file.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.FileDescriptorProto")
class FileDescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -487,6 +489,7 @@ class FileDescriptorProto private constructor(
/**
* Describes a message type.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.DescriptorProto")
class DescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -987,6 +990,7 @@ class DescriptorProto private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.ExtensionRangeOptions")
class ExtensionRangeOptions private constructor(
/**
Expand Down Expand Up @@ -1078,6 +1082,7 @@ class ExtensionRangeOptions private constructor(
/**
* Describes a field within a message.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.FieldDescriptorProto")
class FieldDescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -1490,6 +1495,7 @@ class FieldDescriptorProto private constructor(
/**
* Describes a oneof.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.OneofDescriptorProto")
class OneofDescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -1584,6 +1590,7 @@ class OneofDescriptorProto private constructor(
/**
* Describes an enum type.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.EnumDescriptorProto")
class EnumDescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -1859,6 +1866,7 @@ class EnumDescriptorProto private constructor(
/**
* Describes a value within an enum.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.EnumValueDescriptorProto")
class EnumValueDescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -1971,6 +1979,7 @@ class EnumValueDescriptorProto private constructor(
/**
* Describes a service.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.ServiceDescriptorProto")
class ServiceDescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -2091,6 +2100,7 @@ class ServiceDescriptorProto private constructor(
/**
* Describes a method of a service.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.MethodDescriptorProto")
class MethodDescriptorProto private constructor(
val name: String?,
Expand Down Expand Up @@ -2801,6 +2811,7 @@ class FileOptions private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.MessageOptions")
class MessageOptions private constructor(
/**
Expand Down Expand Up @@ -2994,6 +3005,7 @@ class MessageOptions private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.FieldOptions")
class FieldOptions private constructor(
/**
Expand Down Expand Up @@ -3323,6 +3335,7 @@ class FieldOptions private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.OneofOptions")
class OneofOptions private constructor(
/**
Expand Down Expand Up @@ -3408,6 +3421,7 @@ class OneofOptions private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.EnumOptions")
class EnumOptions private constructor(
/**
Expand Down Expand Up @@ -3535,6 +3549,7 @@ class EnumOptions private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.EnumValueOptions")
class EnumValueOptions private constructor(
/**
Expand Down Expand Up @@ -3643,6 +3658,7 @@ class EnumValueOptions private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.ServiceOptions")
class ServiceOptions private constructor(
/**
Expand Down Expand Up @@ -3751,6 +3767,7 @@ class ServiceOptions private constructor(
}
}

@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.MethodOptions")
class MethodOptions private constructor(
/**
Expand Down Expand Up @@ -3911,6 +3928,7 @@ class MethodOptions private constructor(
* objects. Therefore, options protos in descriptor objects (e.g. returned by Descriptor::options(), or
* produced by Descriptor::CopyTo()) will never have UninterpretedOptions in them.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.UninterpretedOption")
class UninterpretedOption private constructor(
val name: List<NamePart>,
Expand Down Expand Up @@ -4200,6 +4218,7 @@ class UninterpretedOption private constructor(
* Encapsulates information about the original source file from which a FileDescriptorProto was
* generated.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.SourceCodeInfo")
class SourceCodeInfo private constructor(
/**
Expand Down Expand Up @@ -4552,6 +4571,7 @@ class SourceCodeInfo private constructor(
* GeneratedCodeInfo message is associated with only one generated source file, but may contain
* references to different source .proto files.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.GeneratedCodeInfo")
class GeneratedCodeInfo private constructor(
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import kotlin.Unit
*
*
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.Duration")
class Duration private constructor(
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import kotlin.Unit
*
* service Foo { rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty); }
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.Empty")
class Empty private constructor(
val unknownFields: UnknownFieldSet = UnknownFieldSet.empty(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ import kotlin.collections.MutableList
* The implementation of any API method which has a FieldMask type field in the request should
* verify the included field paths, and return an `INVALID_ARGUMENT` error if any path is unmappable.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.FieldMask")
class FieldMask private constructor(
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import kotlin.Unit
* `SourceContext` represents information about the source of a protobuf element, like the file in
* which it is defined.
*/
@Deprecated("for backwards compatibility only")
@KtGeneratedMessage("google.protobuf.SourceContext")
class SourceContext private constructor(
/**
Expand Down
Loading
Loading