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

se.ansman.kotshi.kapt.KotlinPoetExt.kt Maven / Gradle / Ivy

Go to download

An annotations processor that generates Moshi adapters from Kotlin data classes

There is a newer version: 3.0.0
Show newest version
package se.ansman.kotshi.kapt

import com.squareup.kotlinpoet.AnnotationSpec
import com.squareup.kotlinpoet.ClassName
import com.squareup.kotlinpoet.tag
import com.squareup.moshi.JsonQualifier
import se.ansman.kotshi.JSON_UNSET_NAME
import se.ansman.kotshi.Types
import se.ansman.kotshi.kapt.AnnotationModelValueVisitor.Companion.toAnnotationModel
import se.ansman.kotshi.model.AnnotationModel
import javax.lang.model.element.AnnotationMirror


fun List.find(typeName: ClassName): AnnotationSpec? = find { it.typeName == typeName }

fun List.jsonName(): String? =
    (find(Types.Kotshi.jsonProperty) ?: find(Types.Moshi.json))?.let { spec ->
        requireNotNull(spec.tag()).getValueOrNull("name")
            ?.takeUnless { it == JSON_UNSET_NAME }
    }

fun List.isJsonIgnore(): Boolean? =
    find(Types.Moshi.json)?.let { spec ->
        requireNotNull(spec.tag()).getValueOrNull("ignore")
    }

fun List.qualifiers(metadataAccessor: MetadataAccessor): Set =
    mapNotNullTo(mutableSetOf()) { spec ->
        val mirror = requireNotNull(spec.tag())
        if (mirror.annotationType.asElement().getAnnotation(JsonQualifier::class.java) == null) {
            return@mapNotNullTo null
        }
        mirror.toAnnotationModel(metadataAccessor)
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy