Search in sources :

Example 6 with ArgumentConversionContext

use of io.micronaut.core.convert.ArgumentConversionContext in project micronaut-core by micronaut-projects.

the class EnvironmentConvertibleValuesMap method get.

@Override
public <T> Optional<T> get(CharSequence name, ArgumentConversionContext<T> conversionContext) {
    V value = map.get(name);
    if (value instanceof AnnotationClassValue) {
        AnnotationClassValue acv = (AnnotationClassValue) value;
        return environment.convert(acv, conversionContext);
    } else if (value instanceof CharSequence) {
        PropertyPlaceholderResolver placeholderResolver = environment.getPlaceholderResolver();
        String str = doResolveIfNecessary((CharSequence) value, placeholderResolver);
        return environment.convert(str, conversionContext);
    } else if (value instanceof String[]) {
        PropertyPlaceholderResolver placeholderResolver = environment.getPlaceholderResolver();
        String[] resolved = Arrays.stream((String[]) value).flatMap(val -> {
            try {
                String[] values = placeholderResolver.resolveRequiredPlaceholder(val, String[].class);
                return Arrays.stream(values);
            } catch (ConfigurationException e) {
                return Stream.of(doResolveIfNecessary(val, placeholderResolver));
            }
        }).toArray(String[]::new);
        return environment.convert(resolved, conversionContext);
    } else if (value instanceof io.micronaut.core.annotation.AnnotationValue[]) {
        io.micronaut.core.annotation.AnnotationValue[] annotationValues = (io.micronaut.core.annotation.AnnotationValue[]) value;
        io.micronaut.core.annotation.AnnotationValue[] b = new AnnotationValue[annotationValues.length];
        for (int i = 0; i < annotationValues.length; i++) {
            io.micronaut.core.annotation.AnnotationValue annotationValue = annotationValues[i];
            b[i] = new EnvironmentAnnotationValue(environment, annotationValue);
        }
        return environment.convert(b, conversionContext);
    } else if (value instanceof io.micronaut.core.annotation.AnnotationValue) {
        io.micronaut.core.annotation.AnnotationValue av = (io.micronaut.core.annotation.AnnotationValue) value;
        av = new EnvironmentAnnotationValue(environment, av);
        return environment.convert(av, conversionContext);
    } else {
        return super.get(name, conversionContext);
    }
}
Also used : Environment(io.micronaut.context.env.Environment) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) ConvertibleValues(io.micronaut.core.convert.value.ConvertibleValues) Arrays(java.util.Arrays) ConvertibleValuesMap(io.micronaut.core.convert.value.ConvertibleValuesMap) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) Collection(java.util.Collection) Internal(io.micronaut.core.annotation.Internal) Collectors(java.util.stream.Collectors) ConversionContext(io.micronaut.core.convert.ConversionContext) Stream(java.util.stream.Stream) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Map(java.util.Map) Optional(java.util.Optional) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) Argument(io.micronaut.core.type.Argument) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) PropertyPlaceholderResolver(io.micronaut.context.env.PropertyPlaceholderResolver) AnnotationClassValue(io.micronaut.core.annotation.AnnotationClassValue) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) AnnotationValue(io.micronaut.core.annotation.AnnotationValue)

Example 7 with ArgumentConversionContext

use of io.micronaut.core.convert.ArgumentConversionContext in project micronaut-kafka by micronaut-projects.

the class BatchConsumerRecordsBinderRegistry method findArgumentBinder.

@SuppressWarnings("unchecked")
@Override
public <T> Optional<ArgumentBinder<T, ConsumerRecords<?, ?>>> findArgumentBinder(Argument<T> argument, ConsumerRecords<?, ?> source) {
    Class<T> argType = argument.getType();
    if (Iterable.class.isAssignableFrom(argType) || argType.isArray() || Publishers.isConvertibleToPublisher(argType)) {
        Argument<?> batchType = argument.getFirstTypeVariable().orElse(Argument.OBJECT_ARGUMENT);
        List bound = new ArrayList();
        return Optional.of((context, consumerRecords) -> {
            for (ConsumerRecord<?, ?> consumerRecord : consumerRecords) {
                Optional<ArgumentBinder<?, ConsumerRecord<?, ?>>> binder = consumerRecordBinderRegistry.findArgumentBinder((Argument) argument, consumerRecord);
                binder.ifPresent(b -> {
                    Argument<?> newArg = Argument.of(batchType.getType(), argument.getName(), argument.getAnnotationMetadata(), batchType.getTypeParameters());
                    ArgumentConversionContext conversionContext = ConversionContext.of(newArg);
                    ArgumentBinder.BindingResult<?> result = b.bind(conversionContext, consumerRecord);
                    if (result.isPresentAndSatisfied()) {
                        bound.add(result.get());
                    }
                });
            }
            return () -> {
                if (Publisher.class.isAssignableFrom(argument.getType())) {
                    return ConversionService.SHARED.convert(Flux.fromIterable(bound), argument);
                } else {
                    return ConversionService.SHARED.convert(bound, argument);
                }
            };
        });
    }
    return Optional.empty();
}
Also used : ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ArgumentBinder(io.micronaut.core.bind.ArgumentBinder) Publisher(org.reactivestreams.Publisher)

Example 8 with ArgumentConversionContext

use of io.micronaut.core.convert.ArgumentConversionContext in project micronaut-core by micronaut-projects.

the class DefaultExecutableBinder method bind.

@Override
public <T, R> BoundExecutable<T, R> bind(Executable<T, R> target, ArgumentBinderRegistry<S> registry, S source) throws UnsatisfiedArgumentException {
    Argument[] arguments = target.getArguments();
    Object[] boundArguments = new Object[arguments.length];
    for (int i = 0; i < arguments.length; i++) {
        Argument<?> argument = arguments[i];
        if (preBound.containsKey(argument)) {
            boundArguments[i] = preBound.get(argument);
        } else {
            Optional<? extends ArgumentBinder<?, S>> argumentBinder = registry.findArgumentBinder(argument, source);
            if (argumentBinder.isPresent()) {
                ArgumentBinder<?, S> binder = argumentBinder.get();
                ArgumentConversionContext conversionContext = ConversionContext.of(argument);
                ArgumentBinder.BindingResult<?> bindingResult = binder.bind(conversionContext, source);
                if (!bindingResult.isPresentAndSatisfied()) {
                    if (argument.isNullable()) {
                        boundArguments[i] = null;
                    } else {
                        final Optional<ConversionError> lastError = conversionContext.getLastError();
                        if (lastError.isPresent()) {
                            throw new ConversionErrorException(argument, lastError.get());
                        } else {
                            throw new UnsatisfiedArgumentException(argument);
                        }
                    }
                } else {
                    boundArguments[i] = bindingResult.get();
                }
            } else {
                throw new UnsatisfiedArgumentException(argument);
            }
        }
    }
    return new BoundExecutable<T, R>() {

        @Override
        public Executable<T, R> getTarget() {
            return target;
        }

        @Override
        public R invoke(T instance) {
            return target.invoke(instance, getBoundArguments());
        }

        @Override
        public Object[] getBoundArguments() {
            return boundArguments;
        }
    };
}
Also used : UnsatisfiedArgumentException(io.micronaut.core.bind.exceptions.UnsatisfiedArgumentException) Argument(io.micronaut.core.type.Argument) ConversionError(io.micronaut.core.convert.ConversionError) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext)

Example 9 with ArgumentConversionContext

use of io.micronaut.core.convert.ArgumentConversionContext in project micronaut-core by micronaut-projects.

the class DefaultExecutableBinder method tryBind.

