From a22fd2a6b3248d8b9309d3a88ccc7e107d0cfcd8 Mon Sep 17 00:00:00 2001 From: Roman Gräf Date: Thu, 30 Apr 2020 03:28:48 +0200 Subject: Initial commit --- .github/workflows/publish-jar.yml | 38 +++++ .gitignore | 4 + LICENSE | 21 +++ README.md | 67 ++++++++ build.gradle.kts | 28 ++++ gradle.properties | 1 + gradlew | 172 +++++++++++++++++++++ gradlew.bat | 84 ++++++++++ settings.gradle | 2 + src/main/kotlin/com/romangraef/jrconfig/Config.kt | 55 +++++++ .../romangraef/jrconfig/ConfigMissingException.kt | 5 + .../jrconfig/ConfigMissingProviderException.kt | 4 + .../com/romangraef/jrconfig/ConfigProvider.kt | 6 + .../com/romangraef/jrconfig/ConfigVariable.kt | 6 + .../romangraef/jrconfig/FilePropertiesProvider.kt | 45 ++++++ .../jrconfig/TransformerConfigVariable.kt | 41 +++++ .../jrconfig/variables/IntegerVariable.kt | 15 ++ .../jrconfig/variables/StringVariable.kt | 15 ++ src/test/java/com/romangraef/jrconfig/Main.java | 11 ++ src/test/kotlin/com/romangraef/jrconfig/MainKt.kt | 8 + 20 files changed, 628 insertions(+) create mode 100644 .github/workflows/publish-jar.yml create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.gradle.kts create mode 100644 gradle.properties create mode 100644 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle create mode 100644 src/main/kotlin/com/romangraef/jrconfig/Config.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/ConfigMissingException.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/ConfigMissingProviderException.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/ConfigProvider.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/FilePropertiesProvider.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/TransformerConfigVariable.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/variables/IntegerVariable.kt create mode 100644 src/main/kotlin/com/romangraef/jrconfig/variables/StringVariable.kt create mode 100644 src/test/java/com/romangraef/jrconfig/Main.java create mode 100644 src/test/kotlin/com/romangraef/jrconfig/MainKt.kt diff --git a/.github/workflows/publish-jar.yml b/.github/workflows/publish-jar.yml new file mode 100644 index 0000000..b42e61f --- /dev/null +++ b/.github/workflows/publish-jar.yml @@ -0,0 +1,38 @@ +on: + push: + # Sequence of patterns matched against refs/tags + tags: + - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + +name: Upload Release Asset + +jobs: + build: + name: Upload Release Asset + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@master + - name: Build project # This would actually build your project, using zip for an example artifact + run: | + ./gradlew build + - name: Create Release + id: create_release + uses: actions/create-release@v1.0.0 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: false + prerelease: false + - name: Upload Release Asset + id: upload-release-asset + uses: actions/upload-release-asset@v1.0.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps + asset_path: ./build/libs/jrconfig-shadow.jar + asset_name: jrconfig-shadow.zip + asset_content_type: application/zip \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1675d97 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea/ +.gradle/ +build/ +config.properties diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..221e117 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Roman Konrad Gräf + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..df213cb --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +## romangraefs Java Config library. +I intend to mostly use this library for personal projects, so documentation might be lacking in parts. If you have questions, feel free to hit me up on [Twitter](https://twitter.com/romangraef89). That being said: + +## Usage +Java: +```java +public class ConfigurableClass1 { + public ConfigVariable someConfigProp = Config.getString("someProp"); // Define a config property + + public ConfigurableClass1() { + Config.use(FilePropertiesProvider.create("config.properties")); // Define a config source. + } + + public void someMethod() { + System.out.println(someConfigProp.get()); // Load data from the config. + someConfigProp.set("New value"); + } +} +``` + +Kotlin: +```kotlin +val someConfigProp = Config.get("someProp") + +fun main() { + Config.use(FilePropertiesProvider.create("config.properties")) + println(someConfigProp.get()) +} +``` + +### Api breakdown + +The API is split up into 3 parts: + + - Config variables. + +These are what you interact with, for most of your code. You obtain an instance by either calling +`Config.get(someClazz, "propName")` or you can use `Config.get("propName")` in Kotlin. +Some Common Types like `String` have shortcut methods like `Config.getString("propName")`. + + - Variable Transformers. + +These parse / serialize a string value obtained from a config source. There are some default implementations +for some types (`String` and `Integer` as of right now), but you can manually create transformers +by extending `TransformerConfigVariable`. + + - Config providers. + +You usually install one config provider at the very start of your main using `Config.use(provider)`. Currently there +is only one Provider, which is the `FilePropertiesProvider` which directly utilizes standard java properties. If +you want to create your own provider, you can implement `ConfigProvider` + +## Installation + +Gradle via [Jitpack](https://jitpack.io/): +```groovy +repositories { + maven { url "https://jitpack.io" } +} + +dependencies { + implementation("com.github.romangraef", "jrconfig", "v1.0") +} +``` +The version can be either a git shortref, or a [tag](https://github.com/romangraef/jrconfig/tags). + +Alternatively, a uberjar/fatjar/ shadow/shadedjar can be obtained from the [releases](https://github.com/romangraef/jrconfig/releases). \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts new file mode 100644 index 0000000..fc35fe1 --- /dev/null +++ b/build.gradle.kts @@ -0,0 +1,28 @@ +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +plugins { + java + kotlin("jvm") version "1.3.72" + id("com.github.johnrengelman.shadow") version "4.0.4" +} + + +repositories { + mavenCentral() +} + +dependencies { + implementation(kotlin("stdlib-jdk8")) + testCompile("junit", "junit", "4.12") +} +tasks { + named("shadowJar") { + archiveBaseName.set("jrconfig-shadow") + mergeServiceFiles() + } +} + +tasks { + build { + dependsOn(shadowJar) + } +} \ No newline at end of file diff --git a/gradle.properties b/gradle.properties new file mode 100644 index 0000000..29e08e8 --- /dev/null +++ b/gradle.properties @@ -0,0 +1 @@ +kotlin.code.style=official \ No newline at end of file diff --git a/gradlew b/gradlew new file mode 100644 index 0000000..af6708f --- /dev/null +++ b/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..6d57edc --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..4d9593f --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'JRConfig' + diff --git a/src/main/kotlin/com/romangraef/jrconfig/Config.kt b/src/main/kotlin/com/romangraef/jrconfig/Config.kt new file mode 100644 index 0000000..7641689 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/Config.kt @@ -0,0 +1,55 @@ +package com.romangraef.jrconfig + +import com.romangraef.jrconfig.variables.IntegerVariable +import com.romangraef.jrconfig.variables.StringVariable + +typealias ConfigVariableProvider = (ConfigProvider, String) -> ConfigVariable + +object Config { + private var configProvider: ConfigProvider? = null + private val map: MutableMap, ConfigVariableProvider<*>> = mutableMapOf() + private val proxyProvider: ConfigProvider = object : ConfigProvider { + + override fun provideData(point: String): String? = + configProvider?.provideData(point) + + + override fun setData(point: String, data: String) = + configProvider?.setData(point, data) ?: Unit + } + + init { + map[Int::class.java] = ::IntegerVariable + map[String::class.java] = ::StringVariable + } + + + @Suppress("UNCHECKED_CAST") + @JvmStatic + fun get(clazz: Class, point: String): ConfigVariable { + return (map[clazz] as? ConfigVariableProvider)?.invoke(proxyProvider, point) + ?: throw ConfigMissingProviderException(clazz) + } + + inline fun get(point: String) = get(T::class.java, point) + + @JvmStatic + fun registerConfigVariableProvider(clazz: Class, provider: ConfigVariableProvider) { + map[clazz] = provider + } + + inline fun registerConfigVariableProvider(noinline provider: ConfigVariableProvider) = + registerConfigVariableProvider(T::class.java, provider) + + + @JvmStatic + fun use(provider: ConfigProvider?) { + configProvider = provider + } + + @JvmStatic + fun getString(point: String): ConfigVariable { + return get(String::class.java, point) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jrconfig/ConfigMissingException.kt b/src/main/kotlin/com/romangraef/jrconfig/ConfigMissingException.kt new file mode 100644 index 0000000..1b609f9 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/ConfigMissingException.kt @@ -0,0 +1,5 @@ +package com.romangraef.jrconfig + +class ConfigMissingException(val point: String) : RuntimeException("The point $point is missing from your config.") { + +} diff --git a/src/main/kotlin/com/romangraef/jrconfig/ConfigMissingProviderException.kt b/src/main/kotlin/com/romangraef/jrconfig/ConfigMissingProviderException.kt new file mode 100644 index 0000000..4cf03b8 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/ConfigMissingProviderException.kt @@ -0,0 +1,4 @@ +package com.romangraef.jrconfig + +class ConfigMissingProviderException(val clazz: Class<*>) : + RuntimeException("You are missing a config provider for the type ${clazz.canonicalName}") diff --git a/src/main/kotlin/com/romangraef/jrconfig/ConfigProvider.kt b/src/main/kotlin/com/romangraef/jrconfig/ConfigProvider.kt new file mode 100644 index 0000000..019cdef --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/ConfigProvider.kt @@ -0,0 +1,6 @@ +package com.romangraef.jrconfig + +interface ConfigProvider { + fun provideData(point: String): String? + fun setData(point: String, data: String) +} \ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt b/src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt new file mode 100644 index 0000000..5874e76 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/ConfigVariable.kt @@ -0,0 +1,6 @@ +package com.romangraef.jrconfig + +interface ConfigVariable { + fun get(): T + fun set(value: T) +} \ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jrconfig/FilePropertiesProvider.kt b/src/main/kotlin/com/romangraef/jrconfig/FilePropertiesProvider.kt new file mode 100644 index 0000000..fda7bf9 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/FilePropertiesProvider.kt @@ -0,0 +1,45 @@ +package com.romangraef.jrconfig + +import java.io.File +import java.io.FileInputStream +import java.io.FileOutputStream +import java.io.IOException +import java.util.* + +class FilePropertiesProvider(private val file: File) : ConfigProvider { + private var properties: Properties = Properties() + + init { + try { + properties.load(FileInputStream(file)) + } catch (e: IOException) { + e.printStackTrace() + } + } + + companion object { + @JvmStatic + fun create(fileName: String): ConfigProvider { + return FilePropertiesProvider(File(fileName)) + } + } + + override fun provideData(point: String): String? { + return properties.getProperty(point) + } + + override fun setData(point: String, data: String) { + properties.setProperty(point, data) + save() + } + + private fun save() { + try { + properties.store(FileOutputStream(file), "application config") + } catch (e: IOException) { + e.printStackTrace() + } + } + + +} \ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jrconfig/TransformerConfigVariable.kt b/src/main/kotlin/com/romangraef/jrconfig/TransformerConfigVariable.kt new file mode 100644 index 0000000..ae38409 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/TransformerConfigVariable.kt @@ -0,0 +1,41 @@ +package com.romangraef.jrconfig + +import java.util.function.Function + +abstract class TransformerConfigVariable(private val provider: ConfigProvider, private val point: String) : + ConfigVariable { + protected abstract fun transform(value: String): T + protected abstract fun serialize(data: T): String + + override fun get(): T { + return transform(provider.provideData(point) ?: throw ConfigMissingException(point)) + } + + + override fun set(value: T) = + provider.setData(point, serialize(value)) + + + companion object { + /** + * Quick and dirty. Please consider using a subclass. + */ + fun getVariable( + provider: ConfigProvider, + point: String, + transform: Function, + serialize: Function + ): TransformerConfigVariable { + return object : TransformerConfigVariable(provider, point) { + override fun transform(value: String): T { + return transform.apply(value) + } + + override fun serialize(data: T): String { + return serialize.apply(data) + } + } + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jrconfig/variables/IntegerVariable.kt b/src/main/kotlin/com/romangraef/jrconfig/variables/IntegerVariable.kt new file mode 100644 index 0000000..97cd719 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/variables/IntegerVariable.kt @@ -0,0 +1,15 @@ +package com.romangraef.jrconfig.variables + +import com.romangraef.jrconfig.ConfigProvider +import com.romangraef.jrconfig.TransformerConfigVariable + +class IntegerVariable(provider: ConfigProvider, point: String) : + TransformerConfigVariable(provider, point) { + override fun transform(value: String): Int { + return value.toInt() + } + + override fun serialize(data: Int): String { + return data.toString() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/romangraef/jrconfig/variables/StringVariable.kt b/src/main/kotlin/com/romangraef/jrconfig/variables/StringVariable.kt new file mode 100644 index 0000000..62ee801 --- /dev/null +++ b/src/main/kotlin/com/romangraef/jrconfig/variables/StringVariable.kt @@ -0,0 +1,15 @@ +package com.romangraef.jrconfig.variables + +import com.romangraef.jrconfig.ConfigProvider +import com.romangraef.jrconfig.TransformerConfigVariable + +class StringVariable(provider: ConfigProvider, point: String) : + TransformerConfigVariable(provider, point) { + override fun transform(value: String): String { + return value + } + + override fun serialize(data: String): String { + return data + } +} \ No newline at end of file diff --git a/src/test/java/com/romangraef/jrconfig/Main.java b/src/test/java/com/romangraef/jrconfig/Main.java new file mode 100644 index 0000000..28dc9ec --- /dev/null +++ b/src/test/java/com/romangraef/jrconfig/Main.java @@ -0,0 +1,11 @@ +package com.romangraef.jrconfig; + +public class Main { + + public static ConfigVariable token = Config.getString("token"); + + public static void main(String[] args) { + Config.use(FilePropertiesProvider.create("config.properties")); + System.out.println(token.get()); + } +} diff --git a/src/test/kotlin/com/romangraef/jrconfig/MainKt.kt b/src/test/kotlin/com/romangraef/jrconfig/MainKt.kt new file mode 100644 index 0000000..504b446 --- /dev/null +++ b/src/test/kotlin/com/romangraef/jrconfig/MainKt.kt @@ -0,0 +1,8 @@ +package com.romangraef.jrconfig + +val token = Config.get("token") + +fun main() { + Config.use(FilePropertiesProvider.create("config.properties")) + println(token.get()) +} \ No newline at end of file -- cgit