From 59361e14c0637599231760d4392782e6a9c91c28 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 27 Aug 2022 18:03:16 +0200 Subject: [PATCH] Add AbstractSWCommand.AllowNull --- .../steamwar/command/AbstractSWCommand.java | 8 ++- src/de/steamwar/command/CommandPart.java | 5 +- src/de/steamwar/command/SWCommandUtils.java | 2 + .../steamwar/command/NullMapperCommand.java | 59 +++++++++++++++++++ .../command/NullMapperCommandTest.java | 52 ++++++++++++++++ 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 testsrc/de/steamwar/command/NullMapperCommand.java create mode 100644 testsrc/de/steamwar/command/NullMapperCommandTest.java diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index def1527..e30ac7b 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -76,7 +76,8 @@ public abstract class AbstractSWCommand { errors.forEach(Runnable::run); return; } - commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> {}, sender, alias, args)); + commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> { + }, sender, alias, args)); } } catch (CommandNoHelpException e) { // Ignored @@ -361,4 +362,9 @@ public abstract class AbstractSWCommand { */ boolean allowEAs() default true; } + + @Retention(RetentionPolicy.RUNTIME) + @Target({ElementType.PARAMETER}) + protected @interface AllowNull { + } } diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index 4931ef0..80f0048 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -52,6 +52,9 @@ class CommandPart { @Setter private boolean onlyUseIfNoneIsGiven = false; + @Setter + private boolean allowNullValues = false; + public CommandPart(AbstractSWCommand command, AbstractTypeMapper typeMapper, AbstractValidator validator, Class varArgType, String optional) { this.command = command; this.typeMapper = typeMapper; @@ -183,7 +186,7 @@ class CommandPart { return new CheckArgumentResult(false, null); } } - return new CheckArgumentResult(value != null, value); + return new CheckArgumentResult(allowNullValues || value != null, value); } catch (Exception e) { return new CheckArgumentResult(false, null); } diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 0005949..f6aa8f3 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -94,9 +94,11 @@ public class SWCommandUtils { AbstractValidator validator = (AbstractValidator) getValidator(parameter, localValidator); Class varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null; AbstractSWCommand.OptionalValue optionalValue = parameter.getAnnotation(AbstractSWCommand.OptionalValue.class); + AbstractSWCommand.AllowNull allowNull = parameter.getAnnotation(AbstractSWCommand.AllowNull.class); CommandPart commandPart = new CommandPart<>(command, typeMapper, validator, varArgType, optionalValue != null ? optionalValue.value() : null); commandPart.setOnlyUseIfNoneIsGiven(optionalValue != null && optionalValue.onlyUINIG()); + commandPart.setAllowNullValues(allowNull != null); if (current != null) { current.setNext(commandPart); } diff --git a/testsrc/de/steamwar/command/NullMapperCommand.java b/testsrc/de/steamwar/command/NullMapperCommand.java new file mode 100644 index 0000000..e47eab7 --- /dev/null +++ b/testsrc/de/steamwar/command/NullMapperCommand.java @@ -0,0 +1,59 @@ +/* + * 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 . + */ + +package de.steamwar.command; + +import de.steamwar.command.dto.ExecutionIdentifier; +import de.steamwar.command.dto.TestSWCommand; +import de.steamwar.command.dto.TestTypeMapper; + +import java.util.Collection; + +public class NullMapperCommand extends TestSWCommand { + + public NullMapperCommand() { + super("nullmapper"); + } + + @Register + public void test(String sender, @AllowNull String arg) { + if (arg == null) { + throw new ExecutionIdentifier("null"); + } + throw new ExecutionIdentifier("notnull"); + } + + @ClassMapper(value = String.class, local = true) + public TestTypeMapper typeMapper() { + return new TestTypeMapper() { + @Override + public String map(String sender, String[] previousArguments, String s) { + if (s.equals("Hello World")) { + return null; + } + return s; + } + + @Override + public Collection tabCompletes(String sender, String[] previousArguments, String s) { + return null; + } + }; + } +} diff --git a/testsrc/de/steamwar/command/NullMapperCommandTest.java b/testsrc/de/steamwar/command/NullMapperCommandTest.java new file mode 100644 index 0000000..1e19d13 --- /dev/null +++ b/testsrc/de/steamwar/command/NullMapperCommandTest.java @@ -0,0 +1,52 @@ +/* + * 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 . + */ + +package de.steamwar.command; + +import de.steamwar.command.dto.ExecutionIdentifier; +import org.junit.Test; + +import static de.steamwar.AssertionUtils.assertCMDFramework; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +public class NullMapperCommandTest { + + @Test + public void testNull() { + NullMapperCommand command = new NullMapperCommand(); + try { + command.execute("test", "", new String[] {"Hello World"}); + assertThat(true, is(false)); + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "null"); + } + } + + @Test + public void testNonNull() { + NullMapperCommand command = new NullMapperCommand(); + try { + command.execute("test", "", new String[] {"Hello"}); + assertThat(true, is(false)); + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "notnull"); + } + } +}