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

Scala3 finishing touch #192

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Installation

Add the following dependency to your project's build file

For Scala 2.12.x and 2.13.x
For Scala 2.12.x, 2.13.x and 3.0.0

```scala
"com.github.dapperware" %% "zio-slack-api-web" % "0.9.5"
Expand Down Expand Up @@ -94,4 +94,18 @@ object JokeApp extends App {
}
```

Caveat
--

With Scala 3.0.0, you might encounter a run time error similar to
```
Fiber failed.
An unchecked error was produced.
java.lang.Error: Defect in zio.Has: Set({{Has[=Token] & Has[=package$::SlackClient$::Service]} & Has[=package$::SlackRealtimeClient$::Service]}) statically known to be contained within the environment are missing
at zio.Has$HasSyntax$.prune$extension(Has.scala:198)
at zio.Has$HasSyntax$.union$extension(Has.scala:210)
at zio.Has$$anon$2.union(Has.scala:92)
at zio.Has$$anon$2.union(Has.scala:91)

```
The workaround for this is to use the environments explictely instead of using the type aliases. Replace `AccessToken` with `Has[Token]`, `ClientSecret` with `Has[ClientSecretToken]` and `SlackRealtimeClient` with `Has[SlackRealtimeClient.Service]`. The programs in the `examples` module compile and run with Scala 3.0.0.
8 changes: 4 additions & 4 deletions examples/src/main/scala/basic/BasicApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import common.{ accessToken, default, Basic, BasicConfig }
import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import zio.console.{ putStrLn, Console }
import zio.stream.ZStream
import zio.{ App, ExitCode, Layer, ZIO, ZManaged }
import com.github.dapperware.slack.AccessToken
import zio.{ App, ExitCode, Layer, ZIO, ZManaged, Has }
import com.github.dapperware.slack.{AccessToken, Token}

object BasicApp extends App {

Expand All @@ -19,12 +19,12 @@ object BasicApp extends App {
val slackClients: Layer[Throwable, SlackClient with SlackRealtimeClient] =
AsyncHttpClientZioBackend.layer() >>> (SlackClient.live ++ SlackRealtimeClient.live)

val layers: Layer[Throwable, SlackEnv with SlackRealtimeEnv with Basic] =
val layer: Layer[Throwable, Has[SlackClient.Service] with Has[Token] with Has[SlackRealtimeClient.Service] with Basic] =
slackClients ++ accessTokenAndBasic

override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, ExitCode] =
(for {
resp <- (testApi.toManaged_ <&> testRealtime).provideCustomLayer(layers)
resp <- (testApi.toManaged_ <&> testRealtime).provideCustomLayer(layer)
} yield resp).use_(ZIO.unit).exitCode

val testRealtime: ZManaged[SlackRealtimeEnv with Basic with Console, SlackError, Unit] =
Expand Down
10 changes: 5 additions & 5 deletions examples/src/main/scala/chat/ChatApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import com.github.dapperware.slack.api.web.{ getConversationInfo, getUserInfo }
import com.github.dapperware.slack.client.SlackClient
import com.github.dapperware.slack.realtime.SlackRealtimeClient
import com.github.dapperware.slack.realtime.models.{ Message, SendMessage }
import com.github.dapperware.slack.{ realtime, AccessToken }
import com.github.dapperware.slack.realtime.{ SlackRealtimeClient, SlackRealtimeEnv }
import com.github.dapperware.slack.realtime
import common.{ accessToken, default, Basic, BasicConfig }
import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import zio._
import zio.console._
import zio.stream.ZStream
import com.github.dapperware.slack.Token

/**
* A simple interactive application to show how to use slack zio for much profit
Expand All @@ -34,12 +34,12 @@ object ChatApp extends App {
t.replaceAllLiterally(s"<@$ref>", s"@$replace")
}

val accessTokenAndBasic: Layer[Throwable, AccessToken with Basic] = default >+> accessToken.toLayer
val accessTokenAndBasic: Layer[Throwable, Has[Token] with Basic] = default >+> accessToken.toLayer

val slackClients: Layer[Throwable, SlackClient with SlackRealtimeClient] =
val slackClients: Layer[Throwable, Has[SlackClient.Service] with Has[SlackRealtimeClient.Service]] =
AsyncHttpClientZioBackend.layer() >>> (SlackClient.live ++ SlackRealtimeClient.live)

private val layers: ZLayer[Any, Throwable, SlackRealtimeEnv with Basic] =
private val layers: ZLayer[Any, Throwable, Has[SlackClient.Service] with Has[SlackRealtimeClient.Service] with Has[Token] with Basic] =
slackClients ++ accessTokenAndBasic

override def run(args: List[String]): ZIO[zio.ZEnv, Nothing, ExitCode] = {
Expand Down
1 change: 1 addition & 0 deletions examples/src/main/scala/joke/JokeApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import zio.clock.Clock
import zio.duration._
import zio.random.Random
import zio.stream.ZStream
import com.github.dapperware.slack.Token

/**
* Every 3 hours, randomly pick a channel that the bot is part of and send a chuck norris joke to it.
Expand Down
8 changes: 4 additions & 4 deletions examples/src/main/scala/search/SearchApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import common.{ accessToken, default, Basic }
import sttp.client3.asynchttpclient.zio.AsyncHttpClientZioBackend
import zio.console._
import zio.stream.{ ZSink, ZStream }
import zio.{ App, ExitCode, ZIO, Layer}
import com.github.dapperware.slack.AccessToken
import zio.{ App, ExitCode, ZIO, Layer, Has}
import com.github.dapperware.slack.Token

object SearchApp extends App {

val accessTokenAndBasic: Layer[Throwable, AccessToken with Basic] = default >+> accessToken.toLayer
val accessTokenAndBasic: Layer[Throwable, Has[Token] with Basic] = default >+> accessToken.toLayer

val slackClients: Layer[Throwable, SlackClient with SlackRealtimeClient] = AsyncHttpClientZioBackend.layer() >>> (SlackClient.live ++ SlackRealtimeClient.live)
val slackClients: Layer[Throwable, Has[SlackClient.Service] with Has[SlackRealtimeClient.Service]] = AsyncHttpClientZioBackend.layer() >>> (SlackClient.live ++ SlackRealtimeClient.live)

val layers = slackClients ++ accessTokenAndBasic

Expand Down