Skip to content

Commit

Permalink
param"implicit ..." is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Oct 18, 2023
1 parent a705fbc commit db99430
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,19 +496,22 @@ class AkkaHttpClientGenerator private (modelGeneratorType: ModelGeneratorType) e
tracingName: Option[String],
serverUrls: Option[NonEmptyList[URI]],
tracing: Boolean
): Target[List[List[scala.meta.Term.Param]]] = {
): Target[List[Term.ParamClause]] = {
val implicits = List(
param"implicit httpClient: HttpRequest => Future[HttpResponse]",
param"implicit ec: ExecutionContext",
param"implicit mat: Materializer"
param"httpClient: HttpRequest => Future[HttpResponse]",
param"ec: ExecutionContext",
param"mat: Materializer"
)
for {
protocolImplicits <- AkkaHttpHelper.protocolImplicits(modelGeneratorType)
} yield List(
List(formatHost(serverUrls)) ++ (if (tracing)
Some(formatClientName(tracingName))
else None),
implicits ++ protocolImplicits
Term.ParamClause(
List(formatHost(serverUrls)) ++ (if (tracing)
Some(formatClientName(tracingName))
else None),
None
),
Term.ParamClause(implicits ++ protocolImplicits, Some(Mod.Implicit()))
)
}
private def generateResponseDefinitions(
Expand All @@ -526,7 +529,7 @@ class AkkaHttpClientGenerator private (modelGeneratorType: ModelGeneratorType) e
clientName: String,
tracingName: Option[String],
serverUrls: Option[NonEmptyList[URI]],
ctorArgs: List[List[scala.meta.Term.Param]],
ctorArgs: List[Term.ParamClause],
tracing: Boolean
): Target[StaticDefns[ScalaLanguage]] = {
def extraConstructors(
Expand All @@ -537,8 +540,8 @@ class AkkaHttpClientGenerator private (modelGeneratorType: ModelGeneratorType) e
tracing: Boolean
): Target[List[Defn]] = {
val implicits = List(
param"implicit ec: ExecutionContext",
param"implicit mat: Materializer"
param"ec: ExecutionContext",
param"mat: Materializer"
)
val tracingParams: List[Term.Param] = if (tracing) {
List(formatClientName(tracingName))
Expand All @@ -548,16 +551,19 @@ class AkkaHttpClientGenerator private (modelGeneratorType: ModelGeneratorType) e

for {
protocolImplicits <- AkkaHttpHelper.protocolImplicits(modelGeneratorType)
args = List(
Term.ParamClause(
List(param"httpClient: HttpRequest => Future[HttpResponse]", formatHost(serverUrls)) ++ tracingParams,
None
),
Term.ParamClause(implicits ++ protocolImplicits, Some(Mod.Implicit()))
)
} yield List(
q"""
def httpClient(httpClient: HttpRequest => Future[HttpResponse], ${formatHost(
serverUrls
)}, ..$tracingParams)(..${implicits ++ protocolImplicits}): $tpe = $ctorCall
"""
q"""def httpClient(...${args}): $tpe = $ctorCall"""
)
}

def paramsToArgs(params: List[List[Term.Param]]): List[Term.ArgClause] =
def paramsToArgs(params: List[Term.ParamClause]): List[Term.ArgClause] =
params.map { params =>
Term.ArgClause(
params
Expand All @@ -583,7 +589,7 @@ class AkkaHttpClientGenerator private (modelGeneratorType: ModelGeneratorType) e
tracingName: Option[String],
serverUrls: Option[NonEmptyList[URI]],
basePath: Option[String],
ctorArgs: List[List[scala.meta.Term.Param]],
ctorArgs: List[Term.ParamClause],
clientCalls: List[scala.meta.Defn],
supportDefinitions: List[scala.meta.Defn],
tracing: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ object AkkaHttpHelper {
case _ => Left(s"Unknown modelGeneratorType: ${modelGeneratorType}")
}

def protocolImplicits(modelGeneratorType: ModelGeneratorType): Target[List[Term.Param]] = modelGeneratorType match {
case _: CirceModelGenerator => Target.pure(List.empty)
def protocolImplicits(modelGeneratorType: ModelGeneratorType): Target[Term.ParamClause] = modelGeneratorType match {
case _: CirceModelGenerator => Target.pure(Term.ParamClause(Nil))
case _: JacksonModelGenerator =>
Target.pure(
List(
param"implicit mapper: com.fasterxml.jackson.databind.ObjectMapper",
param"implicit validator: javax.validation.Validator"
Term.ParamClause(
List(
param"mapper: com.fasterxml.jackson.databind.ObjectMapper",
param"validator: javax.validation.Validator"
),
Some(Mod.Implicit())
)
)
case _ => Target.raiseError(RuntimeFailure(s"Unknown modelGeneratorType: ${modelGeneratorType}"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class AkkaHttpServerGenerator private (akkaHttpVersion: AkkaHttpVersion, modelGe
q"implicit def ${Term.Name(s"${name.value}Ev")}(value: ${tpe}): ${responseSuperType} = ${name}(value)"
}
protocolImplicits <- AkkaHttpHelper.protocolImplicits(modelGeneratorType)
toResponseImplicits = List(param"implicit ec: scala.concurrent.ExecutionContext") ++ protocolImplicits
toResponseImplicits = List(param"ec: scala.concurrent.ExecutionContext") ++ protocolImplicits
companion = q"""
object ${responseSuperTerm} {
${Defn.Def(
Expand All @@ -298,7 +298,7 @@ class AkkaHttpServerGenerator private (akkaHttpVersion: AkkaHttpVersion, modelGe
List(
Member.ParamClauseGroup(
Type.ParamClause(Nil),
NonEmptyList.fromList(protocolImplicits).fold(List.empty[Term.ParamClause])(nel => List(Term.ParamClause(nel.toList)))
NonEmptyList.fromList(protocolImplicits).fold(List.empty[Term.ParamClause])(nel => List(Term.ParamClause(nel.toList, Some(Mod.Implicit()))))
)
),
Some(t"ToResponseMarshaller[${responseSuperType}]"),
Expand All @@ -307,7 +307,10 @@ class AkkaHttpServerGenerator private (akkaHttpVersion: AkkaHttpVersion, modelGe
implicit def ${Term
.Name(
s"${responseClsName.uncapitalized}TR"
)}(value: ${responseSuperType})(..$toResponseImplicits): scala.concurrent.Future[List[Marshalling[HttpResponse]]] =
)}(value: ${responseSuperType})(..${Term.ParamClause(
toResponseImplicits,
Some(Mod.Implicit())
)}): scala.concurrent.Future[List[Marshalling[HttpResponse]]] =
${Term.Match(Term.Name("value"), marshallers, Nil)}

def apply[T](value: T)(implicit ev: T => ${responseSuperType}): ${responseSuperType} = ev(value)
Expand Down Expand Up @@ -447,13 +450,18 @@ class AkkaHttpServerGenerator private (akkaHttpVersion: AkkaHttpVersion, modelGe
baseHandlerType
}
}
val typeParams = if (customExtraction) List(tparam"$customExtractionTypeName") else List()
val routesParams = List(param"handler: $handlerType") ++ extraRouteParams
val routeImplicits = List(param"implicit mat: akka.stream.Materializer") ++ protocolImplicits
val typeParams = if (customExtraction) List(tparam"$customExtractionTypeName") else List()
val params = List(
Term.ParamClause(
List(param"handler: $handlerType") ++ extraRouteParams,
None
),
Term.ParamClause(List(param"mat: akka.stream.Materializer") ++ protocolImplicits, Some(Mod.Implicit()))
)
List(q"""
object ${Term.Name(resourceName)} {
..${supportDefinitions};
def routes[..${typeParams}](..${routesParams})(..$routeImplicits): Route = {
def routes[..${typeParams}](...${params}): Route = {
..${combinedRouteTerms}
}

Expand Down Expand Up @@ -1156,7 +1164,7 @@ class AkkaHttpServerGenerator private (akkaHttpVersion: AkkaHttpVersion, modelGe
List(
Member.ParamClauseGroup(
Type.ParamClause(Nil),
NonEmptyList.fromList(decoderImplicits).fold(List.empty[List[Term.Param]])(nel => List(Term.ParamClause(nel.toList)))
List(decoderImplicits).filter(_.nonEmpty)
)
),
Some(t"FromRequestUnmarshaller[$baseType]"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,18 @@ class Http4sClientGenerator(version: Http4sVersion) extends ClientTerms[ScalaLan
}
def getImports(tracing: Boolean): Target[List[scala.meta.Import]] = Target.pure(List(q"import org.http4s.circe._"))
def getExtraImports(tracing: Boolean): Target[List[scala.meta.Import]] = Target.pure(List.empty)
def clientClsArgs(tracingName: Option[String], serverUrls: Option[NonEmptyList[URI]], tracing: Boolean): Target[List[List[scala.meta.Term.Param]]] = {
val ihc = param"implicit httpClient: Http4sClient[F]"
val ief = param"implicit F: Async[F]"
def clientClsArgs(tracingName: Option[String], serverUrls: Option[NonEmptyList[URI]], tracing: Boolean): Target[List[Term.ParamClause]] = {
val ihc = param"httpClient: Http4sClient[F]"
val ief = param"F: Async[F]"
Target.pure(
List(
List(formatHost(serverUrls)) ++ (if (tracing)
Some(formatClientName(tracingName))
else None),
List(ief, ihc)
Term.ParamClause(
List(formatHost(serverUrls)) ++ (if (tracing)
Some(formatClientName(tracingName))
else None),
None
),
Term.ParamClause(List(ief, ihc), Some(Mod.Implicit()))
)
)
}
Expand All @@ -516,7 +519,7 @@ class Http4sClientGenerator(version: Http4sVersion) extends ClientTerms[ScalaLan
clientName: String,
tracingName: Option[String],
serverUrls: Option[NonEmptyList[URI]],
ctorArgs: List[List[scala.meta.Term.Param]],
ctorArgs: List[Term.ParamClause],
tracing: Boolean
): Target[StaticDefns[ScalaLanguage]] = {
def extraConstructors(
Expand All @@ -539,7 +542,7 @@ class Http4sClientGenerator(version: Http4sVersion) extends ClientTerms[ScalaLan
)
}

def paramsToArgs(params: List[List[Term.Param]]): List[Term.ArgClause] =
def paramsToArgs(params: List[Term.ParamClause]): List[Term.ArgClause] =
params.map { params =>
Term.ArgClause(
params
Expand Down Expand Up @@ -567,7 +570,7 @@ class Http4sClientGenerator(version: Http4sVersion) extends ClientTerms[ScalaLan
tracingName: Option[String],
serverUrls: Option[NonEmptyList[URI]],
basePath: Option[String],
ctorArgs: List[List[scala.meta.Term.Param]],
ctorArgs: List[Term.ParamClause],
clientCalls: List[scala.meta.Defn],
supportDefinitions: List[scala.meta.Defn],
tracing: Boolean
Expand Down

0 comments on commit db99430

Please sign in to comment.