Skip to content

Commit

Permalink
Actually validate supported projection categories
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim203 committed Aug 24, 2024
1 parent 34b5108 commit 341fcd4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean supportsFilter() {
return supportsFilter;
}

public List<ProjectionKeywordCategory> unsupportedProjectionCategories() {
public List<ProjectionKeywordCategory> supportedProjectionCategories() {
return supportedProjectionCategories;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiConsumer;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down Expand Up @@ -77,12 +76,6 @@ public QueryContext create() {
}

private ParametersTypeInfo analyseValidateAndCreate() {
var parameterTypes =
element.getParameters().stream().map(VariableElement::asType).toList();
var parameterNames = element.getParameters().stream()
.map(VariableElement::getSimpleName)
.toList();

var hasColumnParameter = readResult.projection() != null
&& readResult.projection().columnName() != null
&& action.projectionColumnIsParameter();
Expand All @@ -101,6 +94,15 @@ private ParametersTypeInfo analyseValidateAndCreate() {
}

var category = projection.keyword().category();
if (!action.supportedProjectionCategories().contains(category)) {
if (action.supportedProjectionCategories().isEmpty()) {
throw new InvalidRepositoryException(
"Action %s doesn't support projection", action.actionType());
}
throw new InvalidRepositoryException(
"Action %s doesn't support projection category %s",
action.actionType(), category.toString());
}
if (!handledCategories.add(category)) {
throw new InvalidRepositoryException(
"You can only provide one keyword of category %s, also got %s",
Expand All @@ -122,7 +124,7 @@ private ParametersTypeInfo analyseValidateAndCreate() {
validateColumnNames(
readResult.bySection().factors(), SectionType.BY, (VariableByFactor input, ColumnInfo column) -> {
var keyword = input.keyword();
keyword.validateTypes(column, parameterTypes, parameterNames, handledInputs.get(), typeUtils);
keyword.validateTypes(column, element.getParameters(), handledInputs.get(), typeUtils);
handledInputs.addAndGet(keyword.inputCount());
});
}
Expand All @@ -134,8 +136,10 @@ private ParametersTypeInfo analyseValidateAndCreate() {
($, $$) -> handledInputs.incrementAndGet());
}

var parameterCount = element.getParameters().size();

// if there is no By section and there are parameters, it should be the entity or the provided projection
if (readResult.bySection() == null && parameterTypes.size() == 1) {
if (readResult.bySection() == null && parameterCount == 1) {
if (parameterInfo.isAnySelf() && !action.allowSelfParameter()) {
throw new InvalidRepositoryException(
"Action %s (for %s) doesn't support entity as parameter!",
Expand Down Expand Up @@ -167,9 +171,8 @@ private ParametersTypeInfo analyseValidateAndCreate() {
}

// Otherwise the expected parameter count should equal the actual
if (parameterTypes.size() != handledInputs.get()) {
throw new IllegalStateException(
"Expected %s parameters, received %s".formatted(handledInputs, parameterTypes));
if (parameterCount != handledInputs.get()) {
throw new InvalidRepositoryException("Expected %s parameters, received %s", handledInputs, parameterCount);
}
return parameterInfo;
}
Expand All @@ -181,8 +184,8 @@ private <T extends VariableFactor> void validateColumnNames(
if (factor instanceof VariableFactor variable) {
var column = info.columnFor(variable.columnName());
if (column == null) {
throw new IllegalStateException(
"Could not find column %s for entity %s".formatted(variable.columnName(), info.name()));
throw new InvalidRepositoryException(
"Could not find column %s for entity %s", variable.columnName(), info.name());
}
variableLast = true;
if (customValidation != null) {
Expand All @@ -195,7 +198,7 @@ private <T extends VariableFactor> void validateColumnNames(
}

if (!variableLast) {
throw new IllegalStateException("Cannot end a section (%s) with a factor!".formatted(type));
throw new InvalidRepositoryException("Cannot end a section (%s) with a factor!", type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.element.VariableElement;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.databaseutils.processor.info.ColumnInfo;
import org.geysermc.databaseutils.processor.util.CollectionUtils;
Expand All @@ -32,20 +32,16 @@ public int inputCount() {
}

public void validateTypes(
ColumnInfo column,
List<TypeMirror> inputTypes,
List<? extends CharSequence> inputNames,
int typeOffset,
TypeUtils typeUtils) {

if (typeOffset + inputCount() > inputTypes.size()) {
throw new IllegalStateException(String.format(
"Expected (at least) %s inputs, got %s", typeOffset + inputCount(), inputTypes.size()));
ColumnInfo column, List<? extends VariableElement> inputs, int offset, TypeUtils typeUtils) {
if (offset + inputCount() > inputs.size()) {
throw new IllegalStateException(
String.format("Expected (at least) %s inputs, got %s", offset + inputCount(), inputs.size()));
}

for (int i = 0; i < inputCount(); i++) {
var type = inputTypes.get(typeOffset + i);
var name = inputNames.get(typeOffset + i);
var input = inputs.get(offset + i);
var type = input.asType();
var name = input.getSimpleName();

if (!typeUtils.isAssignable(column.typeName(), type)) {
throw new IllegalStateException(String.format(
Expand Down

0 comments on commit 341fcd4

Please sign in to comment.