-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce types to supported analyzers take2 (#84)
* Refactor createAnalyzer to remove polymorphic arguments The polymorphism makes it harder to introduce types. Because it requires union types. Which are not natively supported by the version of Scala we use. Elimination of the polymorphism would allow us to introduce types. The introduction of types is the vehicle to solve type erasure problem we would have to deal with when we upgrade Scala to the next version. The refactoring is done using following steps 1. Introduce `AnalyzerOptions` class with type specific constructors - `def fromMap(map: Map[_, _])` - `def fromAnalyzerName(name: String)` - `def fromKVsList(options: List[_])` 2. Make sure we correctly go from `Any` to the concrete type. This PR uses the `.collect` combinator instead of relying on `ClassCastException`. Assumptions 1. The keys of `options` passed to `OpenIndexMsg` are strings. 2. It is ok to just ignore all non-string keys in `options`. 3. The analyzer name is either a `String` or a single `String` element wrapped in the `List`. 4. The keys of options passed to OpenIndexMsg are strings. 5. It is ok to just ignore all non-string keys in options. 6. The fields value is a list. 7. The elements of a fields list are tuples. `(String, String)` or (String, [String]). 8. The config is a String in ('analyze, config, text) message in `AnalyzerService.handleCall` and it should really be named `('analyze, analyzerName, text)`. * Avoid matching on `Some(stopwords: List[String])` The matching on inner types of the container will not work on next version of Scala. Newer Scala causes type erasure when matching on inner type of the container (such as List in this case). Assumptions 1. The `stopwords` is a list 2. The elements of a `stopwords` are strings. 3. It is ok to skip non-strings elements of `stopwords`.
- Loading branch information
Showing
9 changed files
with
352 additions
and
203 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/scala/com/cloudant/clouseau/AnalyzerOptions.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
// use this file except in compliance with the License. You may obtain a copy of | ||
// the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
// License for the specific language governing permissions and limitations under | ||
// the License. | ||
|
||
package com.cloudant.clouseau | ||
|
||
class AnalyzerOptions private (inner: Map[String, Any]) { | ||
def toMap = inner | ||
} | ||
|
||
object AnalyzerOptions { | ||
def fromMap(map: Map[_, _]) = | ||
new AnalyzerOptions(map.asInstanceOf[Map[String, Any]]) | ||
def fromAnalyzerName(name: String) = | ||
new AnalyzerOptions(Map("name" -> name).asInstanceOf[Map[String, Any]]) | ||
def fromKVsList(options: List[_]) = { | ||
// options can be a List of key-value pairs or a single String element wrapped in a List | ||
// the latter is a corner case which we should deprecate | ||
options match { | ||
case List(name: String) => new AnalyzerOptions(Map("name" -> name).asInstanceOf[Map[String, Any]]) | ||
case list: List[_] => new AnalyzerOptions(collectKVs(list).toMap[String, Any].asInstanceOf[Map[String, Any]]) | ||
} | ||
} | ||
def collectKVs(list: List[_]): List[(String, Any)] = | ||
list.collect { case t @ (_: String, _: Any) => t }.asInstanceOf[List[(String, Any)]] | ||
|
||
def from(options: Any): Option[AnalyzerOptions] = | ||
options match { | ||
case map: Map[_, _] => Some(fromMap(map)) | ||
case list: List[_] => Some(fromKVsList(list)) | ||
case string: String => Some(fromAnalyzerName(string)) | ||
case _ => None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.