Re-order type discovery and function wrapping

Otherwise the explicit types from the function registration are
not used.
This commit is contained in:
Dave Syer
2018-02-26 16:24:55 +00:00
parent 5203401e00
commit 7bc6d7dfee
5 changed files with 84 additions and 7 deletions

View File

@@ -39,7 +39,7 @@ public class FunctionRegistration<T> {
private final Map<String, String> properties = new LinkedHashMap<>();
private Type type;
private FunctionType type;
public FunctionRegistration(T target) {
this.target = target;
@@ -53,7 +53,7 @@ public class FunctionRegistration<T> {
return names;
}
public Type getType() {
public FunctionType getType() {
return type;
}
@@ -79,7 +79,7 @@ public class FunctionRegistration<T> {
}
public FunctionRegistration<T> type(Type type) {
this.type = type;
this.type = FunctionType.of(type);
return this;
}

View File

@@ -45,6 +45,10 @@ public class FunctionType {
this.type = type;
}
public Type getType() {
return type;
}
public Class<?> getInputWrapper() {
return findType(ParamType.INPUT_WRAPPER);
}
@@ -79,6 +83,10 @@ public class FunctionType {
|| Mono.class.equals(type) || Optional.class.equals(type);
}
public static FunctionType of(Type function) {
return new FunctionType(function);
}
public static FunctionType from(Class<?> input) {
return new FunctionType(ResolvableType
.forClassWithGenerics(Function.class, input, Object.class).getType());

View File

@@ -439,10 +439,15 @@ public class ContextFunctionCatalogAutoConfiguration {
private void wrap(FunctionRegistration<Object> registration, String key) {
Object target = registration.getTarget();
this.registrations.put(target, key);
if (registration.getType() != null) {
this.types.put(key, registration.getType());
}
else {
findType(target);
}
Class<?> type;
if (target instanceof Supplier) {
type = Supplier.class;
findType(target);
registration.target(target((Supplier<?>) target, key));
for (String name : registration.getNames()) {
this.suppliers.put(name, registration.getTarget());
@@ -450,7 +455,6 @@ public class ContextFunctionCatalogAutoConfiguration {
}
else if (target instanceof Consumer) {
type = Consumer.class;
findType(target);
registration.target(target((Consumer<?>) target, key));
for (String name : registration.getNames()) {
this.consumers.put(name, registration.getTarget());
@@ -458,7 +462,6 @@ public class ContextFunctionCatalogAutoConfiguration {
}
else if (target instanceof Function) {
type = Function.class;
findType(target);
registration.target(target((Function<?, ?>) target, key));
for (String name : registration.getNames()) {
this.functions.put(name, registration.getTarget());
@@ -467,7 +470,7 @@ public class ContextFunctionCatalogAutoConfiguration {
else {
return;
}
registrations.remove(target);
this.registrations.remove(target);
this.registrations.put(registration.getTarget(), key);
if (publisher != null) {
publisher.publishEvent(new FunctionRegistrationEvent(

View File

@@ -0,0 +1,45 @@
/*
* 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.function.Function;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Dave Syer
*
*/
public class FunctionRegistrationTests {
@Test
public void noTypeByDefault() {
FunctionRegistration<?> registration = new FunctionRegistration<>(new Foos())
.names("foos");
assertThat(registration.getType()).isNull();
assertThat(registration.getNames()).contains("foos");
}
private static class Foos implements Function<Integer, String> {
@Override
public String apply(Integer t) {
return "i=" + t;
}
}
}

View File

@@ -25,6 +25,7 @@ import java.util.function.Supplier;
import org.junit.Test;
import org.springframework.cloud.function.context.FunctionRegistration;
import org.springframework.cloud.function.context.FunctionType;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration.BeanFactoryFunctionCatalog;
import org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration.ContextFunctionRegistry;
@@ -55,6 +56,26 @@ public class BeanFactoryFunctionCatalogTests {
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("4");
}
@Test
public void registerFunctionWithType() {
processor.register(new FunctionRegistration<Function<Integer, String>>(
(Integer i) -> "i=" + i).names("foos").type(
FunctionType.from(Integer.class).to(String.class).getType()));
Function<Flux<Integer>, Flux<String>> foos = processor.lookupFunction("");
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("i=2");
}
@Test
public void registerFunctionWithFluxType() {
processor
.register(new FunctionRegistration<Function<Flux<Integer>, Flux<String>>>(
ints -> ints.map(i -> "i=" + i)).names("foos")
.type(FunctionType.from(Integer.class).to(String.class)
.wrap(Flux.class).getType()));
Function<Flux<Integer>, Flux<String>> foos = processor.lookupFunction("");
assertThat(foos.apply(Flux.just(2)).blockFirst()).isEqualTo("i=2");
}
@Test
public void lookupNonExistentConsumerWithEmptyName() {
processor.register(new FunctionRegistration<>(new Foos()).names("foos"));