Skip to content

Commit

Permalink
configurator: Release version 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Com6235 committed May 24, 2024
1 parent 5d7d005 commit 25a08c3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 24 deletions.
22 changes: 16 additions & 6 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
name: Publish package to the Github Packages
name: Publish everything
on:
push:
branches:
- main

jobs:
publish-and-test:
dependency-graph:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout dev branch
uses: actions/checkout@v4
Expand All @@ -22,17 +21,28 @@ jobs:
java-version: '21'
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Generate and submit dependency graph
uses: gradle/actions/dependency-submission@v3

package:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Checkout main branch
uses: actions/checkout@v4
with:
ref: 'main'

- name: Set up Java
uses: actions/setup-java@v4
with:
java-version: '21'
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3

- name: Publish package
run: ./gradlew publish
env:
Expand Down
22 changes: 20 additions & 2 deletions configurator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,27 @@ import kotlinx.serialization.Serializable
data class Config(val helpPages: List<String>)

fun main() {
val configStream = this::class.java.getResourceAsStream("config.json")!!
val config = ConfigLoader(Config.serializer()).loadConfig(configStream, "json") // change
val configStream = this::class.java.getResourceAsStream("config.json")!! // change to any format, that it supports
val config = ConfigLoader(Config.serializer()).loadConfig(configStream, "json") // change to any format, that it supports

println(config.helpPages)
}
```

### Read from input and add to a JSON

```kotlin
import io.github.com6235.configurator.ConfigLoader
import kotlinx.serialization.Serializable

@Serializable
data class Config(val name: String)

