Skip to content
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

Support mule component <foreach /> #130

Closed
sanagaraj-pivotal opened this issue May 24, 2022 · 0 comments · Fixed by #136
Closed

Support mule component <foreach /> #130

sanagaraj-pivotal opened this issue May 24, 2022 · 0 comments · Fixed by #136
Assignees
Labels
type: enhancement New feature or request
Milestone

Comments

@sanagaraj-pivotal
Copy link
Contributor

sanagaraj-pivotal commented May 24, 2022

What needs to be done

When component is detected, translate it to appropriate spring DSL translation

Example inputs and outputs

Sample 1

input

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
	xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
	<flow name="foreach">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/foreach" doc:name="HTTP"/>	
        <foreach collection="#[['apple', 'banana', 'orange']]">
        	<logger message="#[payload]" level="INFO" />
        </foreach>
		<logger message="Done with for looping" level="INFO" />
	</flow>
</mule>

output:

    @Bean
    IntegrationFlow flow() {
        return IntegrationFlows.from(Http.inboundGateway("/foreachtest"))
                //TODO: translate expression #[['apple', 'banana', 'orange']] which must produces an array
                // to iterate over
                .split()
                .log()
                .aggregate()
                .log()
                .get();
    }
Sample 2

Children of foreach can be complex such as choice

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
	xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
	<flow name="foreach">
		<http:listener config-ref="HTTP_Listener_Configuration" path="/foreach" doc:name="HTTP"/>	
        <foreach collection="#[[1, 2, 3, 4]]">
        	<logger message="#[payload]" level="INFO" />
        	<choice doc:name="Choice">
			<when expression="#[payload == 1]">
				<logger level="INFO" message="Ondu"></logger>
			</when>
			<when expression="#[payload == 2]">
				<logger level="INFO" message="Eradu"></logger>
			</when>
			<when expression="#[payload == 3]">
				<logger level="INFO" message="Mooru"></logger>
			</when>
			<otherwise>
				<logger level="INFO" message="Moorina mele"></logger>
			</otherwise>
		</choice>
        </foreach>
		<logger message="Done with for looping" level="INFO" />
	</flow>
</mule>

output

@Bean
    IntegrationFlow foreach() {
        return IntegrationFlows.from(Http.inboundChannelAdapter("/foreach")).handle((p, h) -> p)
                //TODO: translate expression #[[1, 2, 3, 4]] which must produces an array
                // to iterate over
                .split()
                /* TODO: LinkedMultiValueMap might not be apt, substitute with right input type*/
                .<LinkedMultiValueMap<String, String>, String>route(
                        p -> p.getFirst("dataKey") /*TODO: use apt condition*/,
                        m -> m
                                .subFlowMapping("dataValue" /*TODO: Translate dataValue to #[payload == 1]*/,
                                        sf -> sf.log(LoggingHandler.Level.INFO, "Ondu")
                                )
                                .subFlowMapping("dataValue" /*TODO: Translate dataValue to #[payload == 2]*/,
                                        sf -> sf.log(LoggingHandler.Level.INFO, "Eradu")
                                )
                                .subFlowMapping("dataValue" /*TODO: Translate dataValue to #[payload == 3]*/,
                                        sf -> sf.log(LoggingHandler.Level.INFO, "Mooru")
                                )
                                .resolutionRequired(false)
                                .defaultSubFlowMapping(sf -> sf.log(LoggingHandler.Level.INFO, "Moorina mele"))
                )
                .aggregate()
                .log(LoggingHandler.Level.INFO, "Done with for looping")
                .get();
    }

More information of foreach mule component can be found here

Additional notes

Batch sizing using foreach can be supported later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants