https://kotlinlang.org logo
#konsist
Title
# konsist
i

igor.wojda

09/07/2023, 6:08 AM
I need API advice for new Konsist feature. Assuming we have this code snippet...
Copy code
@SampleAnnotation("some_text", 1)
...and Konsist that parses it and exposes to the developer.
Copy code
...
   .annotations
   .arguments
   .map { it.value }
How the first argument be represented in the list of arguments? • 🅰️
""some_text"", "1"
🅱️
"some_text", "1"
Please vote with emojis
🅱️ 4
e

Eran Boudjnah

09/07/2023, 10:57 AM
Is this an ArrayList of strings? Do we lose the integer type of the second argument?
i

igor.wojda

09/07/2023, 10:59 AM
Yes. This is the dilemma here.
e

Eran Boudjnah

09/07/2023, 11:00 AM
Can't it be kept as an integer? If there's no way to keep it as an integer, without the quotes we lose valuable information. I wonder if everyone who voted B was clear on that.
i

igor.wojda

09/07/2023, 11:01 AM
This is actually a good point - array could store different types
👍 1
e

Eric

09/08/2023, 7:16 PM
Why not just
List<Any>
? then you can do
arguments[1] as? Integer
checks etc
I also added an ext fun for checking my annotations:
Copy code
inline fun <reified A : Any> KoAnnotationProvider.hasAnnotation(predicate: (KoAnnotationDeclaration) -> Boolean) =
    hasAnnotationsOf(A::class) && predicate(annotations.first { it.fullyQualifiedName == A::class.java.name })
then I can use it like this:
Copy code
@Test
    fun `classes annotated with @RestController should produce application json`() {
        classesAnnotatedWith<RestController>()
            .assert { controller ->
                controller.hasAnnotation<RequestMapping> { requestMapping ->
                    requestMapping.text.contains("APPLICATION_JSON_VALUE")
                }
            }
    }