-
Notifications
You must be signed in to change notification settings - Fork 1
/
GrpcServerSide.scala
133 lines (106 loc) · 5.04 KB
/
GrpcServerSide.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package mu
import io.grpc._
import io.grpc.MethodDescriptor.Marshaller
trait GrpcServerSide[S] {
def (service: S).toServerServiceDefinition: ServerServiceDefinition
}
object GrpcServerSide {
inline def derived[S]: GrpcServerSide[S] = ${ gen[S] }
import scala.quoted._
import scala.reflect._
def gen[S](using qctx: QuoteContext, s: Type[S]): Expr[GrpcServerSide[S]] = {
import qctx.tasty._
def isTraitOrAbstractClass(flags: Flags) =
flags.is(Flags.Trait) || flags.is(Flags.Abstract)
def hasServiceAnnotation(annots: List[Term]) =
annots.exists(_.tpe <:< typeOf[mu.service])
def isValidMethod(tparams: List[TypeDef], paramss: List[List[ValDef]], rhs: Option[Term]) =
tparams.isEmpty && paramss.flatten.size == 1 && rhs.isEmpty
def sequence(exprs: List[Expr[S => ServerMethodDefinition[_, _]]]): Expr[List[S => ServerMethodDefinition[_, _]]] = exprs match {
case h :: t => '{ $h :: ${sequence(t)} }
case Nil => '{ Nil: List[S => ServerMethodDefinition[_, _]] }
}
typeOf[S].classSymbol match {
case Some(classSym) if isTraitOrAbstractClass(classSym.flags) && hasServiceAnnotation(classSym.annots) =>
/*
* In the real Mu we would grab the encoding and other configuration
* from the @service annotation.
* For the demo we just assume protobuf.
*/
val serviceName = Expr(classSym.fullName)
/*
* For each method in the trait, we build a gRPC method definition:
* - construct a ServerCallHandler that, given an instance of the service trait
* and a request, calls the method on the instance to get the response
* - Summon marshallers for the method's request and response types
* - generate the appropriate fully-qualified name ("package.serviceName/methodName")
*/
val methodDefinitions: List[Expr[S => ServerMethodDefinition[_, _]]] =
classSym.classMethods.map { methodSym =>
methodSym.tree match {
case DefDef(methodName, tparams, paramss, returnTpt, rhs)
if isValidMethod(tparams, paramss, rhs) =>
val requestTpe = paramss.flatten.head.tpt.tpe
val responseTpe = returnTpt.tpe
(requestTpe.seal, responseTpe.seal) match {
case ('[$req], '[$resp]) =>
val serverCallHandler: Expr[S => ServerCallHandler[$req, $resp]] =
'{ (srv: $s) =>
CallHandlers.unary[$req, $resp](request =>
${
Apply(
Select('srv.unseal, methodSym),
List('request.unseal)
).seal.cast
}
)
}
val requestMarshaller: Expr[Marshaller[$req]] =
Expr.summon[ProtobufMarshaller[$req]] match {
case Some(marshaller) =>
marshaller
case None =>
Reporting.throwError(s"Could not summon a Protobuf marshaller for request type $req")
}
val responseMarshaller: Expr[Marshaller[$resp]] =
Expr.summon[ProtobufMarshaller[$resp]] match {
case Some(marshaller) =>
marshaller
case None =>
Reporting.throwError(s"Could not summon a Protobuf marshaller for response type $resp")
}
val fullMethodName = Expr(s"${classSym.fullName}/$methodName")
val methodDescriptor: Expr[MethodDescriptor[$req, $resp]] = '{
MethodDescriptor
.newBuilder($requestMarshaller, $responseMarshaller)
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName($fullMethodName)
.build()
}
'{ (srv: $s) =>
ServerMethodDefinition.create($methodDescriptor, $serverCallHandler(srv))
}
}
}
}
// Turn a list of expressions into an expression of a list
val methodDefinitionsExpr: Expr[List[S => ServerMethodDefinition[_, _]]] =
sequence(methodDefinitions)
'{
// this line is a workaround for https://github.com/lampepfl/dotty/issues/9020
type X = $s
new GrpcServerSide[X]{
def (service: S).toServerServiceDefinition: ServerServiceDefinition = {
val builder = ServerServiceDefinition.builder($serviceName)
${methodDefinitionsExpr}.foreach { f =>
builder.addMethod(f(service))
}
builder.build()
}
}
}
case _ =>
Reporting.throwError("Can only derive GrpcServerSide for @service-annotated traits or abstract classes")
}
}
}