mirror of
https://github.com/CarmJos/EasyConfiguration.git
synced 2026-06-04 10:38:19 +08:00
feat: Support kotlin dsl
This commit is contained in:
+70
-4
@@ -48,14 +48,22 @@
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.parent.groupId}</groupId>
|
||||
<artifactId>configured-feature-kotlin</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
@@ -68,6 +76,64 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<source>src/main/java</source>
|
||||
<source>target/generated-sources/annotations</source>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<jvmTarget>1.8</jvmTarget>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>default-testCompile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>testCompile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package cc.carm.lib.configuration.demo.tests;
|
||||
|
||||
import cc.carm.lib.configuration.demo.tests.conf.DemoConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.conf.KotlinConfiguration;
|
||||
import cc.carm.lib.configuration.demo.tests.conf.RegistryConfig;
|
||||
import cc.carm.lib.configuration.demo.tests.model.UserRecord;
|
||||
import cc.carm.lib.configuration.source.ConfigurationHolder;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -80,6 +82,22 @@ public class ConfigurationTest {
|
||||
|
||||
}
|
||||
|
||||
public static void testKotlin(ConfigurationHolder<?> provider) {
|
||||
provider.initialize(KotlinConfiguration.class);
|
||||
|
||||
System.out.println("> Test Kotlin value before:");
|
||||
System.out.println(KotlinConfiguration.INSTANCE.getLINKED_MAP().get());
|
||||
|
||||
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||
map.put("Language", "Kotlin");
|
||||
System.out.println("> Test Kotlin value -> " + map);
|
||||
KotlinConfiguration.INSTANCE.getLINKED_MAP().set(map);
|
||||
|
||||
System.out.println("> Test Kotlin value after:");
|
||||
System.out.println(KotlinConfiguration.INSTANCE.getLINKED_MAP().get());
|
||||
|
||||
}
|
||||
|
||||
public static void save(ConfigurationHolder<?> provider) {
|
||||
try {
|
||||
provider.save();
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package cc.carm.lib.configuration.demo.tests.conf
|
||||
|
||||
import cc.carm.lib.configuration.Configuration
|
||||
import cc.carm.lib.configuration.annotation.ConfigPath
|
||||
import cc.carm.lib.configuration.annotation.ConfigVersion
|
||||
import cc.carm.lib.configuration.kotlin.value.*
|
||||
import java.util.*
|
||||
|
||||
@ConfigPath(root = true)
|
||||
object KotlinConfiguration : Configuration {
|
||||
@ConfigVersion(1)
|
||||
val VERSION = valueFrom(Double::class) {
|
||||
defaults(1.0)
|
||||
}
|
||||
|
||||
val USER_LIST = listFrom(String::class) {
|
||||
defaults("Carm Jos")
|
||||
}
|
||||
|
||||
val NICKNAME = mapFrom(String::class, ::mutableMapOf) {
|
||||
defaultMap(mapOf("Carm Jos" to "Carm"))
|
||||
parse { v -> v }
|
||||
serialize { v -> v }
|
||||
}
|
||||
|
||||
val LINKED_MAP = linkedMapFrom(String::class) {
|
||||
parse { value ->
|
||||
value
|
||||
}
|
||||
serialize { v -> v }
|
||||
defaults("key", "value")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
<artifactId>configured-parent</artifactId>
|
||||
<version>4.1.2</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<properties>
|
||||
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
</properties>
|
||||
|
||||
<artifactId>configured-feature-kotlin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>configured-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<jvmTarget>1.8</jvmTarget>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>default-testCompile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>testCompile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,29 @@
|
||||
package cc.carm.lib.configuration.kotlin.value
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType
|
||||
import cc.carm.lib.configuration.builder.list.SourceListBuilder
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredList
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
inline fun <S : Any, reified V> listFrom(
|
||||
clazz: KClass<S>, block: (SourceListBuilder<S, V>.() -> Unit)
|
||||
): ConfiguredList<V> {
|
||||
return listFrom(clazz.java, block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified V> listFrom(
|
||||
clazz: Class<S>, block: (SourceListBuilder<S, V>.() -> Unit)
|
||||
): ConfiguredList<V> {
|
||||
return listFrom(ValueType.of(clazz), block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified V> listFrom(
|
||||
valueType: ValueType<S>, block: (SourceListBuilder<S, V>.() -> Unit)
|
||||
): ConfiguredList<V> {
|
||||
val configBuilder = ConfiguredList.builderOf(V::class.java)
|
||||
val sourceValueBuilder: SourceListBuilder<S, V> = if (valueType.rawType == String::class.java) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
configBuilder.fromString() as SourceListBuilder<S, V>
|
||||
} else configBuilder.from(valueType)
|
||||
return sourceValueBuilder.also(block).build()
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package cc.carm.lib.configuration.kotlin.value
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType
|
||||
import cc.carm.lib.configuration.builder.map.SourceMapBuilder
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredMap
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
inline fun <S : Any, reified K, reified V> hashmapFrom(
|
||||
clazz: KClass<S>,
|
||||
block: SourceMapBuilder<HashMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return hashmapFrom(clazz.java, block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> hashmapFrom(
|
||||
clazz: Class<S>,
|
||||
block: SourceMapBuilder<HashMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return hashmapFrom(ValueType.of(clazz), block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> hashmapFrom(
|
||||
valueType: ValueType<S>,
|
||||
block: SourceMapBuilder<HashMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
val mapCreator = ConfiguredMap.builderOf(K::class.java, V::class.java)
|
||||
val sourceValueBuilder: SourceMapBuilder<HashMap<K, V>, S, K, V> = mapCreator.asHashMap().from(valueType)
|
||||
return sourceValueBuilder.also(block).build()
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> linkedMapFrom(
|
||||
clazz: KClass<S>,
|
||||
block: SourceMapBuilder<LinkedHashMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return linkedMapFrom(clazz.java, block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> linkedMapFrom(
|
||||
clazz: Class<S>,
|
||||
block: SourceMapBuilder<LinkedHashMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return linkedMapFrom(ValueType.of(clazz), block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> linkedMapFrom(
|
||||
valueType: ValueType<S>,
|
||||
block: SourceMapBuilder<LinkedHashMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
val mapCreator = ConfiguredMap.builderOf(K::class.java, V::class.java)
|
||||
val sourceValueBuilder: SourceMapBuilder<LinkedHashMap<K, V>, S, K, V> = mapCreator.asLinkedMap().from(valueType)
|
||||
return sourceValueBuilder.also(block).build()
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> treeMapFrom(
|
||||
clazz: KClass<S>,
|
||||
block: SourceMapBuilder<TreeMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return treeMapFrom(clazz.java, block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> treeMapFrom(
|
||||
clazz: Class<S>,
|
||||
block: SourceMapBuilder<TreeMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return treeMapFrom(ValueType.of(clazz), block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified K, reified V> treeMapFrom(
|
||||
valueType: ValueType<S>,
|
||||
block: SourceMapBuilder<TreeMap<K, V>, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
val mapCreator = ConfiguredMap.builderOf(K::class.java, V::class.java)
|
||||
val sourceValueBuilder: SourceMapBuilder<TreeMap<K, V>, S, K, V> = mapCreator.asTreeMap().from(valueType)
|
||||
return sourceValueBuilder.also(block).build()
|
||||
}
|
||||
|
||||
inline fun <reified MAP : Map<K, V>, S : Any, reified K, reified V> mapFrom(
|
||||
clazz: KClass<S>,
|
||||
noinline map: () -> MAP,
|
||||
block: SourceMapBuilder<MAP, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return mapFrom(clazz.java, map, block)
|
||||
}
|
||||
|
||||
inline fun <reified MAP : Map<K, V>, S : Any, reified K, reified V> mapFrom(
|
||||
clazz: Class<S>,
|
||||
noinline map: () -> MAP,
|
||||
block: SourceMapBuilder<MAP, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
return mapFrom(ValueType.of(clazz), map, block)
|
||||
}
|
||||
|
||||
inline fun <reified MAP : Map<K, V>, S : Any, reified K, reified V> mapFrom(
|
||||
valueType: ValueType<S>,
|
||||
noinline map: () -> MAP,
|
||||
block: SourceMapBuilder<MAP, S, K, V>.() -> Unit
|
||||
): ConfiguredMap<K, V> {
|
||||
val mapCreator = ConfiguredMap.builderOf(K::class.java, V::class.java)
|
||||
val sourceValueBuilder: SourceMapBuilder<MAP, S, K, V> = mapCreator.constructor(map).from(valueType)
|
||||
return sourceValueBuilder.also(block).build()
|
||||
}
|
||||
|
||||
|
||||
fun <MAP : Map<K, V>, S, K, V> SourceMapBuilder<MAP, S, K, V>.defaultMap(map: MAP): SourceMapBuilder<MAP, S, K, V> {
|
||||
return defaults(map)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package cc.carm.lib.configuration.kotlin.value
|
||||
|
||||
import cc.carm.lib.configuration.adapter.ValueType
|
||||
import cc.carm.lib.configuration.builder.value.SourceValueBuilder
|
||||
import cc.carm.lib.configuration.value.standard.ConfiguredValue
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
inline fun <S : Any, reified T> valueFrom(
|
||||
clazz: KClass<S>, block: (SourceValueBuilder<S, T>.() -> Unit)
|
||||
): ConfiguredValue<T> {
|
||||
return valueFrom(clazz.java, block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified V> valueFrom(
|
||||
clazz: Class<S>, block: (SourceValueBuilder<S, V>.() -> Unit)
|
||||
): ConfiguredValue<V> {
|
||||
return valueFrom(ValueType.of(clazz), block)
|
||||
}
|
||||
|
||||
inline fun <S : Any, reified V> valueFrom(
|
||||
valueType: ValueType<S>, block: (SourceValueBuilder<S, V>.() -> Unit)
|
||||
): ConfiguredValue<V> {
|
||||
val configBuilder = ConfiguredValue.builderOf(V::class.java)
|
||||
val sourceValueBuilder: SourceValueBuilder<S, V> = if (valueType.rawType == String::class.java) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
configBuilder.fromString() as SourceValueBuilder<S, V>
|
||||
} else configBuilder.from(valueType)
|
||||
sourceValueBuilder.parse { holder, data ->
|
||||
holder.deserialize(V::class.java, data)
|
||||
}
|
||||
sourceValueBuilder.serialize { holder, data ->
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
holder.serialize(data) as? S
|
||||
}
|
||||
return sourceValueBuilder.also(block).build()
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
<maven.compiler.target>${project.jdk.version}</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
|
||||
<kotlin.version>2.0.21</kotlin.version>
|
||||
</properties>
|
||||
|
||||
<groupId>cc.carm.lib</groupId>
|
||||
@@ -24,6 +25,7 @@
|
||||
<module>features/versioned</module>
|
||||
<module>features/validators</module>
|
||||
<module>features/text</module>
|
||||
<module>features/kotlin</module>
|
||||
|
||||
<module>providers/yaml</module>
|
||||
<module>providers/gson</module>
|
||||
|
||||
@@ -44,5 +44,13 @@ public class YamlTests {
|
||||
ConfigurationTest.save(holder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKotlin() {
|
||||
ConfigurationHolder<YAMLSource> holder = YAMLConfigFactory.from("target/kotlin.yml").build();
|
||||
|
||||
ConfigurationTest.testKotlin(holder);
|
||||
|
||||
ConfigurationTest.save(holder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user