Skip to content

Commit

Permalink
Narrow Imports from Cats in Core package
Browse files Browse the repository at this point in the history
We remove wild-card imports from the `cats`, `cats.data`, or
`cats.effect` packages, and try to narrow them down.
  • Loading branch information
diesalbla committed May 15, 2019
1 parent e954f5d commit 7c04569
Show file tree
Hide file tree
Showing 35 changed files with 84 additions and 89 deletions.
Expand Up @@ -2,10 +2,10 @@ package org.http4s
package server
package blaze

import cats._
import cats.{Alternative, Applicative}
import cats.data.Kleisli
import cats.implicits._
import cats.effect._
import cats.effect.{ConcurrentEffect, Resource, Timer}
import java.io.FileInputStream
import java.net.InetSocketAddress
import java.nio.ByteBuffer
Expand Down
4 changes: 2 additions & 2 deletions circe/src/main/scala/org/http4s/circe/CirceInstances.scala
Expand Up @@ -3,9 +3,9 @@ package circe

import java.nio.ByteBuffer

import cats._
import cats.Applicative
import cats.data.NonEmptyList
import cats.effect._
import cats.effect.Sync
import cats.implicits._
import fs2.Chunk
import io.circe._
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/org/http4s/AuthedRequest.scala
@@ -1,7 +1,7 @@
package org.http4s

import cats._
import cats.data._
import cats.Functor
import cats.data.Kleisli
import cats.implicits._

final case class AuthedRequest[F[_], A](authInfo: A, req: Request[F])
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/scala/org/http4s/DecodeResult.scala
@@ -1,22 +1,22 @@
package org.http4s

import cats._
import cats.data._
import cats.{Applicative, Functor}
import cats.data.EitherT
import cats.implicits._

object DecodeResult {
def apply[F[_], A](fa: F[Either[DecodeFailure, A]]): DecodeResult[F, A] =
EitherT(fa)

def success[F[_], A](a: F[A])(implicit F: Functor[F]): DecodeResult[F, A] =
DecodeResult(a.map(Either.right(_)))
def success[F[_], A](fa: F[A])(implicit F: Functor[F]): DecodeResult[F, A] =
EitherT(fa.map(Right(_)))

def success[F[_], A](a: A)(implicit F: Applicative[F]): DecodeResult[F, A] =
success(F.pure(a))
EitherT(F.pure(Right(a)))

def failure[F[_], A](e: F[DecodeFailure])(implicit F: Functor[F]): DecodeResult[F, A] =
DecodeResult(e.map(Either.left(_)))
def failure[F[_], A](fe: F[DecodeFailure])(implicit F: Functor[F]): DecodeResult[F, A] =
EitherT(fe.map(Left(_)))

def failure[F[_], A](e: DecodeFailure)(implicit F: Applicative[F]): DecodeResult[F, A] =
failure(F.pure(e))
EitherT(F.pure(Left(e)))
}
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/Entity.scala
@@ -1,6 +1,6 @@
package org.http4s

import cats._
import cats.Monoid
import cats.implicits._

final case class Entity[+F[_]](body: EntityBody[F], length: Option[Long] = None)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/EntityDecoder.scala
@@ -1,6 +1,6 @@
package org.http4s

import cats._
import cats.{Applicative, Functor, Monad, SemigroupK}
import cats.effect.{ContextShift, Sync}
import cats.implicits._
import fs2._
Expand Down
7 changes: 3 additions & 4 deletions core/src/main/scala/org/http4s/EntityEncoder.scala
@@ -1,10 +1,9 @@
package org.http4s

import cats._
import cats.{Contravariant, Show}
import cats.effect.{ContextShift, Effect, Sync}
import cats.implicits._
import fs2.Stream._
import fs2._
import fs2.{Chunk, Stream}
import fs2.io.file.readAll
import fs2.io.readInputStream
import java.io._
Expand Down Expand Up @@ -75,7 +74,7 @@ object EntityEncoder {
def simple[F[_], A](hs: Header*)(toChunk: A => Chunk[Byte]): EntityEncoder[F, A] =
encodeBy(hs: _*) { a =>
val c = toChunk(a)
Entity[F](chunk(c).covary[F], Some(c.size.toLong))
Entity[F](Stream.chunk(c).covary[F], Some(c.size.toLong))
}

/** Encodes a value from its Show instance. Too broad to be implicit, too useful to not exist. */
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/Header.scala
Expand Up @@ -18,7 +18,7 @@
*/
package org.http4s

import cats._
import cats.{Eq, Show}
import cats.data.NonEmptyList
import cats.implicits.{catsSyntaxEither => _, _}
import org.http4s.syntax.string._
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/Headers.scala
@@ -1,6 +1,6 @@
package org.http4s

import cats._
import cats.{Eq, Eval, Foldable, Monoid, Show}
import cats.implicits._
import org.http4s.headers.`Set-Cookie`
import org.http4s.syntax.string._
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/org/http4s/HttpService.scala
@@ -1,7 +1,7 @@
package org.http4s

import cats._
import cats.data._
import cats.{Applicative, Functor}
import cats.data.{Kleisli, OptionT}

@deprecated("Replaced by HttpRoutes", "0.19")
object HttpService extends Serializable {
Expand Down
12 changes: 6 additions & 6 deletions core/src/main/scala/org/http4s/Message.scala
@@ -1,11 +1,11 @@
package org.http4s

import cats._
import cats.{Applicative, Functor, Monad, MonadError, ~>}
import cats.data.NonEmptyList
import cats.implicits._
import cats.effect._
import fs2._
import fs2.text._
import cats.effect.IO
import fs2.{Pure, Stream}
import fs2.text.{utf8Decode, utf8Encode}
import java.io.File
import java.net.{InetAddress, InetSocketAddress}
import org.http4s.headers._
Expand Down Expand Up @@ -516,8 +516,8 @@ object Response {
private[this] val pureNotFound: Response[Pure] =
Response(
Status.NotFound,
body = Stream("Not found").through(text.utf8Encode),
headers = Headers(`Content-Type`(MediaType.text.plain, Charset.`UTF-8`).pure[List]))
body = Stream("Not found").through(utf8Encode),
headers = Headers(`Content-Type`(MediaType.text.plain, Charset.`UTF-8`) :: Nil))

def notFound[F[_]]: Response[F] = pureNotFound.copy(body = pureNotFound.body.covary[F])

Expand Down
8 changes: 4 additions & 4 deletions core/src/main/scala/org/http4s/MessageFailure.scala
@@ -1,6 +1,6 @@
package org.http4s

import cats._
import cats.{Applicative, Eq, MonadError}
import cats.implicits._
import scala.util.control.{NoStackTrace, NonFatal}

Expand Down Expand Up @@ -52,15 +52,15 @@ object ParseFailure {

object ParseResult {
def fail(sanitized: String, details: String): ParseResult[Nothing] =
Either.left(ParseFailure(sanitized, details))
Left(ParseFailure(sanitized, details))

def success[A](a: A): ParseResult[A] =
Either.right(a)
Right(a)

def fromTryCatchNonFatal[A](sanitized: String)(f: => A): ParseResult[A] =
try ParseResult.success(f)
catch {
case NonFatal(e) => Either.left(ParseFailure(sanitized, e.getMessage))
case NonFatal(e) => Left(ParseFailure(sanitized, e.getMessage))
}

implicit val parseResultMonad: MonadError[ParseResult, ParseFailure] =
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/QValue.scala
@@ -1,6 +1,6 @@
package org.http4s

import cats._
import cats.{Order, Show}
import org.http4s.internal.parboiled2.{Parser => PbParser}
import org.http4s.parser.{AdditionalRules, Http4sParser}
import org.http4s.util.Writer
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/scala/org/http4s/Query.scala
@@ -1,6 +1,6 @@
package org.http4s

import cats._
import cats.{Eval, Foldable}
import cats.implicits._
import org.http4s.Query._
import org.http4s.internal.CollectionCompat
Expand Down Expand Up @@ -142,7 +142,7 @@ object Query {
def fromPairs(xs: (String, String)*): Query =
new Query(
xs.toList.foldLeft(Vector.empty[KeyValue]) {
case (m, (k, s)) => m :+ (k -> s.some)
case (m, (k, s)) => m :+ (k -> Some(s))
}
)

Expand All @@ -162,6 +162,6 @@ object Query {
def fromMap(map: collection.Map[String, collection.Seq[String]]): Query =
new Query(map.foldLeft(Vector.empty[KeyValue]) {
case (m, (k, Seq())) => m :+ (k -> None)
case (m, (k, vs)) => vs.toList.foldLeft(m) { case (m, v) => m :+ (k -> v.some) }
case (m, (k, vs)) => vs.toList.foldLeft(m) { case (m, v) => m :+ (k -> Some(v)) }
})
}
5 changes: 2 additions & 3 deletions core/src/main/scala/org/http4s/QueryOps.scala
@@ -1,6 +1,5 @@
package org.http4s

import cats.implicits._
trait QueryOps {

protected type Self <: QueryOps
Expand Down Expand Up @@ -99,7 +98,7 @@ trait QueryOps {
val vec = params.foldLeft(query.toVector) {
case (m, (k, Seq())) => m :+ (penc.getKey(k).value -> None)
case (m, (k, vs)) =>
vs.foldLeft(m) { case (m, v) => m :+ (penc.getKey(k).value -> venc.encode(v).value.some) }
vs.foldLeft(m) { case (m, v) => m :+ (penc.getKey(k).value -> Some(venc.encode(v).value)) }
}
replaceQuery(Query.fromVector(vec))
}
Expand Down Expand Up @@ -147,7 +146,7 @@ trait QueryOps {
if (values.isEmpty) baseQuery :+ (name.value -> None)
else {
values.toList.foldLeft(baseQuery) {
case (vec, v) => vec :+ (name.value -> v.value.some)
case (vec, v) => vec :+ (name.value -> Some(v.value))
}
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/main/scala/org/http4s/QueryParam.scala
@@ -1,8 +1,9 @@
package org.http4s

import cats._
import cats.data._
import cats.{Contravariant, Functor, MonoidK, Show}
import cats.data.{Validated, ValidatedNel}
import cats.implicits._

final case class QueryParameterKey(value: String) extends AnyVal

final case class QueryParameterValue(value: String) extends AnyVal
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/org/http4s/Service.scala
@@ -1,7 +1,7 @@
package org.http4s

import cats._
import cats.data._
import cats.{Applicative, Monoid, Semigroup}
import cats.data.Kleisli
import cats.effect.Sync
import cats.implicits._

Expand Down
8 changes: 4 additions & 4 deletions core/src/main/scala/org/http4s/StaticFile.scala
@@ -1,9 +1,9 @@
package org.http4s

import cats.data._
import cats.effect._
import cats.data.OptionT
import cats.effect.{ContextShift, IO, Sync}
import cats.implicits.{catsSyntaxEither => _, _}
import fs2.Stream._
import fs2.Stream
import fs2.io._
import fs2.io.file.readRange
import java.io._
Expand Down Expand Up @@ -140,7 +140,7 @@ object StaticFile {

notModified.orElse(etagModified).orElse {
val (body, contentLength) =
if (f.length() < end) (empty.covary[F], 0L)
if (f.length() < end) (Stream.empty.covary[F], 0L)
else (fileToBody[F](f, start, end, blockingExecutionContext), end - start)

val contentType = nameToContentType(f.getName)
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/Uri.scala
@@ -1,6 +1,6 @@
package org.http4s

import cats._
import cats.{Eq, Order, Show}
import cats.implicits.{catsSyntaxEither => _, _}
import java.nio.charset.StandardCharsets
import org.http4s.Uri._
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/org/http4s/UrlForm.scala
@@ -1,7 +1,7 @@
package org.http4s

import cats._
import cats.data._
import cats.{Eq, Monoid}
import cats.data.Chain
import cats.effect.Sync
import cats.implicits.{catsSyntaxEither => _, _}
import org.http4s.headers._
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/org/http4s/internal/package.scala
Expand Up @@ -3,7 +3,7 @@ package org.http4s
import java.util.concurrent.{CancellationException, CompletableFuture, CompletionException}
import java.util.function.BiFunction

import cats.effect._
import cats.effect.{Async, Concurrent, ConcurrentEffect, Effect, IO}
import cats.implicits._
import org.http4s.util.execution.direct
import org.log4s.Logger
Expand Down
@@ -1,7 +1,7 @@
package org.http4s
package multipart

import cats.effect._
import cats.effect.{ContextShift, Sync}
import cats.implicits._
import scala.concurrent.ExecutionContext

Expand Down
10 changes: 5 additions & 5 deletions core/src/main/scala/org/http4s/multipart/MultipartParser.scala
@@ -1,11 +1,11 @@
package org.http4s
package multipart

import cats.effect._
import cats.effect.{ContextShift, Sync}
import cats.implicits.{catsSyntaxEither => _, _}
import fs2._
import fs2.{Chunk, Pipe, Pull, Pure, Stream}
import fs2.io.file.{readAll, writeAll}
import java.nio.file._
import java.nio.file.{Files, Path, StandardOpenOption}
import scala.concurrent.ExecutionContext

/** A low-level multipart-parsing pipe. Most end users will prefer EntityDecoder[Multipart]. */
Expand Down Expand Up @@ -789,8 +789,8 @@ object MultipartParser {
} else if (limitCTR >= maxBeforeWrite) {
Pull.eval(
lacc
.through(io.file
.writeAll[F](fileRef, blockingExecutionContext, List(StandardOpenOption.APPEND)))
.through(
writeAll[F](fileRef, blockingExecutionContext, List(StandardOpenOption.APPEND)))
.compile
.drain) >> streamAndWrite(s, state, Stream.empty, racc, 0, fileRef)
} else {
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/scala/org/http4s/package.scala
@@ -1,7 +1,7 @@
package org

import cats.data._
import fs2._
import cats.data.{EitherT, Kleisli, OptionT}
import fs2.Stream

package object http4s { // scalastyle:ignore

Expand Down
3 changes: 1 addition & 2 deletions core/src/main/scala/org/http4s/parser/QueryParser.scala
@@ -1,7 +1,6 @@
package org.http4s
package parser

import cats.implicits._
import java.io.UnsupportedEncodingException
import java.nio.CharBuffer
import org.http4s.util.UrlCodingUtils
Expand Down Expand Up @@ -108,7 +107,7 @@ private[http4s] object QueryParser {
private val InitialBufferCapactiy = 32

def parseQueryString(queryString: String, codec: Codec = Codec.UTF8): ParseResult[Query] =
if (queryString.isEmpty) Either.right(Query.empty)
if (queryString.isEmpty) Right(Query.empty)
else new QueryParser(codec, true).decode(CharBuffer.wrap(queryString), true)

private sealed trait State
Expand Down

0 comments on commit 7c04569

Please sign in to comment.