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

com.ancientlightstudios.quarkus.kotlin.openapi.emitter.statements.OneOfBuilderDeserializationStatement.kt Maven / Gradle / Ivy

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

import com.ancientlightstudios.quarkus.kotlin.openapi.emitter.CodeWriter
import com.ancientlightstudios.quarkus.kotlin.openapi.models.kotlin.KotlinStatement
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.name.ClassName
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.name.ClassName.Companion.className
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.name.VariableName
import com.ancientlightstudios.quarkus.kotlin.openapi.models.transformed.typedefinition.TypeDefinitionUsage

class OneOfBuilderDeserializationStatement(val objectName: ClassName) : KotlinStatement {

    private val parameters = mutableListOf>()

    fun addParameter(maybeName: VariableName, innerTypeDefinition:TypeDefinitionUsage) {
        parameters.add(maybeName to innerTypeDefinition)
    }

    override fun render(writer: CodeWriter) = with(writer) {
        write("return maybeOneOf(context")
        parameters.forEach { (variableName, _) ->
            write(", ${variableName.render()}")
        }
        write(") ")
        block(newLineAfter = true) {
            parameters.forEach { (variableName, innerTypeDefinition) ->
                writeln("${variableName.render()}.validValueOrNull()?.let { return@maybeOneOf ${objectName.extend(postfix = innerTypeDefinition.safeType.className().render()).render()}(it) }")
            }
            writeln("throw IllegalStateException(\"at least one option should be available\")")
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy