Skip to content

Commit

Permalink
fix: Handle lateinit as null in field change (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
thewizkid87 authored Nov 11, 2024
1 parent e296e79 commit 3dedf99
Showing 1 changed file with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,39 @@ abstract class FieldChangeHook<PropertyType, Entity : BaseCrudEntity<*>>(val pro
open fun postChange(originalValue: PropertyType?, newValue: PropertyType?, entity: Entity) {}

fun shouldTrigger(entity: Entity, original: Entity): Boolean {
val originalValue = property.get(original) as PropertyType?
val newValue = property.get(entity) as PropertyType?
val originalValue = property.getValue(original)
val newValue = property.getValue(entity)
return originalValue != newValue
}

fun runPreChange(entity: Entity, original: Entity) {
val originalValue = property.get(original) as PropertyType?
val newValue = property.get(entity) as PropertyType?
val originalValue = property.getValue(original)
val newValue = property.getValue(entity)
if(originalValue == newValue) return
preChange(originalValue, newValue, entity)
}

fun runOnChange(entity: Entity, original: Entity) {
val originalValue = property.get(original) as PropertyType?
val newValue = property.get(entity) as PropertyType?
val originalValue = property.getValue(original)
val newValue = property.getValue(entity)
if(originalValue == newValue) return
onChange(originalValue, newValue, entity)
}

fun runPostChange(entity: Entity, original: Entity) {
val originalValue = property.get(original) as PropertyType?
val newValue = property.get(entity) as PropertyType?
val originalValue = property.getValue(original)
val newValue = property.getValue(entity)
if(originalValue == newValue) return
postChange(originalValue, newValue, entity)
}

fun <Entity, PropertyType> KProperty1<Entity, PropertyType?>.getValue(instance: Entity): PropertyType? {
return try {
this.get(instance)
} catch (e: UninitializedPropertyAccessException) {
null
}
}
}

inline fun <reified T, reified E : BaseCrudEntity<*>> fieldChangeHook(
Expand Down

0 comments on commit 3dedf99

Please sign in to comment.