Skip to content

Commit

Permalink
Merge pull request #48 from geirolz/refactoring_pure_config_module
Browse files Browse the repository at this point in the history
Refactoring pureconfig module
  • Loading branch information
geirolz committed Jul 2, 2023
2 parents 84bd5c3 + 80101a8 commit 1c56624
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 42 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ libraryDependencies += "com.github.geirolz" %% "toolkit-pureconfig" % "0.0.8"
Import the syntax

```scala
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
import com.geirolz.app.toolkit.config.pureconfig.*
```

Which allows you to use `withPureConfigLoader` to load the config from a `ConfigSource.default`
Which allows you to use `withConfigLoader` with `pureconfigLoader[F, CONF]` to load the config from
a `ConfigSource.default` or other sources

```scala
import cats.Show
import cats.effect.IO
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
import com.geirolz.app.toolkit.config.pureconfig.*

case class TestConfig(value: String)

Expand All @@ -138,7 +139,7 @@ App[IO]
sbtVersion = "1.8.0"
)
)
.withPureConfigLoader[TestConfig]
.withConfigLoader(pureconfigLoader[IO, TestConfig])
.withoutDependencies
.provideOne(_ => IO.unit)
.run_
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/scala/com/geirolz/app/toolkit/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,12 @@ object App extends AppSyntax {
def withConfigLoader[CONFIG2: Show](
configF: APP_INFO => F[CONFIG2]
): AppBuilderSelectResAndDeps[F, FAILURE, APP_INFO, LOGGER_T, CONFIG2, RESOURCES] =
copyWith(configLoader = configF(appInfo))
withConfigLoader(configF(appInfo))

def withConfigLoader[CONFIG2: Show](
configF: F[CONFIG2]
): AppBuilderSelectResAndDeps[F, FAILURE, APP_INFO, LOGGER_T, CONFIG2, RESOURCES] =
copyWith(configLoader = configF)

// ------- RESOURCES -------
def withoutResources: AppBuilderSelectResAndDeps[F, FAILURE, APP_INFO, LOGGER_T, CONFIG, NoResources] =
Expand Down
9 changes: 5 additions & 4 deletions docs/compiled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ libraryDependencies += "com.github.geirolz" %% "toolkit-pureconfig" % "0.0.8"
Import the syntax

```scala
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
import com.geirolz.app.toolkit.config.pureconfig.*
```

Which allows you to use `withPureConfigLoader` to load the config from a `ConfigSource.default`
Which allows you to use `withConfigLoader` with `pureconfigLoader[F, CONF]` to load the config from
a `ConfigSource.default` or other sources

```scala
import cats.Show
import cats.effect.IO
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
import com.geirolz.app.toolkit.config.pureconfig.*

case class TestConfig(value: String)

Expand All @@ -138,7 +139,7 @@ App[IO]
sbtVersion = "1.8.0"
)
)
.withPureConfigLoader[TestConfig]
.withConfigLoader(pureconfigLoader[IO, TestConfig])
.withoutDependencies
.provideOne(_ => IO.unit)
.run_
Expand Down
9 changes: 5 additions & 4 deletions docs/source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,17 @@ libraryDependencies += "com.github.geirolz" %% "toolkit-pureconfig" % "@VERSION@
Import the syntax

```scala mdoc:silent:reset:warn
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
import com.geirolz.app.toolkit.config.pureconfig.*
```

Which allows you to use `withPureConfigLoader` to load the config from a `ConfigSource.default`
Which allows you to use `withConfigLoader` with `pureconfigLoader[F, CONF]` to load the config from
a `ConfigSource.default` or other sources

```scala mdoc:silent:reset
import cats.Show
import cats.effect.IO
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
import com.geirolz.app.toolkit.config.pureconfig.*

case class TestConfig(value: String)

Expand All @@ -138,7 +139,7 @@ App[IO]
sbtVersion = "1.8.0"
)
)
.withPureConfigLoader[TestConfig]
.withConfigLoader(pureconfigLoader[IO, TestConfig])
.withoutDependencies
.provideOne(_ => IO.unit)
.run_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.geirolz.example.app

import cats.effect.{ExitCode, IO, IOApp}
import com.geirolz.app.toolkit.App
import com.geirolz.app.toolkit.config.pureconfig.syntax.AppResourcesLoaderOps
import com.geirolz.app.toolkit.config.pureconfig.pureconfigLoader
import com.geirolz.example.app.provided.AppHttpServer
import org.typelevel.log4cats.slf4j.Slf4jLogger

Expand All @@ -12,7 +12,7 @@ object AppMain extends IOApp {
App[IO]
.withInfo(AppInfo.fromBuildInfo)
.withLogger(Slf4jLogger.getLogger[IO])
.withPureConfigLoader[AppConfig]
.withConfigLoader(pureconfigLoader[IO, AppConfig])
.dependsOn(AppDependencyServices.resource(_))
.beforeProviding(_.logger.info("CUSTOM PRE-RUN"))
.provide(deps =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.geirolz.example.app

import cats.effect.{ExitCode, IO, IOApp}
import com.geirolz.app.toolkit.App
import com.geirolz.app.toolkit.config.pureconfig.pureconfigLoader
import com.geirolz.app.toolkit.config.pureconfig.syntax.AppResourcesLoaderOps
import com.geirolz.example.app.provided.AppHttpServer
import org.typelevel.log4cats.slf4j.Slf4jLogger
Expand All @@ -12,7 +13,7 @@ object AppWithFailures extends IOApp {
App[IO, AppError]
.withInfo(AppInfo.fromBuildInfo)
.withLogger(Slf4jLogger.getLogger[IO])
.withPureConfigLoader[AppConfig]
.withConfigLoader(pureconfigLoader[IO, AppConfig])
.dependsOn(AppDependencyServices.resource(_))
.beforeProviding(_.logger.info("CUSTOM PRE-RUN"))
.provide(deps =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.geirolz.example.app
import cats.effect.{ExitCode, IO, IOApp}
import com.geirolz.app.toolkit.App
import com.geirolz.app.toolkit.logger.log4CatsLoggerAdapter
import com.geirolz.app.toolkit.config.pureconfig.syntax.*
import com.geirolz.app.toolkit.config.pureconfig.*
import com.geirolz.example.app.provided.AppHttpServer
import org.typelevel.log4cats.slf4j.Slf4jLogger

Expand All @@ -12,7 +12,7 @@ object AppMain extends IOApp:
App[IO]
.withInfo(AppInfo.fromBuildInfo)
.withLogger(Slf4jLogger.getLogger[IO])
.withPureConfigLoader[AppConfig]
.withConfigLoader(pureconfigLoader[IO, AppConfig])
.dependsOn(AppDependencyServices.resource(_))
.provide(deps =>
List(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.geirolz.app.toolkit.config

import _root_.pureconfig.{ConfigObjectSource, ConfigReader, ConfigSource}
import cats.effect.Async

import scala.reflect.ClassTag

package object pureconfig {

def pureconfigLoader[F[_]: Async, PURE_CONFIG: ClassTag: ConfigReader]: F[PURE_CONFIG] =
pureconfigLoader(_.default)

def pureconfigLoader[F[_]: Async, PURE_CONFIG: ClassTag: ConfigReader](f: ConfigSource.type => ConfigObjectSource): F[PURE_CONFIG] =
pureconfigLoader(f(ConfigSource))

def pureconfigLoader[F[_]: Async, PURE_CONFIG: ClassTag: ConfigReader](appSource: ConfigObjectSource): F[PURE_CONFIG] =
Async[F].delay(appSource.loadOrThrow[PURE_CONFIG])

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.geirolz.app.toolkit.config
import _root_.pureconfig.ConfigReader
import _root_.pureconfig.backend.ConfigFactoryWrapper
import cats.effect.IO
import com.geirolz.app.toolkit.config.pureconfig.syntax.AppResourcesLoaderOps
import com.geirolz.app.toolkit.config.pureconfig.pureconfigLoader
import com.geirolz.app.toolkit.config.testing.TestConfig
import com.geirolz.app.toolkit.{App, SimpleAppInfo}
import com.typesafe.config.Config
Expand All @@ -21,7 +21,7 @@ class PureconfigSecretSupportSuite extends munit.CatsEffectSuite {
sbtVersion = "1.8.0"
)
)
.withPureConfigLoader[TestConfig]
.withConfigLoader(pureconfigLoader[IO, TestConfig])
.withoutDependencies
.provideOne(_ => IO.unit)
.run_
Expand Down

0 comments on commit 1c56624

Please sign in to comment.