Dieser Commit ist enthalten in:
Ursprung
32de0b912d
Commit
53efec2d0f
@ -363,12 +363,14 @@ public abstract class AbstractSWCommand<T> {
|
||||
private boolean invert;
|
||||
|
||||
public Handler(AbstractSWCommand.Validator validator, Class<?> clazz, Map<String, AbstractValidator<T, ?>> localValidator) {
|
||||
System.out.println("Validator: " + validator + " " + clazz);
|
||||
inner = (AbstractValidator<T, Object>) SWCommandUtils.getValidator(validator, clazz, localValidator);
|
||||
invert = validator.invert();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate(T sender, Object value, MessageSender messageSender) {
|
||||
System.out.println("Validator: " + inner + " " + invert);
|
||||
if (invert) {
|
||||
return !inner.validate(sender, value, messageSender);
|
||||
} else {
|
||||
|
@ -63,6 +63,10 @@ public class SubCommand<T> implements Comparable<SubCommand<T>> {
|
||||
AbstractSWCommand.Validator validator = parameters[0].getAnnotation(AbstractSWCommand.Validator.class);
|
||||
if (validator != null) {
|
||||
this.validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator);
|
||||
if (validator.invert()) {
|
||||
AbstractValidator<T, T> current = this.validator;
|
||||
this.validator = (sender, value, messageSender) -> !current.validate(sender, value, messageSender);
|
||||
}
|
||||
}
|
||||
|
||||
commandPart = generateCommandPart(abstractSWCommand, subCommand, parameters, localTypeMapper, localValidator);
|
||||
|
45
testsrc/de/steamwar/command/InvertValidatorCommand.java
Normale Datei
45
testsrc/de/steamwar/command/InvertValidatorCommand.java
Normale Datei
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2022 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||
import de.steamwar.command.dto.TestSWCommand;
|
||||
import de.steamwar.command.dto.TestValidator;
|
||||
|
||||
public class InvertValidatorCommand extends TestSWCommand {
|
||||
|
||||
public InvertValidatorCommand() {
|
||||
super("testvalidator");
|
||||
}
|
||||
|
||||
@Register
|
||||
public void test(@Validator(value = "Text", invert = true) String sender, boolean b, boolean b2) {
|
||||
System.out.println("test: " + sender + " " + b + " " + b2);
|
||||
throw new ExecutionIdentifier("RunTestInvert");
|
||||
}
|
||||
|
||||
@Validator(value = "Text", local = true)
|
||||
public TestValidator<String> testValidator() {
|
||||
return (sender, value, messageSender) -> {
|
||||
System.out.println("testValidator: " + sender + " " + value + " " + messageSender);
|
||||
return false;
|
||||
};
|
||||
}
|
||||
}
|
@ -60,4 +60,15 @@ public class ValidatorCommandTest {
|
||||
assertCMDFramework(e, ExecutionIdentifier.class, "RunOnErrorDouble");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvert() {
|
||||
InvertValidatorCommand cmd = new InvertValidatorCommand();
|
||||
try {
|
||||
cmd.execute("test", "", new String[]{"false", "true"});
|
||||
assertThat(true, is(false));
|
||||
} catch (Exception e) {
|
||||
assertCMDFramework(e, ExecutionIdentifier.class, "RunTestInvert");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren