Skip to content

Commit

Permalink
Add more integration with cats-effect
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed Jul 12, 2024
1 parent 1652f54 commit 452dd47
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Please, drop a ⭐️ if you are interested in this project and you want to supp
> Scala 3 only, Scala 2 is not supported.
```sbt
libraryDependencies += "com.github.geirolz" %% "secret" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret" % "0.0.11"
```

## Obfuscation
Expand Down Expand Up @@ -63,19 +63,28 @@ These integrations aim to enhance the functionality and capabilities of `Secret`

#### Cats Effect
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-effect" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-effect" % "0.0.11"
```

```scala
import com.geirolz.secret.*
import cats.effect.{IO, Resource}

val res: Resource[IO, String] = Secret("password").resource[IO]
val s: Secret[String] = Secret("password")

// !!!! this will not destroy the secret because it uses a duplicated one !!!
val res: Resource[IO, String] = s.resource[IO]

// this will destroy the secret because it uses the original one
val res2: Resource[IO, String] = s.resourceDestroy[IO]

// this will destroy the secret because it uses the original one
val res3 = Secret.resource[IO, String]("password")
```

#### Pureconfig
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-pureconfig" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-pureconfig" % "0.0.11"
```

Just provides the `ConfigReader` instance for `Secret[T]` type.
Expand All @@ -85,15 +94,15 @@ import com.geirolz.secret.pureconfig.given
```
#### Typesafe Config
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-typesafe-config" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-typesafe-config" % "0.0.11"
```
```scala
import com.geirolz.secret.typesafe.config.given
```

#### Ciris
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-ciris" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-ciris" % "0.0.11"
```
```scala
import com.geirolz.secret.ciris.given
Expand All @@ -103,7 +112,7 @@ import com.geirolz.secret.ciris.given
Provides the json `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-circe" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-circe" % "0.0.11"
```
```scala
import com.geirolz.secret.circe.given
Expand All @@ -113,7 +122,7 @@ import com.geirolz.secret.circe.given
Provides the xml `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-cats-xml" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-cats-xml" % "0.0.11"
```
```scala
import com.geirolz.secret.catsxml.given
Expand Down Expand Up @@ -164,11 +173,11 @@ val myCustomAlgebra = new SecretStrategyAlgebra:

final def deObfuscator[P](f: PlainValueBuffer => P): DeObfuscator[P] =
DeObfuscator.of { bufferTuple => f(bufferTuple.roObfuscatedBuffer) }
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp10$$anon$6@204ae769
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp10$$anon$9@d2f29c8

// build factory based on the algebra
val myCustomStrategyFactory = myCustomAlgebra.newFactory
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@24689809
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@5a05cd05

// ----------------------------- USAGE -----------------------------
// implicitly in the scope
Expand Down
29 changes: 19 additions & 10 deletions docs/compiled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Please, drop a ⭐️ if you are interested in this project and you want to supp
> Scala 3 only, Scala 2 is not supported.
```sbt
libraryDependencies += "com.github.geirolz" %% "secret" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret" % "0.0.11"
```

## Obfuscation
Expand Down Expand Up @@ -63,19 +63,28 @@ These integrations aim to enhance the functionality and capabilities of `Secret`

#### Cats Effect
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-effect" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-effect" % "0.0.11"
```

```scala
import com.geirolz.secret.*
import cats.effect.{IO, Resource}

val res: Resource[IO, String] = Secret("password").resource[IO]
val s: Secret[String] = Secret("password")

// !!!! this will not destroy the secret because it uses a duplicated one !!!
val res: Resource[IO, String] = s.resource[IO]

// this will destroy the secret because it uses the original one
val res2: Resource[IO, String] = s.resourceDestroy[IO]

// this will destroy the secret because it uses the original one
val res3 = Secret.resource[IO, String]("password")
```

#### Pureconfig
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-pureconfig" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-pureconfig" % "0.0.11"
```

