-
Notifications
You must be signed in to change notification settings - Fork 5
New tapiro imports #373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
New tapiro imports #373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,29 @@ import scala.meta.contrib._ | |||||
import cats.data.NonEmptyList | ||||||
|
||||||
object Meta { | ||||||
|
||||||
val controllerImports =(routes: List[TapiroRoute],imports: Set[Importer],outputPackage:String) => { | ||||||
val types = routes.flatMap(tr => tr.route.params.map(r=> r.tpe) ++ (if (tr.route.error.isDefined) List(tr.route.error.get,tr.route.returns) else List(tr.route.returns))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
should be equivalent I think? |
||||||
val typeNameList = types.flatMap(typeNameExtractor(_)).toSet | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
val (wildcardImportList,importList) = imports.collect{ | ||||||
case i : Importer if i.syntax.endsWith("._") => List(i) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could extract a function |
||||||
case Importer(ref,importees) => importees.filter(p=> typeNameList.contains(p.syntax)).map(p=> Importer(ref,List(p))) | ||||||
}.flatten.partition(_.syntax.endsWith("._")) | ||||||
val controllerPackageStrings = routes.map(r=> s"import ${r.route.controllerPackage.mkString(".")}._") | ||||||
val controllerPackage : List[Import] =controllerPackageStrings.flatMap(_.parse[Source].getOrElse(Source(List())).tree.children).collect{case i: Import => i} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit strange since we're first building the strings and then constructing the imports by parsing the strings. I think it would be nicer to build the imports directly using quasiquotes (and maybe extract the strings from them). But I'm not sure if this is too cumbersome, otherwise it's fine to leave as is. |
||||||
val importers = (if (importList.flatMap(i=> typeNameList.diff(i.importees.map(_.syntax).toSet)).isEmpty) importList else { | ||||||
(wildcardImportList ++ importList) | ||||||
}).map(i=>Import(List(i))) | ||||||
val result= if (controllerPackageStrings.filter(_.endsWith(outputPackage+"._")).isEmpty) controllerPackage ++ importers else importers | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe use Also, I'm not sure I've understood what the condition in the val someMeaningfulNameForTheCondition = controllerPackageStrings.filter(_.endsWith(outputPackage+"._")
val result= if (someMeaningfulNameForTheCondition) controllerPackage ++ importers else importers using some meaningful name to make this more understandable. |
||||||
deduplicate(result.toList).toSet | ||||||
} : Set[Import] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move the type annotation to after the arguments |
||||||
|
||||||
def typeNameExtractor (tpe : MetarpheusType) : Set[String]= | ||||||
tpe match { | ||||||
case MetarpheusType.Name(name) => Set(name) | ||||||
case MetarpheusType.Apply(head,args) => (head :: args.map(typeNameExtractor(_)).flatten.toList).toSet | ||||||
} | ||||||
|
||||||
val codecsImplicits = (routes: List[TapiroRoute], authTokenName: String) => { | ||||||
val notUnit = (t: MetarpheusType) => t != MetarpheusType.Name("Unit") | ||||||
val toDecoder = (t: Type) => t"Decoder[${extractListType(t)}]" | ||||||
|
@@ -44,10 +67,10 @@ object Meta { | |||||
case _ => t | ||||||
} | ||||||
|
||||||
private[this] val deduplicate: List[Type] => List[Type] = (ts: List[Type]) => | ||||||
private[this] def deduplicate[A<:Tree](ts: List[A]): List[A] = | ||||||
ts match { | ||||||
case Nil => Nil | ||||||
case head :: tail => head :: deduplicate(tail.filter(!_.isEqual(head))) | ||||||
case head :: tail => head :: deduplicate(tail.filter(!_.syntax.equals(head.syntax))) | ||||||
} | ||||||
|
||||||
private[this] val isAuthToken = (t: MetarpheusType, authTokenName: String) => t == MetarpheusType.Name(authTokenName) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function is a bit hard to read. I'll suggest a few small changes below, but also: how much of the complexity is done to avoid redundant imports? Since we're getting redundant imports anyway, maybe we can simplify a bit even if we get more of them? (and then either remove them with scalafix or suppress them during compilation)