Skip to content

GH-8626: Provide cleaner transform() DSL #8653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -538,12 +538,18 @@ public B controlBus(@Nullable Consumer<GenericEndpointSpec<ServiceActivatingHand
/**
* Populate the {@code Transformer} EI Pattern specific {@link MessageHandler} implementation
* for the SpEL {@link Expression}.
* Shortcut for:
* <pre class="code">
* {@code
* .transformWith((transformerSpec) -> transformerSpec.expression(expression))
* }
* </pre>
* @param expression the {@code Transformer} {@link Expression}.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @see ExpressionEvaluatingTransformer
*/
public B transform(String expression) {
return transform(expression, (Consumer<GenericEndpointSpec<MessageTransformingHandler>>) null);
return transformWith((transformerSpec) -> transformerSpec.expression(expression));
}

/**
Expand All @@ -552,8 +558,10 @@ public B transform(String expression) {
* @param expression the {@code Transformer} {@link Expression}.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @deprecated since 6.2 in favor of {@link #transformWith(Consumer)}.
* @see ExpressionEvaluatingTransformer
*/
@Deprecated(since = "6.2", forRemoval = true)
public B transform(String expression,
@Nullable Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {

Expand All @@ -566,9 +574,15 @@ public B transform(String expression,
/**
* Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
* to invoke the discovered service method at runtime.
* Shortcut for:
* <pre class="code">
* {@code
* .transformWith((transformerSpec) -> transformerSpec.ref(service))
* }
* </pre>
* @param service the service to use.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @see ExpressionEvaluatingTransformer
* @see MethodInvokingTransformer
*/
public B transform(Object service) {
return transform(service, null);
Expand All @@ -577,13 +591,36 @@ public B transform(Object service) {
/**
* Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
* to invoke the service method at runtime.
* <pre class="code">
* {@code
* .transformWith((transformerSpec) -> transformerSpec.ref(service).method(methodName))
* }
* </pre>
* @param service the service to use.
* @param methodName the method to invoke.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @see MethodInvokingTransformer
*/
public B transform(Object service, @Nullable String methodName) {
return transform(service, methodName, null);
return transformWith((transformerSpec) -> transformerSpec.ref(service).method(methodName));
}

/**
* Populate the {@code MessageTransformingHandler} for the {@link MethodInvokingTransformer}
* to invoke the bean method at runtime.
* <pre class="code">
* {@code
* .transformWith((transformerSpec) -> transformerSpec.refName(beanName).method(methodName))
* }
* </pre>
* @param beanName the name for bean to resolve lazily.
* @param methodName the method to invoke.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @since 6.2
* @see MethodInvokingTransformer
*/
public B transform(String beanName, @Nullable String methodName) {
return transformWith((transformerSpec) -> transformerSpec.refName(beanName).method(methodName));
}

/**
Expand All @@ -593,8 +630,10 @@ public B transform(Object service, @Nullable String methodName) {
* @param methodName the method to invoke.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @see ExpressionEvaluatingTransformer
* @deprecated since 6.2 in favor of {@link #transformWith(Consumer)}.
* @see MethodInvokingTransformer
*/
@Deprecated(since = "6.2", forRemoval = true)
public B transform(Object service, @Nullable String methodName,
@Nullable Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {

Expand All @@ -617,12 +656,18 @@ public B transform(Object service, @Nullable String methodName,
* .transform(Scripts.script("classpath:myScript.py").variable("foo", bar()))
* }
* </pre>
* Shortcut for:
* <pre class="code">
* {@code
* .transformWith((transformerSpec) -> transformerSpec.processor(messageProcessorSpec))
* }
* </pre>
* @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @see MethodInvokingTransformer
*/
public B transform(MessageProcessorSpec<?> messageProcessorSpec) {
return transform(messageProcessorSpec, (Consumer<GenericEndpointSpec<MessageTransformingHandler>>) null);
return transformWith((transformerSpec) -> transformerSpec.processor(messageProcessorSpec));
}

/**
Expand All @@ -638,8 +683,10 @@ public B transform(MessageProcessorSpec<?> messageProcessorSpec) {
* @param messageProcessorSpec the {@link MessageProcessorSpec} to use.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint options.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @deprecated since 6.2 in favor of {@link #transformWith(Consumer)}.
* @see MethodInvokingTransformer
*/
@Deprecated(since = "6.2", forRemoval = true)
public B transform(MessageProcessorSpec<?> messageProcessorSpec,
@Nullable Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {

Expand Down Expand Up @@ -679,7 +726,8 @@ public <P> B convert(Class<P> payloadType) {
* @see LambdaMessageProcessor
*/
public <P, T> B transform(@Nullable Class<P> expectedType, GenericTransformer<P, T> genericTransformer) {
return transform(expectedType, genericTransformer, null);
return transformWith((transformerSpec) ->
transformerSpec.transformer(genericTransformer).expectedType(expectedType));
}

/**
Expand Down Expand Up @@ -714,10 +762,12 @@ public <P> B convert(Class<P> payloadType,
* @param <P> the payload type - 'transform from', or {@code Message.class}.
* @param <T> the target type - 'transform to'.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @deprecated since 6.2 in favor of {@link #transformWith(Consumer)}
* @see MethodInvokingTransformer
* @see LambdaMessageProcessor
* @see GenericEndpointSpec
*/
@Deprecated(since = "6.2", forRemoval = true)
public <P, T> B transform(@Nullable Class<P> expectedType, GenericTransformer<P, T> genericTransformer,
@Nullable Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {

Expand All @@ -730,6 +780,16 @@ public <P, T> B transform(@Nullable Class<P> expectedType, GenericTransformer<P,
.handle(new MessageTransformingHandler(transformer), endpointConfigurer);
}

/**
* Populate a {@link MessageTransformingHandler} into the endpoint with provided {@link TransformerEndpointSpec} options.
* One of the 'expression', 'ref', 'refName', 'processor' or 'function' must be provided.
* @return the current {@link BaseIntegrationFlowDefinition}.
* @since 6.2
*/
public B transformWith(Consumer<TransformerEndpointSpec> transformerConfigurer) {
return register(new TransformerEndpointSpec(), transformerConfigurer);
}

/**
* Populate a {@link MessageFilter} with {@link MessageSelector} for the provided SpEL expression.
* @param expression the SpEL expression.
Expand Down Expand Up @@ -2787,7 +2847,7 @@ protected <T> Publisher<Message<T>> toReactivePublisher(boolean autoStartOnSubsc
}

protected <S extends ConsumerEndpointSpec<? super S, ? extends MessageHandler>> B register(S endpointSpec,
@Nullable Consumer<S> endpointConfigurer) {
@Nullable Consumer<? super S> endpointConfigurer) {

if (endpointConfigurer != null) {
endpointConfigurer.accept(endpointSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public abstract class IntegrationFlowDefinition<B extends IntegrationFlowDefinit
* Populate the {@link MessageTransformingHandler} instance for the provided
* {@link GenericTransformer}. Use {@link #transform(Class, GenericTransformer)} if
* you need to access the entire message.
* Shortcut for:
* <pre class="code">
* {@code
* .transformWith((transformerSpec) -> transformerSpec.function(genericTransformer))
* }
* </pre>
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param <S> the source type - 'transform from'.
* @param <T> the target type - 'transform to'.
Expand All @@ -58,26 +64,29 @@ public abstract class IntegrationFlowDefinition<B extends IntegrationFlowDefinit
* @see org.springframework.integration.handler.LambdaMessageProcessor
*/
public <S, T> B transform(GenericTransformer<S, T> genericTransformer) {
return transform(null, genericTransformer);
return transformWith((transformerSpec) -> transformerSpec.transformer(genericTransformer));
}


/**
* Populate the {@link MessageTransformingHandler} instance for the provided
* {@link GenericTransformer}. In addition, accept options for the integration endpoint
* using {@link GenericEndpointSpec}. Use
* {@link #transform(Class, GenericTransformer, Consumer)} if you need to access the
* entire message.
* {@code .transform((transformerSpec) -> transformerSpec.function(genericTransformer).expectedType(Message.class))}
* if you need to access the entire message.
* @param genericTransformer the {@link GenericTransformer} to populate.
* @param endpointConfigurer the {@link Consumer} to provide integration endpoint
* options.
* @param <S> the source type - 'transform from'.
* @param <T> the target type - 'transform to'.
* @return the current {@link IntegrationFlowDefinition}.
* @deprecated since 6.2 in favor of {@link #transformWith(Consumer)}
* @see org.springframework.integration.transformer.MethodInvokingTransformer
* @see org.springframework.integration.handler.LambdaMessageProcessor
* @see GenericEndpointSpec
*/
@Deprecated(since = "6.2", forRemoval = true)
@SuppressWarnings("removal")
public <S, T> B transform(GenericTransformer<S, T> genericTransformer,
Consumer<GenericEndpointSpec<MessageTransformingHandler>> endpointConfigurer) {

Expand Down Expand Up @@ -108,7 +117,7 @@ public <P> B filter(GenericSelector<P> genericSelector) {
* Populate a {@link org.springframework.integration.filter.MessageFilter}
* with {@link org.springframework.integration.filter.MethodInvokingSelector}
* for the provided {@link GenericSelector}.
* In addition accept options for the integration endpoint using {@link FilterEndpointSpec}.
* In addition, accept options for the integration endpoint using {@link FilterEndpointSpec}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
Expand Down Expand Up @@ -152,7 +161,7 @@ public <P> B handle(GenericHandler<P> handler) {
* Populate a {@link ServiceActivatingHandler} for the
* {@link org.springframework.integration.handler.MethodInvokingMessageProcessor}
* to invoke the provided {@link GenericHandler} at runtime.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* In addition, accept options for the integration endpoint using {@link GenericEndpointSpec}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
Expand All @@ -177,7 +186,7 @@ public <P> B handle(GenericHandler<P> handler,
/**
* Populate the {@link MethodInvokingSplitter} to evaluate the provided
* {@link Function} at runtime.
* In addition accept options for the integration endpoint using {@link GenericEndpointSpec}.
* In addition, accept options for the integration endpoint using {@link GenericEndpointSpec}.
* Typically used with a Java 8 Lambda expression:
* <pre class="code">
* {@code
Expand Down
Loading