Using session attribute in scala function that returns chain to be executed

I want to use a session attribute to determine what chain to execute (via a Map). None of these is valid:

exec(MyMaps.chains("${bar}") //MyMaps.chains is a Map(String, chain)
exec(MyMaps.chains(session(“bar”).as[String])

exec(session => {
MyMaps.chains(session(“bar”).as[String]) //Doesn’t execute
session
})

I also don’t want to use doIf, as I would either have to use a lot of them, or really long inner blocks with lots of copy/pasting involved, and a lot of hard-coded values that would make it unmaintanable:

doIf("${bar}", “foo”) {
exec(FooRequest.req) //The map wouldn’t help here, anyway
}
.doIf("${bar}", “moo”) {
exec(MooRequest.req)
}

If there are 10 requests and I add another value for bar or change moo to boo, it would take forever to update all my scenarios.

Is there any other way I could accomplish this? I also tried using a var bar before declaring the scenario (variable would do just as well as a session attribute), but that a) is shared between users (not the same for all), and b) can’t be changed from within the scenario. I don’t think there’s any way to get a variable with the right scope. (I’m on 2.0.0-snapshot, if it makes a difference.)

For completeness/clarity, Appendix A: the map

object MyMaps {
val chains = Map(
“foo” → FooRequest.req,
“moo” → MooRequest.req
)
}

doSwitch, new in upcoming version
https://github.com/excilys/gatling/blob/master/src/sphinx/general/scenario.rst#doswitch

Still has the maintainability issues of doIf, but at least it’s a lot cleaner. Can’t seem to get it to work, however (I assume by upcoming version you mean upcoming release, but it’s already in the snapshot, right?)

Tried this:

doSwitch("${bar}") {
“foo” → MyMaps.chains(“foo”),
“moo” → MyMaps.chains(“moo”)
}

And am getting “’;’ expected but ‘,’ found” at the end of the “foo” line.

(It may be worth noting that I can confirm the following works fine):

exec(MyMaps.chains(“foo”))

Figured it out: docs show braces around the cases, but they should be parens. Possibly same issue with other structure elements.

That was an error in the documentation.
Just like randomSwitch, you can’t use curly braces.
In Scala, you may use curly braces instead of parentheses only if the parameter list block is of size 1.

doSwitch("${bar}") ( // parentheses
“foo” → MyMaps.chains(“foo”),
“moo” → MyMaps.chains(“moo”)
)

We shot at the same time.
Documentation is fixed. :slight_smile: