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

de.lancom.openapi.entity.EntityDescriptor.kt Maven / Gradle / Ivy

Go to download

This open-source project provides an OpenAPI 3.0 Parser implemented in Kotlin, utilizing immutable data classes

There is a newer version: 2.1.1
Show newest version
package de.lancom.openapi.entity

import com.fasterxml.jackson.databind.JsonNode
import de.lancom.openapi.field.Field
import de.lancom.openapi.field.getFields
import de.lancom.openapi.refs.Reference

tailrec fun Any?.flatten(): List {
    val flat = when (this) {
        null ->
            emptyList()

        is List ->
            flatMap { entry ->
                @Suppress("NON_TAIL_RECURSIVE_CALL")
                entry.flatten()
            }

        is Map<*, *> ->
            values.toList()

        is Collection<*> ->
            toList()

        is Iterable<*> ->
            toList()

        is Iterator<*> ->
            asSequence().toList()

        is Sequence<*> ->
            toList()

        is Field<*> ->
            if (isDefined) {
                listOf(orNull)
            } else {
                emptyList()
            }

        else ->
            return listOf(this)
    }

    return if (flat == this || flat.isEmpty()) {
        flat
    } else {
        flat.flatten()
    }
}

data class EntityDescriptor(
    val entity: Entity,
    val jsonNode: Field?,
    val map: Map>,
    val flatMap: List?>>,
    val flatten: List>,
) {
    val entityMap: Map by lazy {
        val jsonMap: Map = map.getFields()
        val jsonMapFlat: Map = flatMap.getFields()
            .filterNotNull()
            .flatMap(Map::toList)
            .toMap()
        val flattenMap: Map = flatten
            .getFields()
            .filterIsInstance>()
            .flatMap(Map::toList)
            .toMap()
        jsonMap + jsonMapFlat + flattenMap
    }

    val isFlat: Boolean by lazy {
        val f = flatten.flatten()
        f.isNotEmpty() && !f.any { any ->
            any is Map<*, *> || any is Entity
        }
    }

    val subEntities: Set by lazy {
        listOf(
            map,
            flatMap,
            flatten,
        ).flatten().filterIsInstance().toSet()
    }

    val subEntitiesRecursive: Set by lazy {
        setOf(entity) + subEntities.flatMap { entity ->
            entity.entityDescriptor.subEntitiesRecursive
        }.toSet()
    }

    val references: Set> by lazy {
        subEntitiesRecursive.filterIsInstance>().toSet()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy