Skip to content

Commit

Permalink
Add "GeneratedCode" attribute to generated service stubs (grpc#26164)
Browse files Browse the repository at this point in the history
* mark generated members in *Grpc.cs as [GeneratedCode]

* regenerate protos
  • Loading branch information
jtattermusch authored May 12, 2021
1 parent e002ab4 commit c6fbf43
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/compiler/csharp_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ bool GenerateDocCommentBodyImpl(grpc::protobuf::io::Printer* printer,
return true;
}

void GenerateGeneratedCodeAttribute(grpc::protobuf::io::Printer* printer) {
// Mark the code as generated using the [GeneratedCode] attribute.
// We don't provide plugin version info in attribute the because:
// * the version information is not readily available from the plugin's code.
// * it would cause a lot of churn in the pre-generated code
// in this repository every time the version is updated.
printer->Print(
"[global::System.CodeDom.Compiler.GeneratedCode(\"grpc_csharp_plugin\", "
"null)]\n");
}

template <typename DescriptorType>
bool GenerateDocCommentBody(grpc::protobuf::io::Printer* printer,
const DescriptorType* descriptor) {
Expand Down Expand Up @@ -304,6 +315,7 @@ void GenerateMarshallerFields(Printer* out, const ServiceDescriptor* service) {
std::vector<const Descriptor*> used_messages = GetUsedMessages(service);
if (used_messages.size() != 0) {
// Generate static helper methods for serialization/deserialization
GenerateGeneratedCodeAttribute(out);
out->Print(
"static void __Helper_SerializeMessage("
"global::Google.Protobuf.IMessage message, "
Expand Down Expand Up @@ -331,6 +343,7 @@ void GenerateMarshallerFields(Printer* out, const ServiceDescriptor* service) {
out->Outdent();
out->Print("}\n\n");

GenerateGeneratedCodeAttribute(out);
out->Print(
"static class __Helper_MessageCache<T>\n"
"{\n");
Expand All @@ -343,6 +356,7 @@ void GenerateMarshallerFields(Printer* out, const ServiceDescriptor* service) {
out->Outdent();
out->Print("}\n\n");

GenerateGeneratedCodeAttribute(out);
out->Print(
"static T __Helper_DeserializeMessage<T>("
"grpc::DeserializationContext context, "
Expand All @@ -368,6 +382,7 @@ void GenerateMarshallerFields(Printer* out, const ServiceDescriptor* service) {

for (size_t i = 0; i < used_messages.size(); i++) {
const Descriptor* message = used_messages[i];
GenerateGeneratedCodeAttribute(out);
out->Print(
"static readonly grpc::Marshaller<$type$> $fieldname$ = "
"grpc::Marshallers.Create(__Helper_SerializeMessage, "
Expand All @@ -379,6 +394,7 @@ void GenerateMarshallerFields(Printer* out, const ServiceDescriptor* service) {
}

void GenerateStaticMethodField(Printer* out, const MethodDescriptor* method) {
GenerateGeneratedCodeAttribute(out);
out->Print(
"static readonly grpc::Method<$request$, $response$> $fieldname$ = new "
"grpc::Method<$request$, $response$>(\n",
Expand Down Expand Up @@ -433,6 +449,7 @@ void GenerateServerClass(Printer* out, const ServiceDescriptor* service) {
for (int i = 0; i < service->method_count(); i++) {
const MethodDescriptor* method = service->method(i);
GenerateDocCommentServerMethod(out, method);
GenerateGeneratedCodeAttribute(out);
out->Print(
"public virtual $returntype$ "
"$methodname$($request$$response_stream_maybe$, "
Expand Down Expand Up @@ -468,6 +485,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {
"/// <param name=\"channel\">The channel to use to make remote "
"calls.</param>\n",
"servicename", GetServiceClassName(service));
GenerateGeneratedCodeAttribute(out);
out->Print("public $name$(grpc::ChannelBase channel) : base(channel)\n",
"name", GetClientClassName(service));
out->Print("{\n");
Expand All @@ -478,6 +496,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {
"/// <param name=\"callInvoker\">The callInvoker to use to make remote "
"calls.</param>\n",
"servicename", GetServiceClassName(service));
GenerateGeneratedCodeAttribute(out);
out->Print(
"public $name$(grpc::CallInvoker callInvoker) : base(callInvoker)\n",
"name", GetClientClassName(service));
Expand All @@ -486,6 +505,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {
out->Print(
"/// <summary>Protected parameterless constructor to allow creation"
" of test doubles.</summary>\n");
GenerateGeneratedCodeAttribute(out);
out->Print("protected $name$() : base()\n", "name",
GetClientClassName(service));
out->Print("{\n");
Expand All @@ -494,6 +514,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {
"/// <summary>Protected constructor to allow creation of configured "
"clients.</summary>\n"
"/// <param name=\"configuration\">The client configuration.</param>\n");
GenerateGeneratedCodeAttribute(out);
out->Print(
"protected $name$(ClientBaseConfiguration configuration)"
" : base(configuration)\n",
Expand All @@ -506,6 +527,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {
if (!method->client_streaming() && !method->server_streaming()) {
// unary calls have an extra synchronous stub method
GenerateDocCommentClientMethod(out, method, true, false);
GenerateGeneratedCodeAttribute(out);
out->Print(
"public virtual $response$ $methodname$($request$ request, "
"grpc::Metadata "
Expand All @@ -528,6 +550,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {

// overload taking CallOptions as a param
GenerateDocCommentClientMethod(out, method, true, true);
GenerateGeneratedCodeAttribute(out);
out->Print(
"public virtual $response$ $methodname$($request$ request, "
"grpc::CallOptions options)\n",
Expand All @@ -549,6 +572,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {
method_name += "Async"; // prevent name clash with synchronous method.
}
GenerateDocCommentClientMethod(out, method, false, false);
GenerateGeneratedCodeAttribute(out);
out->Print(
"public virtual $returntype$ "
"$methodname$($request_maybe$grpc::Metadata "
Expand All @@ -573,6 +597,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {

// overload taking CallOptions as a param
GenerateDocCommentClientMethod(out, method, false, true);
GenerateGeneratedCodeAttribute(out);
out->Print(
"public virtual $returntype$ "
"$methodname$($request_maybe$grpc::CallOptions "
Expand Down Expand Up @@ -615,6 +640,7 @@ void GenerateClientStub(Printer* out, const ServiceDescriptor* service) {
out->Print(
"/// <summary>Creates a new instance of client from given "
"<c>ClientBaseConfiguration</c>.</summary>\n");
GenerateGeneratedCodeAttribute(out);
out->Print(
"protected override $name$ NewInstance(ClientBaseConfiguration "
"configuration)\n",
Expand All @@ -638,6 +664,7 @@ void GenerateBindServiceMethod(Printer* out, const ServiceDescriptor* service) {
out->Print(
"/// <param name=\"serviceImpl\">An object implementing the server-side"
" handling logic.</param>\n");
GenerateGeneratedCodeAttribute(out);
out->Print(
"public static grpc::ServerServiceDefinition BindService($implclass$ "
"serviceImpl)\n",
Expand Down Expand Up @@ -679,6 +706,7 @@ void GenerateBindServiceWithBinderMethod(Printer* out,
out->Print(
"/// <param name=\"serviceImpl\">An object implementing the server-side"
" handling logic.</param>\n");
GenerateGeneratedCodeAttribute(out);
out->Print(
"public static void BindService(grpc::ServiceBinderBase serviceBinder, "
"$implclass$ "
Expand Down Expand Up @@ -709,6 +737,7 @@ void GenerateService(Printer* out, const ServiceDescriptor* service,
bool generate_client, bool generate_server,
bool internal_access) {
GenerateDocCommentBody(out, service);

out->Print("$access_level$ static partial class $classname$\n",
"access_level", GetAccessLevel(internal_access), "classname",
GetServiceClassName(service));
Expand Down
Loading

0 comments on commit c6fbf43

Please sign in to comment.