I have to extract information of a JSON response and evaluate if some file dis present or not. 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. An example JSON response could be the following:
{
"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
Can anyone provide a better solution, and / or explain the warnings i get?