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

com.commercetools.rmf.validators.FilenameRule.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-20241120142200
Show newest version
package com.commercetools.rmf.validators

import io.vrap.rmf.nodes.antlr.NodeToken
import io.vrap.rmf.raml.model.modules.TypeContainer
import io.vrap.rmf.raml.model.types.AnyType
import io.vrap.rmf.raml.persistence.antlr.RAMLParser
import io.vrap.rmf.raml.persistence.constructor.RamlParserAdapter
import org.antlr.v4.runtime.ParserRuleContext
import org.antlr.v4.runtime.tree.TerminalNode
import org.eclipse.emf.common.util.Diagnostic
import java.util.*
import kotlin.collections.ArrayList

@ValidatorSet
class FilenameRule(severity: RuleSeverity, options: List? = null) : ModulesRule(severity, options) {
    private val exclude: List =
        (options?.filter { ruleOption -> ruleOption.type.lowercase(Locale.getDefault()) == RuleOptionType.EXCLUDE.toString() }?.map { ruleOption -> ruleOption.value }?.plus("") ?: defaultExcludes)

    override fun caseTypeContainer(container: TypeContainer): List {
        val validationResults: MutableList = ArrayList()

        if (container.types != null) {
            container.types
                .filterNot { exclude.contains(it.name) }
                .forEach {
                    val r = it?.eAdapters()?.filterIsInstance(RamlParserAdapter::class.java)?.map {
                            adapter -> checkIncludedTypeFileName(it, adapter.parserRuleContext)
                    } ?: emptyList()
                    if (r.contains(true).not()) {
                        validationResults.add(create(it, "Type \"{0}\" must have the same file name as type itself", it.name))
                    }
                }
        }

        return validationResults
    }

    private fun checkIncludedTypeFileName(type: AnyType, context: ParserRuleContext): Boolean {
        if (context is RAMLParser.TypeDeclarationMapContext) {
            val id = context.name
            val idIndex = context.children.indexOf(context.name)
            val nextNode = context.children.getOrNull(idIndex + 1)
            if (nextNode is TerminalNode && (nextNode.symbol as NodeToken).location != (id.start as NodeToken).location) {
                return (nextNode.symbol as NodeToken).location.contains(type.name + ".raml")
            }
        }

        return true
    }

    companion object : ValidatorFactory {
        private val defaultExcludes by lazy { listOf("") }

        @JvmStatic
        override fun create(options: List?): FilenameRule {
            return FilenameRule(RuleSeverity.ERROR, options)
        }

        @JvmStatic
        override fun create(severity: RuleSeverity, options: List?): FilenameRule {
            return FilenameRule(severity, options)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy