From eff7696dccf1c3ab28b685a564cdb0dd3ae50631 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Fri, 1 Mar 2024 06:05:06 +0000 Subject: [PATCH 1/5] Update zio to 2.0.21 --- build.sc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sc b/build.sc index 2a3e83a..7999eeb 100644 --- a/build.sc +++ b/build.sc @@ -8,7 +8,7 @@ import com.goyeau.mill.scalafix.ScalafixModule object Versions { val zioLoggingVersion = "2.1.16" - val zioVersion = "2.0.20" + val zioVersion = "2.0.21" val zioJsonVersion = "0.6.2" val zioConfigVersion = "3.0.7" val sttpVersion = "3.9.3" From 595d78e5a7efd8ff858159e39ed2607906630e0b Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Fri, 1 Mar 2024 06:05:09 +0000 Subject: [PATCH 2/5] Update zio-config to 4.0.1 --- build.sc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sc b/build.sc index 7999eeb..36e06f7 100644 --- a/build.sc +++ b/build.sc @@ -10,7 +10,7 @@ object Versions { val zioLoggingVersion = "2.1.16" val zioVersion = "2.0.21" val zioJsonVersion = "0.6.2" - val zioConfigVersion = "3.0.7" + val zioConfigVersion = "4.0.1" val sttpVersion = "3.9.3" val scalafixModuleVersion = "0.6.0" } From dc03432557586520024d11584d0746b23be90923 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Fri, 1 Mar 2024 06:05:12 +0000 Subject: [PATCH 3/5] Update zio-logging to 2.1.17 --- build.sc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sc b/build.sc index 36e06f7..3d7fe41 100644 --- a/build.sc +++ b/build.sc @@ -7,7 +7,7 @@ import $ivy.`com.goyeau::mill-scalafix::0.3.2` import com.goyeau.mill.scalafix.ScalafixModule object Versions { - val zioLoggingVersion = "2.1.16" + val zioLoggingVersion = "2.1.17" val zioVersion = "2.0.21" val zioJsonVersion = "0.6.2" val zioConfigVersion = "4.0.1" From f42294daac396edcdf52be2b2b1c76a31869dab7 Mon Sep 17 00:00:00 2001 From: ex0ns Date: Mon, 22 Apr 2024 23:03:30 +0200 Subject: [PATCH 4/5] fix zio-config --- .../src/com/bot4s/zmatrix/MatrixConfiguration.scala | 12 +++++++----- .../com/bot4s/zmatrix/SyncTokenConfiguration.scala | 13 +++++++------ 2 files changed, 14 insertions(+), 11 deletions(-) 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(())) From 3e9e14c659438681a57135982b0947c9af8543f3 Mon Sep 17 00:00:00 2001 From: ex0ns Date: Wed, 24 Apr 2024 16:54:25 +0200 Subject: [PATCH 5/5] Update readme --- README.md | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) 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