You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/01/09 13:14:16 UTC

[GitHub] chetanmeh closed pull request #3162: Add type property to entity documents

chetanmeh closed pull request #3162: Add type property to entity documents 
URL: https://github.com/apache/incubator-openwhisk/pull/3162
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/common/scala/src/main/scala/whisk/core/entity/WhiskStore.scala b/common/scala/src/main/scala/whisk/core/entity/WhiskStore.scala
index 94be9583a9..add7331e5a 100644
--- a/common/scala/src/main/scala/whisk/core/entity/WhiskStore.scala
+++ b/common/scala/src/main/scala/whisk/core/entity/WhiskStore.scala
@@ -69,6 +69,14 @@ protected[core] trait WhiskDocument extends DocumentSerializer with DocumentRevi
    */
   protected[core] final def docinfo: DocInfo = DocInfo(docid, rev)
 
+  /**
+   * Determines entity type based on entity class name
+   */
+  protected[core] def documentType: String = {
+    val typeName = this.getClass.getSimpleName.toLowerCase
+    if (typeName.startsWith("whisk")) typeName.substring(5) else typeName
+  }
+
   /**
    * The representation as JSON, e.g. for REST calls. Does not include id/rev.
    */
@@ -85,7 +93,8 @@ protected[core] trait WhiskDocument extends DocumentSerializer with DocumentRevi
     // Building up the fields.
     val base = this.toJson.fields
     val withId = base + ("_id" -> JsString(id))
-    val withRev = if (revOrNull == null) withId else { withId + ("_rev" -> JsString(revOrNull)) }
+    val withType = withId + ("type" -> JsString(documentType))
+    val withRev = if (revOrNull == null) withType else { withType + ("_rev" -> JsString(revOrNull))}
     JsObject(withRev)
   }
 }
diff --git a/tests/src/test/scala/whisk/core/entity/test/WhiskEntityTests.scala b/tests/src/test/scala/whisk/core/entity/test/WhiskEntityTests.scala
index 55d8ca941a..a52ac6a570 100644
--- a/tests/src/test/scala/whisk/core/entity/test/WhiskEntityTests.scala
+++ b/tests/src/test/scala/whisk/core/entity/test/WhiskEntityTests.scala
@@ -21,6 +21,7 @@ import org.junit.runner.RunWith
 import org.scalatest.FlatSpec
 import org.scalatest.Matchers
 import org.scalatest.junit.JUnitRunner
+import spray.json.JsString
 import whisk.core.entity.EntityPath
 import whisk.core.entity.EntityName
 import whisk.core.entity.WhiskAction
@@ -70,6 +71,13 @@ class WhiskEntityTests extends FlatSpec with ExecHelpers with Matchers {
     action.rev shouldBe resolved.rev
   }
 
+  it should "define type property" in {
+    val exec = jsDefault("code")
+    val action = WhiskAction(namespace, name, exec, Parameters())
+    val js = action.toDocumentRecord
+    js.fields("type") shouldBe JsString("action")
+  }
+
   behavior of "WhiskPackage"
 
   it should "correctly inherit parameters and preserve revision through the process" in {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services