Skip to content

Commit

Permalink
define type for implicits
Browse files Browse the repository at this point in the history
we treat warnings as fatal, so due to scala/scala#10083 we need to make sure that all implicits have an explicit type
  • Loading branch information
DavidLawes committed Jul 4, 2023
1 parent dba7056 commit 4494c31
Show file tree
Hide file tree
Showing 23 changed files with 73 additions and 75 deletions.
Expand Up @@ -33,7 +33,7 @@ object JsonFormatsHelper {
}


implicit val urlFormat = new Format[URI] {
implicit val urlFormat: Format[URI] = new Format[URI] {
override def writes(o: URI): JsValue = JsString(o.toString)
override def reads(json: JsValue): JsResult[URI] = json match {
case JsString(uri) => Try(JsSuccess(new URI(uri))).getOrElse(JsError(s"Invalid uri: $uri"))
Expand Down
Expand Up @@ -43,7 +43,7 @@ object Editions {
case _ => None
}

implicit val jf = new Format[Edition] {
implicit val jf: Format[Edition] = new Format[Edition] {
override def reads(json: JsValue): JsResult[Edition] = json match {
case JsString("uk") => JsSuccess(UK)
case JsString("us") => JsSuccess(US)
Expand Down
Expand Up @@ -7,7 +7,7 @@ object Importance {
case object Minor extends Importance
case object Major extends Importance

implicit val jf = new Writes[Importance] {
implicit val jf: Writes[Importance] = new Writes[Importance] {
override def writes(o: Importance): JsValue = o match {
case Minor => JsString("Minor")
case Major => JsString("Major")
Expand Down
Expand Up @@ -18,7 +18,7 @@ object NotificationPayloadType {
override def toString = "football-match-status"
}

implicit val jf = new Writes[NotificationPayloadType] {
implicit val jf: Writes[NotificationPayloadType] = new Writes[NotificationPayloadType] {
override def writes(nType: NotificationPayloadType): JsValue = JsString(nType.toString)
}
}
Expand Up @@ -9,7 +9,7 @@ import play.api.libs.json._
import NotificationPayloadType._
sealed case class GuardianItemType(mobileAggregatorPrefix: String)
object GuardianItemType {
implicit val jf = Json.writes[GuardianItemType]
implicit val jf: OWrites[GuardianItemType] = Json.writes[GuardianItemType]
}

object GITSection extends GuardianItemType("section")
Expand All @@ -18,15 +18,15 @@ object GITContent extends GuardianItemType("item-trimmed")

sealed trait Link
object Link {
implicit val jf = new Writes[Link] {
implicit val jf: Writes[Link] = new Writes[Link] {
override def writes(o: Link): JsValue = o match {
case l: ExternalLink => ExternalLink.jf.writes(l)
case l: GuardianLinkDetails => GuardianLinkDetails.jf.writes(l)
}
}
}

object ExternalLink { implicit val jf = Json.writes[ExternalLink] }
object ExternalLink { implicit val jf: OWrites[ExternalLink] = Json.writes[ExternalLink] }
case class ExternalLink(url: String) extends Link {
override val toString = url
}
Expand All @@ -42,7 +42,7 @@ case class GuardianLinkDetails(
}

object GuardianLinkDetails {
implicit val jf = Json.writes[GuardianLinkDetails]
implicit val jf: OWrites[GuardianLinkDetails] = Json.writes[GuardianLinkDetails]
}

sealed trait GoalType
Expand All @@ -51,7 +51,7 @@ object PenaltyGoalType extends GoalType
object DefaultGoalType extends GoalType

object GoalType {
implicit val jf = new Writes[GoalType] {
implicit val jf: Writes[GoalType] = new Writes[GoalType] {
override def writes(o: GoalType): JsValue = o match {
case OwnGoalType => JsString("Own")
case PenaltyGoalType => JsString("Penalty")
Expand All @@ -74,7 +74,7 @@ sealed trait NotificationPayload {
}

object NotificationPayload {
implicit val jf = new Writes[NotificationPayload] {
implicit val jf: Writes[NotificationPayload] = new Writes[NotificationPayload] {
override def writes(o: NotificationPayload): JsValue = o match {
case n: BreakingNewsPayload => BreakingNewsPayload.jf.writes(n)
case n: ContentAlertPayload => ContentAlertPayload.jf.writes(n)
Expand Down Expand Up @@ -104,7 +104,7 @@ case class BreakingNewsPayload(
}

object ContentAlertPayload {
implicit val jf = new Writes[ContentAlertPayload] {
implicit val jf: Writes[ContentAlertPayload] = new Writes[ContentAlertPayload] {
override def writes(o: ContentAlertPayload) = (Json.writes[ContentAlertPayload] withAdditionalStringFields Map("type" -> ContentAlert.toString, "id" -> o.id.toString)).writes(o)
}
}
Expand Down Expand Up @@ -141,35 +141,32 @@ case class ContentAlertPayload(
}

object FootballMatchStatusPayload {
implicit val jf = new Writes[FootballMatchStatusPayload] {
// more than 22 fields so I have to define that manually
override def writes(o: FootballMatchStatusPayload): JsValue = Json.obj(
"id" -> o.id,
"type" -> FootballMatchStatus.toString,
"title" -> o.title,
"message" -> o.message,
"thumbnailUrl" -> o.thumbnailUrl,
"sender" -> o.sender,
"awayTeamName" -> o.awayTeamName,
"awayTeamScore" -> o.awayTeamScore,
"awayTeamMessage" -> o.awayTeamMessage,
"awayTeamId" -> o.awayTeamId,
"homeTeamName" -> o.homeTeamName,
"homeTeamScore" -> o.homeTeamScore,
"homeTeamMessage" -> o.homeTeamMessage,
"homeTeamId" -> o.homeTeamId,
"competitionName" -> o.competitionName,
"venue" -> o.venue,
"matchId" -> o.matchId,
"matchInfoUri" -> o.matchInfoUri,
"articleUri" -> o.articleUri,
"importance" -> o.importance,
"topic" -> o.topic,
"matchStatus" -> o.matchStatus,
"eventId" -> o.eventId,
"debug" -> o.debug
)
}
implicit val jf: Writes[FootballMatchStatusPayload] = (o: FootballMatchStatusPayload) => Json.obj(
"id" -> o.id,
"type" -> FootballMatchStatus.toString,
"title" -> o.title,
"message" -> o.message,
"thumbnailUrl" -> o.thumbnailUrl,
"sender" -> o.sender,
"awayTeamName" -> o.awayTeamName,
"awayTeamScore" -> o.awayTeamScore,
"awayTeamMessage" -> o.awayTeamMessage,
"awayTeamId" -> o.awayTeamId,
"homeTeamName" -> o.homeTeamName,
"homeTeamScore" -> o.homeTeamScore,
"homeTeamMessage" -> o.homeTeamMessage,
"homeTeamId" -> o.homeTeamId,
"competitionName" -> o.competitionName,
"venue" -> o.venue,
"matchId" -> o.matchId,
"matchInfoUri" -> o.matchInfoUri,
"articleUri" -> o.articleUri,
"importance" -> o.importance,
"topic" -> o.topic,
"matchStatus" -> o.matchStatus,
"eventId" -> o.eventId,
"debug" -> o.debug
)
}
case class FootballMatchStatusPayload(
title: Option[String],
Expand Down
@@ -1,10 +1,10 @@
package com.gu.mobile.notifications.client.models

import play.api.libs.json.Json
import play.api.libs.json.{Json, OFormat}

/** Acknowledgement of notification with a message ID for looking up statistics on that message */
case class SendNotificationReply(messageId: String)

object SendNotificationReply {
implicit val jf = Json.format[SendNotificationReply]
implicit val jf: OFormat[SendNotificationReply] = Json.format[SendNotificationReply]
}
Expand Up @@ -7,7 +7,7 @@ import play.api.libs.json._
sealed trait TopicType

object TopicType {
implicit val jf = new Writes[TopicType] {
implicit val jf: Writes[TopicType] = new Writes[TopicType] {
override def writes(o: TopicType): JsValue = JsString(o.toString)
}
}
Expand All @@ -29,7 +29,7 @@ case class Topic(`type`: TopicType, name: String) {
def toTopicString = `type`.toString + "//" + name
}
object Topic {
implicit val jf = Json.writes[Topic]
implicit val jf: OWrites[Topic] = Json.writes[Topic]
val BreakingNewsUk = Topic(Breaking, UK.toString)
val BreakingNewsUs = Topic(Breaking, US.toString)
val BreakingNewsAu = Topic(Breaking, AU.toString)
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/scala/models/GoalType.scala
Expand Up @@ -8,7 +8,7 @@ object GoalType {
case object Penalty extends GoalType
case object Default extends GoalType

implicit val jf = new Format[GoalType] {
implicit val jf: Format[GoalType] = new Format[GoalType] {
override def writes(o: GoalType): JsValue = o match {
case Own => JsString("Own")
case Penalty => JsString("Penalty")
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/scala/models/GuardianItemType.scala
Expand Up @@ -5,13 +5,13 @@ import play.api.libs.json._
sealed case class GuardianItemType(mobileAggregatorPrefix: String)

object GuardianItemType {
implicit val reads = Json.reads[GuardianItemType].collect(JsonValidationError("Unrecognised item type")) {
implicit val reads: Reads[GuardianItemType] = Json.reads[GuardianItemType].collect(JsonValidationError("Unrecognised item type")) {
case GuardianItemType("section") => GITSection
case GuardianItemType("latest") => GITTag
case GuardianItemType("item-trimmed") => GITContent
}

implicit val writes = Json.writes[GuardianItemType]
implicit val writes: OWrites[GuardianItemType] = Json.writes[GuardianItemType]
}

object GITSection extends GuardianItemType("section")
Expand Down
4 changes: 3 additions & 1 deletion common/src/main/scala/models/Importance.scala
@@ -1,13 +1,15 @@
package models

import play.api.libs.json.Format

import PartialFunction.condOpt

sealed trait Importance
object Importance {
case object Minor extends Importance
case object Major extends Importance

implicit val jf = JsonUtils.stringFormat(fromString)
implicit val jf: Format[Importance] = JsonUtils.stringFormat(fromString)

def fromString(s: String): Option[Importance] = condOpt(s) {
case "Major" => Major
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/scala/models/JsonUtils.scala
Expand Up @@ -21,7 +21,7 @@ object JsonUtils {
}
}

lazy implicit val jodaFormat = new Format[DateTime] {
lazy implicit val jodaFormat: Format[DateTime] = new Format[DateTime] {
override def reads(json: JsValue): JsResult[DateTime] =
JodaReads.jodaDateReads("yyyy-MM-dd'T'HH:mm:ss.SSSZ").reads(json).map(_.withZone(DateTimeZone.UTC))

Expand All @@ -39,7 +39,7 @@ object JsonUtils {
o map format.writes getOrElse JsNull
}

implicit val uriFormat = new Format[URI] {
implicit val uriFormat: Format[URI] = new Format[URI] {
override def reads(json: JsValue): JsResult[URI] = json match {
case JsString(uri) => Try(new URI(uri)).map(u => JsSuccess(u)).getOrElse(JsError(s"Invalid URI: $uri"))
case _ => JsError("URI must be a String")
Expand Down
6 changes: 3 additions & 3 deletions common/src/main/scala/models/Link.scala
Expand Up @@ -13,17 +13,17 @@ object Link {
def webUri(frontendBaseUrl: String): URI = new URI(url)
}
object External {
implicit val jf = Json.format[External]
implicit val jf: OFormat[External] = Json.format[External]
}

case class Internal(contentApiId: String, shortUrl: Option[String], git: GuardianItemType, blockId: Option[String]) extends Link {
def webUri(frontendBaseUrl: String): URI = new URI(s"$frontendBaseUrl$contentApiId")
}
object Internal {
implicit val jf = Json.format[Internal]
implicit val jf: OFormat[Internal] = Json.format[Internal]
}

implicit val jf = new Format[Link] {
implicit val jf: Format[Link] = new Format[Link] {
override def writes(o: Link): JsValue = o match {
case external: External => External.jf.writes(external)
case internal: Internal => Internal.jf.writes(internal)
Expand Down
14 changes: 7 additions & 7 deletions common/src/main/scala/models/Notification.scala
Expand Up @@ -21,7 +21,7 @@ sealed trait Notification {

object Notification {

implicit val jf = new Format[Notification] {
implicit val jf: Format[Notification] = new Format[Notification] {
override def writes(o: Notification): JsValue = o match {
case n: BreakingNewsNotification => BreakingNewsNotification.jf.writes(n)
case n: ContentNotification => ContentNotification.jf.writes(n)
Expand Down Expand Up @@ -67,7 +67,7 @@ case class BreakingNewsNotification(
) extends Notification

object BreakingNewsNotification {
implicit val jf = Json.format[BreakingNewsNotification]
implicit val jf: OFormat[BreakingNewsNotification] = Json.format[BreakingNewsNotification]
}

case class NewsstandShardNotification(
Expand All @@ -84,7 +84,7 @@ case class NewsstandShardNotification(

}
object NewsstandShardNotification {
implicit val jf = Json.format[NewsstandShardNotification]
implicit val jf: OFormat[NewsstandShardNotification] = Json.format[NewsstandShardNotification]
}

case class EditionsNotification(
Expand All @@ -103,7 +103,7 @@ case class EditionsNotification(
}

object EditionsNotification {
implicit val jf = Json.format[EditionsNotification]
implicit val jf: OFormat[EditionsNotification] = Json.format[EditionsNotification]
}

case class ContentNotification(
Expand All @@ -121,7 +121,7 @@ case class ContentNotification(
) extends Notification with NotificationWithLink

object ContentNotification {
implicit val jf = Json.format[ContentNotification]
implicit val jf: OFormat[ContentNotification] = Json.format[ContentNotification]
}


Expand Down Expand Up @@ -183,7 +183,7 @@ case class GoalAlertNotification(
) extends Notification

object GoalAlertNotification {
implicit val jf = Json.format[GoalAlertNotification]
implicit val jf: OFormat[GoalAlertNotification] = Json.format[GoalAlertNotification]
}


Expand All @@ -204,7 +204,7 @@ case class LiveEventNotification(
) extends Notification

object LiveEventNotification {
implicit val jf = Json.format[LiveEventNotification]
implicit val jf: OFormat[LiveEventNotification] = Json.format[LiveEventNotification]
}

case class Us2020ResultsNotification (
Expand Down
7 changes: 3 additions & 4 deletions common/src/main/scala/models/NotificationReport.scala
@@ -1,13 +1,12 @@
package models

import java.util.UUID

import JsonUtils._
import com.github.nscala_time.time.Imports._
import com.gu.notifications.events.model.EventAggregation
import models.NotificationType.BreakingNews
import org.joda.time.DateTime
import play.api.libs.json.Json
import play.api.libs.json.{Json, OFormat}

case class NotificationReport(id: UUID,
`type`: NotificationType,
Expand All @@ -27,7 +26,7 @@ case class SenderReport(
)

object SenderReport {
implicit val jf = Json.format[SenderReport]
implicit val jf: OFormat[SenderReport] = Json.format[SenderReport]
}

object NotificationReport {
Expand Down Expand Up @@ -73,5 +72,5 @@ object NotificationReport {
)
}

implicit val jf = Json.format[NotificationReport]
implicit val jf: OFormat[NotificationReport] = Json.format[NotificationReport]
}
2 changes: 1 addition & 1 deletion common/src/main/scala/models/NotificationType.scala
Expand Up @@ -29,7 +29,7 @@ object NotificationType {

val toRep: Map[NotificationType, String] = fromRep.map(_.swap)

implicit val jf = new Format[NotificationType] {
implicit val jf: Format[NotificationType] = new Format[NotificationType] {
override def writes(o: NotificationType): JsValue = JsString(o.value)

override def reads(json: JsValue): JsResult[NotificationType] = json match {
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/scala/models/Platform.scala
Expand Up @@ -24,7 +24,7 @@ object Platform {
case "android-beta" => AndroidBeta
}

implicit val jf = new Format[Platform] {
implicit val jf: Format[Platform] = new Format[Platform] {
def reads(json: JsValue): JsResult[Platform] = json match {
case JsString(s) => fromString(s) map { JsSuccess(_) } getOrElse JsError(s"$s is not a valid platform")
case _ => JsError(s"Platform could not be decoded")
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/scala/models/PlatformStatistics.scala
Expand Up @@ -6,5 +6,5 @@ case class PlatformStatistics(platform: Platform, recipientsCount: Int)
object PlatformStatistics {
import play.api.libs.json._

implicit val jf = Json.format[PlatformStatistics]
implicit val jf: OFormat[PlatformStatistics] = Json.format[PlatformStatistics]
}

0 comments on commit 4494c31

Please sign in to comment.