@@ -22,8 +22,6 @@ import org.apache.openwhisk.core.database.cosmosdb.CosmosDBConstants._
2222import org .apache .openwhisk .core .database .StoreUtils .transform
2323import spray .json .{JsObject , JsString }
2424
25- import scala .collection .immutable .Iterable
26-
2725private [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
0 commit comments