@Override
public <T, R> BoundExecutable<T, R> tryBind(Executable<T, R> target, ArgumentBinderRegistry<S> registry, S source) {
    Argument[] arguments = target.getArguments();
    Object[] boundArguments = new Object[arguments.length];
    List<Argument<?>> unbound = new ArrayList<>(arguments.length);
    for (int i = 0; i < arguments.length; i++) {
        Argument<?> argument = arguments[i];
        if (preBound.containsKey(argument)) {
            boundArguments[i] = preBound.get(argument);
        } else {
            Optional<? extends ArgumentBinder<?, S>> argumentBinder = registry.findArgumentBinder(argument, source);
            if (argumentBinder.isPresent()) {
                ArgumentBinder<?, S> binder = argumentBinder.get();
                ArgumentConversionContext conversionContext = ConversionContext.of(argument);
                ArgumentBinder.BindingResult<?> bindingResult = binder.bind(conversionContext, source);
                if (!bindingResult.isPresentAndSatisfied()) {
                    if (argument.isNullable()) {
                        boundArguments[i] = null;
                    } else {
                        boundArguments[i] = null;
                        unbound.add(argument);
                    }
                } else {
                    boundArguments[i] = bindingResult.get();
                }
            } else {
                boundArguments[i] = null;
                unbound.add(argument);
            }
        }
    }
    return new BoundExecutable<T, R>() {

        @Override
        public List<Argument<?>> getUnboundArguments() {
            return Collections.unmodifiableList(unbound);
        }

        @Override
        public Executable<T, R> getTarget() {
            return target;
        }

        @Override
        public R invoke(T instance) {
            return target.invoke(instance, getBoundArguments());
        }

        @Override
        public Object[] getBoundArguments() {
            return boundArguments;
        }
    };
}
Also used : Argument(io.micronaut.core.type.Argument) ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext)

Example 10 with ArgumentConversionContext

use of io.micronaut.core.convert.ArgumentConversionContext in project micronaut-core by micronaut-projects.

the class StreamFunctionExecutor method doConvertInput.

private Object doConvertInput(ConversionService<?> conversionService, Argument<?> arg, Object object) {
    ArgumentConversionContext conversionContext = ConversionContext.of(arg);
    Optional<?> convert = conversionService.convert(object, conversionContext);
    if (convert.isPresent()) {
        return convert.get();
    } else {
        Optional<ConversionError> lastError = conversionContext.getLastError();
        if (lastError.isPresent()) {
            throw new ConversionErrorException(arg, lastError.get());
        }
    }
    return null;
}
Also used : ArgumentConversionContext(io.micronaut.core.convert.ArgumentConversionContext) ConversionError(io.micronaut.core.convert.ConversionError) ConversionErrorException(io.micronaut.core.convert.exceptions.ConversionErrorException)

Aggregations

ArgumentConversionContext (io.micronaut.core.convert.ArgumentConversionContext)17 Argument (io.micronaut.core.type.Argument)12 Optional (java.util.Optional)9 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)5 ConversionContext (io.micronaut.core.convert.ConversionContext)5 ConversionService (io.micronaut.core.convert.ConversionService)5 ConversionErrorException (io.micronaut.core.convert.exceptions.ConversionErrorException)5 HttpRequest (io.micronaut.http.HttpRequest)5 Collection (java.util.Collection)5 ConversionError (io.micronaut.core.convert.ConversionError)4 Collections (java.util.Collections)4 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)3 Internal (io.micronaut.core.annotation.Internal)3 Nullable (io.micronaut.core.annotation.Nullable)3 ArgumentBinder (io.micronaut.core.bind.ArgumentBinder)3 AbstractAnnotatedArgumentBinder (io.micronaut.core.bind.annotation.AbstractAnnotatedArgumentBinder)3 Collectors (java.util.stream.Collectors)3 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 NonNull (io.micronaut.core.annotation.NonNull)2 Bindable (io.micronaut.core.bind.annotation.Bindable)2