Skip to content

Commit

Permalink
Merge pull request #87 from SchwarzIT/improvement-to-cmobject
Browse files Browse the repository at this point in the history
Improvement to cmobject
  • Loading branch information
jansigi authored Oct 24, 2024
2 parents 67cb9c2 + 58cbea9 commit 6737df3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package com.schwarz.crystalapi.schema
import com.schwarz.crystalapi.ITypeConverter

sealed interface CMType {
val name: String
val path: String
}

open class CMJsonField<T>(val name: String, override val path: String) : CMType
open class CMJsonField<T>(override val name: String, override val path: String) : CMType

open class CMJsonList<T>(val name: String, override val path: String) : CMType
open class CMJsonList<T>(override val name: String, override val path: String) : CMType

class CMObject<out T : Schema>(val element: T, override val path: String) : CMType
class CMObjectField<out T : Schema>(val element: T, override val name: String, override val path: String) : CMType

class CMObjectList<out T : Schema>(val element: T, val name: String, override val path: String) : CMType
class CMObjectList<out T : Schema>(val element: T, override val name: String, override val path: String) : CMType

class CMConverterField<KotlinType, MapType>(
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class SchemaGeneration {

return when {
isIterable && isObject -> buildObjectListFormat(propertyType, fieldName, propertyAccessPath)
isObject -> buildObjectFormat(propertyType, propertyAccessPath)
isObject -> buildObjectFormat(propertyType, fieldName, propertyAccessPath)
else -> buildSimpleFormat(fieldName)
}
}
Expand All @@ -188,9 +188,10 @@ class SchemaGeneration {
private fun buildSimpleFormat(fieldName: String): String =
"""%T("$fieldName", $pathAttributeName)"""

private fun buildObjectFormat(propertyType: TypeName, propertyAccessPath: String): String =
private fun buildObjectFormat(propertyType: TypeName, fieldName: String, propertyAccessPath: String): String =
"""%T(
$propertyType($propertyAccessPath),
"$fieldName",
$pathAttributeName,
)"""

Expand All @@ -203,7 +204,7 @@ class SchemaGeneration {
hasProperty -> CMConverterField::class.asTypeName()
isIterable && isObject -> CMObjectList::class.asTypeName()
isIterable -> CMJsonList::class.asTypeName()
isObject -> CMObject::class.asTypeName()
isObject -> CMObjectField::class.asTypeName()
else -> CMJsonField::class.asTypeName()
}

Expand Down
5 changes: 3 additions & 2 deletions crystal-map-processor/src/test/resources/ExpectedSchema.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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.CMObject
import com.schwarz.crystalapi.schema.CMObjectField
import com.schwarz.crystalapi.schema.CMObjectList
import com.schwarz.crystalapi.schema.Schema
import java.time.OffsetDateTime
Expand All @@ -27,9 +27,10 @@ public open class SubSchema(

public val list: CMJsonList<String> = CMJsonList("list", path)

public val someObject: CMObject<TestObjectSchema> = CMObject(
public val someObject: CMObjectField<TestObjectSchema> = CMObjectField(
com.kaufland.testModels.TestObjectSchema(if (path.isBlank()) "someObject" else
"$path.someObject"),
"someObject",
path,
)

Expand Down

0 comments on commit 6737df3

Please sign in to comment.