All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vrap.codegen.languages.extensions.MethodExtensions.kt Maven / Gradle / Ivy

Go to download

RAML API client code generators based on the REST Modeling Framework. https://github.com/vrapio/rest-modeling-framework

There is a newer version: 1.0.0-20240902110655
Show newest version
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)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy