Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pluggable coercion

  • Loading branch information...
commit 477662582cc9b0917e469afaefffb354e56d9df3 1 parent ba52ed9
@musketyr musketyr authored
View
8 core/src/main/groovyx/gaelyk/datastore/Coercable.java
@@ -17,6 +17,8 @@
*
* Usually called on empty new object.
*
+ * Calling this method shouldn't change its owner anyway.
+ *
* Never use {@link PogoEntityCoercion} directly in this class but
* you can use {@link DatastoreEntityCoercion} for classes implementing
* {@link DatastoreEntity} or {@link ReflectionEntityCoercion} for the rest.
@@ -30,12 +32,16 @@
/**
* Coerce self to the Entity.
*
+ * Calling this method shouldn't change its owner anyway.
+ *
* Never use {@link PogoEntityCoercion} directly in this class but
* you can use {@link DatastoreEntityCoercion} for classes implementing
* {@link DatastoreEntity} or {@link ReflectionEntityCoercion} for the rest.
*
+ * @param t source object
+ *
* @return entity filled with using this object
*/
- com.google.appengine.api.datastore.Entity coerce();
+ com.google.appengine.api.datastore.Entity coerce(T t);
}
View
8 core/src/main/groovyx/gaelyk/datastore/DatastoreEntityCoercion.java
@@ -57,9 +57,13 @@ private static Object transformValueForStorage(Object value) {
}
return newValue;
}
-
+
@SuppressWarnings("unchecked") public static <E extends DatastoreEntity<?>> E convert(Entity en, Class<E> dsEntityClass) throws InstantiationException, IllegalAccessException{
- E dsEntity = dsEntityClass.newInstance();
+ return convert(en, dsEntityClass, dsEntityClass.newInstance());
+ }
+
+ @SuppressWarnings("unchecked") public static <E extends DatastoreEntity<?>> E convert(Entity en, Class<E> dsEntityClass, E dsEntity) throws InstantiationException, IllegalAccessException{
+
if(dsEntity.hasDatastoreKey()){
if(dsEntity.hasDatastoreNumericKey()){
((DatastoreEntity<Long>)dsEntity).setDatastoreKey(en.getKey().getId());
View
4 core/src/main/groovyx/gaelyk/datastore/PogoEntityCoercion.java
@@ -5,9 +5,9 @@
public class PogoEntityCoercion {
- public static Entity convert(Object dsEntity){
+ public static <T> Entity convert(T dsEntity){
if (dsEntity instanceof Coercable<?>){
- return ((Coercable<?>) dsEntity).coerce();
+ return ((Coercable<T>) dsEntity).coerce(dsEntity);
}
if (dsEntity instanceof DatastoreEntity<?>){
return DatastoreEntityCoercion.convert((DatastoreEntity<?>) dsEntity);
View
5 core/src/main/groovyx/gaelyk/datastore/ReflectionEntityCoercion.groovy
@@ -197,10 +197,9 @@ class ReflectionEntityCoercion {
* @param clazz the class of the object to return
* @return an instance of the class parameter
*/
- static Object convert(Entity e, Class clazz) {
+ static convert(Entity e, Class clazz, o = clazz.newInstance()) {
def entityProps = e.getProperties()
-
- def o = clazz.newInstance()
+
if (o instanceof Map) {
entityProps.each { k, v ->
(o as Map)[k] = v
Please sign in to comment.
Something went wrong with that request. Please try again.