Skip to content

Commit 2ba66aa

Browse files
authored
Merge pull request #75 from ergon/bugfix/DOPE-279-fix-converter-field-access-in-nested-object
DOPE-279: fix converterField access in nested object call
2 parents fbbd9db + 6742152 commit 2ba66aa

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

crystal-map-connector/src/main/kotlin/ch/ergon/dope/extension/type/ObjectField.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package ch.ergon.dope.extension.type
33
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
44
import ch.ergon.dope.toDopeType
55
import ch.ergon.dope.validtype.ObjectType
6+
import com.schwarz.crystalapi.ITypeConverter
7+
import com.schwarz.crystalapi.schema.CMConverterField
8+
import com.schwarz.crystalapi.schema.CMConverterList
69
import com.schwarz.crystalapi.schema.CMJsonField
710
import com.schwarz.crystalapi.schema.CMJsonList
811
import com.schwarz.crystalapi.schema.CMObjectField
@@ -37,10 +40,17 @@ class ObjectField<S : Schema>(val schema: S, val name: String, val path: String)
3740
* @throws IllegalStateException if the attribute type is not supported
3841
* @return the retrieved CMType
3942
*/
43+
@Suppress("UNCHECKED_CAST")
4044
inline fun <reified T : CMType, S : Schema> ObjectField<S>.getField(field: KProperty1<S, T>): T {
4145
val schemaField = field.get(schema)
4246
val nestedFieldPath = if (path.isBlank()) name else "$path.$name"
4347
return when (schemaField) {
48+
is CMConverterField<*, *> ->
49+
CMConverterField(schemaField.name, nestedFieldPath, schemaField.typeConverter as ITypeConverter<Any, Any>) as T
50+
51+
is CMConverterList<*, *> ->
52+
CMConverterList(schemaField.name, nestedFieldPath, schemaField.typeConverter as ITypeConverter<Any, Any>) as T
53+
4454
is CMJsonField<*> -> CMJsonField<Any>(schemaField.name, nestedFieldPath) as T
4555
is CMJsonList<*> -> CMJsonList<Any>(schemaField.name, nestedFieldPath) as T
4656
is CMObjectField<*> -> CMObjectField(schemaField.element, schemaField.name, nestedFieldPath) as T

crystal-map-connector/src/test/kotlin/ch/ergon/dope/extensions/type/ObjectTest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import ch.ergon.dope.DopeParameters
44
import ch.ergon.dope.DopeQuery
55
import ch.ergon.dope.DopeQueryManager
66
import ch.ergon.dope.extension.type.getField
7+
import ch.ergon.dope.helper.DateNumberConverterInstance
78
import ch.ergon.dope.helper.ManagerDependentTest
89
import ch.ergon.dope.toDopeType
10+
import com.schwarz.crystalapi.schema.CMConverterField
911
import com.schwarz.crystalapi.schema.CMJsonField
1012
import com.schwarz.crystalapi.schema.CMObjectField
1113
import com.schwarz.crystalapi.schema.Schema
14+
import java.util.*
1215
import kotlin.test.Test
1316
import kotlin.test.assertEquals
1417

@@ -22,6 +25,7 @@ class ObjectTest : ManagerDependentTest {
2225
class Dummy2(path: String = "") : Schema {
2326
val type: CMJsonField<String> = CMJsonField("type", path)
2427
val otherObject: CMObjectField<Dummy3> = CMObjectField(Dummy3(path), "otherObject", path)
28+
val converterField = CMConverterField("converterField", path, DateNumberConverterInstance)
2529
}
2630

2731
class Dummy3(path: String = "") : Schema {
@@ -41,6 +45,20 @@ class ObjectTest : ManagerDependentTest {
4145
assertEquals(expected, actual)
4246
}
4347

48+
@Test
49+
fun `should support object get with converter`() {
50+
val expected = DopeQuery(
51+
"`objectField`.`converterField`",
52+
DopeParameters(),
53+
)
54+
val field: CMConverterField<Date, Number> = Dummy().objectField.getField(Dummy2::converterField)
55+
val underTest = field.toDopeType()
56+
57+
val actual = underTest.toDopeQuery(manager)
58+
59+
assertEquals(expected, actual)
60+
}
61+
4462
@Test
4563
fun `should support object get with path`() {
4664
val expected = DopeQuery(

0 commit comments

Comments
 (0)