diff --git a/README.md b/README.md index 6cefcb0..8b7b5ec 100644 --- a/README.md +++ b/README.md @@ -89,25 +89,18 @@ val mainLoop = mirrorEcho The `updateState` method is a key piece here. Matrix API rely heavily on pagination to keep track of the state, it will by default return the whole state for a client. By calling `updateState` we make sure to save our current pagination and only ask for newer events to the API. -The last step is to provide and environment to this effect, we are using the amazing [zio-magic](https://github.com/kitlangton/zio-magic) lib to help us with -the `ZLayer` configuration: - ```scala -val loggingLayer = Logging.console( - logLevel = LogLevel.Info, - format = LogFormat.ColoredLogFormat() -) >>> Logging.withRootLoggerName("matrix-zio-sync") - -mainLoop.inject( - ZEnv.live, - loggingLayer, - MatrixConfiguration.persistent(), // will read/write the configuration from a `bot.conf` file in the project's resources - Authentication.live, // A HTTP middleware that will take care of creating/checking validity of credential/token - AsyncHttpClientZioBackend.layer(), // STTP zio backend to perform HTTP queries - MatrixClient.live // The actual implementation of the client -) -.retry(Schedule.forever) -.exitCode +mainLoop + .withAutoRefresh.retry(Schedule.forever) + .exitCode + .provide( + SyncTokenConfiguration.persistent(), + MatrixConfiguration.live(), + Authentication.live, + HttpClientZioBackend.layer(): TaskLayer[SttpBackend[Task, Any]], + MatrixClient.live, + Matrix.make + ) ``` ## Examples diff --git a/build.sc b/build.sc index e7e6beb..291df66 100644 --- a/build.sc +++ b/build.sc @@ -7,10 +7,10 @@ import $ivy.`com.goyeau::mill-scalafix::0.3.2` import com.goyeau.mill.scalafix.ScalafixModule object Versions { - val zioLoggingVersion = "2.1.16" - val zioVersion = "2.0.20" + val zioLoggingVersion = "2.1.17" + val zioVersion = "2.0.21" val zioJsonVersion = "0.6.2" - val zioConfigVersion = "3.0.7" + val zioConfigVersion = "4.0.1" val sttpVersion = "3.9.5" val scalafixModuleVersion = "0.6.0" } diff --git a/core/src/com/bot4s/zmatrix/MatrixConfiguration.scala b/core/src/com/bot4s/zmatrix/MatrixConfiguration.scala index f456f3f..1bfd6d8 100644 --- a/core/src/com/bot4s/zmatrix/MatrixConfiguration.scala +++ b/core/src/com/bot4s/zmatrix/MatrixConfiguration.scala @@ -32,7 +32,7 @@ object MatrixConfiguration { val DEFAULT_API_VERSION = "v3" val DEFAULT_CONFIG_FILE = "bot.conf" - val configReader = descriptor[MatrixConfiguration] + val configReader = deriveConfig[MatrixConfiguration] def from(filename: String): Task[MatrixConfiguration] = fromFile(filename) @@ -42,11 +42,13 @@ object MatrixConfiguration { ConfigParseOptions.defaults.setAllowMissing(false) private def fromHoconFile(url: URL) = - read( - configReader from ConfigSource.fromTypesafeConfig( - ZIO.attempt(ConfigFactory.parseURL(url, strictSettings.setClassLoader(null))) + ZIO + .attempt(ConfigFactory.parseURL(url, strictSettings.setClassLoader(null))) + .flatMap(config => + read( + configReader from ConfigProvider.fromTypesafeConfig(config) + ) ) - ) private def fromResource(filename: String) = { val adapted = if (filename.startsWith("/")) filename else s"/$filename" diff --git a/core/src/com/bot4s/zmatrix/SyncTokenConfiguration.scala b/core/src/com/bot4s/zmatrix/SyncTokenConfiguration.scala index babf851..25318af 100644 --- a/core/src/com/bot4s/zmatrix/SyncTokenConfiguration.scala +++ b/core/src/com/bot4s/zmatrix/SyncTokenConfiguration.scala @@ -22,12 +22,12 @@ object SyncTokenConfiguration { def get: URIO[SyncTokenConfiguration, SyncToken] = ZIO.serviceWithZIO(_.get) def set(config: SyncToken): URIO[SyncTokenConfiguration, Unit] = ZIO.serviceWithZIO(_.set(config)) - val configReader = descriptor[SyncToken] + val configReader = deriveConfig[SyncToken] private def refFromFile(filename: String): Task[Ref[SyncToken]] = for { file <- ZIO.attempt(new File(filename)) - source = ConfigSource.fromHoconFile(file) + source = ConfigProvider.fromHoconFile(file) config <- read(configReader from source) result <- Ref.make(config) } yield result @@ -73,13 +73,14 @@ object SyncTokenConfiguration { override def set(config: SyncToken): UIO[Unit] = { val updateConf = for { - _ <- configRef.set(config) - file <- ZIO.attempt(new File(filename)) - content <- ZIO.fromEither(write(configReader, config)) + _ <- configRef.set(config) + file <- ZIO.attempt(new File(filename)) + // zio-config 4.X removed the ability to write a config + content = s"""since="${config.since.mkString}"""" _ <- ZIO.acquireReleaseWith(ZIO.attempt(new BufferedWriter(new FileWriter(file))))(bw => ZIO.succeed(bw.close) - )(c => ZIO.attempt(c.write(content.toHoconString))) + )(c => ZIO.attempt(c.write(content))) } yield () updateConf.catchAll(_ => ZIO.succeed(()))