Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SE-13618: Extend the checks of the TemplateCompiler to include missing required attributes #1464

Merged
merged 9 commits into from
Oct 2, 2024
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/ArgTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:arg</tt> which specifies a template argument.
Expand Down Expand Up @@ -157,4 +159,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_NAME, PARAM_TYPE));
sabieber marked this conversation as resolved.
Show resolved Hide resolved
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/BlockTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
* Handles <tt>i:block</tt> which specifies a template section passed into a tag invocation.
Expand Down Expand Up @@ -85,4 +87,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_NAME));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/DefineTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:define</tt> which defines a string as the evaluation result of its body.
Expand Down Expand Up @@ -81,4 +83,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_NAME));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:dynamicInvoke</tt> which invokes or inlines a given template.
Expand Down Expand Up @@ -69,4 +71,9 @@ public Class<?> getExpectedAttributeType(String name) {
// Accept anything, we don't know yet what to expect at runtime....
return Callable.class;
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(ATTR_TEMPLATE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import sirius.pasta.tagliatelle.emitter.CompositeEmitter;

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:extensions</tt> which invokes all extensions with the given name.
Expand Down Expand Up @@ -86,4 +89,9 @@ public Class<?> getExpectedAttributeType(String name) {

return Callable.class;
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(ATTR_TARGET));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Permits to add an extra block to the global render context.
Expand Down Expand Up @@ -86,4 +88,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_NAME));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/ForTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:for</tt> which emits its body for each item in an {@link Iterable}.
Expand Down Expand Up @@ -116,4 +118,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_ITEMS, PARAM_TYPE, PARAM_VAR));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/IfTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:if</tt> which emits its body if a condition is met.
Expand Down Expand Up @@ -71,4 +73,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of("test"));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/IncludeTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:include</tt> which includes the contents of the given resource without any processing.
Expand Down Expand Up @@ -74,4 +76,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(ATTR_NAME));
}
}
17 changes: 17 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/InvokeTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:invoke</tt> which invokes or inlines a given template.
Expand Down Expand Up @@ -116,6 +118,21 @@ public Class<?> getExpectedAttributeType(String name) {
}
}

@Override
public Set<String> getRequiredAttributeNames() {
Set<String> result = new HashSet<>();
result.add(ATTR_TEMPLATE);

if (ensureThatTemplateIsPresent()) {
for (TemplateArgument arg : template.getArguments()) {
if (arg.getDefaultValue() == null) {
result.add(arg.getName());
}
}
}
return result;
}

private boolean ensureThatTemplateIsPresent() {
if (template == null) {
if (templateResolved) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/LocalTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:local</tt> which defines a local variable.
Expand Down Expand Up @@ -92,4 +94,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_NAME, PARAM_VALUE));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/PragmaTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import javax.annotation.Nonnull;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:pragma</tt> which defines a pragma (key / value pair) for a template.
Expand Down Expand Up @@ -79,4 +81,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_NAME));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/RenderTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:render</tt> which emits the block with the given name.
Expand Down Expand Up @@ -79,4 +81,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(PARAM_NAME));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/SassTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import java.io.Reader;
import java.io.StringReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:sass</tt> which renders a SASS file into the current template as compiled CSS.
Expand Down Expand Up @@ -93,4 +95,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of(SOURCE_ATTRIBUTE));
}
}
7 changes: 7 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/SwitchTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Handles <tt>i:switch</tt> which can hold multiple blocks which are only rendered if their name matches an expression.
Expand Down Expand Up @@ -68,4 +70,9 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
return new HashSet<>(List.of("test"));
}
}
14 changes: 14 additions & 0 deletions src/main/java/sirius/pasta/tagliatelle/tags/TaglibTagHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import sirius.pasta.tagliatelle.TemplateArgument;
import sirius.pasta.tagliatelle.emitter.CompositeEmitter;

import java.util.HashSet;
import java.util.Set;

/**
* Handles the invocation of a user-defined tag.
* <p>
Expand Down Expand Up @@ -50,4 +53,15 @@ public Class<?> getExpectedAttributeType(String name) {

return super.getExpectedAttributeType(name);
}

@Override
public Set<String> getRequiredAttributeNames() {
Set<String> result = new HashSet<>();
for (TemplateArgument arg : template.getArguments()) {
if (arg.getDefaultValue() == null) {
result.add(arg.getName());
}
}
return result;
}
}