diff --git a/src/main/java/com/sshtools/tinytemplate/Templates.java b/src/main/java/com/sshtools/tinytemplate/Templates.java index f8b3da9..d31eb12 100644 --- a/src/main/java/com/sshtools/tinytemplate/Templates.java +++ b/src/main/java/com/sshtools/tinytemplate/Templates.java @@ -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 = Optional.empty(); private Optional expander = Optional.empty(); @@ -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)); } @@ -685,6 +691,7 @@ public TemplateProcessor build() { private final boolean nullsAreEmpty; private final boolean missingThrowsException; + private final boolean missingConditionIsFalse; private final Optional logger; private final Optional expander; @@ -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; } @@ -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(); } @@ -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(); @@ -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 { @@ -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 { diff --git a/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java b/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java index 88c62ac..a9cd630 100644 --- a/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java +++ b/src/test/java/com/sshtools/tinytemplate/TemplatesTest.java @@ -304,7 +304,8 @@ public void testTemplateIf() { """). - condition("aCondition", true))); + condition("aCondition", true). + condition("bCondition", false))); } @Test @@ -383,7 +384,8 @@ public void testTemplateIfWithElse() { """). - condition("aCondition", true))); + condition("aCondition", true). + condition("bCondition", false))); } @Test @@ -781,7 +783,8 @@ public void testTemplateNestedIf() { """). - condition("aCondition", true))); + condition("aCondition", true). + condition("bCondition", false))); } @@ -801,36 +804,6 @@ public void testTEMPYYYY() { } - @Test - public void testTEMPXXXXXXX() { - Assertions.assertEquals(""" - - - -

Show this

- -

And show this too

- - - - - """, - createParser().process(TemplateModel.ofContent(""" - - - -

Show this

- -

And show this too

-
-
- - - """). - condition("aCondition", true))); - - } - @Test public void testTemplateNestedIf2() { Assertions.assertEquals("""