made extraction logic more testable

This commit is contained in:
Tony Tam
2012-09-20 18:01:00 -07:00
parent 7881786fe8
commit 46249530a3
4 changed files with 49 additions and 30 deletions

View File

@@ -42,6 +42,24 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
var codegen = new Codegen(this)
def json = ScalaJsonUtil.getJsonMapper
def extractOperations(subDocs:List[Documentation], allModels: HashMap[String, DocumentationSchema] )(implicit basePath:String) = {
val output = new ListBuffer[(String, String, DocumentationOperation)]
subDocs.foreach(subDoc => {
val basePath = subDoc.basePath
val resourcePath = subDoc.resourcePath
if (subDoc.getApis != null) {
subDoc.getApis.foreach(api => {
for ((apiPath, operation) <- ApiExtractor.extractOperations(basePath, api)) {
output += Tuple3(basePath, apiPath, operation)
}
})
output.map(op => processOperation(op._2, op._3))
allModels ++= CoreUtils.extractModels(subDoc)
}
})
output.toList
}
def generateClient(args: Array[String]) = {
if (args.length == 0) {
throw new RuntimeException("Need url to resources.json as argument. You can also specify VM Argument -DfileMap=/path/to/folder/containing.resources.json/")
@@ -60,7 +78,7 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
}
}
val basePath = getBasePath(doc.basePath)
implicit val basePath = getBasePath(doc.basePath)
val apis = doc.getApis
if (apis == null)
@@ -72,24 +90,10 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil {
new SwaggerSpecValidator(doc, subDocs).validate()
val allModels = new HashMap[String, DocumentationSchema]
val operations = new ListBuffer[(String, String, DocumentationOperation)]
subDocs.foreach(subDoc => {
val basePath = subDoc.basePath
val resourcePath = subDoc.resourcePath
if (subDoc.getApis != null) {
subDoc.getApis.foreach(api => {
for ((apiPath, operation) <- ApiExtractor.extractOperations(doc.basePath, api)) {
operations += Tuple3(basePath, apiPath, operation)
}
})
operations.map(op => processOperation(op._2, op._3))
allModels ++= CoreUtils.extractModels(subDoc)
}
})
val operations = extractOperations(subDocs, allModels)
val apiMap = groupApisToFiles(operations.toList)
val apiMap = groupApisToFiles(operations)
for ((identifier, operationList) <- apiMap) {
val basePath = identifier._1
val name = identifier._2

View File

@@ -39,8 +39,6 @@ class BasicJavaGenerator extends BasicGenerator {
"double" -> "Double",
"object" -> "Object")
override def packageName = "com.wordnik.client"
// location of templates
override def templateDir = "Java"
@@ -53,6 +51,14 @@ class BasicJavaGenerator extends BasicGenerator {
// where to write generated code
override def destinationDir = "src/test/java"
override def reservedWords = Set("abstract", "continue", "for", "new", "switch", "assert",
"default", "if", "package", "synchronized", "boolean", "do", "goto", "private",
"this", "break", "double", "implements", "protected", "throw", "byte", "else",
"import", "public", "throws", "case", "enum", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try", "char", "final", "interface", "static",
"void", "class", "finally", "long", "strictfp", "volatile", "const", "float",
"native", "super", "while")
// import/require statements for specific datatypes
override def importMapping = Map(
"Date" -> "java.util.Date",
@@ -61,10 +67,10 @@ class BasicJavaGenerator extends BasicGenerator {
"List" -> "java.util.List")
// package for models
override def modelPackage = Some("com.wordnik.model")
override def modelPackage = Some("com.wordnik.client.model")
// package for api classes
override def apiPackage = Some("com.wordnik.api")
override def apiPackage = Some("com.wordnik.client.api")
// file suffix
override def fileSuffix = ".java"
@@ -123,8 +129,8 @@ class BasicJavaGenerator extends BasicGenerator {
}
// default values
override def toDefaultValue(properCase: String, obj: DocumentationSchema) = {
properCase match {
override def toDefaultValue(dataType: String, obj: DocumentationSchema) = {
dataType match {
case "boolean" => "false"
case "int" => "0"
case "long" => "0L"
@@ -140,4 +146,10 @@ class BasicJavaGenerator extends BasicGenerator {
case _ => "null"
}
}
override def escapeReservedWord(word: String) = {
if (reservedWords.contains(word))
throw new Exception("reserved word " + "\"" + word + "\" not allowed")
else word
}
}

View File

@@ -72,15 +72,17 @@ class BasicScalaGenerator extends BasicGenerator {
}
override def toDeclaration(obj: DocumentationSchema): (String, String) = {
val datatype = (obj.getType.charAt(0).toUpperCase + obj.getType.substring(1)) match {
obj.getType match {
case "Array" => {
"java.util.List[%s]" format toDeclaredType(
val inner = {
if (obj.items.ref != null) obj.items.ref
else obj.items.getType)
else obj.items.getType
}
val e = "java.util.List[%s]" format toDeclaredType(inner)
(e, toDefaultValue(inner, obj))
}
case e: String => e
case e: String => (e, toDefaultValue(e, obj))
}
(datatype, toDefaultValue(datatype, obj))
}
// escape keywords

View File

@@ -123,10 +123,11 @@ abstract class CodegenConfig {
} else None
}
def toDefaultValue(properCase: String, obj: DocumentationSchema) = {
properCase match {
def toDefaultValue(dataType: String, obj: DocumentationSchema) = {
dataType match {
case "int" => "0"
case "long" => "0L"
case "float" => "0f"
case "double" => "0.0"
case e: String if (Set("List").contains(e)) => {
val inner = {