Skip to content

Commit

Permalink
Polish 'Fix Mustache to not ignore native fetcher'
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Jun 8, 2020
1 parent 5199c11 commit ddbecf6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ public void setEnvironment(Environment environment) {

@Override
public VariableFetcher createFetcher(Object ctx, String name) {
VariableFetcher fetcher = super.createFetcher(ctx, name);
if (fetcher != null) {
return new PropertyVariableFetcher(fetcher);
VariableFetcher nativeFetcher = super.createFetcher(ctx, name);
if (nativeFetcher != null) {
return new PropertyVariableFetcher(nativeFetcher);
}
if (this.environment.containsProperty(name)) {
return new PropertyVariableFetcher();
}
return null;
}

/**
* {@link VariableFetcher} that also checks the {@link Environment}.
*/
private class PropertyVariableFetcher implements VariableFetcher {

private final VariableFetcher nativeFetcher;
Expand All @@ -61,29 +64,29 @@ private class PropertyVariableFetcher implements VariableFetcher {
this.nativeFetcher = null;
}

PropertyVariableFetcher(VariableFetcher nativeFetcher) {
this.nativeFetcher = nativeFetcher;
PropertyVariableFetcher(VariableFetcher delegate) {
this.nativeFetcher = delegate;
}

@Override
public Object get(Object ctx, String name) {
Object result;
if (this.nativeFetcher != null) {
try {
result = this.nativeFetcher.get(ctx, name);
if (result != null && result != Template.NO_FETCHER_FOUND) {
return result;
}
}
catch (Exception ex) {
// fall through
}
Object result = getFromNativeFetcher(ctx, name);
result = (result != null) ? result : getFromEnvironment(name);
return (result != null) ? result : Template.NO_FETCHER_FOUND;
}

private Object getFromNativeFetcher(Object ctx, String name) {
try {
Object result = (this.nativeFetcher != null) ? this.nativeFetcher.get(ctx, name) : null;
return (result != Template.NO_FETCHER_FOUND) ? result : null;
}
result = MustacheEnvironmentCollector.this.environment.getProperty(name);
if (result == null) {
return Template.NO_FETCHER_FOUND;
catch (Exception ex) {
return null;
}
return result;
}

private Object getFromEnvironment(String name) {
return MustacheEnvironmentCollector.this.environment.getProperty(name);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ void environmentCollectorCompoundKeyStandardMap() {

@Test
void environmentCollectorCompoundKeyWithBean() {
assertThat(this.compiler.compile("Hello: {{foo.name}}")
.execute(Collections.singletonMap("foo", new Foo()))).isEqualTo("Hello: Foo");
assertThat(this.compiler.compile("Hello: {{foo.name}}").execute(Collections.singletonMap("foo", new Foo())))
.isEqualTo("Hello: Foo");
}

@Test
void environmentCollectorCompoundKeyWithBeanPrefersEnvironment() {
assertThat(this.compiler.compile("Hello: {{bar.name}}")
.execute(Collections.singletonMap("bar", new Foo()))).isEqualTo("Hello: Bar");
assertThat(this.compiler.compile("Hello: {{bar.name}}").execute(Collections.singletonMap("bar", new Foo())))
.isEqualTo("Hello: Bar");
}

@Test
Expand All @@ -94,17 +94,19 @@ void environmentCollectorSimpleKeyMap() {
static class Application {

}

static class Foo {

private String name = "Foo";

public String getName() {
return name;
String getName() {
return this.name;
}

public void setName(String name) {
void setName(String name) {
this.name = name;
}

}

}

0 comments on commit ddbecf6

Please sign in to comment.