Skip to content

Commit f5c8b34

Browse files
committed
Supported deprecated options
1 parent 1048217 commit f5c8b34

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
syntax = "proto3";
2+
3+
message Options {
4+
option deprecated = true;
5+
string old = 1 [deprecated = true];
6+
oneof oneof {
7+
string first = 2 [deprecated = true];
8+
}
9+
map<string, string> map = 3 [deprecated = true];
10+
repeated string list = 4 [deprecated = true];
11+
}
12+
13+
enum OptionsEnum {
14+
option deprecated = true;
15+
option allow_alias = true;
16+
17+
SOME = 0 [deprecated = true];
18+
SOME_2 = 0 [deprecated = true];
19+
}
20+
21+
service SomeService {
22+
option deprecated = true;
23+
rpc SomeMethod(Options) returns (Options) {
24+
option deprecated = true;
25+
}
26+
}

protoc-gen/common/src/main/kotlin/kotlinx/rpc/protoc/gen/core/AModelToKotlinCommonGenerator.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import kotlinx.rpc.protoc.gen.core.model.FqName
1111
import kotlinx.rpc.protoc.gen.core.model.MessageDeclaration
1212
import kotlinx.rpc.protoc.gen.core.model.Model
1313
import kotlinx.rpc.protoc.gen.core.model.fullName
14-
import org.slf4j.Logger
1514

1615
const val RPC_INTERNAL_PACKAGE_SUFFIX = "_rpc_internal"
1716
const val MSG_INTERNAL_SUFFIX = "Internal"

protoc-gen/common/src/main/kotlin/kotlinx/rpc/protoc/gen/core/CodeGenerator.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:Suppress("DuplicatedCode")
6+
57
package kotlinx.rpc.protoc.gen.core
68

