我正在做一些动态过滤,并且有这个:
class Filterable {
def statusId
def secondaryFilterable
}
...
def filter = new Filter(validIds: [1], fieldName: 'statusId')
...
class Filter {
def validIds = [] as Set
def fieldName
private boolean containsFieldValue(input) {
def fieldValue = input."${fieldName}"
return fieldValue in validIds
}
}
它只适用于一个属性。但是,现在我需要通过第二个可筛选对象进行过滤-类似于
def filter = new Filter(validIds: [1], fieldName: 'secondaryFilterable.statusId')
这会抛出一个groovy.lang.MissingPropertyException
。有什么建议吗?
发布于 2012-07-13 10:16:50
带引号的属性假设一个点是属性名称的一部分。
一个简单的解决方案是:
...
def fieldValue = fieldName.split(/\./).inject(input){ parent, property -> parent?."$property" }
...
这将使用子属性的点标记法递归查找字段值。
Groovy web控制台上的I put up a working example here。
https://stackoverflow.com/questions/11468189
复制