io.vrap.codegen.languages.extensions.MethodExtensions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of codegen-renderers Show documentation
Show all versions of codegen-renderers Show documentation
RAML API client code generators based on the REST Modeling Framework. https://github.com/vrapio/rest-modeling-framework
package io.vrap.codegen.languages.extensions
import com.damnhandy.uri.template.Expression
import com.damnhandy.uri.template.UriTemplate
import io.vrap.rmf.codegen.firstUpperCase
import io.vrap.rmf.raml.model.resources.Method
import io.vrap.rmf.raml.model.resources.Resource
import io.vrap.rmf.raml.model.responses.Response
import io.vrap.rmf.raml.model.types.AnyType
import io.vrap.rmf.raml.model.types.BooleanInstance
import io.vrap.rmf.raml.model.types.NilType
import io.vrap.rmf.raml.model.types.impl.TypesFactoryImpl
import io.vrap.rmf.raml.model.util.StringCaseFormat
import java.util.stream.Collectors
fun Method.toRequestName(): String {
return this.resource().fullUri.toParamName("By") + this.method.toString().firstUpperCase()
}
fun UriTemplate.toParamName(delimiter: String): String {
return this.toParamName(delimiter, "")
}
fun UriTemplate.toParamName(delimiter: String, suffix: String): String {
return this.components.stream().map { uriTemplatePart ->
if (uriTemplatePart is Expression) {
return@map uriTemplatePart.varSpecs.stream()
.map { s -> delimiter + s.variableName.firstUpperCase() + suffix }.collect(Collectors.joining())
}
StringCaseFormat.UPPER_CAMEL_CASE.apply(uriTemplatePart.toString().replace("/", "-"))
}.collect(Collectors.joining()).replace("[^\\p{L}\\p{Nd}]+".toRegex(), "").firstUpperCase()
}
fun Method.resource(): Resource = this.eContainer() as Resource
fun Method.returnType(): AnyType {
return this.responses
.filter { it.isSuccessfull() }
.filter { it.bodies?.isNotEmpty() ?: false }
.firstOrNull()
?.let { it.bodies[0].type }
?: TypesFactoryImpl.eINSTANCE.createNilType()
}
fun Method.hasReturnPayload(): Boolean = this.returnType() !is NilType
fun Method.hasBody(): Boolean = this.bodies.filter { it.type != null }.isNotEmpty()
fun Method.hasQueryParams() = this.queryParameters.isNotEmpty()
fun Method.hasPathParams() = this.resource().fullUri.variables.isNotEmpty()
fun Response.isSuccessfull(): Boolean = this.statusCode.toInt() in (200..299)
fun Method.markDeprecated() : Boolean {
val anno = this.getAnnotation("markDeprecated")
return (anno != null && (anno.value as BooleanInstance).value)
}
fun Method.deprecated() : Boolean {
val anno = this.getAnnotation("deprecated")
return (anno != null && (anno.value as BooleanInstance).value)
}