Skip to content

Commit

Permalink
Common: get rid of Eval instances (close #300)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuwy committed Aug 20, 2020
1 parent 4b669d1 commit 3fb482e
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.net.URI

import inet.ipaddr.HostName

import cats.{Eval, Id, Monad}
import cats.{Id, Monad}
import cats.data.{NonEmptyList, ValidatedNel}
import cats.effect.Sync
import cats.implicits._
Expand Down Expand Up @@ -231,18 +231,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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*/
package com.snowplowanalytics.snowplow.enrich.common.enrichments.registry

import cats.Eval
import cats.Id
import cats.syntax.functor._

import io.circe.literal._

Expand Down Expand Up @@ -80,24 +81,23 @@ 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)
}
}
}

def e2 =
validConfig.enrichment[Eval].map(_.performCheck("", "foo//bar", DateTime.now())).value must
validConfig.enrichment[Id].map(_.performCheck("", "foo//bar", DateTime.now())) must
beLeft

def e3 =
validConfig.enrichment[Eval].map(_.getIabContext(None, None, None)).value must beLeft
validConfig.enrichment[Id].map(_.getIabContext(None, None, None)) must beLeft

def e4 = {
val responseJson =
Expand All @@ -106,9 +106,8 @@ class IabEnrichmentSpec extends Specification with DataTables {
json"""{"spiderOrRobot": false, "category": "BROWSER", "reason": "PASSED_ALL", "primaryImpact": "NONE"}"""
)
validConfig
.enrichment[Eval]
.map(_.getIabContext(Some("Xdroid"), Some("192.168.0.1"), Some(DateTime.now())))
.value must
.enrichment[Id]
.map(_.getIabContext(Some("Xdroid"), Some("192.168.0.1"), Some(DateTime.now()))) must
beRight(responseJson)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"""{
Expand All @@ -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"""{
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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)
}
}
}
Expand Down
Loading

0 comments on commit 3fb482e

Please sign in to comment.