Skip to content

Commit

Permalink
Merge pull request #14 from dvgica/managerial-twitter-util
Browse files Browse the repository at this point in the history
Add conversions to/from Twitter Util
  • Loading branch information
dvgica authored Aug 6, 2021
2 parents bc5ad6c + 6d696e1 commit a77b049
Show file tree
Hide file tree
Showing 12 changed files with 119 additions and 25 deletions.
14 changes: 2 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [3.0.1, 2.13.6, 2.12.14]
scala: [2.13.6, 2.12.14]
java: [adopt@1.8]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -59,7 +59,7 @@ jobs:
run: sbt --client '++${{ matrix.scala }}; test'

- name: Compress target directories
run: tar cf targets.tar target project/target
run: tar cf targets.tar target managerial/target managerial-twitter-util/target project/target

- name: Upload target directories
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -100,16 +100,6 @@ jobs:
~/Library/Caches/Coursier/v1
key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}

- name: Download target directories (3.0.1)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-3.0.1-${{ matrix.java }}

- name: Inflate target directories (3.0.1)
run: |
tar xf targets.tar
rm targets.tar
- name: Download target directories (2.13.6)
uses: actions/download-artifact@v2
with:
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ None of the ideas in the lib are particularly novel (see [Related Libraries](#re

## Installation

Managerial is available on Maven Central for Scala 2.12, 2.13, and 3.0.
Managerial is available on Maven Central for Scala 2.12 and 2.13.

Add the following dependency description to your build.sbt:

`"ca.dvgi" %% "managerial" % "<latest>"`

For usage from a Scala 3 project, use:

`("ca.dvgi" %% "managerial" % "<latest>").cross(CrossVersion.for3Use2_13)`

as detailed in the [Scala 3 Migration Guide](https://docs.scala-lang.org/scala3/guides/migration/compatibility-classpath.html).

## Usage

`Managed[T]` instances are created via `Managed#apply`, `Managed#setup`, or `Managed#from`. Additionally, arbitrary actions can be made into `Managed[Unit]` instances via various `Managed#eval` methods.
Expand Down Expand Up @@ -151,7 +157,7 @@ Unlike the Twitter Util library, Managerial:
Managerial is also quite similar to [Scala ARM](https://github.com/jsuereth/scala-arm).

Unlike Scala ARM, Managerial:
- is (officially) published for Scala 2.13 and 3.0
- is (officially) published for Scala 2.13 (and usable from 3.0)
- lacks some of the "fancy" features, like Delimited Continuation style, reflection-based teardown, or JTA transaction support

### Scala Stdlib `Using`
Expand Down
39 changes: 30 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,40 @@ inThisBuild(

val scala212Version = "2.12.14"
val scala213Version = "2.13.6"
val scala3Version = "3.0.1"
val scalaVersions = Seq(scala3Version, scala213Version, scala212Version)
val scalaVersions =
Seq(
scala213Version,
scala212Version
)

def subproject(name: String) = Project(
id = name,
base = file(name)
).settings(
scalaVersion := scala213Version,
crossScalaVersions := scalaVersions,
libraryDependencies += "org.scalameta" %% "munit" % "0.7.26" % Test,
sonatypeCredentialHost := "s01.oss.sonatype.org",
sonatypeRepository := "https://s01.oss.sonatype.org/service/local"
)

lazy val managerial = subproject("managerial")

lazy val managerialTwitterUtil =
subproject("managerial-twitter-util")
.dependsOn(managerial)
.settings(
libraryDependencies += "com.twitter" %% "util-core" % "21.5.0" % Provided
)

lazy val root = project
.in(file("."))
.aggregate(
managerial,
managerialTwitterUtil
)
.settings(
name := "managerial",
scalaVersion := scala3Version,
crossScalaVersions := scalaVersions,
Compile / run / fork := true,
libraryDependencies += "org.scalameta" %% "munit" % "0.7.27" % Test,
sonatypeCredentialHost := "s01.oss.sonatype.org",
sonatypeRepository := "https://s01.oss.sonatype.org/service/local"
publish / skip := true
)

ThisBuild / crossScalaVersions := scalaVersions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ca.dvgi.managerial.twitter

import ca.dvgi.{managerial => m}
import com.twitter.{util => tu}

package object util {
implicit class CompatibleManagerialManaged[T](val managed: m.Managed[T]) extends AnyVal {
def asTwitterUtil: tu.Managed[T] = new tu.Managed[T] {
def make() = new tu.Disposable[T] {
val underlying = managed.build()
def get = underlying.get
def dispose(deadline: tu.Time) = tu.Future { underlying.teardown() }
}
}
}

implicit class CompatibleTwitterUtilManaged[T](val managed: tu.Managed[T]) extends AnyVal {
def asManagerial: m.Managed[T] = new m.Managed[T] {
def build() = new m.Resource[T] {
val underlying = managed.make()
def get = underlying.get
def teardown() = tu.Await.result(underlying.dispose())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ca.dvgi.managerial.twitter.util

import ca.dvgi.{managerial => m}
import com.twitter.{util => tu}

class PackageTest extends munit.FunSuite {
test("A Twitter Util Managed can be converted to a Managerial Managed") {
val i = 42
var disposed = false
val tum = new tu.Managed[Int] {
def make() = new tu.Disposable[Int] {
val underlying = i
def get = underlying
def dispose(deadline: tu.Time) = {
disposed = true
tu.Future.value(())
}
}
}

val mm = tum.asManagerial
assert(!disposed)

val r = mm.build()
assert(!disposed)

assertEquals(r.get, i)
assert(!disposed)

r.teardown()
assert(disposed)
}

test("A Managerial Managed can be converted to a Twitter Util Managed") {
val i = 42
var torndown = false
val managed = m.Managed(i)(_ => torndown = true)

val tum = managed.asTwitterUtil
assert(!torndown)

val r = tum.make()
assert(!torndown)

assertEquals(r.get, i)
assert(!torndown)

r.dispose()
assert(torndown)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ object Managed {
/** Creates a [[Managed]] instance that requires both setup and teardown.
* The teardown procedure is provided by an instance of the [[Teardown]] type class.
*
* A type class instance for [[java.lang.AutoCloseable]] is provided in [[ca.dvgi.managerial]].
* A type class instance for java.lang.AutoCloseable is provided in [[ca.dvgi.managerial]].
*/
def from[T](setup: => T)(implicit ev: Teardown[T]): Managed[T] =
apply(setup)(ev.teardown(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ package ca.dvgi
*/
package object managerial {

/** A type class instance describing how to teardown a [[java.lang.AutoCloseable]]
/** A type class instance describing how to teardown a java.lang.AutoCloseable
*/
implicit val autoCloseableTeardown: Teardown[AutoCloseable] = new Teardown[AutoCloseable] {
def teardown(ac: AutoCloseable): Unit = ac.close()
Expand Down

0 comments on commit a77b049

Please sign in to comment.