Update things
This commit is contained in:
@@ -4,26 +4,42 @@ package be.vandewalleh.aoc.utils.input
|
||||
|
||||
import io.micronaut.context.annotation.Prototype
|
||||
import io.micronaut.core.annotation.Introspected
|
||||
import javax.inject.Qualifier
|
||||
import jakarta.inject.Qualifier
|
||||
import java.lang.annotation.Inherited
|
||||
|
||||
@Prototype
|
||||
@Qualifier
|
||||
@Introspected
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Day(val value: Int)
|
||||
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class DayInput
|
||||
|
||||
@DayInput
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Csv
|
||||
|
||||
@DayInput
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Text
|
||||
|
||||
@DayInput
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Lines
|
||||
|
||||
@DayInput
|
||||
@Qualifier
|
||||
@Prototype
|
||||
@Inherited
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Groups
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package be.vandewalleh.aoc.utils.input
|
||||
|
||||
/**
|
||||
* Wrapper class so that micronaut is not confused with an other injectable
|
||||
* container type when using an iterable / array
|
||||
*/
|
||||
data class Input<T>(val value: T)
|
||||
@@ -6,75 +6,39 @@ import kotlin.io.path.ExperimentalPathApi
|
||||
import kotlin.io.path.readLines
|
||||
import kotlin.io.path.readText
|
||||
|
||||
/**
|
||||
* Load challenge inputs and convert them to the appropriate format
|
||||
*
|
||||
* contains horrible hacks while waiting for generic support in bean injection
|
||||
* @see [micronaut-2775](https://github.com/micronaut-projects/micronaut-core/issues/2775)
|
||||
*/
|
||||
@Factory
|
||||
@ExperimentalPathApi
|
||||
class InputFactory(private val resourceLoader: ResourceLoader) {
|
||||
|
||||
@Csv
|
||||
fun csv(injectionPoint: InjectionPoint<*>): Input<*> =
|
||||
when (val param = injectionPoint.typeNameOfAnnotation<Csv>()) {
|
||||
INT_ARRAY -> injectionPoint
|
||||
.read()
|
||||
.split(",")
|
||||
.map { it.toInt() }
|
||||
.toIntArray()
|
||||
.wrap()
|
||||
LONG_ARRAY -> injectionPoint
|
||||
.read()
|
||||
.split(",")
|
||||
.map { it.toLong() }
|
||||
.toLongArray()
|
||||
.wrap()
|
||||
else -> error("Unsupported type $param")
|
||||
}
|
||||
fun csvInts(injectionPoint: InjectionPoint<*>): IntArray =
|
||||
injectionPoint.csv().map { it.toInt() }.toIntArray()
|
||||
|
||||
@Csv
|
||||
fun csvLongs(injectionPoint: InjectionPoint<*>): LongArray =
|
||||
injectionPoint.csv().map { it.toLong() }.toLongArray()
|
||||
|
||||
private fun InjectionPoint<*>.csv() = read().split(",")
|
||||
|
||||
@Lines
|
||||
fun lines(injectionPoint: InjectionPoint<*>): Input<*> =
|
||||
when (val param = injectionPoint.typeNameOfAnnotation<Lines>()) {
|
||||
INT_ARRAY -> injectionPoint
|
||||
.lines()
|
||||
.map { it.toInt() }
|
||||
.toList()
|
||||
.toIntArray()
|
||||
.wrap()
|
||||
LONG_ARRAY -> injectionPoint
|
||||
.lines()
|
||||
.map { it.toLong() }
|
||||
.toList()
|
||||
.toLongArray()
|
||||
.wrap()
|
||||
STRING_LIST -> injectionPoint
|
||||
.lines()
|
||||
.toList()
|
||||
.wrap()
|
||||
else -> error("Unsupported type $param")
|
||||
}
|
||||
fun linesInts(injectionPoint: InjectionPoint<*>): IntArray =
|
||||
injectionPoint.lines().map { it.toInt() }.toList().toIntArray()
|
||||
|
||||
@Lines
|
||||
fun linesLongs(injectionPoint: InjectionPoint<*>): LongArray =
|
||||
injectionPoint.lines().map { it.toLong() }.toList().toLongArray()
|
||||
|
||||
@Lines
|
||||
fun lines(injectionPoint: InjectionPoint<*>): List<String> =
|
||||
injectionPoint.lines().toList()
|
||||
|
||||
@Text
|
||||
fun text(injectionPoint: InjectionPoint<*>): Input<String> =
|
||||
injectionPoint.read().wrap()
|
||||
fun text(injectionPoint: InjectionPoint<*>): String =
|
||||
injectionPoint.read()
|
||||
|
||||
@Groups
|
||||
fun groups(injectionPoint: InjectionPoint<*>): Input<List<List<String>>> =
|
||||
injectionPoint.read().split("\n\n").map { it.lines() }.wrap()
|
||||
|
||||
|
||||
private fun <T> T.wrap() = Input(this)
|
||||
|
||||
private inline fun <reified T : Annotation> InjectionPoint<*>.typeNameOfAnnotation() = declaringBean
|
||||
.constructor
|
||||
.arguments
|
||||
.find { it.hasAnnotation<T>() }
|
||||
?.typeName
|
||||
?.removePrefix("be.vandewalleh.aoc.utils.input.Input<")
|
||||
?.removeSuffix(">")
|
||||
?: error("??")
|
||||
fun groups(injectionPoint: InjectionPoint<*>): List<List<String>> =
|
||||
injectionPoint.read().split("\n\n").map { it.lines() }
|
||||
|
||||
private fun InjectionPoint<*>.path() = resourceLoader.ofDay(getDay(this))
|
||||
private fun InjectionPoint<*>.read() = path().readText().trim()
|
||||
@@ -93,10 +57,4 @@ class InputFactory(private val resourceLoader: ResourceLoader) {
|
||||
return dayAnnotation.get().intValue("value").asInt
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val INT_ARRAY = "int[]"
|
||||
private const val LONG_ARRAY = "long[]"
|
||||
private const val STRING_LIST = "java.util.List<java.lang.String>"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,3 @@ import java.util.*
|
||||
|
||||
internal inline fun <reified T : Annotation> AnnotationMetadataProvider.findAnnotation(): Optional<AnnotationValue<T>> =
|
||||
findAnnotation(T::class.java)
|
||||
|
||||
internal inline fun <reified T : Annotation> AnnotationMetadataProvider.hasAnnotation(): Boolean =
|
||||
findAnnotation(T::class.java).isPresent
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package be.vandewalleh.aoc.utils.input
|
||||
|
||||
import jakarta.inject.Singleton
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import javax.inject.Singleton
|
||||
|
||||
interface ResourceLoader {
|
||||
fun ofDay(day: Int): Path
|
||||
|
||||
Reference in New Issue
Block a user