-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from jansigi/feature/schema-class-generation
Feature/schema class generation
- Loading branch information
Showing
13 changed files
with
409 additions
and
40 deletions.
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
crystal-map-api/src/main/java/com/schwarz/crystalapi/SchemaClass.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.schwarz.crystalapi | ||
|
||
@Retention(AnnotationRetention.BINARY) | ||
@Target(AnnotationTarget.CLASS) | ||
annotation class SchemaClass |
13 changes: 13 additions & 0 deletions
13
crystal-map-api/src/main/java/com/schwarz/crystalapi/schema/CMType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.schwarz.crystalapi.schema | ||
|
||
interface CMType { | ||
val path: String | ||
} | ||
|
||
class CMField<T : Any>(val name: String, override val path: String) : CMType | ||
|
||
class CMList<T : Any>(val name: String, override val path: String) : CMType | ||
|
||
class CMObject<out T : Schema>(val element: T, override val path: String) : CMType | ||
|
||
class CMObjectList<out T : Schema>(val element: T, val name: String, override val path: String) : CMType |
3 changes: 3 additions & 0 deletions
3
crystal-map-api/src/main/java/com/schwarz/crystalapi/schema/Schema.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.schwarz.crystalapi.schema | ||
|
||
interface Schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
...processor/src/main/java/com/schwarz/crystalprocessor/generation/model/SchemaGeneration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
package com.schwarz.crystalprocessor.generation.model | ||
|
||
import com.schwarz.crystalapi.schema.CMField | ||
import com.schwarz.crystalapi.schema.CMList | ||
import com.schwarz.crystalapi.schema.CMObject | ||
import com.schwarz.crystalapi.schema.CMObjectList | ||
import com.schwarz.crystalapi.schema.Schema | ||
import com.schwarz.crystalprocessor.model.entity.SchemaClassHolder | ||
import com.schwarz.crystalprocessor.model.field.CblBaseFieldHolder | ||
import com.schwarz.crystalprocessor.model.field.CblFieldHolder | ||
import com.schwarz.crystalprocessor.util.ConversionUtil | ||
import com.schwarz.crystalprocessor.util.TypeUtil | ||
import com.squareup.kotlinpoet.FileSpec | ||
import com.squareup.kotlinpoet.FunSpec | ||
import com.squareup.kotlinpoet.KModifier | ||
import com.squareup.kotlinpoet.ParameterSpec | ||
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy | ||
import com.squareup.kotlinpoet.PropertySpec | ||
import com.squareup.kotlinpoet.TypeName | ||
import com.squareup.kotlinpoet.TypeSpec | ||
import com.squareup.kotlinpoet.asTypeName | ||
|
||
/** | ||
* This class is responsible for generating the Schema classes. | ||
* | ||
* To generate a SchemaClass, add the following annotation to your Class: | ||
* ``` | ||
* @SchemaClass | ||
* ``` | ||
* All the fields will then be generated into a new file. | ||
*/ | ||
class SchemaGeneration { | ||
private val pathAttributeName = "path" | ||
fun generateModel(holder: SchemaClassHolder, schemaClassPaths: List<String>): FileSpec { | ||
val packageName = holder.sourcePackage | ||
val schemaClassName = holder.entitySimpleName | ||
|
||
val schemaClass: TypeSpec.Builder = buildSchemaClass(schemaClassName) | ||
|
||
buildAndAddFieldProperties(holder, schemaClass, schemaClassPaths) | ||
|
||
return FileSpec.builder(packageName, schemaClassName).addType(schemaClass.build()).build() | ||
} | ||
|
||
private fun buildSchemaClass(className: String): TypeSpec.Builder { | ||
val pathParameter = ParameterSpec.builder(pathAttributeName, String::class).defaultValue("%S", "").build() | ||
|
||
return TypeSpec.classBuilder(className) | ||
.addModifiers(KModifier.OPEN) | ||
.addSuperinterface(Schema::class) | ||
.primaryConstructor( | ||
FunSpec.constructorBuilder() | ||
.addParameter(pathParameter) | ||
.build() | ||
) | ||
} | ||
|
||
private fun buildAndAddFieldProperties( | ||
holder: SchemaClassHolder, | ||
schemaClass: TypeSpec.Builder, | ||
schemaClassPaths: List<String> | ||
) { | ||
buildAndAddConstantFieldProperties(holder, schemaClass, schemaClassPaths) | ||
buildAndAddNormalFieldProperties(holder, schemaClass, schemaClassPaths) | ||
} | ||
|
||
private fun buildAndAddConstantFieldProperties( | ||
holder: SchemaClassHolder, | ||
schemaClass: TypeSpec.Builder, | ||
schemaClassPaths: List<String> | ||
) { | ||
holder.fieldConstants.forEach { (fieldName, fieldObject) -> | ||
val defaultVariableName = "DEFAULT_${fieldObject.constantName}" | ||
|
||
val constantProperty = PropertySpec.builder( | ||
defaultVariableName, | ||
fieldObject.fieldType | ||
).initializer( | ||
ConversionUtil.convertStringToDesiredFormat( | ||
fieldObject.typeMirror, | ||
fieldObject.constantValue | ||
) | ||
) | ||
|
||
schemaClass.addProperty(constantProperty.build()) | ||
|
||
buildAndAddFieldProperty( | ||
schemaClass, | ||
fieldName, | ||
fieldObject, | ||
schemaClassPaths | ||
) | ||
} | ||
} | ||
|
||
private fun buildAndAddNormalFieldProperties( | ||
holder: SchemaClassHolder, | ||
schemaClass: TypeSpec.Builder, | ||
schemaClassPaths: List<String> | ||
) { | ||
holder.fields.forEach { (fieldName, fieldObject) -> | ||
buildAndAddFieldProperty( | ||
schemaClass, | ||
fieldName, | ||
fieldObject, | ||
schemaClassPaths | ||
) | ||
} | ||
} | ||
|
||
private fun buildAndAddFieldProperty( | ||
schemaClass: TypeSpec.Builder, | ||
fieldName: String, | ||
fieldObject: CblBaseFieldHolder, | ||
schemaClassPaths: List<String> | ||
): TypeSpec.Builder = schemaClass.addProperty( | ||
buildFieldProperty(fieldObject, fieldName, schemaClassPaths) | ||
) | ||
|
||
private fun buildFieldProperty( | ||
fieldObject: CblBaseFieldHolder, | ||
fieldName: String, | ||
schemaClassPaths: List<String> | ||
): PropertySpec { | ||
val isObject = schemaClassPaths.contains(fieldObject.typeMirror.toString()) | ||
|
||
val outerType = getOuterPropertyType(fieldObject.isIterable, isObject) | ||
|
||
val innerType: TypeName = getInnerPropertyType(fieldObject) | ||
|
||
return PropertySpec.builder( | ||
fieldName, | ||
outerType.parameterizedBy(innerType) | ||
).initializer( | ||
createPropertyFormat(fieldName, innerType, fieldObject.isIterable, isObject), | ||
outerType | ||
).build() | ||
} | ||
|
||
private fun createPropertyFormat( | ||
fieldName: String, | ||
propertyType: TypeName, | ||
isIterable: Boolean, | ||
isObject: Boolean | ||
): String { | ||
val propertyAccessPath = | ||
"if ($pathAttributeName.isBlank()) \"$fieldName\" else \"\$$pathAttributeName.$fieldName\"" | ||
|
||
return when { | ||
isIterable && isObject -> buildObjectListFormat(propertyType, fieldName, propertyAccessPath) | ||
isObject -> buildObjectFormat(propertyType, propertyAccessPath) | ||
else -> buildSimpleFormat(fieldName) | ||
} | ||
} | ||
|
||
private fun buildObjectListFormat(propertyType: TypeName, fieldName: String, propertyAccessPath: String): String = | ||
"""%T( | ||
$propertyType($propertyAccessPath), | ||
"$fieldName", | ||
$pathAttributeName, | ||
)""" | ||
|
||
private fun buildSimpleFormat(fieldName: String): String = | ||
"""%T("$fieldName", $pathAttributeName)""" | ||
|
||
private fun buildObjectFormat(propertyType: TypeName, propertyAccessPath: String): String = | ||
"""%T( | ||
$propertyType($propertyAccessPath), | ||
$pathAttributeName, | ||
)""" | ||
|
||
private fun getOuterPropertyType( | ||
isIterable: Boolean, | ||
isObject: Boolean | ||
) = when { | ||
isIterable && isObject -> CMObjectList::class.asTypeName() | ||
isIterable -> CMList::class.asTypeName() | ||
isObject -> CMObject::class.asTypeName() | ||
else -> CMField::class.asTypeName() | ||
} | ||
|
||
private fun getInnerPropertyType(field: CblBaseFieldHolder): TypeName { | ||
val subEntity = (field as? CblFieldHolder)?.subEntitySimpleName | ||
|
||
return TypeUtil.parseMetaType(field.typeMirror, false, subEntity) | ||
} | ||
} |
Oops, something went wrong.