Skip to content

Commit

Permalink
Merge pull request #75 from ergon/bugfix/DOPE-279-fix-converter-field…
Browse files Browse the repository at this point in the history
…-access-in-nested-object

DOPE-279: fix converterField access in nested object call
  • Loading branch information
pgruntz authored Feb 3, 2025
2 parents fbbd9db + 6742152 commit 2ba66aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package ch.ergon.dope.extension.type
import ch.ergon.dope.resolvable.expression.unaliased.type.Field
import ch.ergon.dope.toDopeType
import ch.ergon.dope.validtype.ObjectType
import com.schwarz.crystalapi.ITypeConverter
import com.schwarz.crystalapi.schema.CMConverterField
import com.schwarz.crystalapi.schema.CMConverterList
import com.schwarz.crystalapi.schema.CMJsonField
import com.schwarz.crystalapi.schema.CMJsonList
import com.schwarz.crystalapi.schema.CMObjectField
Expand Down Expand Up @@ -37,10 +40,17 @@ class ObjectField<S : Schema>(val schema: S, val name: String, val path: String)
* @throws IllegalStateException if the attribute type is not supported
* @return the retrieved CMType
*/
@Suppress("UNCHECKED_CAST")
inline fun <reified T : CMType, S : Schema> ObjectField<S>.getField(field: KProperty1<S, T>): T {
val schemaField = field.get(schema)
val nestedFieldPath = if (path.isBlank()) name else "$path.$name"
return when (schemaField) {
is CMConverterField<*, *> ->
CMConverterField(schemaField.name, nestedFieldPath, schemaField.typeConverter as ITypeConverter<Any, Any>) as T

is CMConverterList<*, *> ->
CMConverterList(schemaField.name, nestedFieldPath, schemaField.typeConverter as ITypeConverter<Any, Any>) as T

is CMJsonField<*> -> CMJsonField<Any>(schemaField.name, nestedFieldPath) as T
is CMJsonList<*> -> CMJsonList<Any>(schemaField.name, nestedFieldPath) as T
is CMObjectField<*> -> CMObjectField(schemaField.element, schemaField.name, nestedFieldPath) as T
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import ch.ergon.dope.DopeParameters
import ch.ergon.dope.DopeQuery
import ch.ergon.dope.DopeQueryManager
import ch.ergon.dope.extension.type.getField
import ch.ergon.dope.helper.DateNumberConverterInstance
import ch.ergon.dope.helper.ManagerDependentTest
import ch.ergon.dope.toDopeType
import com.schwarz.crystalapi.schema.CMConverterField
import com.schwarz.crystalapi.schema.CMJsonField
import com.schwarz.crystalapi.schema.CMObjectField
import com.schwarz.crystalapi.schema.Schema
import java.util.*
import kotlin.test.Test
import kotlin.test.assertEquals

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

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

@Test
fun `should support object get with converter`() {
val expected = DopeQuery(
"`objectField`.`converterField`",
DopeParameters(),
)
val field: CMConverterField<Date, Number> = Dummy().objectField.getField(Dummy2::converterField)
val underTest = field.toDopeType()

val actual = underTest.toDopeQuery(manager)

assertEquals(expected, actual)
}

@Test
fun `should support object get with path`() {
val expected = DopeQuery(
Expand Down

0 comments on commit 2ba66aa

Please sign in to comment.