de.lancom.openapi.entity.EntityDescriptor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openapi-parser Show documentation
Show all versions of openapi-parser Show documentation
This open-source project provides an OpenAPI 3.0 Parser implemented in Kotlin, utilizing immutable data classes
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