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

**URL:** https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086
**Category:** Gatling (Open-Source)
**Created:** [June 10, 2014, 5:27pm UTC](https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086 "2014-06-10T17:27:33Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)
#### Post date: [June 10, 2014, 5:27pm UTC](https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086/1 "2014-06-10T17:27:33Z")

</div>

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](https://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  
)  
}

---

<div class="post-metadata">

### Author: ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)
#### Post date: [June 10, 2014, 5:48pm UTC](https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086/2 "2014-06-10T17:48:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)
#### Post date: [June 10, 2014, 6:19pm UTC](https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086/3 "2014-06-10T18:19:44Z")

</div>

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”))

---

<div class="post-metadata">

### Author: ![Michelle\_Burrows](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Michelle\_Burrows](https://community.gatling.io/u/Michelle_Burrows)
#### Post date: [June 10, 2014, 7:03pm UTC](https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086/4 "2014-06-10T19:03:47Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)
#### Post date: [June 10, 2014, 7:05pm UTC](https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086/5 "2014-06-10T19:05:38Z")

</div>

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”)  
)

---

<div class="post-metadata">

### Author: ![Excilys](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@Excilys](https://community.gatling.io/u/Excilys)
#### Post date: [June 10, 2014, 7:06pm UTC](https://community.gatling.io/t/using-session-attribute-in-scala-function-that-returns-chain-to-be-executed/1086/6 "2014-06-10T19:06:13Z")

</div>

We shot at the same time.  
Documentation is fixed. 🙂
