Skip to content

Commit

Permalink
Separate seeting for missingConditionIsFalse.
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-smith committed Mar 10, 2024
1 parent 35e36b9 commit 8c1180a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
30 changes: 26 additions & 4 deletions src/main/java/com/sshtools/tinytemplate/Templates.java
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ public enum State {
public final static class Builder {
private boolean nullsAreEmpty = true;
private boolean missingThrowsException = true;
private boolean missingConditionIsFalse = true;
private Optional<Logger> logger = Optional.empty();
private Optional<VariableExpander> expander = Optional.empty();

Expand All @@ -663,6 +664,11 @@ public Builder withMissingThrowsException(boolean missingThrowsException) {
return this;
}

public Builder withMissingConditionIsFalse(boolean missingConditionIsFalse) {
this.missingConditionIsFalse = missingConditionIsFalse;
return this;
}

public Builder withLogger(Logger logger) {
return withLogger(Optional.of(logger));
}
Expand All @@ -685,6 +691,7 @@ public TemplateProcessor build() {

private final boolean nullsAreEmpty;
private final boolean missingThrowsException;
private final boolean missingConditionIsFalse;
private final Optional<Logger> logger;
private final Optional<VariableExpander> expander;

Expand Down Expand Up @@ -736,6 +743,7 @@ private final static class Block {
private TemplateProcessor(Builder bldr) {
this.nullsAreEmpty = bldr.nullsAreEmpty;
this.missingThrowsException = bldr.missingThrowsException;
this.missingConditionIsFalse = bldr.missingConditionIsFalse;
this.logger = bldr.logger;
this.expander = bldr.expander;
}
Expand Down Expand Up @@ -960,7 +968,12 @@ private boolean processDirective(Block block, String directive) {
var match = false;

if (conditionOr.isEmpty()) {
logger.ifPresent(l -> l.debug("Missing condition {0}, assuming {1}", condition.name, false));
if(!missingConditionIsFalse) {
if(missingThrowsException)
throw new IllegalStateException(MessageFormat.format("No condition in model named {0}.", condition.name));
else
logger.ifPresent(l -> l.warning("No condition in model named {0}, assuming {1}", condition.name, false));
}
} else {
match = conditionOr.get();
}
Expand All @@ -982,7 +995,10 @@ private boolean processDirective(Block block, String directive) {
else if(dir.equals("t:include")) {
var includeModel = block.model.includes.get(var);
if (includeModel == null) {
logger.ifPresent(l -> l.debug("No include in model named {0}", var));
if(missingThrowsException)
throw new IllegalStateException(MessageFormat.format("No include in model named {0}.", var));
else
logger.ifPresent(l -> l.warning("No include in model named {0}", var));
return false;
} else {
var include = includeModel.get();
Expand All @@ -1002,7 +1018,10 @@ else if(dir.equals("t:else")) {
else if(dir.equals("t:object")) {
var templateSupplier = block.model.templates.get(var);
if (templateSupplier == null) {
logger.ifPresent(l -> l.warning("Missing template {0} in message template", var));
if(missingThrowsException)
throw new IllegalStateException(MessageFormat.format("No object in model name {0}.", var));
else
logger.ifPresent(l -> l.warning("No object in model named {0}.", var));
return false;
}
else {
Expand Down Expand Up @@ -1030,7 +1049,10 @@ else if(dir.equals("t:object")) {
else if(dir.equals("t:list")) {
var listSupplier = block.model.lists.get(var);
if (listSupplier == null) {
logger.ifPresent(l -> l.warning("Missing list {0} in message template", var));
if(missingThrowsException)
throw new IllegalStateException(MessageFormat.format("No list in model named {0}.", var));
else
logger.ifPresent(l -> l.warning("No list in model named {0}", var));
return false;
}
else {
Expand Down
39 changes: 6 additions & 33 deletions src/test/java/com/sshtools/tinytemplate/TemplatesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ public void testTemplateIf() {
</body>
</html>
""").
condition("aCondition", true)));
condition("aCondition", true).
condition("bCondition", false)));
}

@Test
Expand Down Expand Up @@ -383,7 +384,8 @@ public void testTemplateIfWithElse() {
</body>
</html>
""").
condition("aCondition", true)));
condition("aCondition", true).
condition("bCondition", false)));
}

@Test
Expand Down Expand Up @@ -781,7 +783,8 @@ public void testTemplateNestedIf() {
</body>
</html>
""").
condition("aCondition", true)));
condition("aCondition", true).
condition("bCondition", false)));

}

Expand All @@ -801,36 +804,6 @@ public void testTEMPYYYY() {

}

@Test
public void testTEMPXXXXXXX() {
Assertions.assertEquals("""
<html>
<body>
<p>Show this</p>
<p>And show this too</p>
</body>
</html>
""",
createParser().process(TemplateModel.ofContent("""
<html>
<body>
<t:if aCondition>
<p>Show this</p>
<t:if !bCondition>
<p>And show this too</p>
</t:if>
</t:if>
</body>
</html>
""").
condition("aCondition", true)));

}

@Test
public void testTemplateNestedIf2() {
Assertions.assertEquals("""
Expand Down

0 comments on commit 8c1180a

Please sign in to comment.