Skip to content

Commit

Permalink
Add circe and cats-xml support (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz authored Mar 12, 2024
1 parent bda1cee commit c85d23c
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 12 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ libraryDependencies += "com.github.geirolz" %% "secret-ciris" % "0.0.6"
import com.geirolz.secret.ciris.given
```

#### Circe
Provides the json `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-circe" % "0.0.6"
```
```scala
import com.geirolz.secret.circe.given
```

#### Cats-xml
Provides the xml `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-cats-xml" % "0.0.6"
```
```scala
import com.geirolz.secret.catsxml.given
```


## Adopters

If you are using Secret in your company, please let me know and I'll add it to the list! It means a lot to me.
Expand All @@ -118,7 +139,7 @@ given SecretStrategy[String] = SecretStrategy[String](
)

Secret("my_password").euse(secret => secret)
// res7: Either[SecretDestroyed, String] = Right(value = "CUSTOM")
// res9: Either[SecretDestroyed, String] = Right(value = "CUSTOM")
```

## Custom Obfuscation Strategy algebra
Expand All @@ -141,25 +162,25 @@ val myCustomAlgebra = new SecretStrategyAlgebra:

final def deObfuscator[P](f: PlainValueBuffer => P): DeObfuscator[P] =
DeObfuscator.of { bufferTuple => f(bufferTuple.roObfuscatedBuffer) }
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp8$$anon$6@52112691
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp10$$anon$6@2a24f208

// build factory based on the algebra
val myCustomStrategyFactory = myCustomAlgebra.newFactory
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@e3d25e5
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@422c7da0

// ----------------------------- USAGE -----------------------------
// implicitly in the scope

import myCustomStrategyFactory.given

Secret("my_password").euse(secret => secret)
// res9: Either[SecretDestroyed, String] = Right(value = "my_password")
// res11: Either[SecretDestroyed, String] = Right(value = "my_password")

// or restricted to a specific scope
myCustomStrategyFactory {
Secret("my_password").euse(secret => secret)
}
// res10: Either[SecretDestroyed, String] = Right(value = "my_password")
// res12: Either[SecretDestroyed, String] = Right(value = "my_password")
```

## Contributing
Expand Down
22 changes: 20 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ lazy val root: Project = project
.settings(
copyReadMe := IO.copyFile(file("docs/compiled/README.md"), file("README.md"))
)
.aggregate(core, effect, docs, pureconfig, typesafeConfig, ciris)
.aggregate(core, effect, docs, pureconfig, typesafeConfig, ciris, circe, `cats-xml`)

lazy val docs: Project =
project
.in(file("docs"))
.enablePlugins(MdocPlugin)
.dependsOn(core, effect, pureconfig, typesafeConfig, ciris)
.dependsOn(core, effect, pureconfig, typesafeConfig, ciris, circe, `cats-xml`)
.settings(
baseSettings,
noPublishSettings,
Expand Down Expand Up @@ -103,6 +103,24 @@ lazy val ciris: Project =
libraryDependencies ++= ProjectDependencies.Integrations.Ciris.dedicated
)

lazy val circe: Project =
module("circe")(
folder = s"$integrationsFolder/circe",
publishAs = Some(subProjectName("circe"))
).dependsOn(core)
.settings(
libraryDependencies ++= ProjectDependencies.Integrations.Circe.dedicated
)

lazy val `cats-xml`: Project =
module("cats-xml")(
folder = s"$integrationsFolder/cats-xml",
publishAs = Some(subProjectName("cats-xml"))
).dependsOn(core)
.settings(
libraryDependencies ++= ProjectDependencies.Integrations.CatsXml.dedicated
)

//=============================== MODULES UTILS ===============================
def module(modName: String)(
folder: String,
Expand Down
31 changes: 26 additions & 5 deletions docs/compiled/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ libraryDependencies += "com.github.geirolz" %% "secret-ciris" % "0.0.6"
import com.geirolz.secret.ciris.given
```

#### Circe
Provides the json `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-circe" % "0.0.6"
```
```scala
import com.geirolz.secret.circe.given
```

#### Cats-xml
Provides the xml `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-cats-xml" % "0.0.6"
```
```scala
import com.geirolz.secret.catsxml.given
```


## Adopters

If you are using Secret in your company, please let me know and I'll add it to the list! It means a lot to me.
Expand All @@ -118,7 +139,7 @@ given SecretStrategy[String] = SecretStrategy[String](
)

Secret("my_password").euse(secret => secret)
// res7: Either[SecretDestroyed, String] = Right(value = "CUSTOM")
// res9: Either[SecretDestroyed, String] = Right(value = "CUSTOM")
```

## Custom Obfuscation Strategy algebra
Expand All @@ -141,25 +162,25 @@ val myCustomAlgebra = new SecretStrategyAlgebra:

final def deObfuscator[P](f: PlainValueBuffer => P): DeObfuscator[P] =
DeObfuscator.of { bufferTuple => f(bufferTuple.roObfuscatedBuffer) }
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp8$$anon$6@52112691
// myCustomAlgebra: SecretStrategyAlgebra = repl.MdocSession$MdocApp10$$anon$6@2a24f208

// build factory based on the algebra
val myCustomStrategyFactory = myCustomAlgebra.newFactory
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@e3d25e5
// myCustomStrategyFactory: SecretStrategyFactory = com.geirolz.secret.strategy.SecretStrategyFactory@422c7da0

// ----------------------------- USAGE -----------------------------
// implicitly in the scope

import myCustomStrategyFactory.given

Secret("my_password").euse(secret => secret)
// res9: Either[SecretDestroyed, String] = Right(value = "my_password")
// res11: Either[SecretDestroyed, String] = Right(value = "my_password")

// or restricted to a specific scope
myCustomStrategyFactory {
Secret("my_password").euse(secret => secret)
}
// res10: Either[SecretDestroyed, String] = Right(value = "my_password")
// res12: Either[SecretDestroyed, String] = Right(value = "my_password")
```

## Contributing
Expand Down
21 changes: 21 additions & 0 deletions docs/source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ libraryDependencies += "com.github.geirolz" %% "secret-ciris" % "@VERSION@"
import com.geirolz.secret.ciris.given
```

#### Circe
Provides the json `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-circe" % "@VERSION@"
```
```scala mdoc:reset
import com.geirolz.secret.circe.given
```

#### Cats-xml
Provides the xml `Decoder` instance for `Secret[T]` and `OneShotSecret[T]` type.

```sbt
libraryDependencies += "com.github.geirolz" %% "secret-cats-xml" % "@VERSION@"
```
```scala mdoc:reset
import com.geirolz.secret.catsxml.given
```


## Adopters

If you are using Secret in your company, please let me know and I'll add it to the list! It means a lot to me.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.geirolz.secret.catsxml

import cats.xml.codec.Decoder
import com.geirolz.secret.{OneShotSecret, Secret}
import com.geirolz.secret.strategy.SecretStrategy

given [T: Decoder: SecretStrategy]: Decoder[Secret[T]] =
Decoder[T].map(Secret[T](_))

given [T: Decoder: SecretStrategy]: Decoder[OneShotSecret[T]] =
Decoder[T].map(OneShotSecret[T](_))
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.geirolz.secret.catsxml

import cats.xml.codec.Decoder.Result
import cats.xml.{Xml, XmlData}
import com.geirolz.secret.{OneShotSecret, Secret}

class SecretCatsXmlSuite extends munit.FunSuite:

test("Secret should be decoded from xml") {
val xml: XmlData.XmlString = Xml.string("secret_value")
val result: Result[Secret[String]] = xml.as[Secret[String]]

result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}

test("OneShotSecret should be decoded from xml") {
val xml: XmlData.XmlString = Xml.string("secret_value")
val result = xml.as[OneShotSecret[String]]

result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.geirolz.secret.circe

import com.geirolz.secret.{OneShotSecret, Secret}
import com.geirolz.secret.strategy.SecretStrategy
import io.circe.Decoder

given [T: Decoder: SecretStrategy]: Decoder[Secret[T]] =
Decoder[T].map(Secret[T](_))

given [T: Decoder: SecretStrategy]: Decoder[OneShotSecret[T]] =
Decoder[T].map(OneShotSecret[T](_))
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.geirolz.secret.circe

import com.geirolz.secret.{OneShotSecret, Secret}
import io.circe.Json

class SecretCirceSuite extends munit.FunSuite:

test("Secret should be decoded from json") {
val json: Json = Json.fromString("secret_value")
val result = json.as[Secret[String]]

result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}

test("OneShotSecret should be decoded from json") {
val json = Json.fromString("secret_value")
val result = json.as[OneShotSecret[String]]

result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}
12 changes: 12 additions & 0 deletions project/ProjectDependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ object ProjectDependencies {
"org.typelevel" %% "munit-cats-effect-3" % munitEffectVersion % Test
)
}

object Circe {
lazy val dedicated: Seq[ModuleID] = List(
"io.circe" %% "circe-core" % "0.14.6"
)
}

object CatsXml {
lazy val dedicated: Seq[ModuleID] = List(
"com.github.geirolz" %% "cats-xml" % "0.0.15"
)
}
}

object Plugins {
Expand Down

0 comments on commit c85d23c

Please sign in to comment.