Make recurseModel method tail-recursive, fix stack overflow error

This commit is contained in:
Ivan Porto Carrero 2013-04-09 17:20:08 +02:00
parent 025502efad
commit 1724c9328e
4 changed files with 26 additions and 35 deletions

View File

@ -5,7 +5,7 @@ organization := "com.wordnik"
name := "swagger-codegen"
version := "2.0.2-SNAPSHOT"
version := "2.0.3-SNAPSHOT"
scalaVersion := "2.9.2"

View File

@ -3,9 +3,8 @@ package {{package}}
{{#imports}}import {{import}}
{{/imports}}
import com.wordnik.swagger.client._
import akka.dispatch.{ Future, Await }
import akka.util.duration._
import akka.util.Duration
import scala.concurrent.{ Future, Await }
import scala.concurrent.duration._
import collection.mutable
{{#operations}}

View File

@ -2,6 +2,8 @@ organization := "com.wordnik.samples"
name := "{{projectName}}-client"
scalaVersion := "2.10.0"
libraryDependencies += "com.wordnik.swagger" %% "swagger-async-httpclient" % "0.1.4"
libraryDependencies += "joda-time" % "joda-time" % "2.2"

View File

@ -23,6 +23,8 @@ import scala.collection.JavaConversions._
import com.wordnik.swagger.codegen.spec.SwaggerSpec._
import scala.io.Source
import scala.collection.mutable
import scala.annotation.tailrec
object CoreUtils {
def extractAllModels(apis: List[ApiListing]): Map[String, Model] = {
@ -87,40 +89,28 @@ object CoreUtils {
allModels.filter(m => primitives.contains(m._1) == false).toMap
}
def recurseModels(requiredModels: Map[String, Model], allModels: Map[String, Model], subNames: HashSet[String]) = {
requiredModels.map(m => recurseModel(m._2, allModels, subNames))
def recurseModels(requiredModels: Map[String, Model], allModels: Map[String, Model], subNames: HashSet[String]) {
requiredModels foreach (m => subNames ++ recurseModel(m._2.properties.toList, allModels, subNames.toSet))
}
def recurseModel(model: Model, allModels: Map[String, Model], subNames: HashSet[String]): Unit = {
model.properties.foreach(prop => {
val subObject = prop._2
val propertyName = containers.contains(subObject.`type`) match {
case true => subObject.items match {
case Some(subItem) => {
Option(subItem.ref.getOrElse(subItem.`type`)) match {
case Some(sn) => Some(sn)
case _ => None
@tailrec def recurseModel(properties: List[(String, ModelProperty)], allModels: Map[String, Model], subNames: Set[String]): Set[String] = {
properties match {
case Nil => subNames
case (_, subObject) :: rest =>
val propertyName = if (containers.contains(subObject.`type`)) {
subObject.items flatMap { subItem =>
Option(subItem.ref.getOrElse(subItem.`type`))
}
} else Option((subObject.`type`))
if (propertyName.isDefined && !subNames.contains(propertyName.get)) {
val prop = propertyName.get
if (allModels.contains(prop)) {
recurseModel(allModels(prop).properties.toList, allModels, subNames + prop)
} else {
recurseModel(rest, allModels, subNames + prop)
}
case _ => None
} else recurseModel(rest, allModels, subNames)
}
case false => Some(subObject.`type`)
}
propertyName match {
case Some(property) => subNames.contains(property) match {
case false => {
allModels.containsKey(property) match {
case true => {
recurseModel(allModels(property), allModels, subNames)
}
case false =>
}
subNames += property
}
case true =>
}
case None =>
}
})
}
}