2

I want to write a function to format Vaadin messages. These messages have the format

108|0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]]

The first number is the length then there some kind of session id (later called vaadin_security_key) followed by the payload. (In this example I set the value "Agent00232" to the textfield with the connector id "0_11_12_13_login_username") I wrote a function like this:

 def sendMessage(name: String, vaadinCommand: String) = {
    def createMessage(session: Session) = {
      val message = session("vaadin_security_key").as[String] + "\u001d" + vaadinCommand
      val message2 = ELCompiler.compile(message)(classTag[String])(session).toString
      val message3 = message2.substring(8, message2.length - 2)
      val len = message3.length
      val completeMessage = len.toString() + "|" + message3
      completeMessage
    }
    exec(
      ws(name)
        .sendText(createMessage)
        .check(wsAwait
          .within(Vaadin.defaultTimeout)
          .until(1)
          .regex("""(.+)""")
          .saveAs("lastResult")))
      .exec { session =>
        println(session("lastResult").as[String])
        session
      }
  }

I'd like to use this method like this and use EL expressions in the string:

exec(Vaadin.sendMessage("setting Name", "[[\"0_11_12_13_login_username\",\"v\",\"v\",[\"text\",[\"s\",\"${username}\"]]]]"))

Therefore I have to evaluate the String before calculating the length. The method ELCompiler.compile()... returns a Expression[String]. The problem is that I need this string and concat it with the calculated length. When I do message2.toString it returns Success(0ff1255e-e2be-4e7b-ac5c-1ff2709ce886[["0_11_12_13_login_username","v","v",["text",["s","Agent00232"]]]]) and therefor I have to use substring(8, message2.length - 2) to get the evaluated payload (with the security key) to calculate the length of it.

Is there a better (more elegant) way to extract the String out of the expression then the use of substring(...)?

4

1 回答 1

4
  1. 不要将 classTag 显式传递给 ELCompiler.compile。如果您真的要自己使用它,请使用 ELCompiler.compile[String]。
  2. ELCompiler.compile 返回一个 Expression[String],将 Session 的函数别名为 Validation[String],所以如果你应用它,你会得到一个 Validation[String](因为评估可能会失败),因此 toString 打印成功,而不是它包含的价值。您想要的是映射此验证容器。
  3. 您在每次函数执行时一遍又一遍地编译表达式。将其移到函数之外。
  4. 请注意,如果 lastResult 无法保存, session("lastResult").as[String] 会崩溃。请改用验证。
  5. 使用三引号,这样您就不必转义内部双引号。

您的代码的第一部分将如下所示:

import io.gatling.core.session._
import io.gatling.core.session.el._

def sendMessage(name: String, vaadinCommand: String) = {

  val message = ("${vaadin_security_key}\u001d" + vaadinCommand).el[String]

  def createMessage = message.map { resolvedMessage =>
    resolvedMessage.length + "|" + resolvedMessage
  }

  exec(
    ws(name)
      .sendText(createMessage)
      .check(wsAwait
        .within(Vaadin.defaultTimeout)
        .until(1)
        .regex("""(.+)""")
        .saveAs("lastResult")))
    .exec { session =>
      println(session("lastResult").validate[String])
      session
    }
}

然后你会调用它:

exec(Vaadin.sendMessage("setting Name", """[["0_11_12_13_login_username","v","v",["text",["s","${username}"]]]]"""))
于 2014-10-13T22:08:01.553 回答