@@ -19,8 +19,9 @@ class Translator[C <: Context](val c: C) {
19
19
import Validator ._
20
20
21
21
object implicits {
22
- implicit val liftRequestPath : Liftable [RequestPath ] =
23
- Liftable [RequestPath ]{ r => q " new _root_.sloth.RequestPath( ${r.apiName}, ${r.methodName}) " }
22
+ implicit val liftRequestPath : Liftable [RequestPath ] = Liftable [RequestPath ] { r =>
23
+ q " new _root_.sloth.RequestPath( ${r.apiName}, ${r.methodName}) "
24
+ }
24
25
}
25
26
26
27
def abort (msg : String ) = c.abort(c.enclosingPosition, msg)
@@ -38,7 +39,7 @@ class Translator[C <: Context](val c: C) {
38
39
39
40
// TODO rename overloaded methods to fun1, fun2, fun3 or append TypeSignature instead of number?
40
41
private def validateAllMethods (methods : List [(MethodSymbol , Type )]): List [Either [String , (MethodSymbol , Type )]] =
41
- methods.groupBy(m => methodPathPart (m._1)).map {
42
+ methods.groupBy(m => methodPathInfo (m._1)).map {
42
43
case (_, x :: Nil ) => Right (x)
43
44
case (k, _) => Left (s """ method $k is overloaded (rename the method or add a @PathName("other-name")) """ )
44
45
}.toList
@@ -47,6 +48,11 @@ class Translator[C <: Context](val c: C) {
47
48
case Apply (Select (New (annotation), _), Literal (Constant (name)) :: Nil ) if annotation.tpe =:= typeOf[sloth.PathName ] => name.toString
48
49
}
49
50
51
+ private def findMeta (annotations : Seq [Annotation ]) = annotations.map(_.tree).collect {
52
+ case Apply (Select (New (annotation), _), _) if annotation.tpe <:< typeOf[sloth.Meta ] => annotation.tpe.typeSymbol.name.toString
53
+ case Apply (Select (New (annotation), _), Literal (Constant (name)) :: Nil ) if annotation.tpe =:= typeOf[sloth.MetaName ] => name.toString
54
+ }.toVector
55
+
50
56
private def eitherSeq [A , B ](list : List [Either [A , B ]]): Either [List [A ], List [B ]] = list.partition(_.isLeft) match {
51
57
case (Nil , rights) => Right (for (Right (i) <- rights) yield i)
52
58
case (lefts, _) => Left (for (Left (s) <- lefts) yield s)
@@ -75,11 +81,17 @@ class Translator[C <: Context](val c: C) {
75
81
}
76
82
77
83
// TODO what about fqn for trait to not have overlaps?
78
- def traitPathPart (tpe : Type ): String =
79
- findPathName(tpe.typeSymbol.annotations).getOrElse(tpe.typeSymbol.name.toString)
80
-
81
- def methodPathPart (m : MethodSymbol ): String =
82
- findPathName(m.annotations).getOrElse(m.name.toString)
84
+ def traitPathInfo (tpe : Type ): (String , Vector [String ]) =
85
+ (
86
+ findPathName(tpe.typeSymbol.annotations).getOrElse(tpe.typeSymbol.name.toString),
87
+ findMeta(tpe.typeSymbol.annotations)
88
+ )
89
+
90
+ def methodPathInfo (m : MethodSymbol ): (String , Vector [String ]) =
91
+ (
92
+ findPathName(m.annotations).getOrElse(m.name.toString),
93
+ findMeta(m.annotations)
94
+ )
83
95
84
96
def paramAsValDef (p : Symbol ): ValDef = q " val ${p.name.toTermName}: ${p.typeSignature}"
85
97
def paramsAsValDefs (m : Type ): List [List [ValDef ]] = m.paramLists.map(_.map(paramAsValDef))
@@ -134,10 +146,10 @@ object TraitMacro {
134
146
135
147
val validMethods = t.supportedMethodsInType(traitTag.tpe, resultTag.tpe)
136
148
137
- val traitPathPart = t.traitPathPart (traitTag.tpe)
149
+ val ( traitPathPart, traitMeta) = t.traitPathInfo (traitTag.tpe)
138
150
val methodImplList = validMethods.collect { case (symbol, method) =>
139
- val methodPathPart = t.methodPathPart (symbol)
140
- val path = RequestPath (traitPathPart, methodPathPart)
151
+ val ( methodPathPart, methodMeta) = t.methodPathInfo (symbol)
152
+ val path = RequestPath (traitPathPart, methodPathPart, traitMeta ++ methodMeta )
141
153
val parameters = t.paramsAsValDefs(method)
142
154
val paramsType = t.paramsType(method)
143
155
val paramListValue = t.wrapAsParamsType(method)
@@ -189,10 +201,10 @@ object RouterMacro {
189
201
190
202
val validMethods = t.supportedMethodsInType(traitTag.tpe, resultTag.tpe)
191
203
192
- val traitPathPart = t.traitPathPart (traitTag.tpe)
204
+ val ( traitPathPart, traitMeta) = t.traitPathInfo (traitTag.tpe)
193
205
val methodTuples = validMethods.map { case (symbol, method) =>
194
- val methodPathPart = t.methodPathPart (symbol)
195
- val path = RequestPath (traitPathPart, methodPathPart)
206
+ val ( methodPathPart, methodMeta) = t.methodPathInfo (symbol)
207
+ val path = RequestPath (traitPathPart, methodPathPart, traitMeta ++ methodMeta )
196
208
val paramsType = t.paramsType(method)
197
209
val argParams = t.objectToParams(method, TermName (" args" ))
198
210
val innerReturnType = t.getInnerTypeOutOfReturnType(resultTag.tpe, method.finalResultType)
@@ -242,10 +254,10 @@ object ChecksumMacro {
242
254
case class ParamSignature (name : String , tpe : Type ) {
243
255
def checksum : Int = (name, typeChecksum(tpe)).hashCode
244
256
}
245
- case class MethodSignature (name : String , params : List [ParamSignature ], result : Type ) {
246
- def checksum : Int = (name, params.map(_.checksum), typeChecksum(result)).hashCode
257
+ case class MethodSignature (name : String , meta : Seq [ String ], params : List [ParamSignature ], result : Type ) {
258
+ def checksum : Int = (name, meta, params.map(_.checksum), typeChecksum(result)).hashCode
247
259
}
248
- case class ApiSignature (name : String , methods : Set [MethodSignature ]) {
260
+ case class ApiSignature (name : String , meta : Seq [ String ], methods : Set [MethodSignature ]) {
249
261
def checksum : Int = (name, methods.map(_.checksum)).hashCode
250
262
}
251
263
@@ -279,21 +291,21 @@ object ChecksumMacro {
279
291
tpe.typeSymbol.fullName,
280
292
caseAccessors.map(a => (a.name.toString, typeChecksum(a.typeSignatureIn(tpe).finalResultType))),
281
293
directSubClasses.map(typeChecksum).toSet
282
- ).hashCode
294
+ ).hashCode
283
295
}
284
296
285
297
val definedMethods = t.definedMethodsInType(traitTag.tpe)
286
298
287
- val dataMethods : Set [MethodSignature ] = definedMethods.map { case (symbol, method) =>
288
- val name = t.methodPathPart (symbol)
299
+ val dataMethods : Set [MethodSignature ] = definedMethods.map { case (symbol, method) =>
300
+ val ( name, meta) = t.methodPathInfo (symbol)
289
301
val resultType = method.finalResultType
290
302
val params = paramsOfType(method)
291
303
292
- MethodSignature (name, params, resultType)
304
+ MethodSignature (name, meta, params, resultType)
293
305
}.toSet
294
306
295
- val name = t.traitPathPart (traitTag.tpe)
296
- val apiSignature = ApiSignature (name, dataMethods)
307
+ val ( name, meta) = t.traitPathInfo (traitTag.tpe)
308
+ val apiSignature = ApiSignature (name, meta, dataMethods)
297
309
298
310
val checksum = apiSignature.checksum
299
311
0 commit comments