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

Generate @Deprecated annotations #334

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022-2023 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package buf.deprecation.v1;

option deprecated = true;

message DeprecatedByFileRequest {}

message DeprecatedByFileResponse {}

service FileDeprecatedService {
rpc DeprecatedByFile(DeprecatedByFileRequest) returns (DeprecatedByFileResponse) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2022-2023 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package buf.deprecation.v1;

message DeprecatedMethodRequest {}

message DeprecatedMethodResponse {}

service MethodDeprecatedService {
rpc DeprecatedMethod(DeprecatedMethodRequest) returns (DeprecatedMethodResponse) {
option deprecated = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022-2023 The Connect Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package buf.deprecation.v1;

message DeprecatedByServiceRequest {}

message DeprecatedByServiceResponse {}

service ServiceDeprecatedService {
option deprecated = true;
rpc DeprecatedByService(DeprecatedByServiceRequest) returns (DeprecatedByServiceResponse) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.google.protobuf.DescriptorProtos.FileDescriptorProto
import com.google.protobuf.DescriptorProtos.MethodOptions.IdempotencyLevel
import com.google.protobuf.Descriptors
import com.google.protobuf.compiler.PluginProtos
import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
Expand Down Expand Up @@ -122,7 +123,7 @@ class Generator : CodeGenerator {
.addFileComment("Code generated by connect-kotlin. DO NOT EDIT.\n")
.addFileComment("\n")
.addFileComment("Source: ${file.name}\n")
.addType(serviceClientInterface(packageName, service, sourceInfo))
.addType(serviceClientInterface(packageName, service, file, sourceInfo))
.build()
fileSpecs[serviceClientInterfaceClassName(packageName, service)] = interfaceFileSpec

Expand All @@ -133,7 +134,7 @@ class Generator : CodeGenerator {
.addFileComment("\n")
.addFileComment("Source: ${file.name}\n")
// Set the file package for the generated methods.
.addType(serviceClientImplementation(packageName, service, sourceInfo))
.addType(serviceClientImplementation(packageName, service, file, sourceInfo))
for (method in service.methods) {
if (method.options.hasIdempotencyLevel()) {
implementationFileSpecBuilder.addImport(Idempotency::class.java.`package`.name, "Idempotency")
Expand All @@ -149,11 +150,13 @@ class Generator : CodeGenerator {
private fun serviceClientInterface(
packageName: String,
service: Descriptors.ServiceDescriptor,
file: Descriptors.FileDescriptor,
sourceInfo: SourceInfo,
): TypeSpec {
val interfaceBuilder = TypeSpec.interfaceBuilder(serviceClientInterfaceClassName(packageName, service))
val functionSpecs = interfaceMethods(service.methods, sourceInfo)
return interfaceBuilder
.addServiceDeprecation(service, file)
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addFunctions(functionSpecs)
.build()
Expand All @@ -176,6 +179,7 @@ class Generator : CodeGenerator {
if (method.isClientStreaming && method.isServerStreaming) {
val streamingBuilder = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.ABSTRACT)
.addModifiers(KModifier.SUSPEND)
.addParameter(headerParameterSpec)
Expand All @@ -187,6 +191,7 @@ class Generator : CodeGenerator {
} else if (method.isServerStreaming) {
val serverStreamingFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.ABSTRACT)
.addModifiers(KModifier.SUSPEND)
.addParameter(headerParameterSpec)
Expand All @@ -198,6 +203,7 @@ class Generator : CodeGenerator {
} else if (method.isClientStreaming) {
val clientStreamingFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.ABSTRACT)
.addModifiers(KModifier.SUSPEND)
.addParameter(headerParameterSpec)
Expand All @@ -210,6 +216,7 @@ class Generator : CodeGenerator {
if (configuration.generateCoroutineMethods) {
val unarySuspendFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.ABSTRACT)
.addModifiers(KModifier.SUSPEND)
.addParameter("request", inputClassName)
Expand All @@ -230,6 +237,7 @@ class Generator : CodeGenerator {
)
val unaryCallbackFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.ABSTRACT)
.addParameter("request", inputClassName)
.addParameter(headerParameterSpec)
Expand All @@ -256,6 +264,7 @@ class Generator : CodeGenerator {
private fun serviceClientImplementation(
javaPackageName: String,
service: Descriptors.ServiceDescriptor,
file: Descriptors.FileDescriptor,
sourceInfo: SourceInfo,
): TypeSpec {
// The javaPackageName is used instead of the package name for imports and code references.
Expand All @@ -274,6 +283,7 @@ class Generator : CodeGenerator {
val functionSpecs = implementationMethods(service.methods, sourceInfo)
return classBuilder
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addServiceDeprecation(service, file)
.addFunctions(functionSpecs)
.build()
}
Expand Down Expand Up @@ -318,6 +328,7 @@ class Generator : CodeGenerator {
if (method.isClientStreaming && method.isServerStreaming) {
val streamingFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.OVERRIDE)
.addModifiers(KModifier.SUSPEND)
.addParameter("headers", HEADERS_CLASS_NAME)
Expand All @@ -344,6 +355,7 @@ class Generator : CodeGenerator {
} else if (method.isServerStreaming) {
val serverStreamingFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.OVERRIDE)
.addModifiers(KModifier.SUSPEND)
.addParameter("headers", HEADERS_CLASS_NAME)
Expand All @@ -366,6 +378,7 @@ class Generator : CodeGenerator {
} else if (method.isClientStreaming) {
val clientStreamingFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.OVERRIDE)
.addModifiers(KModifier.SUSPEND)
.addParameter("headers", HEADERS_CLASS_NAME)
Expand All @@ -389,6 +402,7 @@ class Generator : CodeGenerator {
if (configuration.generateCoroutineMethods) {
val unarySuspendFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.SUSPEND)
.addModifiers(KModifier.OVERRIDE)
.addParameter("request", inputClassName)
Expand Down Expand Up @@ -421,6 +435,7 @@ class Generator : CodeGenerator {
)
val unaryCallbackFunction = FunSpec.builder(method.name.lowerCamelCase())
.addKdoc(sourceInfo.comment().sanitizeKdoc())
.addMethodDeprecation(method)
.addModifiers(KModifier.OVERRIDE)
.addParameter("request", inputClassName)
.addParameter("headers", HEADERS_CLASS_NAME)
Expand Down Expand Up @@ -524,3 +539,36 @@ private fun String.packageToDirectory(): String {
}
return dir
}

private fun TypeSpec.Builder.addServiceDeprecation(
service: Descriptors.ServiceDescriptor,
file: Descriptors.FileDescriptor
): TypeSpec.Builder {
if (service.options.deprecated) {
this.addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.addMember("%S", "The underlying service is marked deprecated.")
timostamm marked this conversation as resolved.
Show resolved Hide resolved
.build()
)
} else if (file.options.deprecated) {
this.addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.addMember("%S", "The underlying file is marked deprecated.")
timostamm marked this conversation as resolved.
Show resolved Hide resolved
.build()
)
}
return this
}

private fun FunSpec.Builder.addMethodDeprecation(
method: Descriptors.MethodDescriptor,
): FunSpec.Builder {
if (method.options.deprecated) {
this.addAnnotation(
AnnotationSpec.builder(Deprecated::class)
.addMember("%S", "The underlying service method is marked deprecated.")
timostamm marked this conversation as resolved.
Show resolved Hide resolved
.build()
)
}
return this
}
Loading