Skip to content

Commit 12b2b76

Browse files
Simplify Cosmos query string generation. (#4835)
Simplify the selector keys to json transformation
1 parent f061112 commit 12b2b76

File tree

2 files changed

+13
-33
lines changed

2 files changed

+13
-33
lines changed

common/scala/src/main/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtil.scala

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import org.apache.openwhisk.core.database.cosmosdb.CosmosDBConstants._
2222
import org.apache.openwhisk.core.database.StoreUtils.transform
2323
import spray.json.{JsObject, JsString}
2424

25-
import scala.collection.immutable.Iterable
26-
2725
private[cosmosdb] object CosmosDBConstants {
2826

2927
/**
@@ -73,39 +71,21 @@ private[cosmosdb] trait CosmosDBUtil {
7371
* }}}
7472
* Here it uses {{{r['keyName']}}} notation to avoid issues around using reserved words as field name
7573
*/
76-
def prepareFieldClause(fields: Iterable[String]): String = {
77-
val m = fields.foldLeft(Map.empty[String, Any]) { (map, name) =>
78-
addToMap(name, map)
79-
}
80-
val withId = addToMap(cid, m)
81-
val json = asJsonLikeString(withId)
74+
def prepareFieldClause(fields: Set[String]): String = {
75+
val json = (fields + cid)
76+
.map { field =>
77+
val split = field.split('.')
78+
79+
val selector = "r" + split.mkString("['", "']['", "']")
80+
val prefix = split.map(k => s""""$k":""").mkString("{")
81+
val suffix = split.drop(1).map(_ => "}").mkString
82+
83+
prefix + selector + suffix
84+
}
85+
.mkString("{", ",", "}")
8286
s"$json AS $alias"
8387
}
8488

85-
private def addToMap(name: String, map: Map[String, _]): Map[String, Any] = name.split('.').toList match {
86-
case Nil => throw new IllegalStateException(s"'$name' split on '.' should not result in empty list")
87-
case x :: xs => addToMap(x, xs, Nil, map)
88-
}
89-
90-
private def addToMap(key: String,
91-
children: List[String],
92-
keyPath: List[String],
93-
map: Map[String, Any]): Map[String, Any] = children match {
94-
case Nil => map + (key -> s"r${makeKeyPath(key :: keyPath)}")
95-
case x :: xs =>
96-
map + (key -> addToMap(x, xs, key :: keyPath, map.getOrElse(key, Map.empty).asInstanceOf[Map[String, Any]]))
97-
}
98-
99-
private def makeKeyPath(keyPath: List[String]) = keyPath.reverse.map(f => s"['$f']").mkString
100-
101-
private def asJsonLikeString(m: Map[_, _]) =
102-
m.map { case (k, v) => s""" "$k" : ${asString(v)}""" }.mkString("{", ",", "}")
103-
104-
private def asString(v: Any): String = v match {
105-
case m: Map[_, _] => asJsonLikeString(m)
106-
case x => x.toString
107-
}
108-
10989
/**
11090
* CosmosDB id considers '/', '\' , '?' and '#' as invalid. EntityNames can include '/' so
11191
* that need to be escaped. For that we use '|' as the replacement char

tests/src/test/scala/org/apache/openwhisk/core/database/cosmosdb/CosmosDBUtilTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CosmosDBUtilTest extends FlatSpec with Matchers with OptionValues {
5555
JsHelpers.getFieldPath(result, "b", "c").value shouldBe JsString("r['b']['c']")
5656
}
5757

58-
private def fieldsAsJson(fields: String*) = toJson(CosmosDBUtil.prepareFieldClause(fields.toList))
58+
private def fieldsAsJson(fields: String*) = toJson(CosmosDBUtil.prepareFieldClause(fields.toSet))
5959

6060
private def toJson(s: String): JsObject = {
6161
//Strip of last `As VIEW`

0 commit comments

Comments
 (0)