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

com.ancientlightstudios.quarkus.kotlin.openapi.emitter.deserialization.CombineIntoObjectStatementEmitter.kt Maven / Gradle / Ivy

There is a newer version: 0.4.14
Show newest version
package com.ancientlightstudios.quarkus.kotlin.openapi.emitter.deserialization

import com.ancientlightstudios.quarkus.kotlin.openapi.emitter.CodeEmitter
import com.ancientlightstudios.quarkus.kotlin.openapi.emitter.EmitterContext
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.*
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.MethodName.Companion.rawMethodName
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.PropertyExpression.Companion.property
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.TypeName.SimpleTypeName.Companion.typeName
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.VariableName.Companion.variableName

class CombineIntoObjectStatementEmitter(
    private val context: KotlinExpression,
    private val containerClassName: ClassName,
    private val plainPrefixParameterNames: List,
    private val parameterNames: List
) : CodeEmitter {

    var resultStatement: KotlinExpression? = null

    // if at least one parameter is specified, generates an expression like this
    //
    // maybeAllOf(, ) {
    //     (( as Maybe.Success).value)
    // }
    //
    // there is a cast expression for every input parameter in the ctor invocation
    override fun EmitterContext.emit() {
        // TODO:  eeds rework, this test might not work in all situation
        if (plainPrefixParameterNames.isEmpty() && parameterNames.isEmpty()) {
            return
        }

        val parameterExpressions = parameterNames.map {
            it.cast(Library.MaybeSuccessClass.typeName()).property("value".variableName())
        }

        val maybeParameters = listOf(context, *parameterNames.toTypedArray())
        resultStatement = InvocationExpression.invoke("maybeAllOf".rawMethodName(), maybeParameters) {
            InvocationExpression.invoke(containerClassName.constructorName, plainPrefixParameterNames + parameterExpressions).statement()
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy