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

com.commercetools.rmf.diff.DiffSetup.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.diff

import com.commercetools.rmf.validators.Validator
import com.ctc.wstx.stax.WstxInputFactory
import com.ctc.wstx.stax.WstxOutputFactory
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.dataformat.xml.XmlFactory
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.google.common.reflect.ClassPath
import com.google.common.reflect.ClassPath.ClassInfo
import io.vrap.rmf.raml.validation.RamlValidator
import java.io.File
import java.io.InputStream
import kotlin.jvm.internal.Reflection


class DiffSetup {
    companion object {
        @JvmStatic
        fun setup(config: File): List> {
            return setup(config.inputStream())
        }

        @JvmStatic
        fun setup(config: InputStream): List> {
            val mapper = XmlMapper.builder(XmlFactory(WstxInputFactory(), WstxOutputFactory())).defaultUseWrapper(false)
                .addModule(KotlinModule.Builder().build())
                .enable(SerializationFeature.INDENT_OUTPUT)
                .enable(SerializationFeature.WRAP_ROOT_VALUE)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
                .build()
            val sets = readSets()

            val ruleSet = mapper.readValue(config, DiffConfiguration::class.java)

            var checks = mapOf()
            ruleSet.apply.forEach { apply ->
                run {
                    checks = mergeCheck(checks, sets[apply.set]!!)
                }
            }
            checks = mergeCheck(checks, ruleSet.checks.map { it.name to it }.toMap())

            return checks.values.map { rule -> Class.forName(rule.name).getConstructor(CheckSeverity::class.java).newInstance(rule.severity) as Differ }
        }

        private fun mergeCheck(setOne: Map, setTwo: Map): Map {
            var checks = setOne
            setTwo.forEach { (checkName, check) ->
                if (checks.containsKey(checkName)) {
                    checks = checks.plus(checkName to Check(checkName, check.severity, setOne[checkName]!!.options.plus(check.options)))
                } else {
                    checks = checks.plus(checkName to check)
                }
            }

            return checks
        }

        private fun readSets(): Map> {
            return ClassPath.from(ClassLoader.getSystemClassLoader()).allClasses
                .filter { it.packageName.startsWith("com.commercetools.rmf.diff") }
                .map { classInfo -> classInfo.load() }
                .filterNot { it.getAnnotation(DiffSet::class.java) == null && it.getAnnotation(DiffSets::class.java) == null }
                .filter { DiffCheck::class.java.isAssignableFrom(it) }
                .flatMap { clazz ->
                    clazz.getAnnotationsByType(DiffSet::class.java)
                        .map { it.name to Check(clazz.name, it.severity, listOf()) }
                }
                .groupBy { it.first }
                .mapValues { it.value.map { it.second.name to it.second }.toMap() };
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy