Skip to content

Commit efb83a1

Browse files
committed
GH-62 Improve References handling, provide better Kotlin support and release 1.11.4
1 parent 1263af7 commit efb83a1

File tree

17 files changed

+133
-113
lines changed

17 files changed

+133
-113
lines changed

.github/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ repositories {
3636
3737
dependencies {
3838
// Default
39-
implementation 'net.dzikoysk:cdn:1.11.2'
39+
implementation 'net.dzikoysk:cdn:1.11.4'
4040
// Kotlin wrapper
41-
implementation 'net.dzikoysk:cdn-kt:1.11.2'
41+
implementation 'net.dzikoysk:cdn-kt:1.11.4'
4242
}
4343
```
4444

cdn-kt/pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<artifactId>cdn-parent</artifactId>
2121
<groupId>net.dzikoysk</groupId>
22-
<version>1.11.2</version>
22+
<version>1.11.4</version>
2323
</parent>
2424
<modelVersion>4.0.0</modelVersion>
2525

@@ -76,13 +76,23 @@
7676
<goals>
7777
<goal>compile</goal>
7878
</goals>
79+
<configuration>
80+
<sourceDirs>
81+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
82+
</sourceDirs>
83+
</configuration>
7984
</execution>
8085
<execution>
8186
<id>test-compile</id>
8287
<phase>test-compile</phase>
8388
<goals>
8489
<goal>test-compile</goal>
8590
</goals>
91+
<configuration>
92+
<sourceDirs>
93+
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
94+
</sourceDirs>
95+
</configuration>
8696
</execution>
8797
</executions>
8898
<configuration>

cdn-kt/src/main/kotlin/net/dzikoysk/cdn/KCdn.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ class KCdn(val cdn: Cdn) {
2727
fun configure(): CdnSettings = CdnSettings()
2828
}
2929

30-
fun parse(source: Source): KConfiguration = KConfiguration(cdn.load(source))
30+
fun load(source: Source): KConfiguration = KConfiguration(cdn.load(source))
3131

32-
fun <T : Any> parse(source: Source, template: KClass<T>): T = cdn.load(source, template.java)
32+
inline fun <reified T : Any> load(source: Source): T = this.load(source, T::class)
3333

34-
fun <T : Any> parse(source: Source, instance: T): T = cdn.load(source, instance)
34+
fun <T : Any> load(source: Source, template: KClass<T>): T = cdn.load(source, template.java)
3535

36-
inline fun <reified T : Any> parseAs(source: Source): T = this.parse(source, T::class)
36+
fun <T : Any> load(source: Source, instance: T): T = cdn.load(source, instance)
3737

3838
fun render(element: KNamedElement<*>): String = cdn.render(element.namedElement)
3939

cdn-kt/src/main/kotlin/net/dzikoysk/cdn/annotation/KPropertyMember.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package net.dzikoysk.cdn.annotation
33
import panda.std.stream.PandaStream
44
import java.lang.reflect.AnnotatedType
55
import kotlin.reflect.KMutableProperty
6+
import kotlin.reflect.KProperty
67
import kotlin.reflect.jvm.javaField
78
import kotlin.reflect.jvm.javaGetter
89

9-
internal class KPropertyMember(private val instance: Any, private val property: KMutableProperty<*>) : AnnotatedMember {
10+
internal class KPropertyMember(private val instance: Any, private val property: KProperty<*>) : AnnotatedMember {
1011

1112
override fun setValue(value: Any?) {
12-
property.setter.call(instance, value)
13+
(property as KMutableProperty).setter.call(instance, value)
1314
}
1415

1516
override fun getValue(): Any? {

cdn-kt/src/main/kotlin/net/dzikoysk/cdn/annotation/KotlinMemberResolver.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package net.dzikoysk.cdn.annotation
22

3+
import net.dzikoysk.cdn.model.Reference
34
import panda.utilities.StringUtils
45
import java.lang.reflect.Field
56
import kotlin.reflect.KMutableProperty
7+
import kotlin.reflect.KTypeProjection
8+
import kotlin.reflect.full.createType
69
import kotlin.reflect.full.functions
10+
import kotlin.reflect.full.isSubtypeOf
711
import kotlin.reflect.full.memberProperties
812

913
class KotlinMemberResolver : MemberResolver {
@@ -30,9 +34,13 @@ class KotlinMemberResolver : MemberResolver {
3034
}
3135

3236
override fun getProperties(instance: Any): List<AnnotatedMember> {
33-
return instance::class.memberProperties
34-
.filterIsInstance<KMutableProperty<*>>()
35-
.map { KPropertyMember(instance, it) }
37+
return instance::class.memberProperties.mapNotNull {
38+
when {
39+
it is KMutableProperty<*> -> KPropertyMember(instance, it)
40+
it.returnType.isSubtypeOf(Reference::class.createType(listOf(KTypeProjection.STAR))) -> KPropertyMember(instance, it)
41+
else -> null
42+
}
43+
}
3644
}
3745

3846
}

cdn-kt/src/test/kotlin/net/dzikoysk/cdn/kt/KotlinConfiguration.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717
package net.dzikoysk.cdn.kt
1818

1919
import net.dzikoysk.cdn.entity.Description
20+
import net.dzikoysk.cdn.model.reference
21+
import java.io.Serializable
2022

21-
class KotlinConfiguration {
23+
class KotlinConfiguration : Serializable {
2224

2325
@Description("# Description")
2426
var key = "value"
2527

28+
@Description("# Description")
29+
val reference = reference("ref")
30+
2631
}

cdn-kt/src/test/kotlin/net/dzikoysk/cdn/kt/KotlinReaderTest.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,24 @@
1616

1717
package net.dzikoysk.cdn.kt
1818

19-
import net.dzikoysk.cdn.CdnFactory
20-
import net.dzikoysk.cdn.registerKotlinModule
19+
import net.dzikoysk.cdn.KCdnFactory
2120
import org.junit.jupiter.api.Assertions.assertEquals
2221
import org.junit.jupiter.api.Test
2322

2423
class KotlinReaderTest {
2524

2625
@Test
2726
fun `should load configuration in kotlin`() {
28-
val configuration = CdnFactory.createStandard().settings
29-
.registerKotlinModule()
30-
.build()
31-
.load( { "key: custom" }, KotlinConfiguration::class.java)
27+
val configuration = KCdnFactory.createStandard().load<KotlinConfiguration> { "key: custom" }
3228

3329
assertEquals("custom", configuration.key)
30+
assertEquals("ref", configuration.reference.get())
3431
assertEquals("""
3532
# Description
3633
key: custom
37-
""".trimIndent(), CdnFactory.createStandard().settings.registerKotlinModule().build().render(configuration))
34+
# Description
35+
reference: ref
36+
""".trimIndent(), KCdnFactory.createStandard().render(configuration))
3837
}
3938

4039
}

cdn/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<parent>
2020
<artifactId>cdn-parent</artifactId>
2121
<groupId>net.dzikoysk</groupId>
22-
<version>1.11.2</version>
22+
<version>1.11.4</version>
2323
</parent>
2424
<modelVersion>4.0.0</modelVersion>
2525

cdn/src/main/java/net/dzikoysk/cdn/CdnDeserializer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
import net.dzikoysk.cdn.entity.DeserializationHandler;
2222
import net.dzikoysk.cdn.model.Element;
2323
import net.dzikoysk.cdn.model.Section;
24+
import net.dzikoysk.cdn.serialization.Composer;
2425
import net.dzikoysk.cdn.serialization.Deserializer;
2526
import panda.std.Option;
2627
import panda.utilities.ObjectUtils;
27-
import panda.utilities.StringUtils;
28-
2928
import java.lang.reflect.Field;
30-
import java.lang.reflect.Method;
3129

3230
public final class CdnDeserializer<T> {
3331

@@ -87,7 +85,10 @@ private void deserializeMember(Section source, AnnotatedMember member) throws Re
8785

8886
Deserializer<Object> deserializer = CdnUtils.findComposer(settings, member.getType(), member.getAnnotatedType(), member);
8987
Object value = deserializer.deserialize(settings, element, member.getAnnotatedType(), defaultValue, false);
90-
member.setValue(value);
88+
89+
if (value != Composer.MEMBER_ALREADY_PROCESSED) {
90+
member.setValue(value);
91+
}
9192
}
9293

9394
}

cdn/src/main/java/net/dzikoysk/cdn/CdnSettings.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import net.dzikoysk.cdn.composers.MapComposer;
2424
import net.dzikoysk.cdn.composers.ReferenceComposer;
2525
import net.dzikoysk.cdn.model.MutableReference;
26-
import net.dzikoysk.cdn.model.MutableReferenceImpl;
2726
import net.dzikoysk.cdn.model.Reference;
2827
import net.dzikoysk.cdn.serialization.Composer;
2928
import net.dzikoysk.cdn.serialization.Deserializer;
@@ -88,7 +87,6 @@ public final class CdnSettings {
8887

8988
withComposer(Reference.class, new ReferenceComposer<>());
9089
withComposer(MutableReference.class, new ReferenceComposer<>());
91-
withComposer(MutableReferenceImpl.class, new ReferenceComposer<>());
9290

9391
withDynamicComposer(Class::isEnum, new EnumComposer());
9492
}

0 commit comments

Comments
 (0)