0

I am using Gatling (https://gatling.io) and struggling a bit with the scala (just learning).

I have a feeder which pulls in user data from a csv file:

val feeder = csv("seedfile.csv").circular

And I can happily access values in this file, e.g this allows me login using a value from the 'user_email' column:

 exec(http("SubmitLogin")
            .post("/auth/login")
            .formParam("email", "${user_email}")

The issue Im having is that a range of the columns on my csv file are named item1, item2, item3, etc. I would like to iterate over these items in a loop. I was hoping Scala may have a feature like php $$ vars (http://php.net/manual/en/language.variables.variable.php) so i could do something like:

// actual values pulled from csv file
val item1 = "i'm item 1s val"
val item2 = "i'm item 2s val"


// for i in range
var varname = "item"+i

println(s"${$varname}") //so that for i=1 would be equivalent to println($item1)

Note: I have also tried:

s"$${varname}"
s"${${varname}}"

based on my googling and playing with the repl it appears this is not an option in Scala (which I guess makes sense for statically typed language which encourages immutable data) so any advice on how to approach this the Scala way would be greatly appreciated

SwiftD
  • 5,769
  • 6
  • 43
  • 67
  • 1
    Why `$$varname`, and not `s"$${$varname}"`? Run the scala repl and play a bit, until you have the expression right. – 9000 Jan 23 '18 at 19:15
  • above example was sudo code to illustrate what im trying to do (updated to show scala attempt) - but I have already tried various combinations through the repl without success - I hadn't tried with 3 $ symbols (as per your suggestion) but I just tried that and it doesn't work either - for the record neither does - s"${$varname}", s"$${varname}". which were my first attempts, any more concrete suggestions? – SwiftD Jan 23 '18 at 20:06
  • 1
    `item1` etc are not names of Scala variables. They are (I guess, I don't know Gatling) just column names which are known to Gatling at runtime. – Jasper-M Jan 23 '18 at 22:24
  • This looks like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Could you describe how you extract your `item1`, `item2`, etc. from CSV and how would you want that _data_ (not variables!) to be used in your data flow. – SergGr Jan 23 '18 at 22:51
  • When string interpolation errors (e.g. misspelling `varname`) it's a compiler error, not a runtime error. That indicates that string interpolation is not done at runtime and therefore a string variable can't be interpolated, which would indicate that a `String` can't be double-interpolated. – jwvh Jan 24 '18 at 04:04
  • Actually I think @9000 is right on target. But obviously you'll have to present `s"$${$varname}"` to some Gatling method that can interpret the resulting `String` instead of just printing that `String` to stdout. – Jasper-M Jan 24 '18 at 12:23

0 Answers0