简体   繁体   English

如何在Scala中从中获取实际值:正确(Vector(“ abc”))

[英]How do I get the actual value from this in Scala : Right(Vector(“abc”))

I'm using Gatling to test a WebSocket based service and in order to parse a json response I get I use the following: 我正在使用加特林测试基于WebSocket的服务,为了解析json响应,我得到了以下内容:

val initiator = JsonPath.query("$.header.initiator", json).right.map(_.toVector)

Printing initiator tells me that this is : 打印启动器告诉我这是:

Right(Vector(guest3075085133639688955@example.com))

Now to the beginner Scala question: 现在到初学者Scala问题:

How do I get the actual string value "guest3075085133639688955@example.com" ? 如何获取实际的字符串值“ guest3075085133639688955@example.com”?

I've understood that the Right is just a container, that contains a Vector with one value (the value I want), but how do I get to it ?! 我了解到Right只是一个容器,包含一个带有一个值(我想要的值)的Vector,但是我该怎么去呢? :) :)

I've tried this, but that just prints the same thing (Right(Vect....): 我已经尝试过了,但这只是打印出相同的东西(Right(Vect ....):

initiator.foreach{println}

Cheers, Niklas 干杯,尼克拉斯

  • You can use right to get the RightProjection of the Either and get to get the value inside the Right . 您可以使用right获得EitherRightProjectionget Right内部的值。
  • You can use head to get the first (and in your case only) element from the Vector . 您可以使用headVector获取第一个(并且仅在您的情况下)元素。

Which looks like : 看起来像:

val initiator: Either[String, Vector[String]] = 
  Right(Vector("guest3075085133639688955@example.com"))

initiator.right.get.head
// String = guest3075085133639688955@example.com

You could also do a pattern match : 您还可以进行模式匹配:

val Right(Vector(email)) = initiator
// email: String = guest3075085133639688955@example.com

Note that both ways only work when initiatior is a Right and the Vector inside has at least one element. 请注意,这两种方法仅在initiatior器为“ Right且“ Vector内部具有至少一个元素时才起作用。 Your question is not exactly clear how you would like to handle Left and/or an empty Vector . 您的问题还不太清楚,您想如何处理Left和/或空的Vector


If you want to handle all the failing cases in another way, you could use : 如果您想以其他方式处理所有失败的案例,则可以使用:

initiator match {
  case Right(Vector(email)) => email // ok
  case _ => // default -> fail
}

initiator.right.get from Either is unsafe. initiator.right.get无论从是不安全的。 Because it throws an java.util.NoSuchElementException if it is a left value. 因为如果它是一个左值,它将抛出java.util.NoSuchElementException

By pattern matching: 通过模式匹配:

initiator match {
  // when it is a right value, which is a vector with single element
  case Right(Vector(s)) => println(s)
  // when it is a right value, which is a vector with empty or two or more elements.
  case Right(v) => ???
  // when it is a left value
  case Left(l) => ???
}

In acomment you clarified this is for a test case scenario. 在注释中,您澄清了这是针对测试用例的情况。 If you're using ScalaTest matchers you could do: 如果您使用的是ScalaTest匹配器,则可以执行以下操作:

initiator should matchPattern{case Right(Vector("expected value")) => }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM