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);
}
}
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();
}
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;
}
};
}
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;
}
};
}
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;
}
Aggregations