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:
@@ -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()
|
||||
}
|
||||
Reference in New Issue
Block a user