Created
July 4, 2016 11:03
-
-
Save justgook/cb706ebe21b002171d8a671445eaf09b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package protocols | |
import play.api.libs.json._ | |
/** | |
* Created by Roman Potashow on 30.06.2016. | |
*/ | |
//TODO rename to Schema...Builder | |
case class Settings(name: String, label: String, properties: List[Settings.Property]) | |
object Settings { | |
// implicit val boolPropertyReads = Json.reads[BoolProperty] | |
implicit val stringPropertyReads = Json.reads[StringProperty] | |
implicit val inReads = new Reads[Property]() { | |
override def reads(json: JsValue): JsResult[Property] = { | |
def read[T: Reads] = implicitly[Reads[T]].reads((json \ "args").get) | |
(json \ "type").as[String] match { | |
// case "bool" => read[BoolProperty] | |
case "string" => read[StringProperty] | |
} | |
} | |
} | |
implicit val stringPropertyWrites = Json.writes[StringProperty] | |
implicit val propertyWrites = new Writes[Property] { | |
override def writes(o: Property): JsValue = { | |
def write[T: Writes](x: T) = implicitly[Writes[T]].writes(x) | |
val (t, data) = o match { | |
case _: BoolProperty => ("bool", None) | |
case property: StringProperty => ("string", Some(write(property))) | |
} | |
//TODO find solution how output without subobject args | |
Json.obj("type" -> t) ++ { | |
data.map(args => Json.obj("args" -> args)) getOrElse Json.obj() | |
} | |
} | |
} | |
implicit val settingsFormat = Json.format[Settings] | |
sealed trait Property | |
case class BoolProperty() extends Property | |
case class StringProperty(name: String, label: String, defaultValue: String) extends Property | |
case class MultiStringProperty(name: String, label: String, defaultValue: String, enum: Option[String] = None) extends Property | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment