starting with configuration module
parent
2ddeaf2b17
commit
6e46248301
|
|
@ -0,0 +1 @@
|
||||||
|
accounts.toml
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
[account.db-user]
|
||||||
|
user = "kinch"
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
[database]
|
||||||
|
jdbc = "jdbc:postgresql://localhost:5432/postgres"
|
||||||
|
user = "db-user"
|
||||||
|
|
@ -6,6 +6,7 @@ slf4j = "2.0.12"
|
||||||
kotlin-logging = "6.0.4"
|
kotlin-logging = "6.0.4"
|
||||||
kotlinx = "1.7.3"
|
kotlinx = "1.7.3"
|
||||||
kotlin = "1.9.22"
|
kotlin = "1.9.22"
|
||||||
|
hoplite = "2.7.5"
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
koin-core = {module = "io.insert-koin:koin-core", version.ref = "koin" }
|
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-kotlinx-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin"}
|
||||||
gradle-kotlin-jvm = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", 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]
|
[plugins]
|
||||||
jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
|
||||||
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"}
|
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin"}
|
||||||
|
|
||||||
[bundles]
|
[bundles]
|
||||||
|
hoplite = [
|
||||||
|
"hoplite-core",
|
||||||
|
"hoplite-toml",
|
||||||
|
]
|
||||||
koin = [
|
koin = [
|
||||||
"koin-core",
|
"koin-core",
|
||||||
"koin-core-coroutines",
|
"koin-core-coroutines",
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,7 @@
|
||||||
|
dependencies {
|
||||||
|
implementation(project(":modules:ModuleCore"))
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(libs.bundles.hoplite)
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
@ -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>()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
package de.itkl.modConfiguration.types
|
||||||
|
|
||||||
|
class Account
|
||||||
|
|
@ -2,6 +2,12 @@ package de.itkl.module
|
||||||
|
|
||||||
sealed class Credential {
|
sealed class Credential {
|
||||||
abstract val id: String
|
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()
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,3 @@
|
||||||
plugins {
|
|
||||||
`java-library`
|
|
||||||
}
|
|
||||||
|
|
||||||
subprojects {
|
subprojects {
|
||||||
apply(plugin = "module-convention")
|
apply(plugin = "module-convention")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue