Updated Echo for new closure semantics.
This commit is contained in:
parent
e112d81ea8
commit
bd913e07d3
@ -0,0 +1,7 @@
|
||||
---
|
||||
import groovy.transform.Field
|
||||
|
||||
@Field
|
||||
String greeting = 'Hello, World!'
|
||||
---
|
||||
$greeting
|
@ -123,33 +123,12 @@ public class DefaultGroovyTranspiler implements GroovyTranspiler {
|
||||
|
||||
final BlockStatement preambleBlock = convertResult.blockStatement();
|
||||
if (preambleBlock != null) {
|
||||
// Fields
|
||||
final List<Statement> preambleStatements = preambleBlock.getStatements();
|
||||
final List<DeclarationExpression> declarationsWithField = preambleStatements.stream()
|
||||
.filter(statement -> statement instanceof ExpressionStatement)
|
||||
.map(ExpressionStatement.class::cast)
|
||||
.map(ExpressionStatement::getExpression)
|
||||
.filter(expression -> expression instanceof DeclarationExpression)
|
||||
.map(DeclarationExpression.class::cast)
|
||||
.filter(declarationExpression ->
|
||||
!declarationExpression.getAnnotations(FIELD_ANNOTATION).isEmpty()
|
||||
)
|
||||
.toList();
|
||||
if (declarationsWithField.size() != preambleStatements.size()) {
|
||||
// TODO: figure out why we have extraneous statements sometimes when it seems otherwise not
|
||||
logger.warn(
|
||||
"{} contains script statements which are not supported. " +
|
||||
"Currently, only classes, methods, and field declarations " +
|
||||
"(marked with @groovy.transform.Field) " +
|
||||
"are supported. The rest will be ignored.",
|
||||
"{} contains script statements which are not supported. Currently, only classes and"
|
||||
+ " methods are supported. The rest will be ignored.",
|
||||
templateClassName
|
||||
);
|
||||
}
|
||||
declarationsWithField.forEach(declaration -> {
|
||||
declaration.setDeclaringClass(mainClassNode);
|
||||
positionVisitor.visitDeclarationExpression(declaration);
|
||||
});
|
||||
}
|
||||
|
||||
// move methods from script class
|
||||
final ClassNode scriptClass = convertResult.scriptClass();
|
||||
|
@ -1,6 +1,7 @@
|
||||
package groowt.view.component.web.lib
|
||||
|
||||
import groowt.view.View
|
||||
import groowt.view.component.runtime.ComponentWriter
|
||||
import groowt.view.component.runtime.DefaultComponentWriter
|
||||
|
||||
class Echo extends DelegatingWebViewComponent {
|
||||
@ -17,7 +18,16 @@ class Echo extends DelegatingWebViewComponent {
|
||||
return super.getProperty(propertyName)
|
||||
} catch (MissingPropertyException missingPropertyException) {
|
||||
if (this.attr.containsKey(propertyName)) {
|
||||
return this.attr[propertyName]
|
||||
def value = this.attr[propertyName]
|
||||
if (value instanceof Closure) {
|
||||
if (value.maximumNumberOfParameters == 0) {
|
||||
return value
|
||||
} else {
|
||||
return value()
|
||||
}
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
} else {
|
||||
throw missingPropertyException
|
||||
}
|
||||
@ -26,14 +36,22 @@ class Echo extends DelegatingWebViewComponent {
|
||||
|
||||
@Override
|
||||
protected View getDelegate() {
|
||||
return {
|
||||
def componentWriter = new DefaultComponentWriter(it)
|
||||
return { Writer w ->
|
||||
def componentWriter = new DefaultComponentWriter(w)
|
||||
componentWriter.setComponentContext(this.context)
|
||||
componentWriter.setRenderContext(this.context.renderContext) // hacky
|
||||
componentWriter.setRenderContext(this.context.renderContext)
|
||||
this.children.each {
|
||||
if (it instanceof Closure) {
|
||||
if (it.maximumNumberOfParameters == 1 && ComponentWriter.isAssignableFrom(it.parameterTypes[0])) {
|
||||
it(componentWriter)
|
||||
} else {
|
||||
componentWriter << it()
|
||||
}
|
||||
} else {
|
||||
componentWriter << it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -24,19 +24,6 @@ class BaseWebViewComponentTests extends AbstractWebViewComponentTests {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void withPreambleImport() {
|
||||
this.doTest('''
|
||||
---
|
||||
import groovy.transform.Field
|
||||
|
||||
@Field
|
||||
String greeting = 'Hello, World!'
|
||||
---
|
||||
$greeting
|
||||
'''.stripIndent().trim(), "Hello, World!")
|
||||
}
|
||||
|
||||
@Test
|
||||
void nestedGreeter() {
|
||||
def context = this.context() {
|
||||
|
@ -18,22 +18,6 @@ class SimpleWebViewComponentTests extends AbstractWebViewComponentTests {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
void closureWithPropertyExpressionEvaluatesToValue() {
|
||||
this.doTest(
|
||||
'''
|
||||
---
|
||||
import groovy.transform.Field
|
||||
|
||||
@Field
|
||||
Map greetings = [hello: 'Hello!']
|
||||
---
|
||||
<Echo greeting={greetings.hello}>$greeting</Echo>
|
||||
'''.stripIndent().trim(),
|
||||
'Hello!'
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
void closureWithMethodCallIsClosure() {
|
||||
this.doTest(
|
||||
@ -43,7 +27,7 @@ class SimpleWebViewComponentTests extends AbstractWebViewComponentTests {
|
||||
input.capitalize()
|
||||
}
|
||||
---
|
||||
<Echo subHelper={ helper('lowercase') }>${ -> subHelper.call() }</Echo>
|
||||
<Echo subHelper={ -> helper('lowercase') }>${ -> subHelper.call() }</Echo>
|
||||
'''.stripIndent().trim(), 'Lowercase'
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user