fun main() {
val loader = ConfigLoader(Config.serializer())
while (true) {
val s = readln()
loader.saveConfig(Config(s), Path("./name.json"))
}
}
```
5 changes: 3 additions & 2 deletions configurator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "io.github.com6235"
version = "1.0.1"
version = "1.0.2"

plugins {
kotlin("plugin.serialization") version "2.0.0-RC3"
Expand All @@ -10,11 +10,12 @@ val serializationVersion = "1.6.3"
dependencies {
testImplementation(kotlin("test"))

implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
api("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-hocon:$serializationVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-properties:$serializationVersion")
implementation("com.charleskorn.kaml:kaml:0.59.0")
implementation("net.peanuuutz.tomlkt:tomlkt:0.3.7")
implementation("net.benwoodworth.knbt:knbt:0.11.5")
}

val jdFile = layout.buildDirectory.file("libs/$name-$version-javadoc.jar")
Expand Down
34 changes: 22 additions & 12 deletions configurator/src/main/kotlin/ConfigLoaders.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.hocon.Hocon
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.decodeFromStream
import net.benwoodworth.knbt.Nbt
import net.benwoodworth.knbt.NbtCompression
import net.benwoodworth.knbt.NbtVariant
import net.benwoodworth.knbt.decodeFromStream
import net.peanuuutz.tomlkt.Toml
import java.io.InputStream
import java.nio.charset.Charset
import java.util.*

internal class YamlLoader<T : Any>(serializer: KSerializer<T>) : Loader<T>(serializer) {
internal class YamlLoader<T : Any>(
serializer: KSerializer<T>,
private val yaml: Yaml = Yaml()
) : Loader<T>(serializer) {
override fun load(stream: InputStream): T =
yaml.decodeFromString(serializer, stream.readBytes().toString(Charset.defaultCharset()))

override fun save(data: T): String = yaml.encodeToString(serializer, data)

companion object {
val yaml = Yaml()
}
}

internal class HoconLoader<T : Any>(serializer: KSerializer<T>) : Loader<T>( serializer) {
Expand All @@ -38,7 +42,7 @@ internal class HoconLoader<T : Any>(serializer: KSerializer<T>) : Loader<T>( ser

@OptIn(ExperimentalSerializationApi::class)
override fun save(data: T): String {
val renderOptions = ConfigRenderOptions.defaults().setOriginComments(false).setJson(false)
val renderOptions = ConfigRenderOptions.defaults().setOriginComments(false).setJson(false).setFormatted(true)
val serialized = Hocon.encodeToConfig(serializer, data).resolve().root().render(renderOptions)
return serialized
}
Expand All @@ -60,22 +64,28 @@ internal class PropertiesLoader<T : Any>(serializer: KSerializer<T>) : Loader<T>
}
}

internal class JsonLoader<T : Any>(serializer: KSerializer<T>) : Loader<T>(serializer) {
internal class JsonLoader<T : Any>(
serializer: KSerializer<T>,
private val json: Json = Json { prettyPrint = true }
) : Loader<T>(serializer) {
@OptIn(ExperimentalSerializationApi::class)
override fun load(stream: InputStream): T = json.decodeFromStream(serializer, stream)

override fun save(data: T): String = json.encodeToString(serializer, data)

companion object {
val json = Json {
prettyPrint = true
}
}
}

internal class TomlLoader<T : Any>(serializer: KSerializer<T>) : Loader<T>(serializer) {
override fun load(stream: InputStream): T =
Toml.decodeFromString(serializer, stream.readBytes().toString(Charset.defaultCharset()))

override fun save(data: T): String = Toml.encodeToString(serializer, data)
}

internal class NbtLoader<T : Any>(
serializer: KSerializer<T>,
private val nbt: Nbt = Nbt { compression = NbtCompression.None; variant = NbtVariant.Java }
) : Loader<T>(serializer) {
override fun load(stream: InputStream): T = nbt.decodeFromStream(serializer, stream)

override fun save(data: T): String = nbt.encodeToByteArray(serializer, data).toString(Charset.defaultCharset())
}
25 changes: 25 additions & 0 deletions configurator/src/main/kotlin/Loader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ class ConfigLoader<T : Any>(serializer: KSerializer<T>) {
return loader.load(ByteArrayInputStream(string.toByteArray()))
}

/**
* Loads a config from stream.
*
* @param stream The stream with data
* @param loader Data loader
*/
fun loadConfig(stream: InputStream, loader: Loader<T>): T = loader.load(stream)

/**
* Loads a config from string.
*
* @param string The string with data
* @param loader Data loader
*/
fun loadConfig(string: String, loader: Loader<T>): T = loader.load(ByteArrayInputStream(string.toByteArray()))

/**
* Loads a config from a file.
*
Expand All @@ -68,6 +84,15 @@ class ConfigLoader<T : Any>(serializer: KSerializer<T>) {
return loader.save(data)
}

/**
* Serialize a config into a string.
*
* @param data Data to serialize
* @param loader Data loader
* @return Serialized string
*/
fun saveConfig(data: T, loader: Loader<T>): String = loader.save(data)

/**
* Save a config into a file.
*
Expand Down
5 changes: 3 additions & 2 deletions configurator/src/main/kotlin/Others.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FileFormats<T : Any>(serializer: KSerializer<T>) {
listOf("conf", "hocon") to HoconLoader(serializer),
listOf("properties") to PropertiesLoader(serializer),
listOf("toml") to TomlLoader(serializer),
listOf("nbt", "dat") to NbtLoader(serializer),
)

init {
Expand All @@ -30,8 +31,8 @@ class FileFormats<T : Any>(serializer: KSerializer<T>) {
fun findFormat(extension: String): Loader<T>? =
formats.entries.firstOrNull { it.key.contains(extension.lowercase()) }?.value

fun addFormat(extension: List<String>, loader: Loader<T>) {
formats[extension.map { it.lowercase() }] = loader
fun addFormat(extensions: List<String>, loader: Loader<T>) {
formats[extensions.map { it.lowercase() }] = loader
}
}

Expand Down
25 changes: 25 additions & 0 deletions configurator/src/test/kotlin/ConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import kotlinx.serialization.Serializable
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import java.nio.file.Files
import java.util.UUID
import kotlin.io.path.Path

// HOCON
Expand Down Expand Up @@ -41,6 +42,12 @@ data class Toml(val name: String, val account: Account)
@Serializable
data class Account(val email: String, val password: String, val dateOfBirth: MutableList<Int>)

// NBT
@Serializable
data class Nbt(val players: Map<String, Player>)
@Serializable
data class Player(val name: String, val uuid: String)

class ConfigTest {
@Test
fun testLoadingHocon() {
Expand Down Expand Up @@ -100,4 +107,22 @@ class ConfigTest {

Files.deleteIfExists(Path("./test.properties"))
}

@Test
fun testNBT() {
val loader = ConfigLoader(Nbt.serializer())
val uuid1 = UUID.randomUUID().toString()
val uuid2 = UUID.randomUUID().toString()
val data = Nbt(mapOf(
"Alex" to Player("Alex", uuid1),
"Steve" to Player("Steve", uuid2)
))
loader.saveConfig(data, Path("./test.nbt"))
val loading = loader.loadConfig(Path("./test.nbt"))

assertEquals(data.players["Alex"], loading!!.players["Alex"])
assertEquals(data.players["Steve"], loading.players["Steve"])

Files.deleteIfExists(Path("./test.nbt"))
}
}

0 comments on commit 25a08c3

Please sign in to comment.