-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ktor server routes and controller interfaces
- Loading branch information
1 parent
cbb86bd
commit eab370d
Showing
17 changed files
with
1,813 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
.../com/cjbooms/fabrikt/generators/controller/AnnotationBasedControllerInterfaceGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.cjbooms.fabrikt.generators.controller | ||
|
||
import com.cjbooms.fabrikt.configurations.Packages | ||
import com.cjbooms.fabrikt.generators.ValidationAnnotations | ||
import com.cjbooms.fabrikt.model.ControllerType | ||
import com.cjbooms.fabrikt.model.RequestParameter | ||
import com.cjbooms.fabrikt.model.SourceApi | ||
import com.cjbooms.fabrikt.util.KaizenParserExtensions.basePath | ||
import com.cjbooms.fabrikt.util.toUpperCase | ||
import com.reprezen.kaizen.oasparser.model3.Operation | ||
import com.reprezen.kaizen.oasparser.model3.Path | ||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.ParameterSpec | ||
import com.squareup.kotlinpoet.TypeSpec | ||
|
||
abstract class AnnotationBasedControllerInterfaceGenerator( | ||
private val packages: Packages, | ||
private val api: SourceApi, | ||
private val validationAnnotations: ValidationAnnotations, | ||
) { | ||
abstract fun buildFunction(path: Path, op: Operation, verb: String): FunSpec | ||
|
||
abstract fun controllerBuilder(className: String, basePath: String): TypeSpec.Builder | ||
|
||
fun buildController(resourceName: String, paths: Collection<Path>): ControllerType { | ||
val typeBuilder: TypeSpec.Builder = controllerBuilder( | ||
className = ControllerGeneratorUtils.controllerName(resourceName), | ||
basePath = api.openApi3.basePath() | ||
) | ||
|
||
paths.flatMap { path -> | ||
path.operations | ||
.filter { it.key.toUpperCase() != "HEAD" } | ||
.map { op -> | ||
buildFunction( | ||
path, | ||
op.value, | ||
op.key, | ||
) | ||
} | ||
}.forEach { typeBuilder.addFunction(it) } | ||
|
||
return ControllerType( | ||
typeBuilder.build(), | ||
packages.base | ||
) | ||
} | ||
|
||
fun ParameterSpec.Builder.addValidationAnnotations(parameter: RequestParameter): ParameterSpec.Builder { | ||
if (parameter.minimum != null) this.maybeAddAnnotation(validationAnnotations.min(parameter.minimum.toInt())) | ||
if (parameter.maximum != null) this.maybeAddAnnotation(validationAnnotations.max(parameter.maximum.toInt())) | ||
if (parameter.typeInfo.isComplexType) this.maybeAddAnnotation(validationAnnotations.parameterValid()) | ||
return this | ||
} | ||
|
||
fun ParameterSpec.Builder.maybeAddAnnotation(annotation: AnnotationSpec?) = | ||
if (annotation != null) this.addAnnotation(annotation) else this | ||
} |
58 changes: 4 additions & 54 deletions
58
src/main/kotlin/com/cjbooms/fabrikt/generators/controller/ControllerInterfaceGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,9 @@ | ||
package com.cjbooms.fabrikt.generators.controller | ||
|
||
import com.cjbooms.fabrikt.configurations.Packages | ||
import com.cjbooms.fabrikt.generators.ValidationAnnotations | ||
import com.cjbooms.fabrikt.model.ControllerType | ||
import com.cjbooms.fabrikt.model.ControllerLibraryType | ||
import com.cjbooms.fabrikt.model.KotlinTypes | ||
import com.cjbooms.fabrikt.model.RequestParameter | ||
import com.cjbooms.fabrikt.model.SourceApi | ||
import com.cjbooms.fabrikt.util.KaizenParserExtensions.basePath | ||
import com.cjbooms.fabrikt.util.toUpperCase | ||
import com.reprezen.kaizen.oasparser.model3.Operation | ||
import com.reprezen.kaizen.oasparser.model3.Path | ||
import com.squareup.kotlinpoet.AnnotationSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.ParameterSpec | ||
import com.squareup.kotlinpoet.TypeSpec | ||
|
||
abstract class ControllerInterfaceGenerator( | ||
private val packages: Packages, | ||
private val api: SourceApi, | ||
private val validationAnnotations: ValidationAnnotations, | ||
) { | ||
abstract fun generate(): KotlinTypes | ||
abstract fun buildFunction(path: Path, op: Operation, verb: String): FunSpec | ||
abstract fun controllerBuilder(className: String, basePath: String): TypeSpec.Builder | ||
fun buildController(resourceName: String, paths: Collection<Path>): ControllerType { | ||
val typeBuilder: TypeSpec.Builder = controllerBuilder( | ||
className = ControllerGeneratorUtils.controllerName(resourceName), | ||
basePath = api.openApi3.basePath() | ||
) | ||
|
||
paths.flatMap { path -> | ||
path.operations | ||
.filter { it.key.toUpperCase() != "HEAD" } | ||
.map { op -> | ||
buildFunction( | ||
path, | ||
op.value, | ||
op.key, | ||
) | ||
} | ||
}.forEach { typeBuilder.addFunction(it) } | ||
|
||
return ControllerType( | ||
typeBuilder.build(), | ||
packages.base | ||
) | ||
} | ||
|
||
fun ParameterSpec.Builder.addValidationAnnotations(parameter: RequestParameter): ParameterSpec.Builder { | ||
if (parameter.minimum != null) this.maybeAddAnnotation(validationAnnotations.min(parameter.minimum.toInt())) | ||
if (parameter.maximum != null) this.maybeAddAnnotation(validationAnnotations.max(parameter.maximum.toInt())) | ||
if (parameter.typeInfo.isComplexType) this.maybeAddAnnotation(validationAnnotations.parameterValid()) | ||
return this | ||
} | ||
|
||
fun ParameterSpec.Builder.maybeAddAnnotation(annotation: AnnotationSpec?) = | ||
if (annotation != null) this.addAnnotation(annotation) else this | ||
interface ControllerInterfaceGenerator { | ||
fun generate(): KotlinTypes | ||
fun generateLibrary(): Collection<ControllerLibraryType> | ||
} |
Oops, something went wrong.