Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrades Build #284

Merged
merged 11 commits into from
Apr 22, 2020
38 changes: 19 additions & 19 deletions tut/README.md → .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[comment]: # (Start Badges)

[![Join the chat at https://gitter.im/47deg/fetch](https://badges.gitter.im/47deg/fetch.svg)](https://gitter.im/47deg/fetch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/47deg/fetch.svg?branch=master)](https://travis-ci.org/47deg/fetch) [![codecov.io](http://codecov.io/github/47deg/fetch/coverage.svg?branch=master)](http://codecov.io/github/47deg/fetch?branch=master) [![Maven Central](https://img.shields.io/badge/maven%20central-1.2.1-green.svg)](https://oss.sonatype.org/#nexus-search;gav~com.47deg~fetch*) [![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/47deg/fetch/master/LICENSE) [![Latest version](https://img.shields.io/badge/fetch-1.2.1-green.svg)](https://index.scala-lang.org/47deg/fetch) [![Scala.js](http://scala-js.org/assets/badges/scalajs-0.6.15.svg)](http://scala-js.org) [![GitHub Issues](https://img.shields.io/github/issues/47deg/fetch.svg)](https://github.com/47deg/fetch/issues)
[![Join the chat at https://gitter.im/47deg/fetch](https://badges.gitter.im/47deg/fetch.svg)](https://gitter.im/47deg/fetch?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![codecov.io](http://codecov.io/github/47deg/fetch/coverage.svg?branch=master)](http://codecov.io/github/47deg/fetch?branch=master) [![Maven Central](https://img.shields.io/badge/maven%20central-1.2.1-green.svg)](https://oss.sonatype.org/#nexus-search;gav~com.47deg~fetch*) [![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/47deg/fetch/master/LICENSE) [![Latest version](https://img.shields.io/badge/fetch-1.2.1-green.svg)](https://index.scala-lang.org/47deg/fetch) [![Scala.js](http://scala-js.org/assets/badges/scalajs-0.6.15.svg)](http://scala-js.org) [![GitHub Issues](https://img.shields.io/github/issues/47deg/fetch.svg)](https://github.com/47deg/fetch/issues)

[comment]: # (End Badges)

Expand All @@ -19,18 +19,18 @@ For Scala 2.11.x and 2.12.x:
[comment]: # (Start Replace)

```scala
"com.47deg" %% "fetch" % "1.2.2"
"com.47deg" %% "fetch" % "@VERSION@"
```

Or, if using Scala.js (0.6.x):

```scala
"com.47deg" %%% "fetch" % "1.2.2"
"com.47deg" %%% "fetch" % "@VERSION@"
```

[comment]: # (End Replace)

```tut:invisible
```scala mdoc:invisible
val out = Console.out

def println(msg: String): Unit = {
Expand Down Expand Up @@ -72,7 +72,7 @@ Returning `Concurrent` instances from the fetch methods allows us to specify if

We'll implement a dummy data source that can convert integers to strings. For convenience, we define a `fetchString` function that lifts identities (`Int` in our dummy data source) to a `Fetch`.

```tut:silent
```scala mdoc:silent
import cats._
import cats.data.NonEmptyList
import cats.effect._
Expand Down Expand Up @@ -117,7 +117,7 @@ Since `Fetch` relies on `Concurrent` from the `cats-effect` library, we'll need

For executing `IO`, we need a `ContextShift[IO]` used for running `IO` instances and a `Timer[IO]` that is used for scheduling. Let's go ahead and create them. We'll use a `java.util.concurrent.ScheduledThreadPoolExecutor` with a couple of threads to run our fetches.

```tut:silent
```scala mdoc:silent
import java.util.concurrent._
import scala.concurrent.ExecutionContext

Expand All @@ -132,14 +132,14 @@ implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)

Now that we can convert `Int` values to `Fetch[F, String]`, let's try creating a fetch.

```tut:silent
```scala mdoc:silent
def fetchOne[F[_] : Concurrent]: Fetch[F, String] =
fetchString(1)
```

Let's run it and wait for the fetch to complete. We'll use `IO#unsafeRunTimed` for testing purposes, which will run an `IO[A]` to `Option[A]` and return `None` if it didn't complete in time:

```tut:book
```scala mdoc
import scala.concurrent.duration._

Fetch.run[IO](fetchOne).unsafeRunTimed(5.seconds)
Expand All @@ -151,20 +151,20 @@ As you can see in the previous example, the `ToStringSource` is queried once to

Multiple fetches to the same data source are automatically batched. For illustrating this, we are going to compose three independent fetch results as a tuple.

```tut:silent
```scala mdoc:silent
def fetchThree[F[_] : Concurrent]: Fetch[F, (String, String, String)] =
(fetchString(1), fetchString(2), fetchString(3)).tupled
```

When executing the above fetch, note how the three identities get batched, and the data source is only queried once.

```tut:book
```scala mdoc
Fetch.run[IO](fetchThree).unsafeRunTimed(5.seconds)
```

Note that the `DataSource#batch` method is not mandatory. It will be implemented in terms of `DataSource#fetch` if you don't provide an implementation.

```tut:silent
```scala mdoc:silent
object UnbatchedToString extends Data[Int, String] {
def name = "Unbatched to string"

Expand All @@ -187,22 +187,22 @@ def unbatchedString[F[_] : Concurrent](n: Int): Fetch[F, String] =

Let's create a tuple of unbatched string requests.

```tut:silent
```scala mdoc:silent
def fetchUnbatchedThree[F[_] : Concurrent]: Fetch[F, (String, String, String)] =
(unbatchedString(1), unbatchedString(2), unbatchedString(3)).tupled
```

When executing the above fetch, note how the three identities get requested in parallel. You can override `batch` to execute queries sequentially if you need to.

```tut:book
```scala mdoc
Fetch.run[IO](fetchUnbatchedThree).unsafeRunTimed(5.seconds)
```

## Parallelism

If we combine two independent fetches from different data sources, the fetches can be run in parallel. First, let's add a data source that fetches a string's size.

```tut:silent
```scala mdoc:silent
object Length extends Data[String, Int] {
def name = "Length"

Expand Down Expand Up @@ -231,22 +231,22 @@ def fetchLength[F[_] : Concurrent](s: String): Fetch[F, Int] =

And now we can easily receive data from the two sources in a single fetch.

```tut:silent
```scala mdoc:silent
def fetchMulti[F[_] : Concurrent]: Fetch[F, (String, Int)] =
(fetchString(1), fetchLength("one")).tupled
```

Note how the two independent data fetches run in parallel, minimizing the latency cost of querying the two data sources.

```tut:book
```scala mdoc
Fetch.run[IO](fetchMulti).unsafeRunTimed(5.seconds)
```

## Caching

When fetching an identity, subsequent fetches for the same identity are cached. Let's try creating a fetch that asks for the same identity twice.

```tut:silent
```scala mdoc:silent
import cats.syntax.all._

def fetchTwice[F[_] : Concurrent]: Fetch[F, (String, String)] = for {
Expand All @@ -257,12 +257,12 @@ def fetchTwice[F[_] : Concurrent]: Fetch[F, (String, String)] = for {

While running it, notice that the data source is only queried once. The next time the identity is requested, it's served from the cache.

```tut:book
```scala mdoc
Fetch.run[IO](fetchTwice).unsafeRunTimed(5.seconds)
```


```tut:invisible
```scala mdoc:invisible
executor.shutdownNow()
```
---
Expand Down
51 changes: 21 additions & 30 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}
import sbtorgpolicies.model.GitHubSettings

pgpPassphrase := Some(getEnvVar("PGP_PASSPHRASE").getOrElse("").toCharArray)
lazy val checkScalafmt = "+scalafmtCheck; +scalafmtSbtCheck;"
lazy val checkDocs = "docs/tut;"
lazy val checkJSTests = "+fetchJS/test; +debugJS/test;"
lazy val checkJVMTests =
"+coverage; +fetchJVM/test; +debugJVM/test; +examples/clean; +examples/test; +coverageReport; +coverageAggregate;"

addCommandAlias(
"ci-test",
s"$checkScalafmt $checkDocs $checkJSTests $checkJVMTests"
)
addCommandAlias("ci-docs", "project-docs/mdoc; docs/tut; headerCreateAll")
addCommandAlias("ci-microsite", "docs/publishMicrosite")

lazy val root = project
.in(file("."))
.settings(name := "fetch")
.settings(moduleName := "root")
.settings(
orgGithubSetting := GitHubSettings(
organization = "47degrees",
project = (name in LocalRootProject).value,
organizationName = "47 Degrees",
groupId = "com.47deg",
organizationHomePage = url("http://47deg.com"),
organizationEmail = "[email protected]"
)
)
.aggregate(fetchJS, fetchJVM, debugJVM, debugJS)

lazy val fetch = crossProject(JSPlatform, JVMPlatform)
.in(file("."))
.settings(name := "fetch")
.jsSettings(sharedJsSettings: _*)
.settings(commonCrossDependencies)

lazy val fetchJVM = fetch.jvm
Expand All @@ -32,7 +31,6 @@ lazy val debug = crossProject(JSPlatform, JVMPlatform)
.in(file("debug"))
.settings(name := "fetch-debug")
.dependsOn(fetch)
.jsSettings(sharedJsSettings: _*)
.settings(commonCrossDependencies)

lazy val debugJVM = debug.jvm
Expand All @@ -41,28 +39,21 @@ lazy val debugJS = debug.js
lazy val examples = (project in file("examples"))
.settings(name := "fetch-examples")
.dependsOn(fetchJVM, debugJVM)
.settings(noPublishSettings: _*)
.settings(skip in publish := true)
.settings(examplesSettings: _*)
.settings(
Seq(
resolvers += Resolver.sonatypeRepo("snapshots")
)
)

lazy val docs = (project in file("docs"))
.dependsOn(fetchJVM, debugJVM)
.settings(name := "fetch-docs")
.settings(docsSettings: _*)
.settings(noPublishSettings)
.settings(skip in publish := true)
.enablePlugins(MicrositesPlugin)

lazy val readme = (project in file("tut"))
.settings(name := "fetch-readme")
lazy val `project-docs` = (project in file(".docs"))
.aggregate(fetchJVM)
.dependsOn(fetchJVM)
.settings(readmeSettings: _*)
.settings(noPublishSettings)
.enablePlugins(TutPlugin)

addCommandAlias("ci-test", "scalafmtCheck; scalafmtSbtCheck; docs/tut; +test")
addCommandAlias("ci-docs", "docs/tut")
addCommandAlias("ci-microsite", "docs/publishMicrosite")
.settings(moduleName := "fetch-project-docs")
.settings(mdocIn := file(".docs"))
.settings(mdocOut := file("."))
.settings(skip in publish := true)
.enablePlugins(MdocPlugin)
4 changes: 2 additions & 2 deletions docs/src/main/tut/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ To begin, add the following dependency to your SBT build file:
[comment]: # (Start Replace)

```scala
"com.47deg" %% "fetch" % "1.2.2"
"com.47deg" %% "fetch" % "@VERSION@"
BenFradet marked this conversation as resolved.
Show resolved Hide resolved
```

Or, if using Scala.js:

```scala
"com.47deg" %%% "fetch" % "1.2.2"
"com.47deg" %%% "fetch" % "@VERSION@"
```

[comment]: # (End Replace)
Expand Down
13 changes: 10 additions & 3 deletions examples/src/test/scala/DoobieExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import cats.syntax.all._

import doobie.{Query => _, _}
import doobie.h2.H2Transactor
import doobie.implicits._
import doobie.util.ExecutionContexts

import org.scalatest.{Matchers, WordSpec}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import scala.concurrent.ExecutionContext
import java.util.concurrent.Executors
Expand All @@ -36,6 +36,9 @@ object DatabaseExample {
case class Author(id: Int, name: String)

object Queries {

import doobie.implicits._

implicit val authorIdMeta: Meta[AuthorId] =
Meta[Int].imap(AuthorId(_))(_.id)

Expand All @@ -55,6 +58,8 @@ object DatabaseExample {
def transactionPool[F[_]: Sync]: Resource[F, ExecutionContext] =
ExecutionContexts.cachedThreadPool

import doobie.implicits._

val createTable = sql"""
CREATE TABLE author (
id INTEGER PRIMARY KEY,
Expand Down Expand Up @@ -91,6 +96,8 @@ object DatabaseExample {
}

object Authors extends Data[AuthorId, Author] {

import doobie.implicits._
def name = "Authors"

def db[F[_]: Concurrent: ContextShift]: DataSource[F, AuthorId, Author] =
Expand Down Expand Up @@ -118,7 +125,7 @@ object DatabaseExample {
}
}

class DoobieExample extends WordSpec with Matchers {
class DoobieExample extends AnyWordSpec with Matchers {
import DatabaseExample._

val executionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(2))
Expand Down
7 changes: 4 additions & 3 deletions examples/src/test/scala/GithubExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import org.http4s.circe._
import org.http4s.client._
import org.http4s.client.dsl._
import org.http4s.client.blaze._
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import fetch.{Data, DataSource, Fetch}

class GithubExample extends WordSpec with Matchers {
class GithubExample extends AnyWordSpec with Matchers {
implicit val executionContext = ExecutionContext.Implicits.global

val ACCESS_TOKEN: String = sys.env("ORG_GITHUB_TOKEN")
val ACCESS_TOKEN: String = sys.env("GITHUB_TOKEN")

implicit val t: Timer[IO] = IO.timer(executionContext)
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)
Expand Down
5 changes: 3 additions & 2 deletions examples/src/test/scala/GraphQLExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package fetch

import org.scalatest.{Matchers, WordSpec}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import atto._, Atto._
import cats.implicits._
Expand All @@ -30,7 +31,7 @@ case class Organization(org: String, projects: List[Project])
case class Project(name: Option[String], languages: List[String], collaborators: List[String])
case class Repo(name: String)

class GraphQLExample extends WordSpec with Matchers {
class GraphQLExample extends AnyWordSpec with Matchers {
implicit val executionContext = ExecutionContext.Implicits.global
implicit val t: Timer[IO] = IO.timer(executionContext)
implicit val cs: ContextShift[IO] = IO.contextShift(executionContext)
Expand Down
5 changes: 3 additions & 2 deletions examples/src/test/scala/Http4sExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import io.circe.generic.semiauto._
import org.http4s.client.Client
import org.http4s.circe._
import org.http4s.client.blaze._
import org.scalatest.{Matchers, WordSpec}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import java.util.concurrent._

Expand Down Expand Up @@ -119,7 +120,7 @@ object HttpExample {
fetchPostsForUser(user.id).map(posts => (user, posts))
}

class Http4sExample extends WordSpec with Matchers {
class Http4sExample extends AnyWordSpec with Matchers {
import HttpExample._

// runtime
Expand Down
Loading