This commit is contained in:
m724 2025-10-31 11:57:52 +01:00
commit 086bb9b6e4
Signed by untrusted user who does not match committer: m724
GPG key ID: A02E6E67AB961189
10 changed files with 95 additions and 48 deletions

View file

@ -1,5 +1,6 @@
plugins {
kotlin("multiplatform") version "2.2.21"
kotlin("plugin.serialization") version "2.2.21"
id("maven-publish")
}
@ -24,14 +25,6 @@ kotlin {
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
nativeTarget.apply {
binaries {
executable {
entryPoint = "dn42.m724.registry.main"
}
}
}
sourceSets {
nativeMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-io-core:0.8.0")

View file

@ -1,12 +0,0 @@
package dn42.m724.registry
import dn42.m724.registry.model.Maintainer
class FileSystemRegistryLoader(
) : RegistryLoader {
override fun getMaintainer(maintainerName: String): Maintainer {
TODO("Not yet implemented")
}
}

View file

@ -1,8 +0,0 @@
package eu.m724.dn42.m724.registry
import dn42.m724.registry.FileSystemRegistryLoader
fun main() {
val loader = FileSystemRegistryLoader()
}

View file

@ -1,7 +0,0 @@
package dn42.m724.registry
import dn42.m724.registry.model.Maintainer
interface RegistryLoader {
fun getMaintainer(maintainerName: String): Maintainer
}

View file

@ -0,0 +1,20 @@
package dn42.m724.registry.loader
import dn42.m724.registry.serializer.RegistryObjectSerializer
import kotlinx.io.buffered
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
class FileSystemRegistryLoader(
val root: Path
) : RegistryLoader {
override fun getData(path: String): Map<String, List<String>> {
check(!path.contains("..")) { "Path may not contain ..s"}
return SystemFileSystem.source(
path = Path(root, "data", path)
).buffered().use { source ->
RegistryObjectSerializer.readToMap(source)
}
}
}

View file

@ -0,0 +1,21 @@
package dn42.m724.registry.loader
interface RegistryLoader {
/**
* Fetches and processes data from the specified path.
*
* @param path The relative path within the registry.
* @return A map where each key is associated with a list of corresponding values
* as defined by the data file at the specified path.
*/
fun getData(path: String): Map<String, List<String>>
/**
* Fetches and processes data from the specified path.
*
* @param path The segments of a relative path within the registry, passed as a variable-length argument.
* @return A map where each key is associated with a list of corresponding values
* as defined by the data file at the specified path.
*/
fun getData(vararg path: String): Map<String, List<String>> = getData(path.joinToString("/"))
}

View file

@ -1,4 +0,0 @@
package dn42.m724.registry.model
class Maintainer {
}

View file

@ -0,0 +1,45 @@
package dn42.m724.registry.loader
import dn42.m724.registry.test.getResourcePath
import dn42.m724.registry.test.shouldContainExactly
import kotlinx.io.files.FileNotFoundException
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertFailsWith
class FileSystemRegistryLoaderTest {
lateinit var loader: FileSystemRegistryLoader
@BeforeTest
fun initializeLoader() {
loader = FileSystemRegistryLoader(getResourcePath("registry"))
}
@Test
fun `getData should return a map for successful parse`() {
val map = loader.getData("mntner", "EXAMPLE-MNT")
map shouldContainExactly mapOf(
"mntner" to listOf("EXAMPLE-MNT"),
"mnt-by" to listOf("EXAMPLE-MNT"),
"auth" to listOf(
"pgp-fingerprint 2DE81640E41DD84A9ED9B8F8FD4D7C6652E72E7B",
"pgp-fingerprint 139F1460BC66A19A2F880D8D47BA020D8EBCC05E"
),
"remarks" to listOf(
"This has spaces too yay!",
"As well as emojis \uD83D\uDE18!",
"Also: commas",
"" // blank value
),
"source" to listOf("DN42")
)
}
@Test
fun `getData should throw FileNotFoundException for not existing file`() {
assertFailsWith<FileNotFoundException> {
loader.getData("mntner", "NOTEXIST-MNT")
}
}
}

View file

@ -43,7 +43,7 @@ class RegistryObjectSerializerTest {
@Test
fun `readToMap should throw InvalidKeyException when the key contains illegal numbers`() {
withMockSource("li6ne: this has number! not possible have number!") { source ->
assertFailsWith(InvalidKeyException::class) {
assertFailsWith<InvalidKeyException> {
RegistryObjectSerializer.readToMap(source)
}
}
@ -52,7 +52,7 @@ class RegistryObjectSerializerTest {
@Test
fun `readToMap should throw InvalidKeyException when a key contains illegal greater than characters`() {
withMockSource("li>ne: this has >! cannot have !") { source ->
assertFailsWith(InvalidKeyException::class) {
assertFailsWith<InvalidKeyException> {
RegistryObjectSerializer.readToMap(source)
}
}
@ -61,7 +61,7 @@ class RegistryObjectSerializerTest {
@Test
fun `readToMap should throw InvalidKeyException when a key contains illegal spaces`() {
withMockSource(" line: leading ! bad!") { source ->
assertFailsWith(InvalidKeyException::class) {
assertFailsWith<InvalidKeyException> {
RegistryObjectSerializer.readToMap(source)
}
}
@ -70,7 +70,7 @@ class RegistryObjectSerializerTest {
@Test
fun `readToMap should throw InvalidKeyException when a key contains uppercase characters`() {
withMockSource("Line: Content") { source ->
assertFailsWith(InvalidKeyException::class) {
assertFailsWith<InvalidKeyException> {
RegistryObjectSerializer.readToMap(source)
}
}
@ -79,7 +79,7 @@ class RegistryObjectSerializerTest {
@Test
fun `readToMap should throw InvalidKeyException when a key is empty`() {
withMockSource(": what key?") { source ->
assertFailsWith(InvalidKeyException::class) {
assertFailsWith<InvalidKeyException> {
RegistryObjectSerializer.readToMap(source)
}
}
@ -88,7 +88,7 @@ class RegistryObjectSerializerTest {
@Test
fun `readToMap should throw MissingKeyException when there's an invalid pair`() {
withMockSource("line: content\nanother line") { source ->
assertFailsWith(MissingKeyException::class) {
assertFailsWith<MissingKeyException> {
RegistryObjectSerializer.readToMap(source)
}
}

View file

@ -10,12 +10,11 @@ import kotlinx.io.readString
import kotlinx.io.writeString
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.fail
private const val RESOURCES_PATH_PREFIX = "src/nativeTest/resources/"
private val RESOURCE_ROOT_PATH = Path("src/nativeTest/resources/")
fun getResourcePath(resource: String): Path =
Path(RESOURCES_PATH_PREFIX + resource)
Path(RESOURCE_ROOT_PATH, resource)
/**
* Retrieves a raw source for the specified resource.