You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ma...@apache.org on 2020/03/29 09:42:55 UTC

[openwhisk] branch master updated: Remove explicit right biasing of Eithers. (#4848)

This is an automated email from the ASF dual-hosted git repository.

markusthoemmes pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk.git


The following commit(s) were added to refs/heads/master by this push:
     new 1dc31b2  Remove explicit right biasing of Eithers. (#4848)
1dc31b2 is described below

commit 1dc31b271983d3bc703b98052d25967bd9bfb8db
Author: Markus Thömmes <ma...@me.com>
AuthorDate: Sun Mar 29 11:42:45 2020 +0200

    Remove explicit right biasing of Eithers. (#4848)
    
    * Remove usages of .right.get in production code.
    
    - There's no reason parse the ContentType we fully control anyway, so let's just compose it and not think about non-existent errors at all.
    - More explicit error handling where we actually need to parse.
    
    * Remove explicit right biasing.
    
    * Fix compilation error.
    
    * Small improvement.
    
    * Fix compile errors.
---
 .../src/main/scala/org/apache/openwhisk/common/Prometheus.scala  | 5 +++--
 .../org/apache/openwhisk/core/containerpool/Container.scala      | 2 +-
 .../org/apache/openwhisk/core/entity/ActivationResult.scala      | 4 +---
 .../scala/org/apache/openwhisk/core/entity/Attachments.scala     | 9 ++++-----
 .../openwhisk/core/monitoring/metrics/PrometheusEventsApi.scala  | 5 +++--
 .../openwhisk/core/database/test/ExtendedCouchDbRestClient.scala | 5 ++---
 6 files changed, 14 insertions(+), 16 deletions(-)

diff --git a/common/scala/src/main/scala/org/apache/openwhisk/common/Prometheus.scala b/common/scala/src/main/scala/org/apache/openwhisk/common/Prometheus.scala
index 614f836..87dc46f 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/common/Prometheus.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/common/Prometheus.scala
@@ -18,7 +18,7 @@
 package org.apache.openwhisk.common
 import java.nio.charset.StandardCharsets.UTF_8
 
-import akka.http.scaladsl.model.{ContentType, HttpEntity}
+import akka.http.scaladsl.model.{ContentType, HttpCharsets, HttpEntity, MediaType}
 import akka.http.scaladsl.server.Directives._
 import akka.http.scaladsl.server.Route
 import kamon.Kamon
@@ -26,7 +26,8 @@ import kamon.prometheus.PrometheusReporter
 
 class KamonPrometheus extends AutoCloseable {
   private val reporter = new PrometheusReporter
-  private val v4 = ContentType.parse("text/plain; version=0.0.4; charset=utf-8").right.get
+  private val v4: ContentType = ContentType.apply(
+    MediaType.textWithFixedCharset("plain", HttpCharsets.`UTF-8`).withParams(Map("version" -> "0.0.4")))
   Kamon.registerModule("prometheus", reporter)
 
   def route: Route = path("metrics") {
diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/containerpool/Container.scala b/common/scala/src/main/scala/org/apache/openwhisk/core/containerpool/Container.scala
index 20627fb..5080dae 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/core/containerpool/Container.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/core/containerpool/Container.scala
@@ -261,7 +261,7 @@ case class Interval(start: Instant, end: Instant) {
 }
 
 case class RunResult(interval: Interval, response: Either[ContainerConnectionError, ContainerResponse]) {
-  def ok = response.right.exists(_.ok)
+  def ok = response.exists(_.ok)
   def toBriefString = response.fold(_.toString, _.toString)
 }
 
diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/entity/ActivationResult.scala b/common/scala/src/main/scala/org/apache/openwhisk/core/entity/ActivationResult.scala
index d3727c5..6a737d6 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/core/entity/ActivationResult.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/core/entity/ActivationResult.scala
@@ -160,9 +160,7 @@ protected[core] object ActivationResponse extends DefaultJsonProtocol {
    */
   protected[core] def processInitResponseContent(response: Either[ContainerConnectionError, ContainerResponse],
                                                  logger: Logging): ActivationResponse = {
-    require(
-      response.isLeft || !response.right.exists(_.ok),
-      s"should not interpret init response when status code is OK")
+    require(response.isLeft || !response.exists(_.ok), s"should not interpret init response when status code is OK")
     response match {
       case Right(ContainerResponse(code, str, truncated)) =>
         val sizeOpt = Option(str).map(_.length)
diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Attachments.scala b/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Attachments.scala
index e8a18b5..c6b30c8 100644
--- a/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Attachments.scala
+++ b/common/scala/src/main/scala/org/apache/openwhisk/core/entity/Attachments.scala
@@ -66,13 +66,12 @@ object Attachments {
     implicit val serdes = {
       implicit val contentTypeSerdes = new RootJsonFormat[ContentType] {
         override def write(c: ContentType) = JsString(c.value)
-        override def read(js: JsValue) =
-          Try {
-            val JsString(c) = js
-            ContentType.parse(c).right.get
-          } getOrElse {
+
+        override def read(js: JsValue): ContentType = {
+          Try(js.convertTo[String]).toOption.flatMap(ContentType.parse(_).toOption).getOrElse {
             throw new DeserializationException("Could not deserialize content-type")
           }
+        }
       }
 
       jsonFormat4(Attached.apply)
diff --git a/core/monitoring/user-events/src/main/scala/org/apache/openwhisk/core/monitoring/metrics/PrometheusEventsApi.scala b/core/monitoring/user-events/src/main/scala/org/apache/openwhisk/core/monitoring/metrics/PrometheusEventsApi.scala
index c485dcc..2c2a5f4 100644
--- a/core/monitoring/user-events/src/main/scala/org/apache/openwhisk/core/monitoring/metrics/PrometheusEventsApi.scala
+++ b/core/monitoring/user-events/src/main/scala/org/apache/openwhisk/core/monitoring/metrics/PrometheusEventsApi.scala
@@ -18,7 +18,7 @@
 package org.apache.openwhisk.core.monitoring.metrics
 
 import akka.http.scaladsl.model.StatusCodes.ServiceUnavailable
-import akka.http.scaladsl.model.{ContentType, MessageEntity}
+import akka.http.scaladsl.model.{ContentType, HttpCharsets, MediaType, MessageEntity}
 import akka.http.scaladsl.server.Directives._
 import akka.http.scaladsl.server.Route
 import org.apache.openwhisk.connector.kafka.KafkaMetricRoute
@@ -30,7 +30,8 @@ trait PrometheusExporter {
 }
 
 object PrometheusExporter {
-  val textV4: ContentType = ContentType.parse("text/plain; version=0.0.4; charset=utf-8").right.get
+  val textV4: ContentType = ContentType.apply(
+    MediaType.textWithFixedCharset("plain", HttpCharsets.`UTF-8`).withParams(Map("version" -> "0.0.4")))
 }
 
 class PrometheusEventsApi(consumer: EventConsumer, prometheus: PrometheusExporter)(implicit ec: ExecutionContext) {
diff --git a/tests/src/test/scala/org/apache/openwhisk/core/database/test/ExtendedCouchDbRestClient.scala b/tests/src/test/scala/org/apache/openwhisk/core/database/test/ExtendedCouchDbRestClient.scala
index bdbd78f..df843b9 100644
--- a/tests/src/test/scala/org/apache/openwhisk/core/database/test/ExtendedCouchDbRestClient.scala
+++ b/tests/src/test/scala/org/apache/openwhisk/core/database/test/ExtendedCouchDbRestClient.scala
@@ -46,9 +46,8 @@ class ExtendedCouchDbRestClient(protocol: String,
 
   // http://docs.couchdb.org/en/1.6.1/api/server/common.html#all-dbs
   def dbs(): Future[Either[StatusCode, List[String]]] = {
-    requestJson[JsArray](mkRequest(HttpMethods.GET, uri("_all_dbs"), headers = baseHeaders)).map { either =>
-      either.right.map(_.convertTo[List[String]])
-    }
+    requestJson[JsArray](mkRequest(HttpMethods.GET, uri("_all_dbs"), headers = baseHeaders))
+      .map(_.map(_.convertTo[List[String]]))
   }
 
   // http://docs.couchdb.org/en/1.6.1/api/database/common.html#put--db