<commit_before>package io.aconite.server

import io.aconite.ArgumentMissingException
import io.aconite.annotations.*
import io.aconite.utils.UrlTemplate
import io.aconite.utils.asyncCall
import io.aconite.utils.resolve
import kotlin.reflect.*
import kotlin.reflect.full.functions

private val PARAM_ANNOTATIONS = listOf(
        Body::class,
        Header::class,
        Path::class,
        Query::class
)

private val METHOD_ANNOTATION = listOf(
        MODULE::class,
        DELETE::class,
        GET::class,
        HEAD::class,
        OPTIONS::class,
        PATCH::class,
        POST::class,
        PUT::class
)

abstract class AbstractHandler : Comparable<AbstractHandler> {
    abstract val argsCount: Int
    abstract suspend fun accept(obj: Any, url: String, request: Request): Response?
    final override fun compareTo(other: AbstractHandler) = argsCount.compareTo(other.argsCount)
}

class MethodHandler(server: AconiteServer, private val method: String, private val fn: KFunction<*>) : AbstractHandler() {
    private val args = transformParams(server, fn)
    private val responseSerializer = responseSerializer(server, fn)
    override val argsCount = args.size

    override suspend fun accept(obj: Any, url: String, request: Request): Response? {
        if (request.method != method) return null
        val result = fn.httpCall(args, obj, request)
        return Response(body = responseSerializer.serialize(result))
    }
}

class ModuleHandler(server: AconiteServer, iface: KType, fn: KFunction<*>): AbstractHandler() {
    private val fn = resolve(iface, fn)
    private val args = transformParams(server, fn)
    private val routers = buildRouters(server, iface)
    override val argsCount = args.size

    override suspend fun accept(obj: Any, url: String, request: Request): Response? {
        val nextObj = fn.httpCall(args, obj, request)
        for (router in routers)
            return router.accept(nextObj!!, url, request) ?: continue
        return null
    }
}

class RootHandler(server: AconiteServer, private val obj: Any, iface: KType) {
    private val routers = buildRouters(server, iface)

    suspend fun accept(url: String, request: Request): Response? {
        for (router in routers)
            return router.accept(obj, url, request) ?: continue
        return null
    }
}

private fun buildRouters(server: AconiteServer, iface: KType): List<Router> {
    val cls = iface.cls()
    val allHandlers = hashMapOf<String, MutableList<AbstractHandler>>()

    for (fn in cls.functions) {
        if (fn.isOpen) continue // FIXME: simple solution for filter out functions from 'Any' class
        if (!server.methodFilter.predicate(fn)) continue
        val adapted = adaptFunction(server, fn)
        val (url, method) = adapted.getHttpMethod()
        val urlHandlers = allHandlers.computeIfAbsent(url) { ArrayList() }
        val handler = when (method) {
            null -> ModuleHandler(server, adapted.asyncReturnType(), adapted)
            else -> MethodHandler(server, method, adapted)
        }
        urlHandlers.add(handler)<commit_msg>Add forgotten call to resolve function in ModuleHandler<commit_after>package io.aconite.server

import io.aconite.ArgumentMissingException
import io.aconite.annotations.*
import io.aconite.utils.UrlTemplate
import io.aconite.utils.asyncCall
import io.aconite.utils.resolve
import kotlin.reflect.*
import kotlin.reflect.full.functions

private val PARAM_ANNOTATIONS = listOf(
        Body::class,
        Header::class,
        Path::class,
        Query::class
)

private val METHOD_ANNOTATION = listOf(
        MODULE::class,
        DELETE::class,
        GET::class,
        HEAD::class,
        OPTIONS::class,
        PATCH::class,
        POST::class,
        PUT::class
)

abstract class AbstractHandler : Comparable<AbstractHandler> {
    abstract val argsCount: Int
    abstract suspend fun accept(obj: Any, url: String, request: Request): Response?
    final override fun compareTo(other: AbstractHandler) = argsCount.compareTo(other.argsCount)
}

class MethodHandler(server: AconiteServer, private val method: String, private val fn: KFunction<*>) : AbstractHandler() {
    private val args = transformParams(server, fn)
    private val responseSerializer = responseSerializer(server, fn)
    override val argsCount = args.size

    override suspend fun accept(obj: Any, url: String, request: Request): Response? {
        if (request.method != method) return null
        val result = fn.httpCall(args, obj, request)
        return Response(body = responseSerializer.serialize(result))
    }
}

class ModuleHandler(server: AconiteServer, iface: KType, fn: KFunction<*>): AbstractHandler() {
    private val fn = resolve(iface, fn)
    private val args = transformParams(server, fn)
    private val routers = buildRouters(server, iface)
    override val argsCount = args.size

    override suspend fun accept(obj: Any, url: String, request: Request): Response? {
        val nextObj = fn.httpCall(args, obj, request)
        for (router in routers)
            return router.accept(nextObj!!, url, request) ?: continue
        return null
    }
}

class RootHandler(server: AconiteServer, private val obj: Any, iface: KType) {
    private val routers = buildRouters(server, iface)

    suspend fun accept(url: String, request: Request): Response? {
        for (router in routers)
            return router.accept(obj, url, request) ?: continue
        return null
    }
}

private fun buildRouters(server: AconiteServer, iface: KType): List<Router> {
    val cls = iface.cls()
    val allHandlers = hashMapOf<String, MutableList<AbstractHandler>>()

    for (fn in cls.functions) {
        val resolved = resolve(iface, fn)
        if (resolved.isOpen) continue // FIXME: simple solution for filter out functions from 'Any' class
        if (!server.methodFilter.predicate(resolved)) continue
        val adapted = adaptFunction(server, resolved)
        val (url, method) = adapted.getHttpMethod()
        val urlHandlers = allHandlers.computeIfAbsent(url) { ArrayList() }
        val handler = when (method) {
            null -> ModuleHandler(server, adapted.asyncReturnType(), adapted)
            else -> MethodHandler(server, method, adapted)
        }
        urlHandlers.add(handler)
{'fraction_non_alphanumeric': 0.09961928934010152, 'fraction_numerical': 0.0, 'mean_word_length': 3.375433726578765, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7051844', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1747, 'n_words': 516}