@@ -3,7 +3,6 @@ package controllers
33import scala .collection .mutable .Map
44
55import javax .inject ._
6- import play .api ._
76import play .api .libs .json ._
87import play .api .mvc ._
98
@@ -14,35 +13,41 @@ import io.github.dataunitylab.jsonoid.discovery.schemas.{
1413 ZeroSchema
1514}
1615
17- import play .api .libs .{ json => pjson }
18- import org .{ json4s => j4s }
16+ import play .api .libs .{json => pjson }
17+ import org .{json4s => j4s }
1918
2019object Conversions {
21- def toJson4s (json : play.api.libs.json.JsValue ): org.json4s.JValue = json match {
22- case pjson.JsString (str) => j4s.JString (str)
23- case pjson.JsNull => j4s.JNull
24- case pjson.JsBoolean (value) => j4s.JBool (value)
25- case pjson.JsNumber (value) => j4s.JDecimal (value)
26- case pjson.JsArray (items) => j4s.JArray (items.map(toJson4s(_)).toList)
27- case pjson.JsObject (items) => j4s.JObject (items.map { case (k, v) => k -> toJson4s(v)}.toList)
28- }
20+ def toJson4s (json : play.api.libs.json.JsValue ): org.json4s.JValue =
21+ json match {
22+ case pjson.JsString (str) => j4s.JString (str)
23+ case pjson.JsNull => j4s.JNull
24+ case pjson.JsBoolean (value) => j4s.JBool (value)
25+ case pjson.JsTrue => j4s.JBool (true )
26+ case pjson.JsFalse => j4s.JBool (false )
27+ case pjson.JsNumber (value) => j4s.JDecimal (value)
28+ case pjson.JsArray (items) => j4s.JArray (items.map(toJson4s(_)).toList)
29+ case pjson.JsObject (items) =>
30+ j4s.JObject (items.map { case (k, v) => k -> toJson4s(v) }.toList)
31+ }
2932
30- def toPlayJson (json : org.json4s.JValue ): play.api.libs.json.JsValue = json match {
31- case j4s.JString (str) => pjson.JsString (str)
32- case j4s.JNothing => pjson.JsNull
33- case j4s.JNull => pjson.JsNull
34- case j4s.JDecimal (value) => pjson.JsNumber (value)
35- case j4s.JDouble (value) => pjson.JsNumber (value)
36- case j4s.JInt (value) => pjson.JsNumber (BigDecimal (value))
37- case j4s.JLong (value) => pjson.JsNumber (BigDecimal (value))
38- case j4s.JBool (value) => pjson.JsBoolean (value)
39- case j4s.JSet (fields) => pjson.JsArray (fields.toList.map(toPlayJson(_)))
40- case j4s.JArray (fields) => pjson.JsArray (fields.map(toPlayJson(_)))
41- case j4s.JObject (fields) => pjson.JsObject (fields.map { case (k, v) => k -> toPlayJson(v)}.toMap)
42- }
33+ def toPlayJson (json : org.json4s.JValue ): play.api.libs.json.JsValue =
34+ json match {
35+ case j4s.JString (str) => pjson.JsString (str)
36+ case j4s.JNothing => pjson.JsNull
37+ case j4s.JNull => pjson.JsNull
38+ case j4s.JDecimal (value) => pjson.JsNumber (value)
39+ case j4s.JDouble (value) => pjson.JsNumber (value)
40+ case j4s.JInt (value) => pjson.JsNumber (BigDecimal (value))
41+ case j4s.JLong (value) => pjson.JsNumber (BigDecimal (value))
42+ case j4s.JBool (value) => pjson.JsBoolean (value)
43+ case j4s.JSet (fields) => pjson.JsArray (fields.toList.map(toPlayJson(_)))
44+ case j4s.JArray (fields) => pjson.JsArray (fields.map(toPlayJson(_)))
45+ case j4s.JObject (fields) =>
46+ pjson.JsObject (fields.map { case (k, v) => k -> toPlayJson(v) }.toMap)
47+ }
4348
44- def toPlayJson (json : org.json4s.JObject ): play.api.libs.json.JsObject =
45- pjson.JsObject (json.obj.map { case (k, v) => k -> toPlayJson(v)}.toMap)
49+ def toPlayJson (json : org.json4s.JObject ): play.api.libs.json.JsObject =
50+ pjson.JsObject (json.obj.map { case (k, v) => k -> toPlayJson(v) }.toMap)
4651}
4752
4853final case class SchemaParams (name : String , propSet : Option [String ] = None )
@@ -51,12 +56,12 @@ object SchemaParams {
5156}
5257import SchemaParams ._
5358
54- /**
55- * This controller creates an `Action` to handle HTTP requests to the
56- * application's home page.
57- */
59+ /** This controller creates an `Action` to handle HTTP requests to the
60+ * application's home page.
61+ */
5862@ Singleton
59- class JsonoidController @ Inject ()(val cc : ControllerComponents ) extends AbstractController (cc) {
63+ class JsonoidController @ Inject () (val cc : ControllerComponents )
64+ extends AbstractController (cc) {
6065 private val schemas = Map .empty[String , JsonSchema [_]]
6166 private val jsonoidParams = Map .empty[String , JsonoidParams ]
6267
@@ -68,35 +73,37 @@ class JsonoidController @Inject()(val cc: ControllerComponents) extends Abstract
6873 val schemaParamsResult = request.body.validate[SchemaParams ]
6974 schemaParamsResult.fold(
7075 errors => {
71- BadRequest (Json .obj(" status" -> " error" , " error" -> JsError .toJson(errors)))
76+ BadRequest (
77+ Json .obj(" status" -> " error" , " error" -> JsError .toJson(errors))
78+ )
7279 },
7380 schemaParams => {
7481 if (schemas.contains(schemaParams.name)) {
7582 Conflict (Json .obj(" error" -> " Schema already exists" ))
7683 } else {
77- val maybePropSet = schemaParams.propSet match {
78- case Some (" All" ) =>
79- Some (PropertySets .AllProperties )
80- case Some (" Simple" ) =>
81- Some (PropertySets .SimpleProperties )
82- case Some (" Min" ) =>
83- Some (PropertySets .MinProperties )
84- case Some (_) =>
85- None
86- case None =>
87- Some (PropertySets .AllProperties )
88- }
89- maybePropSet match {
90- case Some (propSet) =>
91- schemas.put(schemaParams.name, ZeroSchema ())
92- jsonoidParams.put(
93- schemaParams.name,
94- JsonoidParams ().withPropertySet(propSet)
95- )
96- Created (Json .obj(" status" -> " ok" ))
97- case None =>
98- BadRequest (Json .obj(" error" -> " Invalid property set" ))
99- }
84+ val maybePropSet = schemaParams.propSet match {
85+ case Some (" All" ) =>
86+ Some (PropertySets .AllProperties )
87+ case Some (" Simple" ) =>
88+ Some (PropertySets .SimpleProperties )
89+ case Some (" Min" ) =>
90+ Some (PropertySets .MinProperties )
91+ case Some (_) =>
92+ None
93+ case None =>
94+ Some (PropertySets .AllProperties )
95+ }
96+ maybePropSet match {
97+ case Some (propSet) =>
98+ schemas.put(schemaParams.name, ZeroSchema ())
99+ jsonoidParams.put(
100+ schemaParams.name,
101+ JsonoidParams ().withPropertySet(propSet)
102+ )
103+ Created (Json .obj(" status" -> " ok" ))
104+ case None =>
105+ BadRequest (Json .obj(" error" -> " Invalid property set" ))
106+ }
100107 }
101108 }
102109 )
@@ -113,8 +120,9 @@ class JsonoidController @Inject()(val cc: ControllerComponents) extends Abstract
113120
114121 def getSchema (name : String ) = Action { implicit request =>
115122 schemas.get(name) match {
116- case Some (schema) => Ok (Json .obj(" schema" -> Conversions .toPlayJson(schema.toJson())))
117- case None => NotFound (Json .obj(" error" -> " Schema not found" ))
123+ case Some (schema) =>
124+ Ok (Json .obj(" schema" -> Conversions .toPlayJson(schema.toJson())))
125+ case None => NotFound (Json .obj(" error" -> " Schema not found" ))
118126 }
119127 }
120128
@@ -123,15 +131,17 @@ class JsonoidController @Inject()(val cc: ControllerComponents) extends Abstract
123131 case Some (schema) =>
124132 assert(jsonoidParams.contains(name))
125133 val p = jsonoidParams.get(name).get
126- val newSchema = DiscoverSchema .discoverFromValue(Conversions .toJson4s(request.body))(p)
134+ val newSchema = DiscoverSchema .discoverFromValue(
135+ Conversions .toJson4s(request.body)
136+ )(p)
127137 newSchema match {
128138 case Some (newSchema) =>
129139 schemas.put(name, schema.merge(newSchema)(p))
130140 Ok (Json .obj(" status" -> " ok" ))
131141 case None =>
132142 BadRequest (Json .obj(" error" -> " Invalid schema" ))
133143 }
134- case None => NotFound (Json .obj(" error" -> " Schema not found" ))
144+ case None => NotFound (Json .obj(" error" -> " Schema not found" ))
135145 }
136146 }
137147}
0 commit comments