This is an automated email from the ASF dual-hosted git repository. chetanm 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 12b2b76 Simplify Cosmos query string generation. (#4835) 12b2b76 is described below commit 12b2b76d4bef3e7c2bd990cc441e31cb3d3046be Author: Markus Thömmes <markusthoem...@me.com> AuthorDate: Fri Mar 6 11:55:05 2020 +0100 Simplify Cosmos query string generation. (#4835) Simplify the selector keys to json transformation --- .../core/database/cosmosdb/CosmosDBUtil.scala | 44 ++++++---------------- .../core/database/cosmosdb/CosmosDBUtilTest.scala | 2 +- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtil.scala b/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtil.scala index fbe1b49..60bd611 100644 --- a/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtil.scala +++ b/common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtil.scala @@ -22,8 +22,6 @@ import org.apache.openwhisk.core.database.cosmosdb.CosmosDBConstants._ import org.apache.openwhisk.core.database.StoreUtils.transform import spray.json.{JsObject, JsString} -import scala.collection.immutable.Iterable - private[cosmosdb] object CosmosDBConstants { /** @@ -73,39 +71,21 @@ private[cosmosdb] trait CosmosDBUtil { * }}} * Here it uses {{{r['keyName']}}} notation to avoid issues around using reserved words as field name */ - def prepareFieldClause(fields: Iterable[String]): String = { - val m = fields.foldLeft(Map.empty[String, Any]) { (map, name) => - addToMap(name, map) - } - val withId = addToMap(cid, m) - val json = asJsonLikeString(withId) + def prepareFieldClause(fields: Set[String]): String = { + val json = (fields + cid) + .map { field => + val split = field.split('.') + + val selector = "r" + split.mkString("['", "']['", "']") + val prefix = split.map(k => s""""$k":""").mkString("{") + val suffix = split.drop(1).map(_ => "}").mkString + + prefix + selector + suffix + } + .mkString("{", ",", "}") s"$json AS $alias" } - private def addToMap(name: String, map: Map[String, _]): Map[String, Any] = name.split('.').toList match { - case Nil => throw new IllegalStateException(s"'$name' split on '.' should not result in empty list") - case x :: xs => addToMap(x, xs, Nil, map) - } - - private def addToMap(key: String, - children: List[String], - keyPath: List[String], - map: Map[String, Any]): Map[String, Any] = children match { - case Nil => map + (key -> s"r${makeKeyPath(key :: keyPath)}") - case x :: xs => - map + (key -> addToMap(x, xs, key :: keyPath, map.getOrElse(key, Map.empty).asInstanceOf[Map[String, Any]])) - } - - private def makeKeyPath(keyPath: List[String]) = keyPath.reverse.map(f => s"['$f']").mkString - - private def asJsonLikeString(m: Map[_, _]) = - m.map { case (k, v) => s""" "$k" : ${asString(v)}""" }.mkString("{", ",", "}") - - private def asString(v: Any): String = v match { - case m: Map[_, _] => asJsonLikeString(m) - case x => x.toString - } - /** * CosmosDB id considers '/', '\' , '?' and '#' as invalid. EntityNames can include '/' so * that need to be escaped. For that we use '|' as the replacement char diff --git a/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtilTest.scala b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtilTest.scala index a4e9e8b..68f37b1 100644 --- a/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtilTest.scala +++ b/tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtilTest.scala @@ -55,7 +55,7 @@ class CosmosDBUtilTest extends FlatSpec with Matchers with OptionValues { JsHelpers.getFieldPath(result, "b", "c").value shouldBe JsString("r['b']['c']") } - private def fieldsAsJson(fields: String*) = toJson(CosmosDBUtil.prepareFieldClause(fields.toList)) + private def fieldsAsJson(fields: String*) = toJson(CosmosDBUtil.prepareFieldClause(fields.toSet)) private def toJson(s: String): JsObject = { //Strip of last `As VIEW`