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

remove unneeded Scope requirement in FtpAccessors #353

Merged
merged 8 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
if: ${{ github.event_name == 'pull_request' }}
steps:
- name: Git Checkout
uses: actions/checkout@v3.5.1
uses: actions/checkout@v3.3.0
mberndt123 marked this conversation as resolved.
Show resolved Hide resolved
with:
fetch-depth: '0'
- name: Setup Scala
Expand All @@ -41,7 +41,7 @@ jobs:
if: ${{ ((github.event_name == 'release') && (github.event.action == 'published')) || (github.event_name == 'workflow_dispatch') }}
steps:
- name: Git Checkout
uses: actions/checkout@v3.5.1
uses: actions/checkout@v3.3.0
with:
fetch-depth: '0'
- name: Setup Scala
Expand All @@ -65,7 +65,7 @@ jobs:
if: ${{ (github.event_name == 'push') || ((github.event_name == 'release') && (github.event_name == 'published')) }}
steps:
- name: Git Checkout
uses: actions/checkout@v3.5.1
uses: actions/checkout@v3.3.0
with:
ref: ${{ github.head_ref }}
fetch-depth: '0'
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
In order to use this library, we need to add the following line in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-ftp" % "0.4.0"
libraryDependencies += "dev.zio" %% "zio-ftp" % "0.4.1"
```

## How to use it?
Expand Down Expand Up @@ -78,7 +78,7 @@ object ZIOFTPExample extends ZIOAppDefault {
private val settings =
UnsecureFtpSettings("127.0.0.1", 21, FtpCredentials("one", "1234"))

private val myApp: ZIO[Scope with Ftp, IOException, Unit] =
private val myApp: ZIO[Ftp, IOException, Unit] =
for {
_ <- Console.printLine("List of files at root directory:")
resource <- ls("/").runCollect
Expand All @@ -94,7 +94,7 @@ object ZIOFTPExample extends ZIOAppDefault {
.via(ZPipeline.utf8Decode)
.runCollect
_ <- Console.printLine(s"Content of $path file:")
_ <- Console.printLine(file.fold("")(_ + _))
_ <- Console.printLine(file.mkString)
} yield ()

override def run = myApp.provideSomeLayer(unsecure(settings))
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ object ZIOFTPExample extends ZIOAppDefault {
private val settings =
UnsecureFtpSettings("127.0.0.1", 21, FtpCredentials("one", "1234"))

private val myApp: ZIO[Scope with Ftp, IOException, Unit] =
private val myApp: ZIO[Ftp, IOException, Unit] =
for {
_ <- Console.printLine("List of files at root directory:")
resource <- ls("/").runCollect
Expand All @@ -93,7 +93,7 @@ object ZIOFTPExample extends ZIOAppDefault {
.via(ZPipeline.utf8Decode)
.runCollect
_ <- Console.printLine(s"Content of $path file:")
_ <- Console.printLine(file.fold("")(_ + _))
_ <- Console.printLine(file.mkString)
} yield ()

override def run = myApp.provideSomeLayer(unsecure(settings))
Expand Down
4 changes: 2 additions & 2 deletions zio-ftp/src/main/scala/zio/ftp/FtpAccessors.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.ftp

import java.io.IOException
import zio.{ Scope, ZIO }
import zio.ZIO
import zio.stream.ZStream

trait FtpAccessors[+A] {
Expand Down Expand Up @@ -73,6 +73,6 @@ trait FtpAccessors[+A] {
* @param source data stream to store
* @tparam R environment of the specified stream source, required to extend Blocking
*/
def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R with Scope, IOException, Unit]
def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R, IOException, Unit]

}
24 changes: 13 additions & 11 deletions zio-ftp/src/main/scala/zio/ftp/TestFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import java.nio.file.attribute.PosixFilePermission
import zio.nio.file.{ Path => ZPath }
import zio.nio.file.Files
import zio.stream.{ ZSink, ZStream }
import zio.{ Cause, Scope, ZIO }
import zio.{ Cause, ZIO }

object TestFtp {

Expand Down Expand Up @@ -98,18 +98,20 @@ object TestFtp {
override def upload[R](
path: String,
source: ZStream[R, Throwable, Byte]
): ZIO[R with Scope, IOException, Unit] = {
): ZIO[R, IOException, Unit] = {
val file = (root / ZPath(path).elements.mkString("/")).toFile

ZIO
.fromAutoCloseable(ZIO.attempt(new FileOutputStream(file)))
.flatMap { out =>
source
.run(ZSink.fromOutputStream(out))
.unit
}
.refineToOrDie[IOException]
.catchAll(err => ZIO.fail(new IOException(s"Path is invalid. Cannot upload data to : $path", err)))
ZIO.scoped[R] {
ZIO
.fromAutoCloseable(ZIO.attempt(new FileOutputStream(file)))
.flatMap { out =>
source
.run(ZSink.fromOutputStream(out))
.unit
}
.refineToOrDie[IOException]
.catchAll(err => ZIO.fail(new IOException(s"Path is invalid. Cannot upload data to : $path", err)))
}
}
}
}
18 changes: 10 additions & 8 deletions zio-ftp/src/main/scala/zio/ftp/UnsecureFtp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ final private class UnsecureFtp(unsafeClient: Client) extends FtpAccessors[Clien
ZStream(FtpResource.fromFtpFile(f, Some(path)))
}

def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R with Scope, IOException, Unit] =
source.toInputStream
.mapError(new IOException(_))
.flatMap(is =>
execute(_.storeFile(path, is))
.filterOrFail(identity)(InvalidPathError(s"Path is invalid. Cannot upload data to : $path"))
.unit
)
def upload[R](path: String, source: ZStream[R, Throwable, Byte]): ZIO[R, IOException, Unit] =
ZIO.scoped[R] {
source.toInputStream
.mapError(new IOException(_))
.flatMap(is =>
execute(_.storeFile(path, is))
.filterOrFail(identity)(InvalidPathError(s"Path is invalid. Cannot upload data to : $path"))
.unit
)
}

override def execute[T](f: Client => T): ZIO[Any, IOException, T] =
attemptBlockingIO(f(unsafeClient))
Expand Down
14 changes: 7 additions & 7 deletions zio-ftp/src/main/scala/zio/ftp/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ package object ftp {
def upload[R](
path: String,
source: ZStream[R, Throwable, Byte]
): ZIO[R & Scope with Ftp, IOException, Unit] =
): ZIO[R with Ftp, IOException, Unit] =
for {
ftp <- ZIO.service[Ftp]
_ <- ftp.upload(path, source)
Expand Down Expand Up @@ -91,7 +91,7 @@ package object ftp {
def upload[R](
path: String,
source: ZStream[R, Throwable, Byte]
): ZIO[SFtp with R with Scope, IOException, Unit] =
): ZIO[SFtp with R, IOException, Unit] =
for {
ftp <- ZIO.service[SFtp]
_ <- ftp.upload(path, source)
Expand Down Expand Up @@ -127,7 +127,7 @@ package object ftp {
def upload[R](
path: String,
source: ZStream[R, Throwable, Byte]
): ZIO[StubFtp with R with Scope, IOException, Unit] =
): ZIO[StubFtp with R, IOException, Unit] =
for {
ftp <- ZIO.service[StubFtp]
_ <- ftp.upload(path, source)
Expand All @@ -137,11 +137,11 @@ package object ftp {
ZStream.serviceWithStream(_.readFile(path, chunkSize))
}

def unsecure(settings: UnsecureFtpSettings): ZLayer[Scope, ConnectionError, Ftp] =
ZLayer.fromZIO(UnsecureFtp.connect(settings))
def unsecure(settings: UnsecureFtpSettings): ZLayer[Any, ConnectionError, Ftp] =
ZLayer.scoped(UnsecureFtp.connect(settings))

def secure(settings: SecureFtpSettings): ZLayer[Scope, ConnectionError, SFtp] =
ZLayer.fromZIO(SecureFtp.connect(settings))
def secure(settings: SecureFtpSettings): ZLayer[Any, ConnectionError, SFtp] =
ZLayer.scoped(SecureFtp.connect(settings))

def stub(path: ZPath): Layer[Any, StubFtp] =
ZLayer.succeed(TestFtp.create(path))
Expand Down