Skip to content

Commit

Permalink
Revert "Using Map<String,String> type for modelMapping instead of Lis…
Browse files Browse the repository at this point in the history
…t<Pair<String, String>>"

This reverts commit 1eb04b0.
  • Loading branch information
ashwini-desai committed Jul 9, 2020
1 parent dba12f6 commit 676bf4b
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions codegen/src/main/kotlin/apifi/codegen/ApiBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object ApiBuilder {

private const val micronautHttpAnnotation = "io.micronaut.http.annotation"

fun build(name: String, paths: List<Path>, basePackageName: String, modelMapping: Map<String, String>): FileSpec {
fun build(name: String, paths: List<Path>, basePackageName: String, modelMapping: List<Pair<String, String>>): FileSpec {
val baseName = toTitleCase(name)
val controllerClassName = "${baseName}Api"

Expand All @@ -36,7 +36,7 @@ object ApiBuilder {
return FileSpec.builder(basePackageName, "$controllerClassName.kt").addType(classSpec.build()).addType(controllerInterfaceClass).build()
}

private fun generateOperationFunctions(paths: List<Path>, basePackageName: String, modelMapping: Map<String, String>): List<FunSpec> {
private fun generateOperationFunctions(paths: List<Path>, basePackageName: String, modelMapping: List<Pair<String, String>>): List<FunSpec> {
return paths.flatMap { path ->
path.operations?.map { operation ->
val serviceCallStatement = controllerCallStatement(operation, modelMapping)
Expand Down
4 changes: 2 additions & 2 deletions codegen/src/main/kotlin/apifi/codegen/CodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.squareup.kotlinpoet.TypeSpec

object CodeGenerator {
fun generate(spec: Spec, basePackageName: String): List<FileSpec> {
val modelFiles: List<FileSpec> = if (spec.models.isNotEmpty()) listOf(ModelFileBuilder.build(spec.models, basePackageName)) else emptyList()
val modelMapping = modelFiles.flatMap { it.members.mapNotNull { m -> (m as TypeSpec).name }.map { name -> name to "${it.packageName}.$name" } }.toMap()
val modelFiles: List<FileSpec> = if(spec.models.isNotEmpty()) listOf(ModelFileBuilder.build(spec.models, basePackageName)) else emptyList()
val modelMapping = modelFiles.flatMap { it.members.mapNotNull { m -> (m as TypeSpec).name }.map { name -> name to "${it.packageName}.$name" } }

val apiGroups = spec.paths.groupBy { it.operations?.firstOrNull { o -> o.tags != null }?.tags?.firstOrNull() }.filter { it.key != null }

Expand Down
2 changes: 1 addition & 1 deletion codegen/src/main/kotlin/apifi/codegen/ModelFileBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.squareup.kotlinpoet.*
object ModelFileBuilder {
fun build(models: List<Model>, basePackageName: String): FileSpec {
val packageName = "$basePackageName.models"
val baseModelMapping = models.map { it.name to "$packageName.${it.name}" }.toMap()
val baseModelMapping = models.map { it.name to "$packageName.${it.name}" }
val builder = FileSpec.builder(packageName, "Models.kt")
models.forEach { model ->
builder.addType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.ParameterSpec

object RequestBodyBuilder {
fun build(bodyType: String, modelMapping: Map<String, String>): ParameterSpec =
fun build(bodyType: String, modelMapping: List<Pair<String, String>>): ParameterSpec =
ParameterSpec.builder("body", bodyType.toKotlinPoetType(modelMapping))
.addAnnotation(ClassName("io.micronaut.http.annotation", "Body"))
.build()
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/main/kotlin/apifi/helpers/Functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ fun toCamelCase(s: String): String = CaseUtils.toCamelCase(s, false, '_', '-', '
fun <T> Schema<T>.toCodeGenModel(name: String): CodegenModel = KotlinClientCodegen().fromModel(name, this, null)
fun <T> Schema<T>.toCodeGenModel(): CodegenModel = this.toCodeGenModel("any")

fun String.toKotlinPoetType(): TypeName = toKotlinPoetType(emptyMap())
fun String.toKotlinPoetType(): TypeName = toKotlinPoetType(emptyList())

fun String.toKotlinPoetType(packageNameMapping: Map<String, String>): TypeName {
val withPackage = { name: String -> ClassName.bestGuess(packageNameMapping[name] ?: name) }
fun String.toKotlinPoetType(packageNameMapping: List<Pair<String, String>>): TypeName {
val withPackage = { name: String -> ClassName.bestGuess(packageNameMapping.find { name == it.first }?.second ?: name) }
return if (this.contains("<")) {
val parts = this.split('<')
val primaryType = parts[0]
Expand Down
2 changes: 1 addition & 1 deletion codegen/src/test/kotlin/apifi/codegen/ApiBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ class ApiBuilderTest : DescribeSpec({

})

fun modelMapping() = mapOf("Pet" to "models.Pet", "PetResponse" to "models.PetResponse")
fun modelMapping() = listOf("Pet" to "models.Pet", "PetResponse" to "models.PetResponse")
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class RequestBodyBuilderTest : DescribeSpec( {

describe("Request Body Builder") {
it("should generate request body type") {
val requestBody = RequestBodyBuilder.build("Pet", mapOf("Pet" to "models.Pet"))
val requestBody = RequestBodyBuilder.build("Pet", listOf("Pet" to "models.Pet"))
requestBody.toString() shouldBe "@io.micronaut.http.annotation.Body body: models.Pet"
}
}
Expand Down

0 comments on commit 676bf4b

Please sign in to comment.