Skip to content

Commit

Permalink
Imply idempotent when safe is set. Emit options only when idempotency…
Browse files Browse the repository at this point in the history
… option is present.
  • Loading branch information
igor-vovk committed Nov 29, 2024
1 parent f472ae9 commit 8fe5f10
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,33 @@ final class GrpcServicePrinter(service: ServiceDescriptor, implicits: Descriptor
case StreamType.Bidirectional => "BIDI_STREAMING"
}

val grpcMethodDescriptor = "_root_.io.grpc.MethodDescriptor"

val idempotencyLevel = method.getOptions.getIdempotencyLevel
val safe = idempotencyLevel == IdempotencyLevel.NO_SIDE_EFFECTS
val idempotent = idempotencyLevel == IdempotencyLevel.IDEMPOTENT
val idempotent = idempotencyLevel == IdempotencyLevel.IDEMPOTENT || safe

val grpcMethodDescriptor = "_root_.io.grpc.MethodDescriptor"

p.add(
s"""${method.deprecatedAnnotation}val ${method.grpcDescriptor.nameSymbol}: $grpcMethodDescriptor[${method.inputType.scalaType}, ${method.outputType.scalaType}] =
| $grpcMethodDescriptor.newBuilder()
| .setType($grpcMethodDescriptor.MethodType.$methodType)
| .setFullMethodName($grpcMethodDescriptor.generateFullMethodName("${service.getFullName}", "${method.getName}"))
| .setSampledToLocalTracing(true)
| .setSafe($safe)
| .setIdempotent($idempotent)
| .setRequestMarshaller(${marshaller(method.inputType)})
| .setResponseMarshaller(${marshaller(method.outputType)})
| .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(${method.javaDescriptorSource}))
| .build()
|""".stripMargin
)
).indent
.indent
.when(safe) {
_.add(".setSafe(true)")
}
.when(idempotent) {
_.add(".setIdempotent(true)")
}
.add(".build()")
.outdent
.outdent
}

private[this] def serviceDescriptor(service: ServiceDescriptor) = {
Expand Down

0 comments on commit 8fe5f10

Please sign in to comment.