Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions grpc/grpc-core/src/commonTest/proto/options.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";

message Options {
option deprecated = true;
string old = 1 [deprecated = true];
oneof oneof {
string first = 2 [deprecated = true];
}
map<string, string> map = 3 [deprecated = true];
repeated string list = 4 [deprecated = true];
}

enum OptionsEnum {
option deprecated = true;
option allow_alias = true;

SOME = 0 [deprecated = true];
SOME_2 = 0 [deprecated = true];
}

service SomeService {
option deprecated = true;
rpc SomeMethod(Options) returns (Options) {
option deprecated = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.rpc.protoc.gen.core.model.FqName
import kotlinx.rpc.protoc.gen.core.model.MessageDeclaration
import kotlinx.rpc.protoc.gen.core.model.Model
import kotlinx.rpc.protoc.gen.core.model.fullName
import org.slf4j.Logger

const val RPC_INTERNAL_PACKAGE_SUFFIX = "_rpc_internal"
const val MSG_INTERNAL_SUFFIX = "Internal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("DuplicatedCode")

package kotlinx.rpc.protoc.gen.core

@DslMarker
Expand Down Expand Up @@ -176,6 +178,7 @@ open class CodeGenerator(
modifiers: String = "",
contextReceiver: String = "",
annotations: List<String> = emptyList(),
deprecation: DeprecationLevel? = null,
type: String,
propertyInitializer: PropertyInitializer = PropertyInitializer.PLAIN,
value: String = "",
Expand All @@ -187,6 +190,7 @@ open class CodeGenerator(
for (annotation in annotations) {
addLine(annotation)
}
addDeprecation(deprecation)

val modifiersString = (if (modifiers.isEmpty()) "" else "$modifiers ").withVisibility()
val contextString = if (contextReceiver.isEmpty()) "" else "$contextReceiver."
Expand Down Expand Up @@ -218,13 +222,16 @@ open class CodeGenerator(
args: String = "",
contextReceiver: String = "",
annotations: List<String> = emptyList(),
deprecation: DeprecationLevel? = null,
returnType: String,
block: (CodeGenerator.() -> Unit)? = null,
) {
appendComment(comment)
for (annotation in annotations) {
addLine(annotation)
}
addDeprecation(deprecation)

val modifiersString = (if (modifiers.isEmpty()) "" else "$modifiers ").withVisibility()
val contextString = if (contextReceiver.isEmpty()) "" else "$contextReceiver."
val returnTypeString = if (returnType.isEmpty() || returnType == "Unit") "" else ": $returnType"
Expand All @@ -243,6 +250,7 @@ open class CodeGenerator(
modifiers: String = "",
superTypes: List<String> = emptyList(),
annotations: List<String> = emptyList(),
deprecation: DeprecationLevel? = null,
declarationType: DeclarationType = DeclarationType.Class,
block: (CodeGenerator.() -> Unit)? = null,
) {
Expand All @@ -253,6 +261,7 @@ open class CodeGenerator(
constructorArgs = emptyList<String>(),
superTypes = superTypes,
annotations = annotations,
deprecation = deprecation,
declarationType = declarationType,
block = block,
)
Expand All @@ -266,6 +275,7 @@ open class CodeGenerator(
constructorArgs: List<String> = emptyList(),
superTypes: List<String> = emptyList(),
annotations: List<String> = emptyList(),
deprecation: DeprecationLevel? = null,
declarationType: DeclarationType = DeclarationType.Class,
block: (CodeGenerator.() -> Unit)? = null,
) {
Expand All @@ -276,6 +286,7 @@ open class CodeGenerator(
constructorArgs = constructorArgs.map { it to null },
superTypes = superTypes,
annotations = annotations,
deprecation = deprecation,
declarationType = declarationType,
block = block,
)
Expand All @@ -289,13 +300,15 @@ open class CodeGenerator(
constructorArgs: List<Pair<String, String?>> = emptyList(),
superTypes: List<String> = emptyList(),
annotations: List<String> = emptyList(),
deprecation: DeprecationLevel? = null,
declarationType: DeclarationType = DeclarationType.Class,
block: (CodeGenerator.() -> Unit)? = null,
) {
appendComment(comment)
for (annotation in annotations) {
addLine(annotation)
}
addDeprecation(deprecation)

val modifiersString = (if (modifiers.isEmpty()) "" else "$modifiers ").withVisibility()

Expand Down Expand Up @@ -437,6 +450,17 @@ open class CodeGenerator(
}
}

private fun addDeprecation(deprecation: DeprecationLevel?) {
if (deprecation != null) {
val value = if (deprecation != DeprecationLevel.WARNING) {
"@Deprecated(\"This declaration is deprecated in .proto file\", DeprecationLevel.$deprecation)"
} else {
"@Deprecated(\"This declaration is deprecated in .proto file\")"
}
addLine(value)
}
}

@Suppress("PrivatePropertyName")
private val ONE_INDENT = " ".repeat(config.indentSize)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import kotlinx.rpc.protoc.gen.core.model.MethodDeclaration
import kotlinx.rpc.protoc.gen.core.model.Model
import kotlinx.rpc.protoc.gen.core.model.OneOfDeclaration
import kotlinx.rpc.protoc.gen.core.model.ServiceDeclaration
import kotlin.Boolean
import kotlin.collections.plus

private val nameCache = mutableMapOf<Descriptors.GenericDescriptor, FqName>()
Expand Down Expand Up @@ -127,6 +128,7 @@ private fun Descriptors.FileDescriptor.toModel(): FileDeclaration = cached {
(comments + Paths.editionsCommentPath).get(),
(comments + Paths.packageCommentPath).get()
),
deprecated = options.deprecated,
dec = this,
)
}
Expand Down Expand Up @@ -156,6 +158,7 @@ private fun Descriptors.Descriptor.toModel(comments: Comments?): MessageDeclarat
type = FieldType.OneOf(it),
doc = it.doc,
dec = it.variants.first().dec,
deprecated = options.deprecated,
)
}

Expand All @@ -169,6 +172,7 @@ private fun Descriptors.Descriptor.toModel(comments: Comments?): MessageDeclarat
nestedDeclarations = nestedTypes.map { it.toModel(comments + Paths.messageMessageCommentPath + it.index) },
doc = comments.get(),
dec = this,
deprecated = options.deprecated,
)
}

Expand All @@ -180,6 +184,7 @@ private fun Descriptors.FieldDescriptor.toModel(comments: Comments, presenceIdx:
presenceIdx = presenceIdx,
doc = comments.get(),
dec = this,
deprecated = options.deprecated,
)
}

