From c98e4475ac51f0edd518bd3fe7dcb509da68d486 Mon Sep 17 00:00:00 2001 From: Sven Braune Date: Mon, 8 Jan 2024 10:59:27 +0100 Subject: [PATCH] # implement mandatory flag and validate method on entity/wrapper level # add basic interface to generated companion object --- .../java/com/schwarz/crystalapi/CrystalCreator.kt | 2 +- .../java/com/schwarz/crystalapi/MandatoryCheck.kt | 2 +- .../com/schwarz/crystalapi/WrapperCompanion.kt | 15 +++++++-------- .../com/schwarz/crystalapi/schema/EntitySchema.kt | 1 - .../schwarz/crystalapi/util/CrystalWrapTest.kt | 8 +++----- .../generation/model/ValidateMethodGeneration.kt | 7 ++----- .../generation/model/WrapperGeneration.kt | 2 +- .../crystalprocessor/meta/SchemaGenerator.kt | 2 +- .../model/field/CblFieldHolder.kt | 6 +++--- .../com/schwarz/crystalprocessor/util/TypeUtil.kt | 4 ++-- 10 files changed, 21 insertions(+), 28 deletions(-) diff --git a/crystal-map-api/src/main/java/com/schwarz/crystalapi/CrystalCreator.kt b/crystal-map-api/src/main/java/com/schwarz/crystalapi/CrystalCreator.kt index ff9eb0b0..bb162e10 100644 --- a/crystal-map-api/src/main/java/com/schwarz/crystalapi/CrystalCreator.kt +++ b/crystal-map-api/src/main/java/com/schwarz/crystalapi/CrystalCreator.kt @@ -5,4 +5,4 @@ abstract class CrystalCreator { abstract fun create(): T abstract fun create(map: MutableMap): T -} \ No newline at end of file +} diff --git a/crystal-map-api/src/main/java/com/schwarz/crystalapi/MandatoryCheck.kt b/crystal-map-api/src/main/java/com/schwarz/crystalapi/MandatoryCheck.kt index 879d86d3..7df2d725 100644 --- a/crystal-map-api/src/main/java/com/schwarz/crystalapi/MandatoryCheck.kt +++ b/crystal-map-api/src/main/java/com/schwarz/crystalapi/MandatoryCheck.kt @@ -2,4 +2,4 @@ package com.schwarz.crystalapi interface MandatoryCheck { fun validate() -} \ No newline at end of file +} diff --git a/crystal-map-api/src/main/java/com/schwarz/crystalapi/WrapperCompanion.kt b/crystal-map-api/src/main/java/com/schwarz/crystalapi/WrapperCompanion.kt index 12a374d4..6056455a 100644 --- a/crystal-map-api/src/main/java/com/schwarz/crystalapi/WrapperCompanion.kt +++ b/crystal-map-api/src/main/java/com/schwarz/crystalapi/WrapperCompanion.kt @@ -3,18 +3,18 @@ package com.schwarz.crystalapi abstract class WrapperCompanion : CrystalCreator() { fun fromMap(obj: MutableMap?): T? { - if(obj == null) { + if (obj == null) { return null } return create(obj) } fun fromMap(obj: List>?): List? { - if(obj == null) { + if (obj == null) { return null } var result = ArrayList() - for(entry in obj) { + for (entry in obj) { result.add(create(entry)) } return result @@ -23,16 +23,15 @@ abstract class WrapperCompanion : CrystalCreator() { abstract fun toMap(obj: T?): MutableMap fun toMap(obj: List?): List> { - if(obj == null) { + if (obj == null) { return listOf() } var result = ArrayList>() - for(entry in obj) { - var temp = mutableMapOf() + for (entry in obj) { + var temp = mutableMapOf() temp.putAll(toMap(entry)!!) result.add(temp) } return result } - -} \ No newline at end of file +} diff --git a/crystal-map-api/src/main/java/com/schwarz/crystalapi/schema/EntitySchema.kt b/crystal-map-api/src/main/java/com/schwarz/crystalapi/schema/EntitySchema.kt index 1d9a2f7b..047222a5 100644 --- a/crystal-map-api/src/main/java/com/schwarz/crystalapi/schema/EntitySchema.kt +++ b/crystal-map-api/src/main/java/com/schwarz/crystalapi/schema/EntitySchema.kt @@ -1,6 +1,5 @@ package com.schwarz.crystalapi.schema - data class EntitySchema(val name: String, val fields: List, val basedOn: List, val queries: List, val docId: DocId?, val deprecatedSchema: DeprecatedSchema?) data class Fields(val dbField: String, val fieldType: String, val isIterable: Boolean, val isConstant: Boolean, val defaultValue: String, val mandatory: Boolean?) diff --git a/crystal-map-api/src/test/java/com/schwarz/crystalapi/util/CrystalWrapTest.kt b/crystal-map-api/src/test/java/com/schwarz/crystalapi/util/CrystalWrapTest.kt index 0bb9669d..bb0b58e8 100644 --- a/crystal-map-api/src/test/java/com/schwarz/crystalapi/util/CrystalWrapTest.kt +++ b/crystal-map-api/src/test/java/com/schwarz/crystalapi/util/CrystalWrapTest.kt @@ -6,13 +6,11 @@ import org.junit.Test class CrystalWrapTest { @Test fun `test validate throws exception on null value`() { - val values = mutableMapOf("foo" to "bar") - try{ + try { CrystalWrap.validate(values, arrayOf("foo", "foobar")) Assert.fail("there should be an exception") - }catch (e: NullPointerException){ - + } catch (e: NullPointerException) { } } -} \ No newline at end of file +} diff --git a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/ValidateMethodGeneration.kt b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/ValidateMethodGeneration.kt index af3f63aa..b84a8a6e 100644 --- a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/ValidateMethodGeneration.kt +++ b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/ValidateMethodGeneration.kt @@ -5,18 +5,15 @@ import com.schwarz.crystalprocessor.model.entity.BaseEntityHolder import com.schwarz.crystalprocessor.util.TypeUtil import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.KModifier -import com.squareup.kotlinpoet.MemberName object ValidateMethodGeneration { fun generate(holder: BaseEntityHolder, useMDocChanges: Boolean): FunSpec { - val validateBuilder = FunSpec.builder("validate").addModifiers(KModifier.PUBLIC, KModifier.OVERRIDE) val mandatoryFields = holder.fields.values.filter { it.mandatory }.map { it.constantName } if (mandatoryFields.isNotEmpty()) { - val statement = "%T.validate(toMap(), %M(${mandatoryFields.map { "%N" }.joinToString()}))" val arguments = mutableListOf(CrystalWrap::class, TypeUtil.arrayOf()) @@ -25,11 +22,11 @@ object ValidateMethodGeneration { } validateBuilder.addStatement( - statement, *arguments.toTypedArray() + statement, + *arguments.toTypedArray() ) } return validateBuilder.build() } - } diff --git a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/WrapperGeneration.kt b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/WrapperGeneration.kt index 75b14cee..135ea122 100644 --- a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/WrapperGeneration.kt +++ b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/generation/model/WrapperGeneration.kt @@ -78,7 +78,7 @@ class WrapperGeneration { .beginControlFlow("obj.mDoc.forEach") .beginControlFlow("if(it.value != null)").addStatement("result[it.key] = it.value!!").endControlFlow() .endControlFlow() - .addStatement("return result").build(), + .addStatement("return result").build() ) } diff --git a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/meta/SchemaGenerator.kt b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/meta/SchemaGenerator.kt index 69ab6b61..39ae697f 100644 --- a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/meta/SchemaGenerator.kt +++ b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/meta/SchemaGenerator.kt @@ -40,7 +40,7 @@ class SchemaGenerator(path: String, val fileName: String) { private fun DeprecatedModel.deprecatedToSchema(): DeprecatedSchema = DeprecatedSchema(replacedBy = this.replacedByTypeMirror.toString(), inUse = this.deprecationType == DeprecationType.FIELD_DEPRECATION || this.deprecationType == DeprecationType.ENTITY_DEPRECATION, deprecatedFields = this.deprecatedFields.values.map { DeprecatedFields(field = it.field, replacedBy = it.replacedBy, inUse = it.inUse) }) - private fun List.fieldsToSchemaList(): List = map { Fields(dbField = it.dbField, fieldType = it.fieldType.toString(), isIterable = it.isIterable, isConstant = it.isConstant, defaultValue = it.defaultValue, mandatory = (if(it.mandatory) true else null)) } + private fun List.fieldsToSchemaList(): List = map { Fields(dbField = it.dbField, fieldType = it.fieldType.toString(), isIterable = it.isIterable, isConstant = it.isConstant, defaultValue = it.defaultValue, mandatory = (if (it.mandatory) true else null)) } private fun List.queriesToSchemaList(): List = map { Queries(it.fields.asList()) } } diff --git a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/model/field/CblFieldHolder.kt b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/model/field/CblFieldHolder.kt index 9810ff6f..de7a0540 100644 --- a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/model/field/CblFieldHolder.kt +++ b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/model/field/CblFieldHolder.kt @@ -52,7 +52,7 @@ class CblFieldHolder(field: Field, allWrappers: List) : ): PropertySpec { var returnType = TypeUtil.parseMetaType(typeMirror, isIterable, subEntitySimpleName) - if(mandatory.not()){ + if (mandatory.not()) { returnType = returnType.copy(nullable = true) } @@ -72,7 +72,7 @@ class CblFieldHolder(field: Field, allWrappers: List) : ): PropertySpec { var returnType = TypeUtil.parseMetaType(typeMirror, isIterable, subEntitySimpleName) - if(mandatory.not()){ + if (mandatory.not()) { returnType = returnType.copy(nullable = true) } @@ -213,7 +213,7 @@ class CblFieldHolder(field: Field, allWrappers: List) : } private fun String.forceCastIfMandatory(mandatory: Boolean): String { - if(mandatory){ + if (mandatory) { return "$this!!" } return this diff --git a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/util/TypeUtil.kt b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/util/TypeUtil.kt index 789e2485..e3ff2f6a 100644 --- a/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/util/TypeUtil.kt +++ b/crystal-map-processor/src/main/java/com/schwarz/crystalprocessor/util/TypeUtil.kt @@ -80,11 +80,11 @@ object TypeUtil { return ClassName("kotlin.collections", "List").parameterizedBy(typeName) } - fun crystalCreator(valueType: TypeName, type: TypeName): ParameterizedTypeName{ + fun crystalCreator(valueType: TypeName, type: TypeName): ParameterizedTypeName { return CrystalCreator::class.asTypeName().parameterizedBy(listOf(type, valueType)) } - fun wrapperCompanion(type: TypeName): ParameterizedTypeName{ + fun wrapperCompanion(type: TypeName): ParameterizedTypeName { return WrapperCompanion::class.asTypeName().parameterizedBy(type) }