-
Notifications
You must be signed in to change notification settings - Fork 124
/
Method.scala
73 lines (60 loc) · 2.08 KB
/
Method.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Copyright (C) 2018-2020 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.grpc.gen.scaladsl
import com.google.protobuf.Descriptors.{ Descriptor, MethodDescriptor }
import akka.grpc.gen._
import scalapb.compiler.DescriptorImplicits
case class Method(
name: String,
grpcName: String,
inputType: Descriptor,
inputStreaming: Boolean,
outputType: Descriptor,
outputStreaming: Boolean,
comment: Option[String] = None)(implicit ops: DescriptorImplicits) {
import Method._
def deserializer = Serializer(inputType)
def serializer = Serializer(outputType)
def unmarshal =
if (inputStreaming) "GrpcMarshalling.unmarshalStream"
else "GrpcMarshalling.unmarshal"
def marshal =
if (outputStreaming) "GrpcMarshalling.marshalStream"
else "GrpcMarshalling.marshal"
def parameterType =
if (inputStreaming) s"akka.stream.scaladsl.Source[${messageType(inputType)}, akka.NotUsed]"
else messageType(inputType)
def inputTypeUnboxed = messageType(inputType)
def outputTypeUnboxed = messageType(outputType)
def returnType =
if (outputStreaming) s"akka.stream.scaladsl.Source[${messageType(outputType)}, akka.NotUsed]"
else s"scala.concurrent.Future[${messageType(outputType)}]"
val methodType: MethodType = {
(inputStreaming, outputStreaming) match {
case (false, false) => Unary
case (true, false) => ClientStreaming
case (false, true) => ServerStreaming
case (true, true) => BidiStreaming
}
}
}
object Method {
def apply(descriptor: MethodDescriptor)(implicit ops: DescriptorImplicits): Method = {
import ops._
Method(
name = methodName(descriptor.getName),
grpcName = descriptor.getName,
descriptor.getInputType,
descriptor.toProto.getClientStreaming,
descriptor.getOutputType,
descriptor.toProto.getServerStreaming,
descriptor.comment)
}
private def methodName(name: String) =
name.head.toLower +: name.tail
def messageType(messageType: Descriptor)(implicit ops: DescriptorImplicits) = {
import ops._
messageType.scalaType.fullName
}
}