Skip to content
Permalink
Browse files

Refactor custom type name extraction

This just 'hides' the vendor extension name construction in the extract
package object where it belongs.
  • Loading branch information...
kelnos committed May 1, 2019
1 parent 3603652 commit fb10f5e8caa17b836875afc9d187da316bcaa661
@@ -13,7 +13,7 @@ import com.github.javaparser.ast.NodeList
import com.github.javaparser.ast.expr._
import com.twilio.guardrail.terms.{ ScalaTerms, SwaggerTerms }
import com.twilio.guardrail.terms.framework.FrameworkTerms
import com.twilio.guardrail.extract.{ Default, VendorExtension, extractFromNames }
import com.twilio.guardrail.extract.{ CustomTypeName, Default, VendorExtension }
import com.twilio.guardrail.extract.VendorExtension.VendorExtensible._
import com.twilio.guardrail.generators.ScalaParameter
import com.twilio.guardrail.languages.{ JavaLanguage, LA, ScalaLanguage }
@@ -141,7 +141,7 @@ object SwaggerUtil {
def customTypeName[L <: LA, F[_], A: VendorExtension.VendorExtensible](v: A)(implicit S: ScalaTerms[L, F]): Free[F, Option[String]] =
for {
prefixes <- S.vendorPrefixes()
} yield extractFromNames[String, A](prefixes.map(_ + "-type"), v)
} yield CustomTypeName(v, prefixes)

sealed class ModelMetaTypePartiallyApplied[L <: LA, F[_]](val dummy: Boolean = true) {
def apply[T <: Schema[_]](model: T)(implicit Sc: ScalaTerms[L, F], Sw: SwaggerTerms[L, F], F: FrameworkTerms[L, F]): Free[F, ResolvedType[L]] =
@@ -1,6 +1,11 @@
package com.twilio.guardrail

package object extract {
private def extractFromNames[F: VendorExtension.VendorExtensible, T: Extractable](v: F, names: List[String]): Option[T] =
names.foldLeft(Option.empty[T])(
(accum, name) => accum.orElse(VendorExtension(v).extract[T](name))
)

private def extractWithFallback[F: VendorExtension.VendorExtensible, T: Extractable](v: F, name: String, fallbackName: String): Option[T] = {
val ve = VendorExtension(v)
ve.extract[T](name)
@@ -13,11 +18,14 @@ package object extract {
})
}

def CustomTypeName[F: VendorExtension.VendorExtensible](v: F, vendorPrefixes: List[String]): Option[String] =
extractFromNames[F, String](v, vendorPrefixes.map(_ + "-type"))

def TracingLabel[F: VendorExtension.VendorExtensible](v: F): Option[String] =
extractWithFallback[F, String](v, "x-tracing-label", "x-scala-tracing-label")

def PackageName[F: VendorExtension.VendorExtensible](v: F, vendorPrefixes: List[String]): Option[String] =
extractFromNames[String, F](vendorPrefixes.map(_ + "-package"), v)
extractFromNames[F, String](v, vendorPrefixes.map(_ + "-package"))

def ServerRawResponse[F: VendorExtension.VendorExtensible](v: F): Option[Boolean] =
VendorExtension(v).extract[Boolean]("x-server-raw-response")
@@ -27,9 +35,4 @@ package object extract {

def FileHashAlgorithm[F: VendorExtension.VendorExtensible](v: F): Option[String] =
extractWithFallback[F, String](v, "x-file-hash", "x-scala-file-hash")

def extractFromNames[T: Extractable, F: VendorExtension.VendorExtensible](names: List[String], v: F): Option[T] =
names.foldLeft(Option.empty[T])(
(accum, name) => accum.orElse(VendorExtension(v).extract[T](name))
)
}

0 comments on commit fb10f5e

Please sign in to comment.
You can’t perform that action at this time.