Skip to content

Commit 2d2f5aa

Browse files
committed
Imply idempotent when safe is set. Emit options only when idempotency option is present. Add tests
1 parent f472ae9 commit 2d2f5aa

File tree

4 files changed

+32
-9
lines changed

4 files changed

+32
-9
lines changed

compiler-plugin/src/main/scala/scalapb/compiler/GrpcServicePrinter.scala

+15-8
Original file line numberDiff line numberDiff line change
@@ -178,26 +178,33 @@ final class GrpcServicePrinter(service: ServiceDescriptor, implicits: Descriptor
178178
case StreamType.Bidirectional => "BIDI_STREAMING"
179179
}
180180

181-
val grpcMethodDescriptor = "_root_.io.grpc.MethodDescriptor"
182-
183181
val idempotencyLevel = method.getOptions.getIdempotencyLevel
184182
val safe = idempotencyLevel == IdempotencyLevel.NO_SIDE_EFFECTS
185-
val idempotent = idempotencyLevel == IdempotencyLevel.IDEMPOTENT
183+
val idempotent = idempotencyLevel == IdempotencyLevel.IDEMPOTENT || safe
184+
185+
val grpcMethodDescriptor = "_root_.io.grpc.MethodDescriptor"
186186

187187
p.add(
188188
s"""${method.deprecatedAnnotation}val ${method.grpcDescriptor.nameSymbol}: $grpcMethodDescriptor[${method.inputType.scalaType}, ${method.outputType.scalaType}] =
189189
| $grpcMethodDescriptor.newBuilder()
190190
| .setType($grpcMethodDescriptor.MethodType.$methodType)
191191
| .setFullMethodName($grpcMethodDescriptor.generateFullMethodName("${service.getFullName}", "${method.getName}"))
192192
| .setSampledToLocalTracing(true)
193-
| .setSafe($safe)
194-
| .setIdempotent($idempotent)
195193
| .setRequestMarshaller(${marshaller(method.inputType)})
196194
| .setResponseMarshaller(${marshaller(method.outputType)})
197195
| .setSchemaDescriptor(_root_.scalapb.grpc.ConcreteProtoMethodDescriptorSupplier.fromMethodDescriptor(${method.javaDescriptorSource}))
198-
| .build()
199-
|""".stripMargin
200-
)
196+
|""".stripMargin.stripSuffix("\n")
197+
).indented(
198+
_.indented(
199+
_.when(safe) {
200+
_.add(".setSafe(true)")
201+
}
202+
.when(idempotent) {
203+
_.add(".setIdempotent(true)")
204+
}
205+
.add(".build()")
206+
)
207+
).newline
201208
}
202209

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

e2e-grpc/src/main/protobuf/service.proto

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ service Service1 {
9797
rpc PrimitiveValues(google.protobuf.Int32Value) returns (google.protobuf.StringValue) {};
9898

9999
rpc EchoRequest(Req1) returns (Res6) {}
100+
101+
rpc NoSideEffects(Req1) returns (Res6) {
102+
option idempotency_level = NO_SIDE_EFFECTS;
103+
}
100104
}
101105

102106
service Issue774 {

e2e-grpc/src/main/scala/com/thesamet/pb/Service1ScalaImpl.scala

+4
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,8 @@ class Service1ScalaImpl extends Service1 {
126126
}
127127

128128
override def primitiveValues(request: Int): Future[String] = Future.successful("boo")
129+
130+
override def noSideEffects(request: Req1): Future[Res6] =
131+
Future.successful(Res6(Some(request)))
132+
129133
}

e2e-grpc/src/test/scala/MethodDescriptorSpec.scala

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ import org.scalatest.{LoneElement, OptionValues}
77
class MethodDescriptorSpec extends AnyFlatSpec with Matchers with LoneElement with OptionValues {
88

99
"scala descriptor" must "expose correct input and output message descriptors" in {
10-
val unaryMethod = Service1Grpc.Service1.scalaDescriptor.methods.find(_.name == "CustomUnary").get
10+
val unaryMethod =
11+
Service1Grpc.Service1.scalaDescriptor.methods.find(_.name == "CustomUnary").get
1112

1213
unaryMethod.inputType must be theSameInstanceAs XYMessage.scalaDescriptor
1314
unaryMethod.outputType must be theSameInstanceAs Res5.scalaDescriptor
1415
}
16+
17+
"java descriptor" must "set idempotent and safe options when idempotency_level is set" in {
18+
val noSideEffMethod = Service1Grpc.METHOD_NO_SIDE_EFFECTS
19+
20+
noSideEffMethod.isSafe must be(true)
21+
noSideEffMethod.isIdempotent must be(true)
22+
}
1523
}

0 commit comments

Comments
 (0)