How to reduce the following code using for loop, need to capture jsonPath matches dynamically

Hi alli ,
Can someone help me optimize the code below using for/foreach loop.The problem is if the list is big the doElse loop can grow too big.if in foreach loop we can wrap the operations as mentioned below and capture jsonPath matches in wid<0>,wid<1> etc based on counter value, it will be great

Relevant code:

.repeat(session =>{ session(“list”).as[Seq[String]].size},“index”){
exec(session => {
index = session(“index”).as[Int]
session.set(“field”, list(index))
})
// .exec({ session =>
// println(“list element is " + list(index) + " wid index is " + wid(index) + " count is " + index)
// session
// })
.doIfEquals(session=>{session(“index”).as[Int]},0) {
exec(http(“Fields Lookup”)
.get(”/ots/super/services/wql/v1/dataSources/${datasourceId}/fields?offset=0&limit=100")
.headers(Map("" → “0”, “Session-Secure-Token” → “${csrfToken}”, “Content-Type” → “application/json”
, “Authorization” → auth))
.check(
status.is(200)
)
.check(jsonPath("$.data[?(@.descriptor == “${field}”)].id").find.saveAs(“wid0”)))
}.
doIfEquals(session=>{session(“index”).as[Int]},1) {
exec(http(“Fields Lookup”)
.get("/ots/super/services/wql/v1/dataSources/${datasourceId}/fields?offset=0&limit=100")
.headers(Map(""" → “0”, “Session-Secure-Token” → “${csrfToken}”, “Content-Type” → “application/json”
, “Authorization” → auth))
.check(
status.is(200)
)
.check(jsonPath("$.data[?(@.descriptor == “${field}”)].id").find.saveAs(“wid1”)))
}.doIfEquals(session=>{session(“index”).as[Int]},2) {
exec(http(“Fields Lookup”)
.get("/ots/super/services/wql/v1/dataSources/${datasourceId}/fields?offset=0&limit=100")
.headers(Map(""" → “0”, “Session-Secure-Token” → “${csrfToken}”, “Content-Type” → “application/json”
, “Authorization” → auth))
.check(
status.is(200)
)
.check(jsonPath("$.data[?(@.descriptor == “${field}”)].id").find.saveAs(“wid2”)))
}.doIfEquals(session=>{session(“index”).as[Int]},3) {
exec(http(“Fields Lookup”)
.get("/ots/super/services/wql/v1/dataSources/${datasourceId}/fields?offset=0&limit=100")
.headers(Map(""" → “0”, “Session-Secure-Token” → “${csrfToken}”, “Content-Type” → “application/json”
, “Authorization” → auth))
.check(
status.is(200)
)
.check(jsonPath("$.data[?(@.descriptor == “${field}”)].id").find.saveAs(“wid3”)))
}.doIfEquals(session=>{session(“index”).as[Int]},4) {
exec(http(“Fields Lookup”)
.get("/ots/super/services/wql/v1/dataSources/${datasourceId}/fields?offset=0&limit=100")
.headers(Map("" → “0”, “Session-Secure-Token” → “${csrfToken}”, “Content-Type” → “application/json”
, “Authorization” → auth))
.check(
status.is(200)
)
.check(jsonPath("$.data[?(@.descriptor == “${field}”)].id").find.saveAs(“wid4”)))
}.doIfEquals(session=>{session(“index”).as[Int]},5) {
exec(http(“Fields Lookup”)
.get("/ots/super/services/wql/v1/dataSources/${datasourceId}/fields?offset=0&limit=100")
.headers(Map(""" → “0”, “Session-Secure-Token” → “${csrfToken}”, “Content-Type” → “application/json”
, “Authorization” → auth))
.check(
status.is(200)
)
.check(jsonPath("$.data[?(@.descriptor == “${field}”)].id").find.saveAs(“wid5”)))
}.doIfEquals(session=>{session(“index”).as[Int]},6) {
exec(http(“Fields Lookup”)
.get("/ots/super/services/wql/v1/dataSources/${datasourceId}/fields?offset=0&limit=100")
.headers(Map(“X-Workday-Client” → “0”, “Session-Secure-Token” → “${csrfToken}”, “Content-Type” → “application/json”
, “Authorization” → auth))
.check(
status.is(200)
)
.check(jsonPath("$.data[?(@.descriptor == “${field}”)].id").find.saveAs(“wid6”)))
}
}.exec(QueryRequests.chartQueries)
// }
}
)

Thanks
Sandeep

Hi Sandeep,

It’s not an issue related to gatling, but to scala in general.Please see how to refactor code in scala.
Specifically here, how to extract function, I guess.

Regards,

Thanks Sebastien,
So looks like you are suggesting it can work using function extraction in scala ? it will be good to know whether this is hunch or you are sure about it
Also , any example in this regard if you can point me too
Regards
Sandeep