Echo working.
This commit is contained in:
parent
d05b4f4c0f
commit
6af7e4fd82
@ -14,12 +14,15 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
|||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(StandardGStringTemplateViewMetaClass.class);
|
private static final Logger logger = LoggerFactory.getLogger(StandardGStringTemplateViewMetaClass.class);
|
||||||
|
|
||||||
private static StandardGStringTemplateView asView(Object object) {
|
private static StandardGStringTemplateView asStandardGStringTemplateView(Object object) {
|
||||||
return object instanceof StandardGStringTemplateView view ? view : null;
|
return object instanceof StandardGStringTemplateView view ? view : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Object[] asArgsArray(Object object) {
|
private static Object[] asArgsArray(Object object) {
|
||||||
return object instanceof Object[] objects ? objects : new Object[] { object };
|
return object instanceof Object[] objects
|
||||||
|
? objects
|
||||||
|
: new Object[] { object }
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static MetaMethod findMetaMethod(MetaClass metaClass, String methodName, Object[] argsArray) {
|
private static MetaMethod findMetaMethod(MetaClass metaClass, String methodName, Object[] argsArray) {
|
||||||
@ -57,9 +60,9 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object invokeMethod(Object object, String methodName, Object arguments) {
|
public Object invokeMethod(Object object, String methodName, Object arguments) {
|
||||||
final StandardGStringTemplateView view = asView(object);
|
final StandardGStringTemplateView view = asStandardGStringTemplateView(object);
|
||||||
if (view == null) {
|
if (view == null) {
|
||||||
logger.warn("StandardGStringTemplateViewMetaClass should only be used as a MetaClass of StandardGStringTemplateViewMetaClass.");
|
warnWrongType(object);
|
||||||
return super.invokeMethod(object, methodName, arguments);
|
return super.invokeMethod(object, methodName, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +79,7 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
|||||||
final Object[] argsArray = asArgsArray(arguments);
|
final Object[] argsArray = asArgsArray(arguments);
|
||||||
|
|
||||||
// self
|
// self
|
||||||
final MetaMethod selfMethod = findMetaMethod(this, methodName, argsArray);
|
final MetaMethod selfMethod = this.getMetaMethod(methodName, argsArray);
|
||||||
if (selfMethod != null) {
|
if (selfMethod != null) {
|
||||||
return selfMethod.invoke(view, argsArray);
|
return selfMethod.invoke(view, argsArray);
|
||||||
}
|
}
|
||||||
@ -84,11 +87,12 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
|||||||
// parent hierarchy
|
// parent hierarchy
|
||||||
View parent = view.getParent();
|
View parent = view.getParent();
|
||||||
while (parent != null) {
|
while (parent != null) {
|
||||||
final var parentMetaMethod = findMetaMethod(findMetaClass(parent), methodName, argsArray);
|
final var parentMetaClass = findMetaClass(parent);
|
||||||
|
final var parentMetaMethod = parentMetaClass.getMetaMethod(methodName, argsArray);
|
||||||
if (parentMetaMethod != null) {
|
if (parentMetaMethod != null) {
|
||||||
return parentMetaMethod.invoke(parent, argsArray);
|
return parentMetaMethod.invoke(parent, argsArray);
|
||||||
} else if (parent instanceof StandardGStringTemplateView) {
|
} else if (parent instanceof StandardGStringTemplateView standardGStringTemplateView) {
|
||||||
parent = ((StandardGStringTemplateView) parent).getParent();
|
parent = standardGStringTemplateView.getParent();
|
||||||
} else {
|
} else {
|
||||||
parent = null;
|
parent = null;
|
||||||
}
|
}
|
||||||
@ -99,7 +103,7 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getProperty(Object object, String name) {
|
public Object getProperty(Object object, String name) {
|
||||||
final StandardGStringTemplateView view = asView(object);
|
final StandardGStringTemplateView view = asStandardGStringTemplateView(object);
|
||||||
if (view == null) {
|
if (view == null) {
|
||||||
warnWrongType(object);
|
warnWrongType(object);
|
||||||
return super.getProperty(object, name);
|
return super.getProperty(object, name);
|
||||||
@ -136,7 +140,7 @@ public class StandardGStringTemplateViewMetaClass extends ExpandoMetaClass {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setProperty(Object object, String name, Object value) {
|
public void setProperty(Object object, String name, Object value) {
|
||||||
final StandardGStringTemplateView view = asView(object);
|
final StandardGStringTemplateView view = asStandardGStringTemplateView(object);
|
||||||
if (view == null) {
|
if (view == null) {
|
||||||
warnWrongType(object);
|
warnWrongType(object);
|
||||||
super.setProperty(object, name, value);
|
super.setProperty(object, name, value);
|
||||||
|
@ -10,37 +10,32 @@ class Echo extends DelegatingWebViewComponent {
|
|||||||
|
|
||||||
static final class EchoFactory implements ComponentFactory<Echo> {
|
static final class EchoFactory implements ComponentFactory<Echo> {
|
||||||
|
|
||||||
Echo doCreate(String typeName, ComponentContext context, Map<String, Object> attr) {
|
Echo doCreate(String typeName, boolean selfClose) {
|
||||||
doCreate(typeName, context, attr, true)
|
doCreate(typeName, [:], selfClose)
|
||||||
}
|
}
|
||||||
|
|
||||||
Echo doCreate(String typeName, ComponentContext context, Map<String, Object> attr, boolean selfClose) {
|
Echo doCreate(String typeName, Map<String, Object> attr) {
|
||||||
|
doCreate(typeName, attr, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
Echo doCreate(String typeName, Map<String, Object> attr, boolean selfClose) {
|
||||||
def echo = new Echo(attr, typeName, selfClose)
|
def echo = new Echo(attr, typeName, selfClose)
|
||||||
echo.context = context
|
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
Echo doCreate(
|
Echo doCreate(
|
||||||
String typeName,
|
String typeName,
|
||||||
ComponentContext context,
|
|
||||||
Map<String, Object> attr,
|
Map<String, Object> attr,
|
||||||
List<WebViewChildComponentRenderer> children
|
List<WebViewChildComponentRenderer> children
|
||||||
) {
|
) {
|
||||||
def echo = new Echo(attr, typeName, false)
|
def echo = new Echo(attr, typeName, false)
|
||||||
echo.context = context
|
|
||||||
echo.childRenderers = children
|
echo.childRenderers = children
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
Echo create(String type, ComponentContext componentContext, Object... args) {
|
Echo create(String type, ComponentContext componentContext, Object... args) {
|
||||||
if (args == null || args.length < 1) {
|
this.doCreate(type, *args) as Echo
|
||||||
throw new IllegalArgumentException(
|
|
||||||
'<Echo> must have at least one attribute. ' +
|
|
||||||
'If you are just echoing children, use a fragment (<>...</>) instead. '
|
|
||||||
)
|
|
||||||
}
|
|
||||||
this.invokeMethod('doCreate', type as String, componentContext, *args) as Echo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -50,12 +45,13 @@ class Echo extends DelegatingWebViewComponent {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String typeName
|
String name
|
||||||
boolean selfClose
|
boolean selfClose
|
||||||
|
|
||||||
Echo(Map<String, Object> attr, String typeName, boolean selfClose) {
|
Echo(Map<String, Object> attr, String name, boolean selfClose) {
|
||||||
super(attr)
|
super(attr)
|
||||||
this.typeName = typeName
|
this.name = name
|
||||||
|
this.selfClose = selfClose
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -66,18 +62,20 @@ class Echo extends DelegatingWebViewComponent {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
void renderAttr(Writer out) {
|
String formatAttr() {
|
||||||
|
def sb = new StringBuilder()
|
||||||
def iter = this.attr.iterator()
|
def iter = this.attr.iterator()
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
def entry = iter.next()
|
def entry = iter.next()
|
||||||
out << entry.key
|
sb << entry.key
|
||||||
out << '="'
|
sb << '="'
|
||||||
out << entry.value
|
sb << entry.value
|
||||||
out << '"'
|
sb << '"'
|
||||||
if (iter.hasNext()) {
|
if (iter.hasNext()) {
|
||||||
out << ' '
|
sb << ' '
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sb.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
<$typeName ${renderAttr()}${selfClose ? ' /' : ''}>${renderChildren()}${!selfClose ? "</$typeName>" : ''}
|
<$name ${formatAttr()}${selfClose ? '/' : ''}>${hasChildren() ? renderChildren() : ''}${hasChildren() || !selfClose ? "</$name>" : ''}
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package groowt.view.web.lib
|
||||||
|
|
||||||
|
import groowt.view.web.DefaultWebViewComponentContext
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
|
class EchoTests extends AbstractWebViewComponentTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void typeOnlySelfClose() {
|
||||||
|
def context = new DefaultWebViewComponentContext()
|
||||||
|
context.pushDefaultScope()
|
||||||
|
context.currentScope.add('Echo', new Echo.EchoFactory())
|
||||||
|
this.doTest('<Echo(true) />', '<Echo />', context)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -7,7 +7,7 @@ import groowt.view.web.DefaultWebViewComponentContext
|
|||||||
import groowt.view.web.WebViewTemplateComponentSource
|
import groowt.view.web.WebViewTemplateComponentSource
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
|
||||||
class FragmentTests extends AbstractComponentTests {
|
class FragmentTests extends AbstractWebViewComponentTests {
|
||||||
|
|
||||||
static class Greeter extends DefaultWebViewComponent {
|
static class Greeter extends DefaultWebViewComponent {
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import java.io.StringWriter;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public abstract class AbstractComponentTests {
|
public abstract class AbstractWebViewComponentTests {
|
||||||
|
|
||||||
protected void doTest(Reader source, String expected, ComponentContext context) {
|
protected void doTest(Reader source, String expected, ComponentContext context) {
|
||||||
final var compiler = new DefaultWebComponentTemplateCompiler(
|
final var compiler = new DefaultWebComponentTemplateCompiler(
|
||||||
@ -25,7 +25,7 @@ public abstract class AbstractComponentTests {
|
|||||||
final StringWriter sw = new StringWriter();
|
final StringWriter sw = new StringWriter();
|
||||||
final WebViewComponentWriter out = new WebViewComponentWriter(sw);
|
final WebViewComponentWriter out = new WebViewComponentWriter(sw);
|
||||||
renderer.call(context, out);
|
renderer.call(context, out);
|
||||||
assertEquals(expected, sw.toString());
|
assertEquals(expected, sw.toString().trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void doTest(String source, String expected, ComponentContext context) {
|
protected void doTest(String source, String expected, ComponentContext context) {
|
Loading…
Reference in New Issue
Block a user