-
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.
* Do not brackets for function without args * Do not use postfix notation * Do not use procedural syntax * Use case class to get hashCode and equals The `ValueSource` requires overriding `equals` and `hashCode()`. These two are automatically derived for case classes. * Collection is removed from recent scala be more explicit * Refactor request parsing to validate the types of nested structures * Use unchecked for inner types we cannot match on * Use `case e: Throwable => ...` idiom to catch all possible exceptions. The previous implementation `case e => ...` produces following warning on modern scala ``` warning: This catches all Throwables. If this is really intended, use `case e : Throwable` to clear this warning. case e => ^ ``` * Add return type where asked by compiler
- Loading branch information
Showing
14 changed files
with
718 additions
and
190 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// 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 | ||
|
||
object conversions { | ||
/** | ||
* Scala 2.9.1 doesn't have extractors on PartialFunction. These were introduced in | ||
* | ||
* [scala/scala#7111](https://github.com/scala/scala/pull/7111) and released in 2.13.0 | ||
* | ||
* See also [SIP-38](https://docs.scala-lang.org/sips/converters-among-optional-functions-partialfunctions-and-extractor-objects.html) | ||
* | ||
* with extractors you can do something like | ||
* | ||
* ```scala | ||
* val asString: PartialFunction[Any, String] = | ||
* { case string: String => string } | ||
* | ||
* def test(value: Any): Option[String] = { | ||
* value match { | ||
* case asString(s) => Some(s) | ||
* case _ => None | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* This implicit conversion adds a middle ground solution. It can be used as follows: | ||
* | ||
* ```scala | ||
* import conversions._ /// Here we bring implicit convertors in scope | ||
* | ||
* val asString: PartialFunction[Any, String] = | ||
* { case string: String => string } | ||
* | ||
* def test(value: Any): Option[String] = { | ||
* val asStringExtractor = asString.Extractor /// This is the extra line we need to add | ||
* value match { | ||
* case asStringExtractor(s) => Some(s) /// Use Extractor to get the matching content | ||
* case _ => None | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param pf | ||
*/ | ||
class ExtendedPF[A, B](val pf: PartialFunction[A, B]) { | ||
object Extractor { | ||
def unapply(a: A): Option[B] = pf.lift(a) | ||
} | ||
} | ||
implicit def extendPartialFunction[A, B](pf: PartialFunction[A, B]): ExtendedPF[A, B] = new ExtendedPF(pf) | ||
} |
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.