79
@DslMarker
@@ -176,6 +178,7 @@ open class CodeGenerator(
176178
modifiers: String = "",
177179
contextReceiver: String = "",
178180
annotations: List<String> = emptyList(),
181+
deprecation: DeprecationLevel? = null,
179182
type: String,
180183
propertyInitializer: PropertyInitializer = PropertyInitializer.PLAIN,
181184
value: String = "",
@@ -187,6 +190,7 @@ open class CodeGenerator(
187190
for (annotation in annotations) {
188191
addLine(annotation)
189192
}
193+
addDeprecation(deprecation)
190194

191195
val modifiersString = (if (modifiers.isEmpty()) "" else "$modifiers ").withVisibility()
192196
val contextString = if (contextReceiver.isEmpty()) "" else "$contextReceiver."
@@ -218,13 +222,16 @@ open class CodeGenerator(
218222
args: String = "",
219223
contextReceiver: String = "",
220224
annotations: List<String> = emptyList(),
225+
deprecation: DeprecationLevel? = null,
221226
returnType: String,
222227
block: (CodeGenerator.() -> Unit)? = null,
223228
) {
224229
appendComment(comment)
225230
for (annotation in annotations) {
226231
addLine(annotation)
227232
}
233+
addDeprecation(deprecation)
234+
228235
val modifiersString = (if (modifiers.isEmpty()) "" else "$modifiers ").withVisibility()
229236
val contextString = if (contextReceiver.isEmpty()) "" else "$contextReceiver."
230237
val returnTypeString = if (returnType.isEmpty() || returnType == "Unit") "" else ": $returnType"
@@ -243,6 +250,7 @@ open class CodeGenerator(
243250
modifiers: String = "",
244251
superTypes: List<String> = emptyList(),
245252
annotations: List<String> = emptyList(),
253+
deprecation: DeprecationLevel? = null,
246254
declarationType: DeclarationType = DeclarationType.Class,
247255
block: (CodeGenerator.() -> Unit)? = null,
248256
) {
@@ -253,6 +261,7 @@ open class CodeGenerator(
253261
constructorArgs = emptyList<String>(),
254262
superTypes = superTypes,
255263
annotations = annotations,
264+
deprecation = deprecation,
256265
declarationType = declarationType,
257266
block = block,
258267
)
@@ -266,6 +275,7 @@ open class CodeGenerator(
266275
constructorArgs: List<String> = emptyList(),
267276
superTypes: List<String> = emptyList(),
268277
annotations: List<String> = emptyList(),
278+
deprecation: DeprecationLevel? = null,
269279
declarationType: DeclarationType = DeclarationType.Class,
270280
block: (CodeGenerator.() -> Unit)? = null,
271281
) {
@@ -276,6 +286,7 @@ open class CodeGenerator(
276286
constructorArgs = constructorArgs.map { it to null },
277287
superTypes = superTypes,
278288
annotations = annotations,
289+
deprecation = deprecation,
279290
declarationType = declarationType,
280291
block = block,
281292
)
@@ -289,13 +300,15 @@ open class CodeGenerator(
289300
constructorArgs: List<Pair<String, String?>> = emptyList(),
290301
superTypes: List<String> = emptyList(),
291302
annotations: List<String> = emptyList(),
303+
deprecation: DeprecationLevel? = null,
292304
declarationType: DeclarationType = DeclarationType.Class,
293305
block: (CodeGenerator.() -> Unit)? = null,
294306
) {
295307
appendComment(comment)
296308
for (annotation in annotations) {
297309
addLine(annotation)
298310
}
311+
addDeprecation(deprecation)
299312

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

@@ -437,6 +450,17 @@ open class CodeGenerator(
437450
}
438451
}
439452

453+
private fun addDeprecation(deprecation: DeprecationLevel?) {
454+
if (deprecation != null) {
455+
val value = if (deprecation != DeprecationLevel.WARNING) {
456+
"@Deprecated(\"This declaration is deprecated in .proto file\", DeprecationLevel.$deprecation)"
457+
} else {
458+
"@Deprecated(\"This declaration is deprecated in .proto file\")"
459+
}
460+
addLine(value)
461+
}
462+
}
463+
440464
@Suppress("PrivatePropertyName")
441465
private val ONE_INDENT = " ".repeat(config.indentSize)
442466
}

protoc-gen/common/src/main/kotlin/kotlinx/rpc/protoc/gen/core/codeRequestToModel.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import kotlinx.rpc.protoc.gen.core.model.MethodDeclaration
1717
import kotlinx.rpc.protoc.gen.core.model.Model
1818
import kotlinx.rpc.protoc.gen.core.model.OneOfDeclaration
1919
import kotlinx.rpc.protoc.gen.core.model.ServiceDeclaration
20+
import kotlin.Boolean
2021
import kotlin.collections.plus
2122

2223
private val nameCache = mutableMapOf<Descriptors.GenericDescriptor, FqName>()
@@ -127,6 +128,7 @@ private fun Descriptors.FileDescriptor.toModel(): FileDeclaration = cached {
127128
(comments + Paths.editionsCommentPath).get(),
128129
(comments + Paths.packageCommentPath).get()
129130
),
131+
deprecated = options.deprecated,
130132
dec = this,
131133
)
132134
}
@@ -156,6 +158,7 @@ private fun Descriptors.Descriptor.toModel(comments: Comments?): MessageDeclarat
156158
type = FieldType.OneOf(it),
157159
doc = it.doc,
158160
dec = it.variants.first().dec,
161+
deprecated = options.deprecated,
159162
)
160163
}
161164

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

@@ -180,6 +184,7 @@ private fun Descriptors.FieldDescriptor.toModel(comments: Comments, presenceIdx:
180184
presenceIdx = presenceIdx,
181185
doc = comments.get(),
182186
dec = this,
187+
deprecated = options.deprecated,
183188
)
184189
}
185190

@@ -219,6 +224,7 @@ private fun Descriptors.EnumDescriptor.toModel(comments: Comments?): EnumDeclara
219224
aliases = aliases,
220225
doc = comments.get(),
221226
dec = this,
227+
deprecated = options.deprecated,
222228
)
223229
}
224230

@@ -227,6 +233,7 @@ private fun Descriptors.EnumValueDescriptor.toModel(comments: Comments): EnumDec
227233
name = fqName(),
228234
doc = comments.get(),
229235
dec = this,
236+
deprecated = options.deprecated,
230237
)
231238
}
232239

@@ -237,6 +244,7 @@ private fun Descriptors.EnumValueDescriptor.toAliasModel(enumComments: Comments,
237244
original = original,
238245
doc = enumComments.get(),
239246
dec = this,
247+
deprecated = options.deprecated,
240248
)
241249
}
242250

