Skip to content

Commit

Permalink
Support suspending parsers. (Incendo#715)
Browse files Browse the repository at this point in the history
* Support suspending parsers.

* Update KotlinAnnotatedMethods.kt

* Update KotlinAnnotatedMethods.kt

* Fix Incendo#713

* remove Nonnul to make the kotlin happy.

* Revert "Fix Incendo#713"

This reverts commit 1260d94.
  • Loading branch information
portlek authored Apr 17, 2024
1 parent 550317b commit 85e5235
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
import org.incendo.cloud.annotations.extractor.FlagExtractorImpl;
import org.incendo.cloud.annotations.extractor.StandardArgumentExtractor;
import org.incendo.cloud.annotations.injection.RawArgs;
import org.incendo.cloud.annotations.parser.MethodArgumentParser;
import org.incendo.cloud.annotations.parser.MethodArgumentParserFactory;
import org.incendo.cloud.annotations.parser.Parser;
import org.incendo.cloud.annotations.processing.CommandContainer;
import org.incendo.cloud.annotations.processing.CommandContainerProcessor;
Expand All @@ -85,7 +85,7 @@
import org.incendo.cloud.internal.CommandInputTokenizer;
import org.incendo.cloud.meta.CommandMeta;
import org.incendo.cloud.meta.CommandMetaBuilder;
import org.incendo.cloud.parser.ArgumentParser;
import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.parser.ParserParameter;
import org.incendo.cloud.parser.ParserParameters;
import org.incendo.cloud.parser.flag.CommandFlag;
Expand Down Expand Up @@ -133,6 +133,7 @@ public final class AnnotationParser<C> {
private FlagAssembler flagAssembler;
private CommandExtractor commandExtractor;
private SuggestionProviderFactory<C> suggestionProviderFactory;
private MethodArgumentParserFactory<C> methodArgumentParserFactory;
private ExceptionHandlerFactory<C> exceptionHandlerFactory;
private DescriptionMapper descriptionMapper;
private DefaultValueRegistry<C> defaultValueRegistry;
Expand Down Expand Up @@ -212,6 +213,7 @@ public AnnotationParser(
this.argumentAssembler = new ArgumentAssemblerImpl<>(this);
this.commandExtractor = new CommandExtractorImpl(this);
this.suggestionProviderFactory = SuggestionProviderFactory.defaultFactory();
this.methodArgumentParserFactory = MethodArgumentParserFactory.defaultFactory();
this.exceptionHandlerFactory = ExceptionHandlerFactory.defaultFactory();
this.builderDecorators = new ArrayList<>();
this.defaultValueRegistry = new DefaultValueRegistryImpl<>();
Expand Down Expand Up @@ -570,6 +572,26 @@ public void suggestionProviderFactory(final @NonNull SuggestionProviderFactory<C
this.suggestionProviderFactory = suggestionProviderFactory;
}

/**
* Returns the method argument parser factory.
*
* @return the method argument parser factory
*/
@API(status = API.Status.EXPERIMENTAL)
public @NonNull MethodArgumentParserFactory<C> methodArgumentParserFactory() {
return this.methodArgumentParserFactory;
}

/**
* Sets the method argument parser factory.
*
* @param methodArgumentParserFactory new method argument parser factory
*/
@API(status = API.Status.EXPERIMENTAL)
public void methodArgumentParserFactory(final @NonNull MethodArgumentParserFactory<C> methodArgumentParserFactory) {
this.methodArgumentParserFactory = methodArgumentParserFactory;
}

/**
* Returns the exception provider factory.
*
Expand Down Expand Up @@ -859,25 +881,17 @@ private <T> void parseParsers(final @NonNull T instance) {
)
));
}
final MethodArgumentParser<C, ?> methodArgumentParser = new MethodArgumentParser<>(
final ParserDescriptor<C, ?> parserDescriptor = this.methodArgumentParserFactory.createArgumentParser(
suggestionProvider,
instance,
method,
this.manager.parameterInjectorRegistry()
);
final Function<ParserParameters, ArgumentParser<C, ?>> parserFunction =
parameters -> methodArgumentParser;
final String name = this.processString(parser.name());
if (name.isEmpty()) {
this.manager.parserRegistry().registerParserSupplier(
TypeToken.get(method.getGenericReturnType()),
parserFunction
);
this.manager.parserRegistry().registerParser(parserDescriptor);
} else {
this.manager.parserRegistry().registerNamedParserSupplier(
name,
parserFunction
);
this.manager.parserRegistry().registerNamedParser(name, parserDescriptor);
}
} catch (final Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.annotations.parser;

import java.lang.reflect.Method;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.injection.ParameterInjectorRegistry;
import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.suggestion.SuggestionProvider;

@FunctionalInterface
@API(status = API.Status.EXPERIMENTAL)
public interface MethodArgumentParserFactory<C> {

/**
* Returns a factory that produces {@link MethodArgumentParserFactoryImpl} instances.
*
* @param <C> the command sender type
* @return the created factory
*/
static <C> @NonNull MethodArgumentParserFactory<C> defaultFactory() {
return new MethodArgumentParserFactoryImpl<>();
}

/**
* Creates a suggestion provider using the given {@code method}.
*
* @param suggestionProvider suggestion provider
* @param instance parsed instance
* @param method suggestion method
* @param injectorRegistry injector registry
* @return the suggestion provider
*/
@NonNull ParserDescriptor<C, ?> createArgumentParser(
@NonNull SuggestionProvider<C> suggestionProvider,
@NonNull Object instance,
@NonNull Method method,
@NonNull ParameterInjectorRegistry<C> injectorRegistry
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.annotations.parser;

import io.leangen.geantyref.TypeToken;
import java.lang.reflect.Method;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.injection.ParameterInjectorRegistry;
import org.incendo.cloud.parser.ParserDescriptor;
import org.incendo.cloud.suggestion.SuggestionProvider;

/**
* Represents a method argument parser annotated with {@link Parser}
*
* @param <C> command sender type
*/
public final class MethodArgumentParserFactoryImpl<C> implements MethodArgumentParserFactory<C> {

@Override
public @NonNull ParserDescriptor<C, ?> createArgumentParser(
final @NonNull SuggestionProvider<C> suggestionProvider,
final @NonNull Object instance,
final @NonNull Method method,
final @NonNull ParameterInjectorRegistry<C> injectorRegistry
) {
return ParserDescriptor.of(
new MethodArgumentParser<>(suggestionProvider, instance, method, injectorRegistry),
TypeToken.get(method.getGenericReturnType())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public interface ArgumentParser<C, T> extends SuggestionProviderHolder<C> {
*/
@API(status = API.Status.STABLE)
default @NonNull CompletableFuture<@NonNull ArgumentParseResult<T>> parseFuture(
@NonNull CommandContext<@NonNull C> commandContext,
@NonNull CommandContext<C> commandContext,
@NonNull CommandInput commandInput
) {
return CompletableFuture.completedFuture(this.parse(commandContext, commandInput));
Expand Down Expand Up @@ -215,7 +215,7 @@ interface FutureArgumentParser<C, T> extends ArgumentParser<C, T> {

@Override
@NonNull CompletableFuture<@NonNull ArgumentParseResult<T>> parseFuture(
@NonNull CommandContext<@NonNull C> commandContext,
@NonNull CommandContext<C> commandContext,
@NonNull CommandInput commandInput
);
}
Expand Down
Loading

0 comments on commit 85e5235

Please sign in to comment.