diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcCoroutinesGenerator.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcCoroutinesGenerator.kt index d18f591..1dfc4ed 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcCoroutinesGenerator.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcCoroutinesGenerator.kt @@ -92,6 +92,7 @@ object GrpcCoroutinesGenerator : Generator { private fun ProtoService.buildOuterObject(): TypeSpec = TypeSpec.objectBuilder(outerObjectName) + .addKdoc(attachedComments) .addAnnotation(protoFile.getGeneratedAnnotationSpec()) .addFunction( FunSpec.builder("newStub") diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcStubExtsGenerator.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcStubExtsGenerator.kt index b3ab21a..e21b02e 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcStubExtsGenerator.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/GrpcStubExtsGenerator.kt @@ -105,6 +105,7 @@ object GrpcStubExtsGenerator : Generator { private fun ProtoMethod.buildStubClientStreamingMethod(): FunSpec = FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .returns( CommonClassNames.ClientChannels.clientStreamingCallChannel.parameterizedBy( @@ -122,6 +123,7 @@ object GrpcStubExtsGenerator : Generator { private fun ProtoMethod.buildStubCoroutineBidiStreamingMethod(): FunSpec = FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .returns( CommonClassNames.ClientChannels.clientBidiCallChannel.parameterizedBy( diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcServiceBaseImplBuilder.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcServiceBaseImplBuilder.kt index 01b1b69..d20ec40 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcServiceBaseImplBuilder.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcServiceBaseImplBuilder.kt @@ -41,6 +41,7 @@ class GrpcServiceBaseImplBuilder(val context: GeneratorContext){ val delegateValName = "delegate" TypeSpec.classBuilder(baseImplName) + .addKdoc(attachedComments) .addModifiers(KModifier.ABSTRACT) .addSuperinterface(CommonClassNames.bindableService) .addSuperinterface(CommonClassNames.serviceScope) @@ -115,6 +116,7 @@ class GrpcServiceBaseImplBuilder(val context: GeneratorContext){ private fun buildUnaryBaseImpl(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND, KModifier.OPEN) .addParameter("request", requestClassName) .returns(responseClassName) @@ -132,6 +134,7 @@ class GrpcServiceBaseImplBuilder(val context: GeneratorContext){ private fun buildServerStreamingBaseImpl(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND, KModifier.OPEN) .addParameter("request", requestClassName) .addParameter( @@ -157,6 +160,7 @@ class GrpcServiceBaseImplBuilder(val context: GeneratorContext){ private fun buildClientStreamingBaseImpl(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND, KModifier.OPEN) .addParameter( name = "requestChannel", @@ -177,6 +181,7 @@ class GrpcServiceBaseImplBuilder(val context: GeneratorContext){ private fun buildBidiStreamingBaseImpl(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND, KModifier.OPEN) .addParameter( name = "requestChannel", diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcStubBuilder.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcStubBuilder.kt index 53664d7..629cfe3 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcStubBuilder.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/GrpcStubBuilder.kt @@ -19,14 +19,11 @@ package com.github.marcoferrer.krotoplus.generators.builders import com.github.marcoferrer.krotoplus.generators.GeneratorContext import com.github.marcoferrer.krotoplus.proto.ProtoMethod import com.github.marcoferrer.krotoplus.proto.ProtoService -import com.github.marcoferrer.krotoplus.proto.getFieldClassName import com.github.marcoferrer.krotoplus.utils.CommonClassNames -import com.github.marcoferrer.krotoplus.utils.addForEach import com.github.marcoferrer.krotoplus.utils.builderLambdaTypeName import com.github.marcoferrer.krotoplus.utils.requestParamSpec import com.github.marcoferrer.krotoplus.utils.requestValueBuilderCodeBlock import com.github.marcoferrer.krotoplus.utils.requestValueMethodSigCodeBlock -import com.github.marcoferrer.krotoplus.utils.toUpperCamelCase import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.KModifier import com.squareup.kotlinpoet.ParameterSpec @@ -39,13 +36,13 @@ import io.grpc.MethodDescriptor class GrpcStubBuilder(val context: GeneratorContext){ - fun buildStub(protoService: ProtoService): TypeSpec = with(protoService) { val paramNameChannel = "channel" val paramNameCallOptions = "callOptions" TypeSpec.classBuilder(stubName) + .addKdoc(attachedComments) .superclass(CommonClassNames.grpcAbstractStub.parameterizedBy(stubClassName)) .addSuperclassConstructorParameter(paramNameChannel) .addSuperclassConstructorParameter(paramNameCallOptions) @@ -77,7 +74,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ .build() } - fun TypeSpec.Builder.addRpcMethods(service: ProtoService): TypeSpec.Builder = apply { + private fun TypeSpec.Builder.addRpcMethods(service: ProtoService): TypeSpec.Builder = apply { for(method in service.methodDefinitions) when(method.type){ MethodDescriptor.MethodType.UNARY -> { @@ -102,11 +99,10 @@ class GrpcStubBuilder(val context: GeneratorContext){ } } - // Default method - private fun buildUnaryMethod(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) .addModifiers(KModifier.SUSPEND) + .addKdoc(attachedComments) .returns(responseClassName) .addParameter(requestClassName.requestParamSpec) .addStatement( @@ -120,6 +116,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ private fun buildServerStreamingMethod(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .returns(CommonClassNames.receiveChannel.parameterizedBy(responseClassName)) .addParameter(requestClassName.requestParamSpec) .addStatement( @@ -133,6 +130,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ private fun buildClientStreamingMethod(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .returns( CommonClassNames.ClientChannels.clientStreamingCallChannel.parameterizedBy( requestClassName, @@ -150,6 +148,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ private fun buildBidiStreamingMethod(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .returns( CommonClassNames.ClientChannels.clientBidiCallChannel.parameterizedBy( requestClassName, @@ -169,6 +168,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ private fun buildUnaryLambdaOverload(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND, KModifier.INLINE) .returns(responseClassName) .addParameter("block", requestClassName.builderLambdaTypeName) @@ -179,6 +179,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ private fun buildServerStreamingLambdaOverload(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.INLINE) .returns(CommonClassNames.receiveChannel.parameterizedBy(responseClassName)) .addParameter("block", requestClassName.builderLambdaTypeName) @@ -192,6 +193,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ private fun buildUnaryMethodSigOverload(protoMethod: ProtoMethod): FunSpec? = with(protoMethod){ if(methodSignatureFields.isEmpty()) null else FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND) .returns(responseClassName) .addMethodSignatureParameter(methodSignatureFields,context.schema) @@ -203,6 +205,7 @@ class GrpcStubBuilder(val context: GeneratorContext){ private fun buildServerStreamingMethodSigOverload(protoMethod: ProtoMethod): FunSpec? = with(protoMethod){ if(methodSignatureFields.isEmpty()) null else FunSpec.builder(functionName) + .addKdoc(attachedComments) .returns(CommonClassNames.receiveChannel.parameterizedBy(responseClassName)) .addMethodSignatureParameter(methodSignatureFields,context.schema) .addCode(requestClassName.requestValueMethodSigCodeBlock(methodSignatureFields)) diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/ServerStreamingStubExtsBuilder.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/ServerStreamingStubExtsBuilder.kt index 7236312..3e5f0ab 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/ServerStreamingStubExtsBuilder.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/ServerStreamingStubExtsBuilder.kt @@ -69,6 +69,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildCoroutineExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .returns(CommonClassNames.receiveChannel.parameterizedBy(responseClassName)) .receiver(protoService.asyncStubClassName) .addParameter(requestClassName.requestParamSpec) @@ -85,6 +86,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildAsyncMethodSigExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .returns(UNIT) .addMethodSignatureParameter(methodSignatureFields, context.schema) @@ -96,6 +98,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildCoroutineMethodSigExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .addMethodSignatureParameter(methodSignatureFields, context.schema) .returns(CommonClassNames.receiveChannel.parameterizedBy(responseClassName)) @@ -106,6 +109,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildBlockingMethodSigExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.blockingStubClassName) .addMethodSignatureParameter(methodSignatureFields, context.schema) .returns(Iterator::class.asClassName().parameterizedBy(responseClassName)) @@ -118,6 +122,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildAsyncLambdaBuilderExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.INLINE) .receiver(protoService.asyncStubClassName) .addResponseObserverParameter(responseClassName) @@ -130,6 +135,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildCoroutineLambdaBuilderExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .returns(CommonClassNames.receiveChannel.parameterizedBy(responseClassName)) .addModifiers(KModifier.INLINE) @@ -141,6 +147,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildBlockingLambdaBuilderExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.INLINE) .receiver(protoService.blockingStubClassName) .addParameter("block", requestClassName.builderLambdaTypeName) @@ -154,6 +161,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildAsyncDefaultArgExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .addResponseObserverParameter(responseClassName) .returns(UNIT) @@ -163,6 +171,7 @@ class ServerStreamingStubExtsBuilder(val context: GeneratorContext){ private fun buildBlockingDefaultArgExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.blockingStubClassName) .returns(Iterator::class.asClassName().parameterizedBy(responseClassName)) .addStatement("return %N(%T.getDefaultInstance())", functionName, requestClassName) diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/UnaryStubExtsBuilder.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/UnaryStubExtsBuilder.kt index c73ea82..c60109b 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/UnaryStubExtsBuilder.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/generators/builders/UnaryStubExtsBuilder.kt @@ -71,6 +71,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildCoroutineExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND) .receiver(protoService.asyncStubClassName) .addParameter(requestClassName.requestParamSpec) @@ -88,6 +89,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildAsyncMethodSigExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .addMethodSignatureParameter(methodSignatureFields, context.schema) .addResponseObserverParameter(responseClassName) @@ -99,6 +101,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildCoroutineMethodSigExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.SUSPEND) .receiver(protoService.asyncStubClassName) .returns(responseClassName) @@ -110,6 +113,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildFutureMethodSigExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.futureStubClassName) .addMethodSignatureParameter(methodSignatureFields, context.schema) .returns(CommonClassNames.listenableFuture.parameterizedBy(responseClassName)) @@ -120,6 +124,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildBlockingMethodSigExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.blockingStubClassName) .returns(responseClassName) .addMethodSignatureParameter(methodSignatureFields, context.schema) @@ -132,6 +137,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildAsyncLambdaBuilderExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .addModifiers(KModifier.INLINE) .addResponseObserverParameter(responseClassName) @@ -144,6 +150,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildCoroutineLambdaBuilderExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .addModifiers(KModifier.INLINE, KModifier.SUSPEND) .addParameter("block", requestClassName.builderLambdaTypeName) @@ -155,6 +162,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildFutureLambdaBuilderExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.INLINE) .receiver(protoService.futureStubClassName) .addParameter("block", requestClassName.builderLambdaTypeName) @@ -166,6 +174,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildBlockingLambdaBuilderExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addModifiers(KModifier.INLINE) .addCode(requestClassName.requestValueBuilderCodeBlock) .addParameter("block", requestClassName.builderLambdaTypeName) @@ -179,6 +188,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildAsyncDefaultArgExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .receiver(protoService.asyncStubClassName) .addResponseObserverParameter(responseClassName) .addStatement("%N(%T.getDefaultInstance(),responseObserver)", functionName, requestClassName) @@ -188,6 +198,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildFutureDefaultArgExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addStatement("return %N(%T.getDefaultInstance())", functionName, requestClassName) .receiver(protoService.futureStubClassName) .returns(CommonClassNames.listenableFuture.parameterizedBy(responseClassName)) @@ -196,6 +207,7 @@ class UnaryStubExtsBuilder(val context: GeneratorContext){ private fun buildBlockingDefaultArgExt(protoMethod: ProtoMethod): FunSpec = with(protoMethod){ FunSpec.builder(functionName) + .addKdoc(attachedComments) .addStatement("return %N(%T.getDefaultInstance())", functionName, requestClassName) .receiver(protoService.blockingStubClassName) .returns(responseClassName) diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoFile.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoFile.kt index f97068a..ce5685e 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoFile.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoFile.kt @@ -43,22 +43,25 @@ data class ProtoFile( ProtoEnum(enumType, this@ProtoFile) } - val services: List = descriptorProto.serviceList.map { serviceDescriptor -> - ProtoService(serviceDescriptor, this@ProtoFile) + val services: List = descriptorProto.serviceList.mapIndexed { serviceIndex, serviceDescriptor -> + val sourceLocation = descriptorProto.sourceCodeInfo.locationList.findByServiceIndex(serviceIndex) + ProtoService(serviceDescriptor, sourceLocation, this@ProtoFile) } } -val DescriptorProtos.FileDescriptorProto.javaPackage: String +data class ProtoComments(val leading: String, val trailing: String) + +private val DescriptorProtos.FileDescriptorProto.javaPackage: String get() = when { this.options.hasJavaPackage() -> this.options.javaPackage this.hasPackage() -> this.`package` else -> "" } -val DescriptorProtos.FileDescriptorProto.protoPackage: String +private val DescriptorProtos.FileDescriptorProto.protoPackage: String get() = if (this.hasPackage()) this.`package` else "" -val DescriptorProtos.FileDescriptorProto.javaOuterClassname: String +private val DescriptorProtos.FileDescriptorProto.javaOuterClassname: String get() = if (this.options.hasJavaOuterClassname()) this.options.javaOuterClassname else this.name .substringAfterLast("/") @@ -80,4 +83,11 @@ fun DescriptorProtos.FileDescriptorProto.getGeneratedAnnotationSpec() = .addMember("comments = %S", "Source: $name") .build() -fun ProtoFile.getGeneratedAnnotationSpec() = descriptorProto.getGeneratedAnnotationSpec() \ No newline at end of file +fun ProtoFile.getGeneratedAnnotationSpec() = descriptorProto.getGeneratedAnnotationSpec() + +fun DescriptorProtos.SourceCodeInfo.Location.buildAttachedComments(): String { + val attachedComments = if (leadingComments.isNotEmpty()) + "$leadingComments\n$trailingComments" else trailingComments + + return attachedComments.replace("*/", "*/").replace("*", "*").trim() +} \ No newline at end of file diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoMethod.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoMethod.kt index 9234233..9396714 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoMethod.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoMethod.kt @@ -19,10 +19,13 @@ package com.github.marcoferrer.krotoplus.proto import com.github.marcoferrer.krotoplus.utils.toUpperCamelCase import com.google.api.ClientProto import com.google.protobuf.DescriptorProtos +import com.google.protobuf.DescriptorProtos.SourceCodeInfo.Location +import com.squareup.kotlinpoet.CodeBlock import io.grpc.MethodDescriptor class ProtoMethod( override val descriptorProto: DescriptorProtos.MethodDescriptorProto, + val sourceLocation: Location, val protoService: ProtoService ) : Schema.DescriptorWrapper { @@ -34,6 +37,8 @@ class ProtoMethod( "_$it" else it.decapitalize() } + val attachedComments: String = sourceLocation.buildAttachedComments() + val methodDefinitionGetterName = "get${descriptorProto.name.toUpperCamelCase()}Method" val requestType: ProtoMessage = requireNotNull(protoService.protoFile.schema.protoTypes[descriptorProto.inputType] as? ProtoMessage){ @@ -82,4 +87,15 @@ private fun ProtoMethod.getMethodSignatureFields(): List.findByMethodIndex(methodIndex: Int): Location = find { + it.pathCount == METHOD_NUMBER_OF_PATHS && it.getPath(METHOD_NUMBER_OF_PATHS - 1) == methodIndex +} ?: Location.getDefaultInstance() diff --git a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoService.kt b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoService.kt index de8d1a0..60d8fcd 100644 --- a/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoService.kt +++ b/protoc-gen-kroto-plus/src/main/kotlin/com/github/marcoferrer/krotoplus/proto/ProtoService.kt @@ -17,16 +17,21 @@ package com.github.marcoferrer.krotoplus.proto import com.google.protobuf.DescriptorProtos +import com.google.protobuf.DescriptorProtos.SourceCodeInfo.Location import com.squareup.kotlinpoet.ClassName +import com.squareup.kotlinpoet.CodeBlock data class ProtoService( override val descriptorProto: DescriptorProtos.ServiceDescriptorProto, + val sourceLocation: Location, val protoFile: ProtoFile ) : Schema.DescriptorWrapper { val name: String get() = descriptorProto.name + val attachedComments: String = sourceLocation.buildAttachedComments() + val enclosingServiceClassName = ClassName(protoFile.javaPackage, "${name}Grpc") val asyncStubClassName = ClassName(enclosingServiceClassName.canonicalName, "${name}Stub") @@ -35,6 +40,31 @@ data class ProtoService( val blockingStubClassName = ClassName(enclosingServiceClassName.canonicalName, "${name}BlockingStub") - val methodDefinitions by lazy { descriptorProto.methodList.map { ProtoMethod(it, this@ProtoService) } } + val methodDefinitions by lazy { + descriptorProto.methodList.mapIndexed { methodIndex, methodDescriptor -> + val sourceLocation = this@ProtoService.protoFile.descriptorProto.sourceCodeInfo.locationList + .findByMethodIndex(methodIndex) + ProtoMethod(methodDescriptor, sourceLocation, this@ProtoService) + } + } + +} + +/** + * Comment parsing is based on the following implementation + * https://github.com/salesforce/reactive-grpc/blob/ab8e86e1d951f3a7ab9802a31ab73cd5ba3b8e3b/common/reactive-grpc-gencommon/src/main/java/com/salesforce/reactivegrpc/gen/ReactiveGrpcGenerator.java#L89 + */ +private const val SERVICE_NUMBER_OF_PATHS = 2 + +fun List.findByServiceIndex(serviceIndex: Int): Location { + + val allServiceLocations = filter { + it.pathCount >= 2 && + it.getPath(0) == DescriptorProtos.FileDescriptorProto.SERVICE_FIELD_NUMBER && + it.getPath(1) == serviceIndex + } + + return allServiceLocations + .find { it.pathCount == SERVICE_NUMBER_OF_PATHS } ?: Location.getDefaultInstance() +} -} \ No newline at end of file diff --git a/test-api/src/main/proto/helloworld.proto b/test-api/src/main/proto/helloworld.proto index c877f8d..28a8b6b 100644 --- a/test-api/src/main/proto/helloworld.proto +++ b/test-api/src/main/proto/helloworld.proto @@ -9,19 +9,21 @@ option objc_class_prefix = "HLW"; package helloworld; -// The greeting service definition. +// The greeting service definition. "" */ fun `unsafe comment `() 123 service Greeter { - // Sends a greeting + // Sends a greeting "" */ fun `unsafe comment `() 123 rpc SayHello (HelloRequest) returns (HelloReply){ option (google.api.method_signature) = "name"; }; - // Streams a many greetings + // Streams many greetings "" */ fun `unsafe comment `() 123 rpc SayHelloStreaming (stream HelloRequest) returns (stream HelloReply); + // Sends many greetings "" */ fun `unsafe comment `() 123 rpc SayHelloClientStreaming (stream HelloRequest) returns (HelloReply); + // Receives many greetings "" */ fun `unsafe comment `() 123 rpc SayHelloServerStreaming (HelloRequest) returns (stream HelloReply){ option (google.api.method_signature) = "name"; };