Skip to content

Commit

Permalink
Removing redundant toString (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
irach-ramosh authored Nov 3, 2022
1 parent 87cdb3d commit 8e28eec
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ final case class LiveS3Connector(s3: S3) extends S3Connector {
.foreach[Any, AwsError, CopyObject] { m =>
s3.copyObject(
CopyObjectRequest(
destinationBucket = AwsBucketName(m.targetBucketName.toString),
destinationKey = AwsObjectKey(m.objectKey.toString),
destinationBucket = AwsBucketName(m.targetBucketName),
destinationKey = AwsObjectKey(m.objectKey),
copySource =
CopySource(URLEncoder.encode(s"${m.sourceBucketName}/${m.objectKey}", StandardCharsets.UTF_8.toString))
)
Expand All @@ -40,14 +40,14 @@ final case class LiveS3Connector(s3: S3) extends S3Connector {
override def createBucket(implicit trace: Trace): ZSink[Any, S3Exception, BucketName, BucketName, Unit] =
ZSink
.foreach[Any, AwsError, BucketName] { name =>
s3.createBucket(CreateBucketRequest(bucket = AwsBucketName(name.toString)))
s3.createBucket(CreateBucketRequest(bucket = AwsBucketName(name)))
}
.mapError(a => S3Exception(a.toThrowable))

override def deleteEmptyBucket(implicit trace: Trace): ZSink[Any, S3Exception, BucketName, BucketName, Unit] =
ZSink
.foreach[Any, AwsError, BucketName] { name =>
s3.deleteBucket(DeleteBucketRequest(bucket = AwsBucketName(name.toString)))
s3.deleteBucket(DeleteBucketRequest(bucket = AwsBucketName(name)))
}
.mapError(a => S3Exception(a.toThrowable))

Expand All @@ -58,8 +58,8 @@ final case class LiveS3Connector(s3: S3) extends S3Connector {
.foreachChunk[Any, AwsError, ObjectKey] { objectKeys =>
s3.deleteObjects(
DeleteObjectsRequest(
bucket = AwsBucketName(bucketName.toString),
delete = Delete(objects = objectKeys.map(a => ObjectIdentifier(AwsObjectKey(a.toString))))
bucket = AwsBucketName(bucketName),
delete = Delete(objects = objectKeys.map(a => ObjectIdentifier(AwsObjectKey(a))))
)
)
}
Expand All @@ -70,7 +70,7 @@ final case class LiveS3Connector(s3: S3) extends S3Connector {
): ZStream[Any, S3Exception, Byte] =
ZStream
.unwrap(
s3.getObject(GetObjectRequest(bucket = AwsBucketName(bucketName.toString), key = AwsObjectKey(key.toString)))
s3.getObject(GetObjectRequest(bucket = AwsBucketName(bucketName), key = AwsObjectKey(key)))
.map(a => a.output)
)
.mapError(a => S3Exception(a.toThrowable))
Expand All @@ -89,7 +89,7 @@ final case class LiveS3Connector(s3: S3) extends S3Connector {
): ZStream[Any, S3Exception, ObjectKey] =
ZStream
.fromIterableZIO(
s3.listObjects(ListObjectsRequest(bucket = AwsBucketName(bucketName.toString)))
s3.listObjects(ListObjectsRequest(bucket = AwsBucketName(bucketName)))
.map(_.contents.map(_.flatMap(_.key.toChunk.map(ObjectKey(_)))).getOrElse(Chunk.empty[ObjectKey]))
)
.mapError(a => S3Exception(a.toThrowable))
Expand All @@ -101,15 +101,14 @@ final case class LiveS3Connector(s3: S3) extends S3Connector {
.foreach[Any, AwsError, S3Connector.MoveObject] { m =>
s3.copyObject(
CopyObjectRequest(
destinationBucket = AwsBucketName(m.targetBucketName.toString),
destinationKey = AwsObjectKey(m.targetObjectKey.toString),
copySource =
CopySource(URLEncoder.encode(s"${m.bucketName}/${m.objectKey}", StandardCharsets.UTF_8.toString))
destinationBucket = AwsBucketName(m.targetBucketName),
destinationKey = AwsObjectKey(m.targetObjectKey),
copySource = CopySource(URLEncoder.encode(s"${m.bucketName}/${m.objectKey}", StandardCharsets.UTF_8))
)
) *> s3.deleteObject(
DeleteObjectRequest(
bucket = AwsBucketName(m.bucketName.toString),
key = AwsObjectKey(m.objectKey.toString)
bucket = AwsBucketName(m.bucketName),
key = AwsObjectKey(m.objectKey)
)
)
}
Expand All @@ -122,8 +121,8 @@ final case class LiveS3Connector(s3: S3) extends S3Connector {
.foreachChunk[Any, AwsError, Byte] { content =>
s3.putObject(
request = PutObjectRequest(
bucket = AwsBucketName(bucketName.toString),
key = AwsObjectKey(key.toString),
bucket = AwsBucketName(bucketName),
key = AwsObjectKey(key),
contentLength = Some(ContentLength(content.length.toLong))
),
body = ZStream.fromChunk(content).rechunk(1024)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.connect.s3

import zio.connect.s3.S3Connector.{BucketName, CopyObject, MoveObject, ObjectKey, S3Exception}
import zio.prelude.Newtype
import zio.connect.s3.S3Connector._
import zio.prelude.Subtype
import zio.stream.{ZSink, ZStream}
import zio.{Trace, ZIO}

Expand Down Expand Up @@ -61,10 +61,10 @@ trait S3Connector {

object S3Connector {

object BucketName extends Newtype[String]
object BucketName extends Subtype[String]
type BucketName = BucketName.Type

object ObjectKey extends Newtype[String]
object ObjectKey extends Subtype[String]
type ObjectKey = ObjectKey.Type

case class S3Exception(reason: Throwable)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package zio.connect.s3

import zio.connect.s3.S3Connector.{BucketName, CopyObject, MoveObject, ObjectKey, S3Exception}
import zio.connect.s3.S3Connector._
import zio.connect.s3.TestS3Connector.S3Node.{S3Bucket, S3Obj}
import zio.connect.s3.TestS3Connector.TestS3
import zio.stm.{STM, TRef, ZSTM}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package zio.connect

import zio.Trace
import zio.aws.s3.S3
import zio.connect.s3.S3Connector.{BucketName, CopyObject, ObjectKey, S3Exception}
import zio.stream.{ZSink, ZStream}
import zio.{Trace, ZLayer}

package object s3 {

Expand Down Expand Up @@ -44,8 +45,8 @@ package object s3 {
): ZSink[S3Connector, S3Exception, S3Connector.MoveObject, S3Connector.MoveObject, Unit] =
ZSink.serviceWithSink(_.moveObject)

val s3ConnectorLiveLayer = LiveS3Connector.layer
val s3ConnectorTestLayer = TestS3Connector.layer
val s3ConnectorLiveLayer: ZLayer[S3, Nothing, LiveS3Connector] = LiveS3Connector.layer
val s3ConnectorTestLayer: ZLayer[Any, Nothing, TestS3Connector] = TestS3Connector.layer

def putObject(bucketName: => BucketName, key: ObjectKey)(implicit
trace: Trace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.testcontainers.utility.DockerImageName
import software.amazon.awssdk.auth.credentials.{AwsBasicCredentials, StaticCredentialsProvider}
import software.amazon.awssdk.regions.Region
import zio.aws.core.config.AwsConfig
import zio.aws.core.httpclient.HttpClient
import zio.aws.netty.NettyHttpClient
import zio.aws.s3.S3
import zio.{Scope, ZIO, ZLayer}
Expand All @@ -19,8 +20,8 @@ object LiveS3ConnectorSpec extends S3ConnectorSpec {
zio.connect.s3.s3ConnectorLiveLayer
)

lazy val httpClient = NettyHttpClient.default
lazy val awsConfig: ZLayer[Any, Throwable, AwsConfig] = httpClient >>> AwsConfig.default
lazy val httpClient: ZLayer[Any, Throwable, HttpClient] = NettyHttpClient.default
lazy val awsConfig: ZLayer[Any, Throwable, AwsConfig] = httpClient >>> AwsConfig.default

lazy val localStackContainer: ZLayer[Scope, Throwable, LocalStackContainer] =
ZLayer.fromZIO(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait S3ConnectorSpec extends ZIOSpecDefault {
_ <- ZStream.fromChunk(content2) >>> putObject(bucket2, key)
_ <- ZStream(S3Connector.CopyObject(bucket1, key, bucket2)) >>> copyObject
copiedContent <- getObject(bucket2, key).runCollect
} yield assert(copiedContent)(equalTo(content1))
} yield assertTrue(copiedContent == content1)
},
test("succeeds") {
val bucket1 = BucketName(UUID.randomUUID().toString)
Expand Down Expand Up @@ -66,7 +66,7 @@ trait S3ConnectorSpec extends ZIOSpecDefault {
objectsBefore <- listObjects(bucketName).runCollect
_ <- ZStream(bucketName) >>> createBucket
objectsAfter <- listObjects(bucketName).runCollect
} yield assert(objectsBefore)(equalTo(objectsAfter))
} yield assertTrue(objectsBefore == objectsAfter)
},
test("succeeds") {
val bucketName = BucketName(UUID.randomUUID().toString)
Expand Down Expand Up @@ -170,7 +170,7 @@ trait S3ConnectorSpec extends ZIOSpecDefault {
actual <- listObjects(bucketName).runCollect
_ <- ZStream.fromChunk(Chunk(obj1, obj2)) >>> deleteObjects(bucketName)
afterObjectDeletion <- listObjects(bucketName).runCollect
} yield assert(actual.sortBy(_.toString))(equalTo(Chunk(obj1, obj2).sortBy(_.toString))) && assertTrue(
} yield assertTrue(actual.sortBy(_.toString) == Chunk(obj1, obj2).sortBy(_.toString)) && assertTrue(
afterObjectDeletion.isEmpty
)
}
Expand All @@ -190,7 +190,7 @@ trait S3ConnectorSpec extends ZIOSpecDefault {
_ <- ZStream.fromChunk(content2) >>> putObject(bucket2, key)
_ <- ZStream(S3Connector.MoveObject(bucket1, key, bucket2, key)) >>> moveObject
copiedContent <- getObject(bucket2, key).runCollect
} yield assert(copiedContent)(equalTo(content1))
} yield assertTrue(copiedContent == content1)
},
test("succeeds") {

Expand All @@ -217,10 +217,10 @@ trait S3ConnectorSpec extends ZIOSpecDefault {
b1Objects <- listObjects(bucket1).runCollect
b2Objects <- listObjects(bucket2).runCollect

} yield assert(initialB1Objects.sortBy(_.toString))(equalTo(Chunk(key1, key2, key3).sortBy(_.toString))) &&
} yield assertTrue(initialB1Objects.sortBy(_.toString) == Chunk(key1, key2, key3).sortBy(_.toString)) &&
assertTrue(initialB2Objects.isEmpty) &&
assert(b1Objects)(equalTo(Chunk(key1))) &&
assert(b2Objects.sortBy(_.toString))(equalTo(Chunk(key1, key2, key4).sortBy(_.toString)))
assertTrue(b1Objects == Chunk(key1)) &&
assertTrue(b2Objects.sortBy(_.toString) == Chunk(key1, key2, key4).sortBy(_.toString))

}
)
Expand All @@ -235,7 +235,7 @@ trait S3ConnectorSpec extends ZIOSpecDefault {
testData <- Random.nextBytes(5)
_ <- ZStream.fromChunk(testData) >>> putObject(bucketName, objectKey)
actual <- getObject(bucketName, objectKey).runCollect
} yield assert(actual)(equalTo(testData))
} yield assertTrue(actual == testData)
}
)

Expand Down

0 comments on commit 8e28eec

Please sign in to comment.