Add public registration method to bean post processor
Also simplifies logic of looking up message types
This commit is contained in:
@@ -318,6 +318,12 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> void register(FunctionRegistration<T> function) {
|
||||||
|
wrap((FunctionRegistration<Object>) function,
|
||||||
|
function.getNames().iterator().next());
|
||||||
|
}
|
||||||
|
|
||||||
public Set<FunctionRegistration<?>> merge(
|
public Set<FunctionRegistration<?>> merge(
|
||||||
Map<String, FunctionRegistration<?>> initial,
|
Map<String, FunctionRegistration<?>> initial,
|
||||||
Map<String, Consumer<?>> consumers, Map<String, Supplier<?>> suppliers,
|
Map<String, Consumer<?>> consumers, Map<String, Supplier<?>> suppliers,
|
||||||
@@ -387,7 +393,7 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
private Collection<String> getAliases(String key) {
|
private Collection<String> getAliases(String key) {
|
||||||
Collection<String> names = new LinkedHashSet<>();
|
Collection<String> names = new LinkedHashSet<>();
|
||||||
String value = getQualifier(key);
|
String value = getQualifier(key);
|
||||||
if (value.equals(key)) {
|
if (value.equals(key) && registry != null) {
|
||||||
names.addAll(Arrays.asList(registry.getAliases(key)));
|
names.addAll(Arrays.asList(registry.getAliases(key)));
|
||||||
}
|
}
|
||||||
names.add(value);
|
names.add(value);
|
||||||
@@ -416,7 +422,7 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getQualifier(String key) {
|
private String getQualifier(String key) {
|
||||||
if (registry.containsBeanDefinition(key)) {
|
if (registry != null && registry.containsBeanDefinition(key)) {
|
||||||
BeanDefinition beanDefinition = registry.getBeanDefinition(key);
|
BeanDefinition beanDefinition = registry.getBeanDefinition(key);
|
||||||
Object source = beanDefinition.getSource();
|
Object source = beanDefinition.getSource();
|
||||||
if (source instanceof StandardMethodMetadata) {
|
if (source instanceof StandardMethodMetadata) {
|
||||||
@@ -658,16 +664,10 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean isMessage(Object function) {
|
private boolean isMessage(Object function) {
|
||||||
String name = registrations.get(function);
|
return Message.class
|
||||||
if (name == null || !registry.containsBeanDefinition(name)) {
|
.isAssignableFrom(findType(function, ParamType.INPUT_INNER_WRAPPER))
|
||||||
return false;
|
|| Message.class.isAssignableFrom(
|
||||||
}
|
findType(function, ParamType.OUTPUT_INNER_WRAPPER));
|
||||||
return Message.class.isAssignableFrom(findType(name,
|
|
||||||
(AbstractBeanDefinition) registry.getBeanDefinition(name),
|
|
||||||
ParamType.INPUT_INNER_WRAPPER))
|
|
||||||
|| Message.class.isAssignableFrom(findType(name,
|
|
||||||
(AbstractBeanDefinition) registry.getBeanDefinition(name),
|
|
||||||
ParamType.OUTPUT_INNER_WRAPPER));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Class<?> findType(Object function, ParamType type) {
|
private Class<?> findType(Object function, ParamType type) {
|
||||||
@@ -678,7 +678,8 @@ public class ContextFunctionCatalogAutoConfiguration {
|
|||||||
return values.get(type);
|
return values.get(type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (name == null || !registry.containsBeanDefinition(name)) {
|
if (name == null || registry == null
|
||||||
|
|| !registry.containsBeanDefinition(name)) {
|
||||||
if (function != null) {
|
if (function != null) {
|
||||||
Type param = findTypeFromBeanClass(function.getClass(), type);
|
Type param = findTypeFromBeanClass(function.getClass(), type);
|
||||||
if (param != null) {
|
if (param != null) {
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2016-2017 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.cloud.function.context;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import org.springframework.cloud.function.context.ContextFunctionCatalogAutoConfiguration.ContextFunctionPostProcessor;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Dave Syer
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ContextFunctionPostProcessorTests {
|
||||||
|
|
||||||
|
private ContextFunctionPostProcessor processor = new ContextFunctionPostProcessor();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void basicRegistrationFeatures() {
|
||||||
|
processor.register(new FunctionRegistration<>(new Foos()).names("foos"));
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Function<Flux<Integer>, Flux<String>> foos = (Function<Flux<Integer>, Flux<String>>) processor
|
||||||
|
.compose("foos");
|
||||||
|
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("4");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void registrationThroughMerge() {
|
||||||
|
FunctionRegistration<Foos> registration = new FunctionRegistration<>(new Foos())
|
||||||
|
.names("foos");
|
||||||
|
processor.merge(Collections.singletonMap("foos", registration),
|
||||||
|
Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Function<Flux<Integer>, Flux<String>> foos = (Function<Flux<Integer>, Flux<String>>) processor
|
||||||
|
.compose("foos");
|
||||||
|
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("4");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void registrationThroughMergeFromNamedFunction() {
|
||||||
|
processor.merge(Collections.emptyMap(), Collections.emptyMap(),
|
||||||
|
Collections.emptyMap(), Collections.singletonMap("foos", new Foos()));
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Function<Flux<Integer>, Flux<String>> foos = (Function<Flux<Integer>, Flux<String>>) processor
|
||||||
|
.compose("foos");
|
||||||
|
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("4");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void compose() {
|
||||||
|
processor.register(new FunctionRegistration<>(new Foos()).names("foos"));
|
||||||
|
processor.register(new FunctionRegistration<>(new Bars()).names("bars"));
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Function<Flux<Integer>, Flux<String>> foos = (Function<Flux<Integer>, Flux<String>>) processor
|
||||||
|
.compose("foos,bars");
|
||||||
|
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("Hello 4");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static class Foos implements Function<Integer, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String apply(Integer t) {
|
||||||
|
return "" + 2 * t;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static class Bars implements Function<String, String> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String apply(String t) {
|
||||||
|
return "Hello " + t;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user