Skip to content

Commit

Permalink
Use enum instead of boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
codeconsole committed Dec 14, 2024
1 parent 84ef9bc commit 4c1a670
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.grails.datastore.gorm

import grails.gorm.annotation.AutoTimestamp
import static grails.gorm.annotation.AutoTimestamp.EventType.*;
import grails.gorm.tests.GormDatastoreSpec
import grails.persistence.Entity

Expand Down Expand Up @@ -39,6 +40,6 @@ class CustomAutoTimestampSpec extends GormDatastoreSpec {
class RecordCustom {
Long id
String name
@AutoTimestamp(false) Date created
@AutoTimestamp(ON_INSERT) Date created
@AutoTimestamp Date modified
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface AutoTimestamp {

/**
* Enum to specify when auto-timestamping should occur.
*/
enum EventType {
ON_INSERT,
ON_UPDATE
}

/**
* Whether to include auto-timestamping on update events.
* Setting value to false only performs auto-timestamping on insert events.
* When to apply auto-timestamping
*/
boolean value() default true;
EventType value() default EventType.ON_UPDATE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ protected void storeDateCreatedAndLastUpdatedInfo(PersistentEntity persistentEnt
Field field = getFieldFromHierarchy(persistentEntity, property.getName());
if (field != null && field.isAnnotationPresent(AutoTimestamp.class)) {
AutoTimestamp autoTimestamp = field.getAnnotation(AutoTimestamp.class);
if (autoTimestamp.value()) {
if (autoTimestamp.value() == AutoTimestamp.EventType.ON_UPDATE) {
storeTimestampAvailability(entitiesWithLastUpdated, persistentEntity, property);
} else {
storeTimestampAvailability(entitiesWithDateCreated, persistentEntity, property);
Expand Down

0 comments on commit 4c1a670

Please sign in to comment.