Expand Down Expand Up @@ -219,6 +224,7 @@ private fun Descriptors.EnumDescriptor.toModel(comments: Comments?): EnumDeclara
aliases = aliases,
doc = comments.get(),
dec = this,
deprecated = options.deprecated,
)
}

Expand All @@ -227,6 +233,7 @@ private fun Descriptors.EnumValueDescriptor.toModel(comments: Comments): EnumDec
name = fqName(),
doc = comments.get(),
dec = this,
deprecated = options.deprecated,
)
}

Expand All @@ -237,6 +244,7 @@ private fun Descriptors.EnumValueDescriptor.toAliasModel(enumComments: Comments,
original = original,
doc = enumComments.get(),
dec = this,
deprecated = options.deprecated,
)
}

Expand All @@ -246,6 +254,7 @@ private fun Descriptors.ServiceDescriptor.toModel(comments: Comments): ServiceDe
methods = methods.map { it.toModel(comments + Paths.serviceMethodCommentPath + it.index) },
dec = this,
doc = comments.get(),
deprecated = options.deprecated,
)
}

Expand All @@ -256,6 +265,7 @@ private fun Descriptors.MethodDescriptor.toModel(comments: Comments): MethodDecl
outputType = lazy { outputType.toModel(null) },
dec = this,
doc = comments.get(),
deprecated = options.deprecated,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data class FileDeclaration(
val serviceDeclarations: List<ServiceDeclaration>,
val doc: List<Comment>,
val dec: Descriptors.FileDescriptor,
val deprecated: Boolean,
)

