Skip to content

Commit

Permalink
# implement mandatory flag and validate method on entity/wrapper level
Browse files Browse the repository at this point in the history
# add basic interface to generated companion object
  • Loading branch information
sbra0902 committed Jan 8, 2024
1 parent 449bc6a commit c98e447
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ abstract class CrystalCreator<T : MapSupport, V> {
abstract fun create(): T

abstract fun create(map: MutableMap<String, V>): T
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package com.schwarz.crystalapi

interface MandatoryCheck {
fun validate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package com.schwarz.crystalapi
abstract class WrapperCompanion<T : MapSupport> : CrystalCreator<T, Any?>() {

fun fromMap(obj: MutableMap<String, Any?>?): T? {
if(obj == null) {
if (obj == null) {
return null
}
return create(obj)
}

fun fromMap(obj: List<MutableMap<String, Any?>>?): List<T>? {
if(obj == null) {
if (obj == null) {
return null
}
var result = ArrayList<T>()
for(entry in obj) {
for (entry in obj) {
result.add(create(entry))
}
return result
Expand All @@ -23,16 +23,15 @@ abstract class WrapperCompanion<T : MapSupport> : CrystalCreator<T, Any?>() {
abstract fun toMap(obj: T?): MutableMap<String, Any>

fun toMap(obj: List<T>?): List<MutableMap<String, Any>> {
if(obj == null) {
if (obj == null) {
return listOf()
}
var result = ArrayList<MutableMap<String, Any>>()
for(entry in obj) {
var temp = mutableMapOf<String,Any>()
for (entry in obj) {
var temp = mutableMapOf<String, Any>()
temp.putAll(toMap(entry)!!)
result.add(temp)
}
return result
}

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.schwarz.crystalapi.schema


data class EntitySchema(val name: String, val fields: List<Fields>, val basedOn: List<String>, val queries: List<Queries>, 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?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ import org.junit.Test
class CrystalWrapTest {
@Test
fun `test validate throws exception on null value`() {

val values = mutableMapOf<String, Any>("foo" to "bar")
try{
try {
CrystalWrap.validate(values, arrayOf("foo", "foobar"))
Assert.fail("there should be an exception")
}catch (e: NullPointerException){

} catch (e: NullPointerException) {
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -25,11 +22,11 @@ object ValidateMethodGeneration {
}

validateBuilder.addStatement(
statement, *arguments.toTypedArray()
statement,
*arguments.toTypedArray()
)
}

return validateBuilder.build()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CblBaseFieldHolder>.fieldsToSchemaList(): List<Fields> = 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<CblBaseFieldHolder>.fieldsToSchemaList(): List<Fields> = 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<CblQueryHolder>.queriesToSchemaList(): List<Queries> = map { Queries(it.fields.asList()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CblFieldHolder(field: Field, allWrappers: List<String>) :
): PropertySpec {
var returnType = TypeUtil.parseMetaType(typeMirror, isIterable, subEntitySimpleName)

if(mandatory.not()){
if (mandatory.not()) {
returnType = returnType.copy(nullable = true)
}

Expand All @@ -72,7 +72,7 @@ class CblFieldHolder(field: Field, allWrappers: List<String>) :
): PropertySpec {
var returnType = TypeUtil.parseMetaType(typeMirror, isIterable, subEntitySimpleName)

if(mandatory.not()){
if (mandatory.not()) {
returnType = returnType.copy(nullable = true)
}

Expand Down Expand Up @@ -213,7 +213,7 @@ class CblFieldHolder(field: Field, allWrappers: List<String>) :
}

private fun String.forceCastIfMandatory(mandatory: Boolean): String {
if(mandatory){
if (mandatory) {
return "$this!!"
}
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit c98e447

Please sign in to comment.