0
我必須提取JSON響應的信息並評估是否有某個文件存在或不存在。我用下面的方法定義:模式匹配中的Scala擦除
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)
}
的方法檢查通過模式匹配的JSON響應,如果有特定名稱的字段存在應返回true。一個例子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":[]
}
}
此實現的工作,但我敢肯定有一個更直接的/不那麼複雜的實現來實現這一目標。我還得到很多類似於以下的警告:
SchemaManager.scala:38:類型模式中的非變量類型參數Map [String,Any]某些[Map [String,Any]]未被選中,因爲它被消除通過刪除
任何人都可以提供更好的解決方案,和/或解釋我得到的警告?