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
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

package sirius.pasta.tagliatelle.compiler;

import sirius.kernel.tokenizer.Char;
import sirius.kernel.tokenizer.ParseError;
import sirius.kernel.tokenizer.Position;
import sirius.kernel.commons.Explain;
import sirius.kernel.commons.Strings;
import sirius.kernel.di.std.PriorityParts;
import sirius.kernel.health.Exceptions;
import sirius.kernel.health.HandledException;
import sirius.kernel.tokenizer.Char;
import sirius.kernel.tokenizer.ParseError;
import sirius.kernel.tokenizer.Position;
import sirius.pasta.Pasta;
import sirius.pasta.noodle.Callable;
import sirius.pasta.noodle.ConstantCall;
Expand All @@ -33,7 +33,9 @@

import javax.annotation.Nullable;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Compiles a sources file into a {@link sirius.pasta.tagliatelle.Template}.
Expand Down Expand Up @@ -369,6 +371,8 @@ private void handleTag(TagHandler handler, CompositeEmitter block) {
* @param handler the handler to add the attributes to
*/
private void parseAttributes(TagHandler handler) {
// Collect all parsed attributes.
Set<String> attributes = new HashSet<>();
while (true) {
skipWhitespaces();
if (reader.current().isEndOfInput() || reader.current().is('>', '/')) {
Expand All @@ -379,6 +383,8 @@ private void parseAttributes(TagHandler handler) {
verifyAttributeNameAndType(handler, name, attributeType);
if (attributeType == null) {
attributeType = String.class;
} else {
idlira marked this conversation as resolved.
Show resolved Hide resolved
attributes.add(name);
}

skipWhitespaces();
Expand Down Expand Up @@ -417,6 +423,27 @@ private void parseAttributes(TagHandler handler) {
}
consumeExpectedCharacter('"');
}

checkMissingAttributes(handler, attributes);
}

/**
* Checks if all required attributes are present.
*
* @param handler the tag handler
* @param attributes the set of attributes which were parsed
*/
private void checkMissingAttributes(TagHandler handler, Set<String> attributes) {
// Check if all required attributes are present.
Set<String> missingAttributes = handler.getAttributeNames(true);
missingAttributes.removeAll(attributes);
if (!missingAttributes.isEmpty()) {
// If we have required attributes left, we report them as missing
missingAttributes.forEach(attr -> context.error(reader.current(),
"Missing required attribute. %s missing the required attribute '%s'.",
handler.getTagName(),
attr));
}
}

private void parseAttributeExpression(TagHandler handler,
Expand Down