Skip to content

Commit

Permalink
Merge pull request #47 from SchwarzIT/feature/Handle_lists_of_Enums_i…
Browse files Browse the repository at this point in the history
…n_EnumConversion

Feature/handle lists of enums in enum conversion
  • Loading branch information
michael-gut-ergon authored Sep 15, 2021
2 parents 3bbd93a + 1f951fd commit 79515d6
Show file tree
Hide file tree
Showing 27 changed files with 239 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ captures/
.idea/gradle.xml
.idea/dictionaries
.idea/libraries
.idea/sonarlint
.idea/shelf

# Keystore files
*.jks
Expand Down
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.72'
ext.kotlin_version = '1.5.30'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
Expand All @@ -19,10 +19,9 @@ allprojects {
repositories {
google()
mavenCentral()
jcenter()
maven { url 'https://maven.google.com' }
maven {
url "http://mobile.maven.couchbase.com/maven2/dev/"
url "https://mobile.maven.couchbase.com/maven2/dev/"
}
}
}
4 changes: 2 additions & 2 deletions couchbase-entity-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ artifacts {
archives javadocJar
}
buildscript {
ext.kotlin_version = '1.3.41'
ext.kotlin_version = '1.5.30'
repositories {
mavenCentral()
}
Expand All @@ -43,4 +43,4 @@ compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package kaufland.com.coachbasebinderapi


import java.lang.annotation.ElementType
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.Target
import kotlin.reflect.KClass

@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
@kotlin.annotation.Retention(AnnotationRetention.BINARY)
@kotlin.annotation.Target(AnnotationTarget.CLASS)
annotation class Comment(val comment: Array<String> = [])
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class DefaultSchemaValidator : SchemaValidator {
current?.let {

released.docId?.let {
if (current?.docId?.scheme != it.scheme) {
if (current.docId?.scheme != it.scheme) {
logger.error(released, "forbidden DocId Schema change")
}
}
Expand All @@ -40,18 +40,18 @@ open class DefaultSchemaValidator : SchemaValidator {
}

protected open fun validateFieldLevel(released: EntitySchema, key: String, logger: SchemaValidationLogger) {
if (released?.deprecatedSchema?.deprecatedFields?.find { it.field == key }?.inUse != false) {
if (released.deprecatedSchema?.deprecatedFields?.find { it.field == key }?.inUse != false) {
logger.error(released, "forbidden change on existing field [$key]")
} else {
logger.info(released, "allowed change on existing field [$key] since it's deprecated and no longer in use")
}
}

private fun modelDeletedDuringValidDeprecationPeriod(released: EntitySchema, logger: SchemaValidationLogger) {
if (released?.deprecatedSchema == null || released?.deprecatedSchema.inUse) {
if (released.deprecatedSchema == null || released.deprecatedSchema.inUse) {
logger.error(released, "forbidden model deletion")
} else {
logger.error(released, "allowed model deletion")
}
}
}
}
2 changes: 1 addition & 1 deletion couchbase-entity-connector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'kotlin-android'
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
Expand Down
3 changes: 1 addition & 2 deletions couchbase-entity/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ dependencies {
kapt "com.google.auto.service:auto-service:1.0-rc7"
compileOnly "com.google.auto.service:auto-service:1.0-rc7"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

}

targetCompatibility = '1.7'
Expand Down Expand Up @@ -54,7 +53,7 @@ artifacts {
archives javadocJar
}
buildscript {
ext.kotlin_version = '1.3.41'
ext.kotlin_version = '1.5.30'
repositories {
mavenCentral()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,4 @@ object ProcessingContext {
return createdQualifiedClazzNames.any { it.simpleName == name } && name.let { it.endsWith("Wrapper") || it.endsWith("Entity") }
}
}

}
}
75 changes: 64 additions & 11 deletions couchbase-entity/src/main/java/com/kaufland/model/EntityFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,58 @@ import javax.lang.model.element.Modifier

object EntityFactory {

fun createEntityHolder(cblEntityElement: Element, allWrappers: List<String>, allBaseModels: Map<String, BaseModelHolder>): EntityHolder {
fun createEntityHolder(
cblEntityElement: Element,
allWrappers: List<String>,
allBaseModels: Map<String, BaseModelHolder>
): EntityHolder {
val annotation = cblEntityElement.getAnnotation(Entity::class.java)
return create(cblEntityElement, EntityHolder(annotation.database, annotation.modifierOpen, annotation.type), allWrappers, allBaseModels) as EntityHolder
return create(
cblEntityElement,
EntityHolder(annotation.database, annotation.modifierOpen, annotation.type),
allWrappers,
allBaseModels
) as EntityHolder
}

fun createBaseModelHolder(cblEntityElement: Element, allWrappers: List<String>): BaseModelHolder {
return create(cblEntityElement, BaseModelHolder(), allWrappers, emptyMap()) as BaseModelHolder
fun createBaseModelHolder(
cblEntityElement: Element,
allWrappers: List<String>
): BaseModelHolder {
return create(
cblEntityElement,
BaseModelHolder(),
allWrappers,
emptyMap()
) as BaseModelHolder
}

fun createChildEntityHolder(cblEntityElement: Element, allWrappers: List<String>, allBaseModels: Map<String, BaseModelHolder>): WrapperEntityHolder {
fun createChildEntityHolder(
cblEntityElement: Element,
allWrappers: List<String>,
allBaseModels: Map<String, BaseModelHolder>
): WrapperEntityHolder {
val annotation = cblEntityElement.getAnnotation(MapWrapper::class.java)
return create(cblEntityElement, WrapperEntityHolder(annotation.modifierOpen), allWrappers, allBaseModels) as WrapperEntityHolder
return create(
cblEntityElement,
WrapperEntityHolder(annotation.modifierOpen),
allWrappers,
allBaseModels
) as WrapperEntityHolder
}

private fun create(cblEntityElement: Element, content: BaseEntityHolder, allWrappers: List<String>, allBaseModels: Map<String, BaseModelHolder>): BaseEntityHolder {
private fun create(
cblEntityElement: Element,
content: BaseEntityHolder,
allWrappers: List<String>,
allBaseModels: Map<String, BaseModelHolder>
): BaseEntityHolder {

content.abstractParts = findPossibleOverrides(cblEntityElement)
content.sourceElement = cblEntityElement
content.comment = cblEntityElement.getAnnotation(Comment::class.java)?.comment ?: arrayOf()
content.deprecated = cblEntityElement.getAnnotation(Deprecated::class.java)?.let { DeprecatedModel(it) }
content.deprecated =
cblEntityElement.getAnnotation(Deprecated::class.java)?.let { DeprecatedModel(it) }


addBasedOn(cblEntityElement, allBaseModels, content)
Expand All @@ -56,14 +88,22 @@ object EntityFactory {

parseStaticsFromStructure(cblEntityElement) {
if (it.getAnnotation(GenerateAccessor::class.java) != null) {
content.generateAccessors.add(CblGenerateAccessorHolder(content.sourceClazzTypeName, it))
content.generateAccessors.add(
CblGenerateAccessorHolder(
content.sourceClazzTypeName,
it
)
)
}
it.getAnnotation(DocIdSegment::class.java)?.apply {
docIdSegments.add(DocIdSegmentHolder(this, it))
}
}

content.docId = docId?.let { DocIdHolder(it, docIdSegments) }
content.docId = docId?.let { DocIdHolder(it, docIdSegments) } ?: content.docId?.apply {
customSegmentSource.addAll(docIdSegments)
recompile()
}

return content

Expand All @@ -79,6 +119,14 @@ object EntityFactory {

basedOnValue?.forEach { type ->
allBaseModels[type.toString()]?.let {
it.docId?.let {
if (content.docId == null) {
content.docId = it
} else {
content.docId?.customSegmentSource?.addAll(it.customSegmentSource)
content.docId?.recompile()
}
}
content.basedOn.add(it)
content.fieldConstants.putAll(it.fieldConstants)
content.fields.putAll(it.fields)
Expand All @@ -88,7 +136,12 @@ object EntityFactory {
}
}

private fun parseFields(cblEntityElement: Element, content: BaseEntityHolder, allWrappers: List<String>, allBaseModels: Map<String, BaseModelHolder>) {
private fun parseFields(
cblEntityElement: Element,
content: BaseEntityHolder,
allWrappers: List<String>,
allBaseModels: Map<String, BaseModelHolder>
) {
val fields = cblEntityElement.getAnnotation(Fields::class.java)

for (cblField in fields.value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,26 @@ class DeprecatedModel(deprecated: Deprecated) {

val deprecatedFields: Map<String, DeprecatedField> = deprecated.fields.map { it.field to it }.toMap()

init {
deprecatedFields.forEach {
print("field ${it.key}, value ${it.value.inUse}")
}
}

fun addDeprecated(field: String, spec: PropertySpec.Builder) {

deprecatedFields[field]?.let {
print("field ${it.field} inUse ${it.inUse}")
spec.addAnnotation(buildDeprecatedAnnotation(it.inUse, it.replacedBy))
}
}

fun evaluateFieldDeprecationLevel(field: String) : DeprecationLevel? = deprecatedFields[field]?.let { evaluateDeprecationLevel(it.inUse) }

fun addDeprecated(field: String, spec: FunSpec.Builder) : Boolean {

return deprecatedFields[field]?.let {
print("field ${it.field} inUse ${it.inUse}")
spec.addAnnotation(buildDeprecatedAnnotation(it.inUse, it.replacedBy))
true
} ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class CblFieldHolder(field: Field, allWrappers: List<String>) : CblBaseFieldHold
builder.addKdoc(KDocGeneration.generate(comment))
}

if (deprecated?.inUse == false && deprecated.addDeprecated(dbField, builder)){
if (deprecated?.evaluateFieldDeprecationLevel(dbField) == DeprecationLevel.ERROR && deprecated?.addDeprecated(dbField, builder)){
builder.addStatement("throw %T()", UnsupportedOperationException::class)
} else {
builder.addStatement("obj.${accessorSuffix()} = value")
Expand All @@ -132,7 +132,11 @@ class CblFieldHolder(field: Field, allWrappers: List<String>) : CblBaseFieldHold

private fun evaluateClazzForTypeConversion(): TypeName {
return if (isIterable) {
TypeUtil.string()
if (TypeUtil.isMap(fieldType)) {
TypeUtil.string()
} else {
fieldType
}
} else TypeUtil.parseMetaType(typeMirror, isIterable, false, subEntitySimpleName)
}
}
Loading

0 comments on commit 79515d6

Please sign in to comment.