@@ -246,6 +254,7 @@ private fun Descriptors.ServiceDescriptor.toModel(comments: Comments): ServiceDe
246254
methods = methods.map { it.toModel(comments + Paths.serviceMethodCommentPath + it.index) },
247255
dec = this,
248256
doc = comments.get(),
257+
deprecated = options.deprecated,
249258
)
250259
}
251260

@@ -256,6 +265,7 @@ private fun Descriptors.MethodDescriptor.toModel(comments: Comments): MethodDecl
256265
outputType = lazy { outputType.toModel(null) },
257266
dec = this,
258267
doc = comments.get(),
268+
deprecated = options.deprecated,
259269
)
260270
}
261271

protoc-gen/common/src/main/kotlin/kotlinx/rpc/protoc/gen/core/model/model.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ data class FileDeclaration(
2020
val serviceDeclarations: List<ServiceDeclaration>,
2121
val doc: List<Comment>,
2222
val dec: Descriptors.FileDescriptor,
23+
val deprecated: Boolean,
2324
)
2425

2526
data class MessageDeclaration(
@@ -31,6 +32,7 @@ data class MessageDeclaration(
3132
val nestedDeclarations: List<MessageDeclaration>,
3233
val doc: Comment?,
3334
val dec: Descriptors.Descriptor,
35+
val deprecated: Boolean,
3436
) {
3537
val isMapEntry = dec.options.mapEntry
3638
val isUserFacing = !isMapEntry
@@ -49,6 +51,7 @@ data class EnumDeclaration(
4951
val aliases: List<Alias>,
5052
val doc: Comment?,
5153
val dec: Descriptors.EnumDescriptor,
54+
val deprecated: Boolean,
5255
) {
5356

5457
fun defaultEntry(): Entry {
@@ -63,13 +66,15 @@ data class EnumDeclaration(
6366
val name: FqName,
6467
val doc: Comment?,
6568
val dec: Descriptors.EnumValueDescriptor,
69+
val deprecated: Boolean,
6670
)
6771

6872
data class Alias(
6973
val name: FqName,
7074
val original: Entry,
7175
val doc: Comment?,
7276
val dec: Descriptors.EnumValueDescriptor,
77+
val deprecated: Boolean,
7378
)
7479
}
7580

@@ -85,6 +90,7 @@ data class FieldDeclaration(
8590
val type: FieldType,
8691
val doc: Comment?,
8792
val dec: Descriptors.FieldDescriptor,
93+
val deprecated: Boolean,
8894
// defines the index in the presenceMask of the Message.
8995
// this cannot be the number, as only fields with hasPresence == true are part of the presenceMask
9096
val presenceIdx: Int? = null,
@@ -111,6 +117,7 @@ data class ServiceDeclaration(
111117
val methods: List<MethodDeclaration>,
112118
val dec: Descriptors.ServiceDescriptor,
113119
val doc: Comment?,
120+
val deprecated: Boolean,
114121
)
115122

116123
data class MethodDeclaration(
@@ -119,5 +126,6 @@ data class MethodDeclaration(
119126
val outputType: Lazy<MessageDeclaration>,
120127
val dec: Descriptors.MethodDescriptor,
121128
val doc: Comment?,
129+
val deprecated: Boolean,
122130
)
123131

protoc-gen/grpc/src/main/kotlin/kotlinx/rpc/protoc/gen/grpc/ModelToGrpcKotlinCommonGenerator.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class ModelToGrpcKotlinCommonGenerator(
3636
name = service.name.simpleName,
3737
comment = service.doc,
3838
declarationType = CodeGenerator.DeclarationType.Interface,
39-
annotations = listOf("@kotlinx.rpc.grpc.annotations.Grpc$annotationParams")
39+
annotations = listOf("@kotlinx.rpc.grpc.annotations.Grpc$annotationParams"),
40+
deprecation = if (service.deprecated) DeprecationLevel.WARNING else null,
4041
) {
4142
service.methods.forEach { method ->
4243
val inputType = method.inputType
@@ -53,6 +54,7 @@ class ModelToGrpcKotlinCommonGenerator(
5354
modifiers = if (method.dec.isServerStreaming) "" else "suspend",
5455
args = "message: ${inputType.value.name.safeFullName().wrapInFlowIf(method.dec.isClientStreaming)}",
5556
annotations = annotations,
57+
deprecation = if (method.deprecated) DeprecationLevel.WARNING else null,
5658
returnType = outputType.value.name.safeFullName().wrapInFlowIf(method.dec.isServerStreaming),
5759
)
5860
}

protoc-gen/protobuf/src/main/kotlin/kotlinx/rpc/protoc/gen/ModelToProtobufKotlinCommonGenerator.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,16 @@ class ModelToProtobufKotlinCommonGenerator(
8080
name = declaration.name.simpleName,
8181
comment = declaration.doc,
8282
declarationType = CodeGenerator.DeclarationType.Interface,
83-
annotations = annotations
83+
annotations = annotations,
84+
deprecation = if (declaration.deprecated) DeprecationLevel.WARNING else null,
8485
) {
8586
declaration.actualFields.forEachIndexed { i, field ->
8687
property(
8788
name = field.name,
8889
comment = field.doc,
8990
type = field.typeFqName(),
9091
needsNewLineAfterDeclaration = i == declaration.actualFields.lastIndex,
92+
deprecation = if (field.deprecated) DeprecationLevel.WARNING else null,
9193
)
9294
}
9395

@@ -300,7 +302,7 @@ class ModelToProtobufKotlinCommonGenerator(
300302
}
301303

302304
private fun CodeGenerator.generateMessageDecoder(declaration: MessageDeclaration) {
303-
var args = "msg: ${declaration.internalClassFullName()}, decoder: $PB_PKG.WireDecoder";
305+
var args = "msg: ${declaration.internalClassFullName()}, decoder: $PB_PKG.WireDecoder"
304306
if (declaration.isGroup) {
305307
args += ", startGroup: $PB_PKG.KTag"
306308
}
@@ -406,8 +408,6 @@ class ModelToProtobufKotlinCommonGenerator(
406408
}
407409

408410
is FieldType.Message -> {
409-
val msg = fieldType.dec.value
410-
411411
whenCase("tag.fieldNr == ${field.number} && tag.wireType == $PB_PKG.WireType.${fieldType.wireType.name}") {
412412
if (field.presenceIdx != null) {
413413
// check if the current sub message object was already set, if not, set a new one
@@ -995,6 +995,7 @@ class ModelToProtobufKotlinCommonGenerator(
995995
constructorArgs = listOf("val value: ${variant.typeFqName()}"),
996996
annotations = listOf("@JvmInline"),
997997
superTypes = listOf(interfaceName),
998+
deprecation = if (variant.deprecated) DeprecationLevel.WARNING else null,
998999
)
9991000

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

10051006
private fun CodeGenerator.generatePublicEnum(declaration: EnumDeclaration) {
1006-
10071007
val className = declaration.name.simpleName
1008-
10091008
val entriesSorted = declaration.originalEntries.sortedBy { it.dec.number }
10101009

10111010
clazz(
10121011
name = className,
10131012
comment = declaration.doc,
10141013
modifiers = "sealed",
10151014
constructorArgs = listOf("open val number: Int"),
1015+
deprecation = if (declaration.deprecated) DeprecationLevel.WARNING else null,
10161016
) {
1017-
10181017
declaration.originalEntries.forEach { variant ->
10191018
clazz(
10201019
name = variant.name.simpleName,
10211020
comment = variant.doc,
10221021
declarationType = CodeGenerator.DeclarationType.Object,
10231022
superTypes = listOf("$className(number = ${variant.dec.number})"),
1023+
deprecation = if (variant.deprecated) DeprecationLevel.WARNING else null,
10241024
)
10251025
}
10261026

@@ -1042,6 +1042,7 @@ class ModelToProtobufKotlinCommonGenerator(
10421042
type = className,
10431043
propertyInitializer = CodeGenerator.PropertyInitializer.GETTER,
10441044
value = alias.original.name.simpleName,
1045+
deprecation = if (alias.deprecated) DeprecationLevel.WARNING else null,
10451046
)
10461047
}
10471048

0 commit comments

Comments
 (0)