starting with configuration module

main
Timo Bryant 2024-04-20 23:03:04 +02:00
parent 2ddeaf2b17
commit 6e46248301
10 changed files with 108 additions and 5 deletions

1
configuration/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
accounts.toml

View File

@ -0,0 +1,2 @@
[account.db-user]
user = "kinch"

View File

@ -0,0 +1,3 @@
[database]
jdbc = "jdbc:postgresql://localhost:5432/postgres"
user = "db-user"

View File

@ -6,6 +6,7 @@ slf4j = "2.0.12"
kotlin-logging = "6.0.4"
kotlinx = "1.7.3"
kotlin = "1.9.22"
hoplite = "2.7.5"
[libraries]
koin-core = {module = "io.insert-koin:koin-core", version.ref = "koin" }
@ -27,11 +28,18 @@ ktor-serialization-kotlinx-json = { module = "io.ktor:ktor-serialization-kotlinx
gradle-kotlinx-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin"}
gradle-kotlin-jvm = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin"}
hoplite-core = { module = "com.sksamuel.hoplite:hoplite-core", version.ref = "hoplite" }
hoplite-toml = { module = "com.sksamuel.hoplite:hoplite-toml", version.ref = "hoplite" }
[plugins]
jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"}
[bundles]
hoplite = [
"hoplite-core",
"hoplite-toml",
]
koin = [
"koin-core",
"koin-core-coroutines",

View File

@ -0,0 +1,7 @@
dependencies {
implementation(project(":modules:ModuleCore"))
}
dependencies {
implementation(libs.bundles.hoplite)
}

View File

@ -0,0 +1,38 @@
package de.itkl.modConfiguration.decoders
import com.sksamuel.hoplite.*
import com.sksamuel.hoplite.decoder.NullHandlingDecoder
import com.sksamuel.hoplite.decoder.toValidated
import com.sksamuel.hoplite.fp.invalid
import java.net.InetSocketAddress
import java.net.Proxy
import java.util.*
import kotlin.reflect.KType
class ProxyDecoder : NullHandlingDecoder<Proxy> {
override fun safeDecode(
node: Node,
type: KType,
context: DecoderContext,
): ConfigResult<Proxy> {
return when (node) {
is StringNode -> {
kotlin.runCatching {
val parts = node.value.split(':')
if (parts.size != 3) throw IllegalArgumentException("Invalid format. Expected format is 'type:host:port'")
val proxyType = when (parts[0].uppercase(Locale.getDefault())) {
"HTTP" -> Proxy.Type.HTTP
"SOCKS" -> Proxy.Type.SOCKS
"DIRECT" -> Proxy.Type.DIRECT
else -> throw IllegalArgumentException("Invalid proxy type '${parts[0]}'")
}
val address = InetSocketAddress(parts[1].trimStart('/'), parts[2].toInt())
Proxy(proxyType, address)
}.toValidated { ConfigFailure.DecodeError(node, type) }
}
else -> ConfigFailure.DecodeError(node, type).invalid()
}
}
override fun supports(type: KType): Boolean = type.classifier == Proxy::class
}

View File

@ -0,0 +1,39 @@
package de.itkl.modConfiguration.decoders
import com.sksamuel.hoplite.*
import com.sksamuel.hoplite.decoder.NullHandlingDecoder
import com.sksamuel.hoplite.decoder.toValidated
import com.sksamuel.hoplite.fp.invalid
import kotlin.reflect.KType
import kotlin.reflect.typeOf
abstract class SimpleDecoder<T : Any> : NullHandlingDecoder<T> {
override fun safeDecode(
node: Node,
type: KType,
context: DecoderContext,
): ConfigResult<T> {
return when (node) {
is StringNode -> {
kotlin.runCatching {
parse(node.value)
}.toValidated { ConfigFailure.DecodeError(node, type) }
}
else -> ConfigFailure.DecodeError(node, type).invalid()
}
}
abstract fun parse(stringValue: String): T
}
inline fun <reified T : Any> decodeString(crossinline parser: (value: String) -> T): SimpleDecoder<T> {
return object : SimpleDecoder<T>() {
override fun parse(stringValue: String): T {
return parser(stringValue)
}
override fun supports(type: KType): Boolean {
return type == typeOf<T>()
}
}
}

View File

@ -0,0 +1,3 @@
package de.itkl.modConfiguration.types
class Account

View File

@ -2,6 +2,12 @@ package de.itkl.module
sealed class Credential {
abstract val id: String
abstract val notes: String
}
data class UsernameAndPassword(val username: String, val password: String, override val id: String) : Credential()
data class UsernameAndPassword(
override val id: String,
override val notes: String,
val username: String,
val password: String,
) : Credential()

View File

@ -1,7 +1,3 @@
plugins {
`java-library`
}
subprojects {
apply(plugin = "module-convention")
}