data class MessageDeclaration(
Expand All @@ -31,6 +32,7 @@ data class MessageDeclaration(
val nestedDeclarations: List<MessageDeclaration>,
val doc: Comment?,
val dec: Descriptors.Descriptor,
val deprecated: Boolean,
) {
val isMapEntry = dec.options.mapEntry
val isUserFacing = !isMapEntry
Expand All @@ -49,6 +51,7 @@ data class EnumDeclaration(
val aliases: List<Alias>,
val doc: Comment?,
val dec: Descriptors.EnumDescriptor,
val deprecated: Boolean,
) {

fun defaultEntry(): Entry {
Expand All @@ -63,13 +66,15 @@ data class EnumDeclaration(
val name: FqName,
val doc: Comment?,
val dec: Descriptors.EnumValueDescriptor,
val deprecated: Boolean,
)

data class Alias(
val name: FqName,
val original: Entry,
val doc: Comment?,
val dec: Descriptors.EnumValueDescriptor,
val deprecated: Boolean,
)
}

Expand All @@ -85,6 +90,7 @@ data class FieldDeclaration(
val type: FieldType,
val doc: Comment?,
val dec: Descriptors.FieldDescriptor,
val deprecated: Boolean,
// defines the index in the presenceMask of the Message.
// this cannot be the number, as only fields with hasPresence == true are part of the presenceMask
val presenceIdx: Int? = null,
Expand All @@ -111,6 +117,7 @@ data class ServiceDeclaration(
val methods: List<MethodDeclaration>,
val dec: Descriptors.ServiceDescriptor,
val doc: Comment?,
val deprecated: Boolean,
)

data class MethodDeclaration(
Expand All @@ -119,5 +126,6 @@ data class MethodDeclaration(
val outputType: Lazy<MessageDeclaration>,
val dec: Descriptors.MethodDescriptor,
val doc: Comment?,
val deprecated: Boolean,
)

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class ModelToGrpcKotlinCommonGenerator(
name = service.name.simpleName,
comment = service.doc,
declarationType = CodeGenerator.DeclarationType.Interface,
annotations = listOf("@kotlinx.rpc.grpc.annotations.Grpc$annotationParams")
annotations = listOf("@kotlinx.rpc.grpc.annotations.Grpc$annotationParams"),
deprecation = if (service.deprecated) DeprecationLevel.WARNING else null,
) {
service.methods.forEach { method ->
val inputType = method.inputType
Expand All @@ -53,6 +54,7 @@ class ModelToGrpcKotlinCommonGenerator(
modifiers = if (method.dec.isServerStreaming) "" else "suspend",
args = "message: ${inputType.value.name.safeFullName().wrapInFlowIf(method.dec.isClientStreaming)}",
annotations = annotations,
deprecation = if (method.deprecated) DeprecationLevel.WARNING else null,
returnType = outputType.value.name.safeFullName().wrapInFlowIf(method.dec.isServerStreaming),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,16 @@ class ModelToProtobufKotlinCommonGenerator(
name = declaration.name.simpleName,
comment = declaration.doc,
declarationType = CodeGenerator.DeclarationType.Interface,
annotations = annotations
annotations = annotations,
deprecation = if (declaration.deprecated) DeprecationLevel.WARNING else null,
) {
declaration.actualFields.forEachIndexed { i, field ->
property(
name = field.name,
comment = field.doc,
type = field.typeFqName(),
needsNewLineAfterDeclaration = i == declaration.actualFields.lastIndex,
deprecation = if (field.deprecated) DeprecationLevel.WARNING else null,
)
}

Expand Down Expand Up @@ -300,7 +302,7 @@ class ModelToProtobufKotlinCommonGenerator(
}

private fun CodeGenerator.generateMessageDecoder(declaration: MessageDeclaration) {
var args = "msg: ${declaration.internalClassFullName()}, decoder: $PB_PKG.WireDecoder";
var args = "msg: ${declaration.internalClassFullName()}, decoder: $PB_PKG.WireDecoder"
if (declaration.isGroup) {
args += ", startGroup: $PB_PKG.KTag"
}
Expand Down Expand Up @@ -406,8 +408,6 @@ class ModelToProtobufKotlinCommonGenerator(
}

is FieldType.Message -> {
val msg = fieldType.dec.value

whenCase("tag.fieldNr == ${field.number} && tag.wireType == $PB_PKG.WireType.${fieldType.wireType.name}") {
if (field.presenceIdx != null) {
// check if the current sub message object was already set, if not, set a new one
Expand Down Expand Up @@ -995,6 +995,7 @@ class ModelToProtobufKotlinCommonGenerator(
constructorArgs = listOf("val value: ${variant.typeFqName()}"),
annotations = listOf("@JvmInline"),
superTypes = listOf(interfaceName),
deprecation = if (variant.deprecated) DeprecationLevel.WARNING else null,
)

additionalPublicImports.add("kotlin.jvm.JvmInline")
Expand All @@ -1003,24 +1004,23 @@ class ModelToProtobufKotlinCommonGenerator(
}

private fun CodeGenerator.generatePublicEnum(declaration: EnumDeclaration) {

val className = declaration.name.simpleName

val entriesSorted = declaration.originalEntries.sortedBy { it.dec.number }

clazz(
name = className,
comment = declaration.doc,
modifiers = "sealed",
constructorArgs = listOf("open val number: Int"),
deprecation = if (declaration.deprecated) DeprecationLevel.WARNING else null,
) {

declaration.originalEntries.forEach { variant ->
clazz(
name = variant.name.simpleName,
comment = variant.doc,
declarationType = CodeGenerator.DeclarationType.Object,
superTypes = listOf("$className(number = ${variant.dec.number})"),
deprecation = if (variant.deprecated) DeprecationLevel.WARNING else null,
)
}

Expand All @@ -1042,6 +1042,7 @@ class ModelToProtobufKotlinCommonGenerator(
type = className,
propertyInitializer = CodeGenerator.PropertyInitializer.GETTER,
value = alias.original.name.simpleName,
deprecation = if (alias.deprecated) DeprecationLevel.WARNING else null,
)
}

Expand Down
Loading