Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding the ability to evaluate groovy expressions #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/groovy/pl/touk/excel/export/Formatters.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pl.touk.excel.export
import pl.touk.excel.export.getters.Getter
import pl.touk.excel.export.getters.PropertyGetter
import pl.touk.excel.export.getters.AsIsPropertyGetter
import pl.touk.excel.export.getters.AsClosurePropertyGetter
import pl.touk.excel.export.getters.LongToDatePropertyGetter

class Formatters {
Expand All @@ -14,14 +15,20 @@ class Formatters {
return new AsIsPropertyGetter(propertyName)
}

static PropertyGetter asClosure(Closure closure) {
return new AsClosurePropertyGetter(closure)
}

static List<Getter> convertSafelyToGetters(List properties) {
properties.collect {
if (it instanceof Getter) {
return it
} else if(it instanceof String) {
return asIs(it)
} else if(it instanceof Closure) {
return asClosure(it)
} else {
throw IllegalArgumentException('List of properties, which should be either String, a Getter. Found: ' +
throw IllegalArgumentException('List of properties, which should be either String, a Getter or a Groovy Closure. Found: ' +
it?.toString() + ' of class ' + it?.getClass())
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pl.touk.excel.export.getters

class AsClosurePropertyGetter extends PropertyGetter<Object, Object> {

def closure

AsClosurePropertyGetter(Closure closure) {
super(null)
this.closure = closure
}

def getFormattedValue(Object object) {
closure.call(object)
}

def format(Object value) {
null
}
}
25 changes: 25 additions & 0 deletions test/unit/pl/touk/excel/export/XlsxExporterRowTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ class XlsxExporterRowTest extends XlsxExporterTest {
testObject.verifyRowHasSelectedProperties(xlsxReporter.withDefaultSheet(), 3)
}

@Test
void shouldFillRowEvaluatingPropertyList() {
//given
SampleObject testObject = new SampleObject()
def propertyExpressions = [
'stringValue',
{ it.stringValue },
{ it.child.stringValue.toUpperCase() },
{ it.booleanValue?" it is true ":"it is false " },
{ it.shortValue + it.integerValue },
{ Math.sqrt(it.doubleValue) }
]

//when
xlsxReporter.add(testObject, propertyExpressions, 0)

//then
assert xlsxReporter.getCellAt(0, 0).getStringCellValue() == testObject.stringValue
assert xlsxReporter.getCellAt(0, 1).getStringCellValue() == testObject.stringValue
assert xlsxReporter.getCellAt(0, 2).getStringCellValue() == testObject.child.stringValue.toUpperCase()
assert xlsxReporter.getCellAt(0, 3).getStringCellValue() == testObject.booleanValue?" it is true ":"it is false "
assert xlsxReporter.getCellAt(0, 4).getNumericCellValue() == testObject.integerValue + testObject.shortValue
assert xlsxReporter.getCellAt(0, 5).getNumericCellValue() == Math.sqrt(testObject.doubleValue)
}

@Test
void shouldFillRows() {
//given
Expand Down