-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from flowcommerce/form_data_helper
Form data helper
- Loading branch information
Showing
4 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea/ | ||
target/ | ||
logs/ | ||
.ivy2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.flow.play.util | ||
|
||
import play.api.libs.json.{JsObject, Json, JsValue} | ||
|
||
|
||
object FormData { | ||
def formDataToJson(data: Map[String, Seq[String]]): JsValue = { | ||
val nested = data.map{ case (key, value) => | ||
key.split("\\[").foldRight( | ||
if(key.contains("[]")) | ||
Json.toJson(value) //take seq for arrays | ||
else | ||
Json.toJson(value.headOption.getOrElse("")) | ||
){ case (newKey, v) => | ||
val newVal = { | ||
val js = (v \ "").getOrElse(v) | ||
|
||
//convert '{key: val}' to '[{key: val}]' if previous key specifies array type, otherwise nothing | ||
if(newKey == "]"){ | ||
if(!js.toString.startsWith("[")) { | ||
val s = (v \ "").getOrElse(v).toString. | ||
replaceFirst("\\{", "[{"). | ||
reverse. | ||
replaceFirst("\\}", "]}"). | ||
reverse | ||
|
||
Json.toJson(Json.parse(s)) | ||
} | ||
else | ||
js | ||
} | ||
else | ||
js | ||
} | ||
|
||
Json.obj(newKey.replace("]", "") -> newVal) | ||
} | ||
} | ||
|
||
Json.toJson(nested.foldLeft(Json.obj()){ case (a, b) => a.deepMerge(b.as[JsObject]) }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.flow.play.util | ||
|
||
import org.scalatest.{Matchers, FunSpec} | ||
import play.api.libs.json._ | ||
|
||
|
||
class FormDataSpec extends FunSpec with Matchers { | ||
val fdHelper = FormData | ||
|
||
describe("formDataToJson") { | ||
|
||
val data: Map[String, Seq[String]] = Map( | ||
"email" -> Seq("[email protected]"), | ||
"name[first]" -> Seq("mike"), | ||
"name[last]" -> Seq("roth"), | ||
"one[two][three][four]" -> Seq("haha"), | ||
"one[two][three][five]" -> Seq("wow"), | ||
"arr[][arr2][]" -> Seq("fruit", "vegitables"), | ||
"tags[]" -> Seq("foo", "bar"), | ||
"yikes" -> Seq("yes", "no")) | ||
|
||
it("returns JsValue") { | ||
fdHelper.formDataToJson(data) match { | ||
case res: JsValue => assert(true) | ||
case _ => assert(false) | ||
} | ||
} | ||
|
||
it("creates simple json object") { | ||
(fdHelper.formDataToJson(data) \ "email").validate[String] match { | ||
case JsSuccess(succ,_) => succ should be("[email protected]") | ||
case JsError(_) => assert(false) | ||
} | ||
} | ||
|
||
it("creates complex json object") { | ||
(fdHelper.formDataToJson(data) \ "name" \ "first").validate[String] match { | ||
case JsSuccess(succ,_) => succ should be("mike") | ||
case JsError(_) => assert(false) | ||
} | ||
|
||
(fdHelper.formDataToJson(data) \ "name" \ "last").validate[String] match { | ||
case JsSuccess(succ,_) => succ should be("roth") | ||
case JsError(_) => assert(false) | ||
} | ||
} | ||
|
||
it("creates simple array json object") { | ||
(fdHelper.formDataToJson(data) \ "tags").validate[Seq[String]] match { | ||
case JsSuccess(succ,_) => succ should be(Seq("foo", "bar")) | ||
case JsError(_) => assert(false) | ||
} | ||
} | ||
|
||
it("creates complex array json object") { | ||
(fdHelper.formDataToJson(data) \ "arr").validate[Seq[JsValue]] match { | ||
case JsSuccess(succ,_) => assert((succ.head \ "arr2").toOption.isDefined) | ||
case JsError(_) => assert(false) | ||
} | ||
} | ||
|
||
it("takes first instance of non-array key") { | ||
(fdHelper.formDataToJson(data) \ "yikes").validate[String] match { | ||
case JsSuccess(succ,_) => succ should be("yes") | ||
case JsError(_) => assert(false) | ||
} | ||
} | ||
} | ||
} |