Skip to content

Instantly share code, notes, and snippets.

@hyunjun
Last active April 14, 2020 13:07
Show Gist options
  • Save hyunjun/3807ba075422376ad56490642847d05c to your computer and use it in GitHub Desktop.
Save hyunjun/3807ba075422376ad56490642847d05c to your computer and use it in GitHub Desktop.
Scala Play
final case class MetaData(
messageType: MessageType = MessageType.Regular,
messageTags: Seq[MessageTag] = Seq.empty,
targetOnly: Boolean = false,
markUnread: Option[Boolean] = None,
sms: Boolean = false,
push: Boolean = true,
email: Boolean = true,
line: Boolean = false,
escalated: Boolean = false,
initial: Boolean = false,
firstResponse: Boolean = false,
noTranslate: Boolean = false,
offerId: Option[Long] = None,
experimentRunId: Option[Long] = None,
expiryDays: Option[Int] = None,
shortMessage: Option[String] = None,
pushHeader: Option[String] = None,
pushMessage: Option[String] = None,
pushUrl: Option[String] = None,
pushButtons: Option[Seq[PushButton]] = None,
forceEnablePush: Boolean = false,
emailContext: Option[EmailContext] = None,
attachmentItems: Option[Seq[CephAttachmentItem]] = None
)
import play.api.libs.functional.syntax.{unlift, _}
...
val metaDataFormat1: OFormat[(
MessageType,
Seq[MessageTag],
Boolean,
Option[Boolean],
Boolean,
Boolean,
Boolean,
Boolean,
Boolean,
Boolean,
Boolean,
Boolean)] =
((JsPath \ "message_type")
.formatNullable[MessageType]
.inmap[MessageType](_.getOrElse(MessageType.Regular), Some.apply _) and
(JsPath \ "message_tags").formatWithDefault(Seq.empty[MessageTag]) and
(JsPath \ "target_only")
.formatNullable[Boolean]
.inmap(falseIfNull, nullIfFalse) and
(JsPath \ "mark_unread").formatNullable[Boolean] and
(JsPath \ "sms")
.formatNullable[Boolean]
.inmap[Boolean](falseIfNull, Some.apply _) and
(JsPath \ "push")
.formatNullable[Boolean]
.inmap[Boolean](trueIfNull, Some.apply _) and
(JsPath \ "email")
.formatNullable[Boolean]
.inmap[Boolean](trueIfNull, Some.apply _) and
(JsPath \ "line")
.formatNullable[Boolean]
.inmap[Boolean](falseIfNull, nullIfFalse) and
(JsPath \ "escalated")
.formatNullable[Boolean]
.inmap(falseIfNull, nullIfFalse) and
(JsPath \ "initial")
.formatNullable[Boolean]
.inmap[Boolean](falseIfNull, nullIfFalse) and
(JsPath \ "first_response")
.formatNullable[Boolean]
.inmap[Boolean](falseIfNull, nullIfFalse) and
(JsPath \ "no_translate")
.formatNullable[Boolean]
.inmap[Boolean](falseIfNull, nullIfFalse)).tupled
val metaDataFormat2: OFormat[(
Option[Long],
Option[Long],
Option[Int],
Option[String],
Option[String],
Option[String],
Option[String],
Option[Seq[PushButton]],
Boolean,
Option[EmailContext],
Option[Seq[CephAttachmentItem]])] =
((JsPath \ "offer_id").formatNullable[Long] and
(JsPath \ "experiment_run_id").formatNullable[Long] and
(JsPath \ "expiry_days").formatNullable[Int] and
(JsPath \ "short_message").formatNullable[String] and
(JsPath \ "push_header").formatNullable[String] and
(JsPath \ "push_message").formatNullable[String] and
(JsPath \ "push_url").formatNullable[String] and
(JsPath \ "push_buttons").formatNullable[Seq[PushButton]] and
(JsPath \ "force_enable_push")
.formatNullable[Boolean]
.inmap[Boolean](falseIfNull, nullIfFalse) and
(JsPath \ "email_context").formatNullable[EmailContext] and
(JsPath \ "attachment").formatNullable[Seq[CephAttachmentItem]]).tupled
implicit val f18: Format[MetaData] = (metaDataFormat1 ~ metaDataFormat2)(
{
case (
(
messageType,
messageTag,
targetOnly,
markUnread,
sms,
push,
email,
line,
escalated,
initial,
firstResponse,
noTranslate),
(
offerId,
experimentRunId,
expiryDays,
shortMessage,
pushHeader,
pushMessage,
pushUrl,
pushButtons,
forceEnablePush,
emailContext,
attachmentItems)
) =>
new MetaData(
messageType,
messageTag,
targetOnly,
markUnread,
sms,
push,
email,
line,
escalated,
initial,
firstResponse,
noTranslate,
offerId,
experimentRunId,
expiryDays,
shortMessage,
pushHeader,
pushMessage,
pushUrl,
pushButtons,
forceEnablePush,
emailContext,
attachmentItems)
},
(metaData: MetaData) =>
(
(
metaData.messageType,
metaData.messageTags,
metaData.targetOnly,
metaData.markUnread,
metaData.sms,
metaData.push,
metaData.email,
metaData.line,
metaData.escalated,
metaData.initial,
metaData.firstResponse,
metaData.noTranslate),
(
metaData.offerId,
metaData.experimentRunId,
metaData.expiryDays,
metaData.shortMessage,
metaData.pushHeader,
metaData.pushMessage,
metaData.pushUrl,
metaData.pushButtons,
metaData.forceEnablePush,
metaData.emailContext,
metaData.attachmentItems))
)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment