Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement to cmobject #87

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading