Skip to content

Commit

Permalink
ignored static fields #158, added a few DelegatesTo annos
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Feb 9, 2013
1 parent a33e35b commit 4e9cede
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
19 changes: 13 additions & 6 deletions core/src/main/groovyx/gaelyk/datastore/PogoEntityCoercion.groovy
Expand Up @@ -15,6 +15,8 @@
*/
package groovyx.gaelyk.datastore

import java.lang.reflect.Modifier;

import com.google.appengine.api.datastore.Entities
import com.google.appengine.api.datastore.Entity
import com.google.appengine.api.datastore.EntityNotFoundException
Expand Down Expand Up @@ -49,20 +51,25 @@ class PogoEntityCoercion {
cachedProps[clazz] = p.properties.findAll { String k, v -> !(k in ['class', 'metaClass']) && !(k.startsWith('$') || k.startsWith('_')) }
.collectEntries { String k, v ->
def annos
def isStatic = false
try {
annos = p.class.getDeclaredField(k).annotations
def field = p.class.getDeclaredField(k)
isStatic = Modifier.isStatic(field.modifiers)
annos = field.annotations
} catch (e) {
try {
annos = p.class.getDeclaredMethod("get${k.capitalize()}").annotations
def method = p.class.getDeclaredMethod("get${k.capitalize()}")
annos = method.annotations
isStatic = Modifier.isStatic(method.modifiers)
} catch (NoSuchMethodException nsme){
return [(k), [ignore: {true}, unindexed: {false}, key: {false}, version: { false }]]
}
}
[(k), [
ignore: { annos.any { it instanceof Ignore } },
unindexed: { defaultIndexed ? annos.any { it instanceof Unindexed } : !annos.any { it instanceof Indexed } },
key: { annos.any { it instanceof Key } },
version: { annos.any { it instanceof Version } }
ignore: { isStatic || annos.any { it instanceof Ignore } },
unindexed: { isStatic || defaultIndexed ? annos.any { it instanceof Unindexed } : !annos.any { it instanceof Indexed } },
key: { !isStatic || annos.any { it instanceof Key } },
version: { !isStatic || annos.any { it instanceof Version } }
]]
}
}
Expand Down
Expand Up @@ -36,7 +36,7 @@ class BackendExtensions {
* @param manager the lifecycle manager
* @param c the closure as shutdown hook
*/
static void shutdownHook(LifecycleManager manager, Closure c) {
static void shutdownHook(LifecycleManager manager, @DelegatesTo(LifecycleManager.ShutdownHook) Closure c) {
manager.setShutdownHook(c as LifecycleManager.ShutdownHook)
}

Expand Down
Expand Up @@ -557,7 +557,7 @@ class DatastoreExtensions {
* @param c the closure representing the query
* @return the query
*/
static Query query(DatastoreService service, Closure c) {
static Query query(DatastoreService service, @DelegatesTo(value=QueryBuilder, strategy=Closure.DELEGATE_FIRST) Closure c) {
Closure cQuery = c.clone()
cQuery.resolveStrategy = Closure.DELEGATE_FIRST
def builder = new QueryBuilder(c.thisObject instanceof Script ? c.thisObject.binding : null)
Expand All @@ -573,7 +573,7 @@ class DatastoreExtensions {
* @return the results
*/
@CompileStatic
static execute(DatastoreService service, Closure c) {
static execute(DatastoreService service, @DelegatesTo(value=QueryBuilder, strategy=Closure.DELEGATE_FIRST) Closure c) {
QueryBuilder builder = prepareAndLaunchQuery(c)
return builder.execute()
}
Expand All @@ -585,12 +585,12 @@ class DatastoreExtensions {
* @return the iterator over the results
*/
@CompileStatic
static iterate(DatastoreService service, Closure c) {
static iterate(DatastoreService service, @DelegatesTo(value=QueryBuilder, strategy=Closure.DELEGATE_FIRST) Closure c) {
QueryBuilder builder = prepareAndLaunchQuery(c)
return builder.iterate()
}

private static QueryBuilder prepareAndLaunchQuery(Closure c) {
private static QueryBuilder prepareAndLaunchQuery(@DelegatesTo(value=QueryBuilder, strategy=Closure.DELEGATE_FIRST) Closure c) {
Closure cQuery = c.clone()
cQuery.resolveStrategy = Closure.DELEGATE_FIRST
def builder = new QueryBuilder(c.thisObject instanceof Script ? c.thisObject.binding : null)
Expand Down
Expand Up @@ -95,7 +95,7 @@ class ImageExtensions {
* @param c the closure containg the various transform steps
* @return a transformed image
*/
static Image transform(Image selfImage, Closure c) {
static Image transform(Image selfImage, @DelegatesTo(value=CompositeTransform, strategy=Closure.DELEGATE_ONLY) Closure c) {
Closure clone = c.clone()
clone.resolveStrategy = Closure.DELEGATE_ONLY

Expand Down
Expand Up @@ -93,7 +93,7 @@ class SearchExtensions {
*/
@Deprecated
@CompileStatic
static AddResponse add(Index index, Closure closure) {
static AddResponse add(Index index, @DelegatesTo(value=DocumentDefinitions, strategy=Closure.DELEGATE_FIRST) Closure closure) {
def docDefClosure = (Closure)closure.clone()
docDefClosure.resolveStrategy = Closure.DELEGATE_FIRST
def definitions = new DocumentDefinitions()
Expand Down Expand Up @@ -131,7 +131,7 @@ class SearchExtensions {
* @return an instance of PutResponse
*/
@CompileStatic
static PutResponse put(Index index, Closure closure) {
static PutResponse put(Index index, @DelegatesTo(value=DocumentDefinitions, strategy=Closure.DELEGATE_FIRST) Closure closure) {
def docDefClosure = (Closure)closure.clone()
docDefClosure.resolveStrategy = Closure.DELEGATE_FIRST
def definitions = new DocumentDefinitions()
Expand Down Expand Up @@ -169,7 +169,7 @@ class SearchExtensions {
* @return an instance of PutResponse
*/
@CompileStatic
static Future<PutResponse> putAsync(Index index, Closure closure) {
static Future<PutResponse> putAsync(Index index, @DelegatesTo(value=DocumentDefinitions, strategy=Closure.DELEGATE_FIRST) Closure closure) {
def docDefClosure = (Closure)closure.clone()
docDefClosure.resolveStrategy = Closure.DELEGATE_FIRST
def definitions = new DocumentDefinitions()
Expand Down
Expand Up @@ -47,6 +47,11 @@ class PogoEntityCoercionTest extends GroovyTestCase {
assert !props.s4.ignore()
assert !props.s4.key()
assert props.s4.version()

assert props.s5.unindexed()
assert props.s5.ignore()
assert !props.s4.key()
assert !props.s4.version()

assert PogoEntityCoercion.findKey(props) == 's3'

Expand Down Expand Up @@ -160,6 +165,7 @@ class P1 {
@Ignore String s2
@Key String s3
@Version long s4
static String s5
}

@groovyx.gaelyk.datastore.Entity(unindexed = false)
Expand Down

0 comments on commit 4e9cede

Please sign in to comment.