Just provides the `ConfigReader` instance for `Secret[T]` type.
Expand All @@ -85,15 +94,15 @@ import com.geirolz.secret.pureconfig.given
```
#### Typesafe Config
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-typesafe-config" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-typesafe-config" % "0.0.11"
```
```scala
import com.geirolz.secret.typesafe.config.given
```

#### Ciris
```sbt
libraryDependencies += "com.github.geirolz" %% "secret-ciris" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-ciris" % "0.0.11"
```
```scala
import com.geirolz.secret.ciris.given
Expand All @@ -103,7 +112,7 @@ import com.geirolz.secret.ciris.given
Provides the json `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-circe" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-circe" % "0.0.11"
```
```scala
import com.geirolz.secret.circe.given
Expand All @@ -113,7 +122,7 @@ import com.geirolz.secret.circe.given
Provides the xml `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-cats-xml" % "0.0.10"
libraryDependencies += "com.github.geirolz" %% "secret-cats-xml" % "0.0.11"
```
```scala
import com.geirolz.secret.catsxml.given
Expand Down Expand Up @@ -164,11 +173,11 @@ val myCustomAlgebra = new SecretStrategyAlgebra:

final def deObfuscator[P](f: PlainValueBuffer => P): DeObfuscator[P] =
DeObfuscator.of { bufferTuple => f(bufferTuple.roObfuscatedBuffer) }
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp10$$anon$6@204ae769
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp10$$anon$9@d2f29c8

// build factory based on the algebra
val myCustomStrategyFactory = myCustomAlgebra.newFactory
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@24689809
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@5a05cd05

// ----------------------------- USAGE -----------------------------
// implicitly in the scope
Expand Down
11 changes: 10 additions & 1 deletion docs/source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ libraryDependencies += "com.github.geirolz" %% "secret-effect" % "@VERSION@"
import com.geirolz.secret.*
import cats.effect.{IO, Resource}

val res: Resource[IO, String] = Secret("password").resource[IO]
val s: Secret[String] = Secret("password")

// !!!! this will not destroy the secret because it uses a duplicated one !!!
val res: Resource[IO, String] = s.resource[IO]

// this will destroy the secret because it uses the original one
val res2: Resource[IO, String] = s.resourceDestroy[IO]

// this will destroy the secret because it uses the original one
val res3 = Secret.resource[IO, String]("password")
```

#### Pureconfig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.geirolz.secret

import cats.MonadThrow
import cats.effect.{Async, Resource}
import com.geirolz.secret.strategy.SecretStrategy
import com.geirolz.secret.util.Hasher

// one shot secret
extension [T](secret: OneShotSecret[T])
Expand All @@ -9,6 +12,10 @@ extension [T](secret: OneShotSecret[T])
.fromAutoCloseable(F.delay(secret))
.evalMap(_.accessValue[F])

extension (obj: OneShotSecret.type)
def resource[F[_]: Async, T: SecretStrategy](secret: T)(using Hasher): Resource[F, T] =
OneShotSecret(secret).resourceDestroy[F]

// secret
extension [T](secret: Secret[T])
inline def resource[F[_]](using F: Async[F]): Resource[F, T] =
Expand All @@ -21,9 +28,17 @@ extension [T](secret: Secret[T])
.fromAutoCloseable(F.defer(F.delay(secret)))
.evalMap(_.accessValue[F])

extension (obj: Secret.type)
def resource[F[_]: Async, T: SecretStrategy](secret: T)(using Hasher): Resource[F, T] =
Secret(secret).resourceDestroy[F]

// deferred secret
extension [F[_], T](secret: DeferredSecret[F, T])(using F: Async[F])
inline def resource: Resource[F, T] =
Resource
.make(secret.acquire)(s => F.delay(s.destroy()))
.evalMap(_.accessValue[F])

extension (obj: DeferredSecret.type)
def resource[F[_]: Async, T: SecretStrategy](acquire: => F[T])(using Hasher): Resource[F, T] =
DeferredSecret(acquire).resource

0 comments on commit 452dd47

Please sign in to comment.