All Downloads are FREE. Search and download functionalities are using the official Maven repository.

scalaz.model.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
package {{package}}

import argonaut._
import argonaut.EncodeJson._
import argonaut.DecodeJson._

import org.http4s.{EntityDecoder, EntityEncoder}
import org.http4s.argonaut._
import org.joda.time.DateTime
{{#models}}
{{#model}}
import {{classname}}._

case class {{classname}} (
  {{#vars}}{{#description}}/* {{{description}}} */
  {{/description}}{{name}}: {{^required}}Option[{{/required}}{{^isEnum}}{{datatype}}{{/isEnum}}{{#isEnum}}{{datatypeWithEnum}}{{/isEnum}}{{^required}}]{{/required}}{{#hasMore}},{{/hasMore}}{{^hasMore}}){{/hasMore}}
  {{/vars}}

object {{classname}} {
  import DateTimeCodecs._
  {{#hasEnums}}
  {{#vars}}
  {{#isEnum}}
  sealed trait {{datatypeWithEnum}}
  {{#_enum}}
  case object {{#fnEnumEntry}}{{.}}{{/fnEnumEntry}} extends {{datatypeWithEnum}}
  {{/_enum}}

  object {{datatypeWithEnum}} {
    def to{{datatypeWithEnum}}(s: String): Option[{{datatypeWithEnum}}] = s match {
{{#_enum}}      case "{{#fnEnumEntry}}{{.}}{{/fnEnumEntry}}" => Some({{#fnEnumEntry}}{{.}}{{/fnEnumEntry}})
{{/_enum}}
      case _ => None
    }

    def from{{datatypeWithEnum}}(x: {{datatypeWithEnum}}): String = x match {
{{#_enum}}      case {{#fnEnumEntry}}{{.}}{{/fnEnumEntry}} => "{{#fnEnumEntry}}{{.}}{{/fnEnumEntry}}"
{{/_enum}}
    }
  }

  implicit val {{datatypeWithEnum}}EnumEncoder: EncodeJson[{{datatypeWithEnum}}] =
    EncodeJson[{{datatypeWithEnum}}](is => StringEncodeJson({{datatypeWithEnum}}.from{{datatypeWithEnum}}(is)))

  implicit val {{datatypeWithEnum}}EnumDecoder: DecodeJson[{{datatypeWithEnum}}] =
    DecodeJson.optionDecoder[{{datatypeWithEnum}}](n => n.string.flatMap(jStr => {{datatypeWithEnum}}.to{{datatypeWithEnum}}(jStr)), "{{datatypeWithEnum}} failed to de-serialize")
  {{/isEnum}}
  {{/vars}}
  {{/hasEnums}}

  implicit val {{classname}}CodecJson: CodecJson[{{classname}}] = CodecJson.derive[{{classname}}]
  implicit val {{classname}}Decoder: EntityDecoder[{{classname}}] = jsonOf[{{classname}}]
  implicit val {{classname}}Encoder: EntityEncoder[{{classname}}] = jsonEncoderOf[{{classname}}]
}
{{/model}}
{{/models}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy