简体   繁体   English

模式匹配中的Scala擦除

[英]Scala erasure in pattern matching

I have to extract information of a JSON response and evaluate if some file dis present or not. 我必须提取JSON响应的信息并评估是否存在某些文件。 I'm using the following method definition: 我正在使用以下方法定义:

override def hasField(field: Field): Boolean = {
  val schema = parse(httpClient.doGet(url + Solr5EndpointUris.schemaOverviewEndpoint)).extract[Map[String, Any]]

  val fieldsNames: List[String] = schema.get("schema") match {
    case schema: Some[Map[String, Any]] => schema.get(if (field.dynamic) "dynamicFields" else "fields") match {
      case fields: List[Map[String, Any]] => fields.map {
        case field: Map[String, Any] => field.get("name") match {
          case name: Some[String] => name.getOrElse("")
        }
      }
      case _ => throw new ApiException("Malformed Response! Missing definition for schema > fields/dynamicFields.")
    }
    case _ => throw new ApiException("Malformed Response! Could not extract schema from JSON.")
  }

  fieldsNames.contains(field.name)
}

The method inspects the JSON response via pattern matching and should return true if a field with specific name is present. 该方法通过模式匹配检查JSON响应,如果存在具有特定名称的字段,则该方法应返回true。 An example JSON response could be the following: JSON响应示例如下:

{
  "responseHeader":{
  "status":0,
  "QTime":2},
  "schema":{
    "name":"example-data-driven-schema",
    "version":1.5,
    "uniqueKey":"id",
    "fieldTypes":[],
    "fields":[{
      "name":"id",
      "type":"string",
      "multiValued":false,
      "indexed":true,
      "required":true,
      "stored":true}],
    "dynamicFields":[],
    "copyFields":[]
  }
}

This implementations does work, but i'm pretty sure there is a more straight forward / less complex implementation to achieve this. 此实现确实可行,但是我敢肯定,有一个更简单/更简单的实现可以实现。 Also i get many warnings like the following: 我也收到很多如下警告:

SchemaManager.scala:38: non-variable type argument Map[String,Any] in type pattern Some[Map[String,Any]] is unchecked since it is eliminated by erasure SchemaManager.scala:38:类型模式Some [Map [String,Any]]中的非变量类型参数Map [String,Any]未选中,因为它已通过擦除消除

Can anyone provide a better solution, and / or explain the warnings i get? 谁能提供更好的解决方案,和/或解释我得到的警告?

SchemaManager.scala:38: non-variable type argument Map[String,Any] in type pattern Some[Map[String,Any]] is unchecked since it is eliminated by erasure

Scala compiler will erase the generic type in compile time. Scala编译器将在编译时擦除通用类型 so when you use pattern match , the compiler will erase your match type parameter. 因此,当您使用pattern match ,编译器将删除您的匹配类型参数。 and throw this warning. 并发出此警告。 it calls type erasure . 它称为类型擦除

For your question, you can use json4s for your JSON extract: 对于您的问题,可以将json4s用于JSON提取:

scala> import org.json4s._
scala> import org.json4s.native.JsonMethods._
scala> val jsonStr = "{\n  \"responseHeader\":{\n  \"status\":0,\n  \"QTime\":2},\n  \"schema\":{\n    \"name\":\"example-data-driven-schema\",\n    \"version\":1.5,\n    \"uniqueKey\":\"id\",\n    \"fieldTypes\":[],\n    \"fields\":[{\n      \"name\":\"id\",\n      \"type\":\"string\",\n      \"multiValued\":false,\n      \"indexed\":true,\n      \"required\":true,\n      \"stored\":true}],\n    \"dynamicFields\":[],\n    \"copyFields\":[]\n  }\n}"
scala> implicit val formats = DefaultFormats
scala> val f = parse(jsonStr)
scala> println((f \\ "schema" \\ "fields" \\ "name").extractOrElse("Null"))
id
scala> println((f \\ "schema" \\ "fields" \\ "unknow").extractOrElse("Null"))
Null

Use extractOrElse to set the default value. 使用extractOrElse设置默认值。

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

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