Support filterFunction and spelFunction composition
This commit is contained in:
@@ -14,11 +14,11 @@ Once injected, you can use the `apply` method of the `Function` to invoke it and
|
||||
|
||||
## Configuration Options
|
||||
|
||||
The filter function makes uses of link:../spel-function/README.adoc[SpEL function].
|
||||
All configuration properties are prefixed with `filter.function`.
|
||||
|
||||
For more information on the various options available, please see link:../spel-function/src/main/java/org/springframework/cloud/fn/spel/SpelFunctionProperties.java[SpelFunctionProperties.java]
|
||||
For more information on the various options available, please see link:src/main/java/org/springframework/cloud/fn/filter/FilterFunctionProperties.java[FilterFunctionProperties.java]
|
||||
|
||||
## Tests
|
||||
## Examples
|
||||
|
||||
See this link:src/test/java/org/springframework/cloud/fn/filter/FilterFunctionApplicationTests.java[test suite] for examples of how this function is used.
|
||||
|
||||
|
||||
@@ -17,8 +17,12 @@
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud.fn</groupId>
|
||||
<artifactId>spel-function</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>payload-converter-function</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-integration</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -19,25 +19,36 @@ package org.springframework.cloud.fn.filter;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.cloud.fn.spel.SpelFunctionConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
import org.springframework.integration.transformer.ExpressionEvaluatingTransformer;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
/**
|
||||
* @author Artem Bilan
|
||||
* @author David Turanski
|
||||
*/
|
||||
@Configuration
|
||||
@Import(SpelFunctionConfiguration.class)
|
||||
@EnableConfigurationProperties(FilterFunctionProperties.class)
|
||||
public class FilterFunctionConfiguration {
|
||||
|
||||
@Bean
|
||||
public Function<Message<?>, Message<?>> filterFunction(
|
||||
@Qualifier("spelFunction") Function<Message<?>, Message<?>> spelFunction) {
|
||||
ExpressionEvaluatingTransformer filterExpressionEvaluatingTransformer) {
|
||||
|
||||
return message ->
|
||||
Optional.of(message)
|
||||
.filter(m -> (Boolean) spelFunction.apply(m).getPayload())
|
||||
.orElse(null);
|
||||
return message -> Optional.of(message)
|
||||
.filter(m -> (Boolean) filterExpressionEvaluatingTransformer.transform(m).getPayload())
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExpressionEvaluatingTransformer filterExpressionEvaluatingTransformer(
|
||||
FilterFunctionProperties filterFunctionProperties) {
|
||||
|
||||
return new ExpressionEvaluatingTransformer(new SpelExpressionParser()
|
||||
.parseExpression(filterFunctionProperties.getExpression()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2020-2020 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
|
||||
*
|
||||
* https://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.fn.filter;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.expression.spel.standard.SpelExpressionParser;
|
||||
|
||||
/**
|
||||
* Configuration properties for the SpEL function.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @author Artem Bilan
|
||||
*/
|
||||
@ConfigurationProperties("filter.function")
|
||||
public class FilterFunctionProperties {
|
||||
|
||||
private static final Expression DEFAULT_EXPRESSION = new SpelExpressionParser().parseExpression("payload");
|
||||
|
||||
/**
|
||||
* A SpEL expression to apply.
|
||||
*/
|
||||
private String expression = DEFAULT_EXPRESSION.getExpressionString();
|
||||
|
||||
public String getExpression() {
|
||||
return this.expression;
|
||||
}
|
||||
|
||||
public void setExpression(String expression) {
|
||||
this.expression = expression;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -30,7 +30,7 @@ import org.springframework.test.annotation.DirtiesContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest(properties = "spel.function.expression=payload.length() > 5")
|
||||
@SpringBootTest(properties = "filter.function.expression=payload.length() > 5")
|
||||
@DirtiesContext
|
||||
public class FilterFunctionApplicationTests {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user