diff --git a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/IabEnrichment.scala b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/IabEnrichment.scala index f695777c4..0ef2ceffa 100644 --- a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/IabEnrichment.scala +++ b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/IabEnrichment.scala @@ -16,7 +16,7 @@ package enrichments.registry import java.io.File import java.net.{InetAddress, URI} -import cats.{Eval, Id, Monad} +import cats.{Id, Monad} import cats.data.{NonEmptyList, ValidatedNel} import cats.effect.Sync @@ -188,18 +188,6 @@ object CreateIabClient { } } - implicit def evalCreateIabClient: CreateIabClient[Eval] = - new CreateIabClient[Eval] { - def create( - ipFile: String, - excludeUaFile: String, - includeUaFile: String - ): Eval[IabClient] = - Eval.later { - new IabClient(new File(ipFile), new File(excludeUaFile), new File(includeUaFile)) - } - } - implicit def idCreateIabClient: CreateIabClient[Id] = new CreateIabClient[Id] { def create( diff --git a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/UaParserEnrichment.scala b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/UaParserEnrichment.scala index a61d0b063..cb9931a8b 100644 --- a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/UaParserEnrichment.scala +++ b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/UaParserEnrichment.scala @@ -15,7 +15,7 @@ package enrichments.registry import java.io.{FileInputStream, InputStream} import java.net.URI -import cats.{Eval, Id, Monad} +import cats.{Id, Monad} import cats.data.{EitherT, NonEmptyList, ValidatedNel} import cats.effect.Sync import cats.implicits._ @@ -168,12 +168,6 @@ object CreateUaParser { Sync[F].delay(parser(uaFile)) } - implicit def evalCreateUaParser: CreateUaParser[Eval] = - new CreateUaParser[Eval] { - def create(uaFile: Option[String]): Eval[Either[String, Parser]] = - Eval.later(parser(uaFile)) - } - implicit def idCreateUaParser: CreateUaParser[Id] = new CreateUaParser[Id] { def create(uaFile: Option[String]): Id[Either[String, Parser]] = diff --git a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/CreateSqlQueryEnrichment.scala b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/CreateSqlQueryEnrichment.scala index a35684aab..3ca02cf07 100644 --- a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/CreateSqlQueryEnrichment.scala +++ b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/CreateSqlQueryEnrichment.scala @@ -12,7 +12,7 @@ */ package com.snowplowanalytics.snowplow.enrich.common.enrichments.registry.sqlquery -import cats.{Eval, Id} +import cats.Id import cats.effect.Sync import cats.syntax.functor._ import cats.syntax.flatMap._ @@ -49,28 +49,6 @@ object CreateSqlQueryEnrichment { ) } - implicit def evalCreateSqlQueryEnrichment( - implicit CLM: SqlCacheInit[Eval], - CN: ConnectionRefInit[Eval], - DB: DbExecutor[Eval] - ): CreateSqlQueryEnrichment[Eval] = - new CreateSqlQueryEnrichment[Eval] { - def create(conf: SqlQueryConf): Eval[SqlQueryEnrichment[Eval]] = - for { - cache <- CLM.create(conf.cache.size) - connection <- CN.create(1) - } yield SqlQueryEnrichment( - conf.schemaKey, - conf.inputs, - conf.db, - conf.query, - conf.output, - conf.cache.ttl, - cache, - connection - ) - } - implicit def idCreateSqlQueryEnrichment( implicit CLM: SqlCacheInit[Id], CN: ConnectionRefInit[Id], diff --git a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/DbExecutor.scala b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/DbExecutor.scala index 8a909229b..cfd798489 100644 --- a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/DbExecutor.scala +++ b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/enrichments/registry/sqlquery/DbExecutor.scala @@ -19,7 +19,7 @@ import scala.util.control.NonFatal import io.circe.Json -import cats.{Eval, Id, Monad} +import cats.{Id, Monad} import cats.data.EitherT import cats.effect.{Bracket, Sync} import cats.implicits._ @@ -142,77 +142,6 @@ object DbExecutor { } - implicit def evalDbExecutor: DbExecutor[Eval] = - new DbExecutor[Eval] { - self => - def getConnection(rdbms: Rdbms, connectionRef: ConnectionRef[Eval])(implicit M: Monad[Eval]): Eval[Either[Throwable, Connection]] = - for { - cachedConnection <- connectionRef.get(()).map(flattenCached) - connection <- cachedConnection match { - case Right(conn) => - for { - closed <- Eval.now(conn.isClosed) - result <- if (!closed) conn.asRight[Throwable].pure[Eval] - else - for { - newConn <- Eval.now { - Either.catchNonFatal(DriverManager.getConnection(rdbms.connectionString)) - } - _ <- connectionRef.put((), newConn) - } yield newConn - } yield result - case Left(error) => - Eval.now(error.asLeft[Connection]) - - } - } yield connection - - def execute(query: PreparedStatement): EitherT[Eval, Throwable, ResultSet] = - EitherT(Eval.now(Either.catchNonFatal(query.executeQuery()))) - - def convert(resultSet: ResultSet, names: JsonOutput.PropertyNameMode): EitherT[Eval, Throwable, List[Json]] = - EitherT { - Eval.always { - try { - val buffer = ListBuffer.empty[EitherT[Id, Throwable, Json]] - while (resultSet.next()) - buffer += transform[Id](resultSet, names)(idDbExecutor, Monad[Id]) - val parsedJsons = buffer.result().sequence - resultSet.close() - parsedJsons.value: Either[Throwable, List[Json]] - } catch { - case NonFatal(error) => error.asLeft - } - } - } - - def getMetaData(rs: ResultSet): EitherT[Eval, Throwable, ResultSetMetaData] = - Either.catchNonFatal(rs.getMetaData).toEitherT[Eval] - - def getColumnCount(rsMeta: ResultSetMetaData): EitherT[Eval, Throwable, Int] = - Either.catchNonFatal(rsMeta.getColumnCount).toEitherT[Eval] - - def getColumnLabel(column: Int, rsMeta: ResultSetMetaData): EitherT[Eval, Throwable, String] = - Either.catchNonFatal(rsMeta.getColumnLabel(column)).toEitherT[Eval] - - def getColumnType(column: Int, rsMeta: ResultSetMetaData): EitherT[Eval, Throwable, String] = - Either.catchNonFatal(rsMeta.getColumnClassName(column)).toEitherT[Eval] - - def getColumnValue( - datatype: String, - columnIdx: Int, - rs: ResultSet - ): EitherT[Eval, Throwable, Json] = - Either - .catchNonFatal(rs.getObject(columnIdx)) - .map(Option.apply) - .map { - case Some(any) => JsonOutput.getValue(any, datatype) - case None => Json.Null - } - .toEitherT - } - implicit def idDbExecutor: DbExecutor[Id] = new DbExecutor[Id] { def getConnection(rdbms: Rdbms, connectionRef: ConnectionRef[Id])(implicit M: Monad[Id]): Id[Either[Throwable, Connection]] = diff --git a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/utils/HttpClient.scala b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/utils/HttpClient.scala index 9bce3385b..773dff448 100644 --- a/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/utils/HttpClient.scala +++ b/modules/common/src/main/scala/com.snowplowanalytics.snowplow.enrich/common/utils/HttpClient.scala @@ -14,7 +14,7 @@ package com.snowplowanalytics.snowplow.enrich.common.utils import scala.util.control.NonFatal -import cats.{Eval, Id} +import cats.Id import cats.effect.Sync import cats.syntax.either._ import scalaj.http._ @@ -32,12 +32,6 @@ object HttpClient { Sync[F].delay(getBody(request)) } - implicit def evalHttpClient: HttpClient[Eval] = - new HttpClient[Eval] { - override def getResponse(request: HttpRequest): Eval[Either[Throwable, String]] = - Eval.later(getBody(request)) - } - implicit def idHttpClient: HttpClient[Id] = new HttpClient[Id] { override def getResponse(request: HttpRequest): Id[Either[Throwable, String]] = getBody(request) diff --git a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/IabEnrichmentSpec.scala b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/IabEnrichmentSpec.scala index 861869b11..cc570ba33 100644 --- a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/IabEnrichmentSpec.scala +++ b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/IabEnrichmentSpec.scala @@ -14,7 +14,8 @@ package com.snowplowanalytics.snowplow.enrich.common.enrichments.registry import java.net.InetAddress -import cats.Eval +import cats.Id +import cats.syntax.functor._ import io.circe.literal._ @@ -79,15 +80,14 @@ class IabEnrichmentSpec extends Specification with DataTables { expectedReason, expectedPrimaryImpact ) => - (for { - e <- validConfig.enrichment[Eval] - res = e.performCheck(userAgent, ipAddress, DateTime.now()) - } yield res).value must beRight.like { - case check => - check.spiderOrRobot must_== expectedSpiderOrRobot and - (check.category must_== expectedCategory) and - (check.reason must_== expectedReason) and - (check.primaryImpact must_== expectedPrimaryImpact) + validConfig.enrichment[Id].map { e => + e.performCheck(userAgent, ipAddress, DateTime.now()) must beRight.like { + case check => + check.spiderOrRobot must_== expectedSpiderOrRobot and + (check.category must_== expectedCategory) and + (check.reason must_== expectedReason) and + (check.primaryImpact must_== expectedPrimaryImpact) + } } } @@ -98,9 +98,8 @@ class IabEnrichmentSpec extends Specification with DataTables { json"""{"spiderOrRobot": false, "category": "BROWSER", "reason": "PASSED_ALL", "primaryImpact": "NONE"}""" ) validConfig - .enrichment[Eval] - .map(_.getIabContext("Xdroid", "192.168.0.1".ip, DateTime.now())) - .value must + .enrichment[Id] + .map(_.getIabContext("Xdroid", "192.168.0.1".ip, DateTime.now())) must beRight(responseJson) } diff --git a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/RefererParserEnrichmentSpec.scala b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/RefererParserEnrichmentSpec.scala index 4cfdb2b33..7cf9f653f 100644 --- a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/RefererParserEnrichmentSpec.scala +++ b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/RefererParserEnrichmentSpec.scala @@ -14,12 +14,15 @@ package com.snowplowanalytics.snowplow.enrich.common.enrichments.registry import java.net.URI -import cats.Eval +import cats.Id import cats.data.EitherT import cats.syntax.either._ + +import io.circe.literal._ + import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer} import com.snowplowanalytics.refererparser._ -import io.circe.literal._ + import org.specs2.Specification import org.specs2.matcher.DataTables @@ -57,7 +60,7 @@ class RefererParserEnrichmentSpec extends Specification with DataTables { Medium.Unknown ) |> { (_, refererUri, referer) => (for { - c <- EitherT.fromEither[Eval]( + c <- EitherT.fromEither[Id]( RefererParserEnrichment .parse( json"""{ @@ -81,16 +84,16 @@ class RefererParserEnrichmentSpec extends Specification with DataTables { .toEither .leftMap(_.head) ) - e <- c.enrichment[Eval] + e <- c.enrichment[Id] res = e.extractRefererDetails(new URI(refererUri), PageHost) - } yield res).value.value must beRight.like { - case o => o must_== Some(referer) + } yield res).value must beRight.like { + case o => o must beSome(referer) } } def e2 = (for { - c <- EitherT.fromEither[Eval]( + c <- EitherT.fromEither[Id]( RefererParserEnrichment .parse( json"""{ @@ -114,16 +117,16 @@ class RefererParserEnrichmentSpec extends Specification with DataTables { .toEither .leftMap(_.head) ) - e <- c.enrichment[Eval] + e <- c.enrichment[Id] res = e.extractRefererDetails( new URI( "http://www.google.com/search?q=%0Agateway%09oracle%09cards%09denise%09linn&hl=en&client=safari" ), PageHost ) - } yield res).value.value must beRight.like { + } yield res).value must beRight.like { case o => - o must_== Some( + o must beSome( SearchReferer( Medium.Search, "Google", diff --git a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/UaParserEnrichmentSpec.scala b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/UaParserEnrichmentSpec.scala index 030fcf3d6..5fdf789d8 100644 --- a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/UaParserEnrichmentSpec.scala +++ b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/UaParserEnrichmentSpec.scala @@ -14,7 +14,7 @@ package com.snowplowanalytics.snowplow.enrich.common.enrichments.registry import java.net.URI -import cats.Eval +import cats.Id import cats.data.EitherT import io.circe.literal._ @@ -75,10 +75,10 @@ class UaParserEnrichmentSpec extends Specification with DataTables { "Custom Rules" | "Input UserAgent" | "Parsed UserAgent" | Some(badRulefile) !! mobileSafariUserAgent !! "Failed to initialize ua parser" |> { (rules, input, errorPrefix) => (for { - c <- EitherT.rightT[Eval, String](UaParserConf(schemaKey, rules)) - e <- c.enrichment[Eval] + c <- EitherT.rightT[Id, String](UaParserConf(schemaKey, rules)) + e <- c.enrichment[Id] res = e.extractUserAgent(input) - } yield res).value.value must beLeft.like { + } yield res).value must beLeft.like { case a => a must startWith(errorPrefix) } } @@ -90,11 +90,11 @@ class UaParserEnrichmentSpec extends Specification with DataTables { None !! safariUserAgent !! safariJson | Some(customRules) !! mobileSafariUserAgent !! testAgentJson |> { (rules, input, expected) => val json = for { - c <- EitherT.rightT[Eval, String](UaParserConf(schemaKey, rules)) - e <- c.enrichment[Eval].leftMap(_.toString) - res <- EitherT.fromEither[Eval](e.extractUserAgent(input)).leftMap(_.toString) + c <- EitherT.rightT[Id, String](UaParserConf(schemaKey, rules)) + e <- c.enrichment[Id].leftMap(_.toString) + res <- EitherT.fromEither[Id](e.extractUserAgent(input)).leftMap(_.toString) } yield res - json.value.value must beRight(expected) + json.value must beRight(expected) } } } diff --git a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/WeatherEnrichmentSpec.scala b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/WeatherEnrichmentSpec.scala index 4155b5c8f..149fc087b 100644 --- a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/WeatherEnrichmentSpec.scala +++ b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/enrichments/registry/WeatherEnrichmentSpec.scala @@ -14,12 +14,16 @@ package com.snowplowanalytics.snowplow.enrich.common.enrichments.registry import java.lang.{Float => JFloat} -import cats.Eval +import cats.Id import cats.data.EitherT -import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer} + import io.circe.generic.auto._ import io.circe.literal._ + import org.joda.time.DateTime + +import com.snowplowanalytics.iglu.core.{SchemaKey, SchemaVer} + import org.specs2.Specification object WeatherEnrichmentSpec { @@ -43,11 +47,10 @@ class WeatherEnrichmentSpec extends Specification { val schemaKey = SchemaKey("vendor", "name", "format", SchemaVer.Full(1, 0, 0)) lazy val validAppKey = sys.env - .get(OwmApiKey) - .getOrElse( - throw new IllegalStateException( - s"No $OwmApiKey environment variable found, test should have been skipped" - ) + .getOrElse(OwmApiKey, + throw new IllegalStateException( + s"No $OwmApiKey environment variable found, test should have been skipped" + ) ) object invalidEvent { @@ -65,7 +68,7 @@ class WeatherEnrichmentSpec extends Specification { def e1 = { val res = for { enr <- WeatherConf(schemaKey, "history.openweathermap.org", "KEY", 10, 5200, 1) - .enrichment[Eval] + .enrichment[Id] stamp <- EitherT( enr.getWeatherContext( Option(invalidEvent.lat), @@ -74,7 +77,7 @@ class WeatherEnrichmentSpec extends Specification { ) ).leftMap(_.head.toString) } yield stamp - res.value.value must beLeft.like { + res.value must beLeft.like { case e => e must contain("InputData(derived_tstamp,None,missing)") } @@ -83,7 +86,7 @@ class WeatherEnrichmentSpec extends Specification { def e2 = { val res = for { enr <- WeatherConf(schemaKey, "history.openweathermap.org", validAppKey, 10, 5200, 1) - .enrichment[Eval] + .enrichment[Id] stamp <- EitherT( enr.getWeatherContext( Option(validEvent.lat), @@ -92,13 +95,13 @@ class WeatherEnrichmentSpec extends Specification { ) ).leftMap(_.head.toString) } yield stamp - res.value.value must beRight + res.value must beRight } def e3 = { val res = for { enr <- WeatherConf(schemaKey, "history.openweathermap.org", "KEY", 10, 5200, 1) - .enrichment[Eval] + .enrichment[Id] stamp <- EitherT( enr.getWeatherContext( Option(validEvent.lat), @@ -107,13 +110,13 @@ class WeatherEnrichmentSpec extends Specification { ) ).leftMap(_.head.toString) } yield stamp - res.value.value must beLeft.like { case e => e must contain("Check your API key") } + res.value must beLeft.like { case e => e must contain("Check your API key") } } def e4 = { val res = for { enr <- WeatherConf(schemaKey, "history.openweathermap.org", validAppKey, 15, 5200, 1) - .enrichment[Eval] + .enrichment[Id] stamp <- EitherT( enr.getWeatherContext( Option(validEvent.lat), @@ -122,7 +125,7 @@ class WeatherEnrichmentSpec extends Specification { ) ).leftMap(_.head.toString) } yield stamp - res.value.value must beRight.like { + res.value must beRight.like { case weather => val temp = weather.data.hcursor.downField("main").get[Double]("humidity") temp must beRight(69.0d) @@ -164,7 +167,7 @@ class WeatherEnrichmentSpec extends Specification { def e6 = { val res = for { enr <- WeatherConf(schemaKey, "history.openweathermap.org", validAppKey, 15, 2, 1) - .enrichment[Eval] + .enrichment[Id] stamp <- EitherT( enr.getWeatherContext( Option(validEvent.lat), @@ -173,7 +176,7 @@ class WeatherEnrichmentSpec extends Specification { ) ).leftMap(_.head.toString) } yield stamp - res.value.value must beRight.like { // successful request + res.value must beRight.like { // successful request case weather => weather.data.hcursor.as[TransformedWeather] must beRight.like { case w => diff --git a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/utils/Clock.scala b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/utils/Clock.scala index 4cdbc9cf0..c5c0574a4 100644 --- a/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/utils/Clock.scala +++ b/modules/common/src/test/scala/com.snowplowanalytics.snowplow.enrich.common/utils/Clock.scala @@ -15,17 +15,10 @@ package utils import java.util.concurrent.TimeUnit -import cats.{Eval, Id} +import cats.Id import cats.effect.{Clock => CEClock} object Clock { - implicit val evalClock: CEClock[Eval] = new CEClock[Eval] { - final def realTime(unit: TimeUnit): Eval[Long] = - Eval.later(unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS)) - final def monotonic(unit: TimeUnit): Eval[Long] = - Eval.later(unit.convert(System.nanoTime(), TimeUnit.NANOSECONDS)) - } - implicit val idClock: CEClock[Id] = new CEClock[Id] { final def realTime(unit: TimeUnit): Id[Long] = unit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS)