Skip to content

Commit

Permalink
Internal renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed Jul 18, 2024
1 parent 50178a4 commit ee294d4
Show file tree
Hide file tree
Showing 22 changed files with 117 additions and 113 deletions.
28 changes: 14 additions & 14 deletions core/src/main/scala/com/geirolz/secret/DeferredSecret.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,44 +47,44 @@ sealed trait DeferredSecret[F[_], T]:
def evalUseRaw[U](f: Secret[T] => F[U]): F[U]

/** Map the secret value to `U` */
def map[U: SecretStrategy](f: T => U)(using Hasher): DeferredSecret[F, U]
def map[U: SecretStrategy](f: T => U)(using Hasher): Secret.Deferred[F, U]

/** FlatMap the secret value to `U` */
def flatMap[U: SecretStrategy](f: T => DeferredSecret[F, U])(using Hasher): DeferredSecret[F, U]
def flatMap[U: SecretStrategy](f: T => Secret.Deferred[F, U])(using Hasher): Secret.Deferred[F, U]

/** Handle the error of the acquisition of the secret value */
def handleError(f: Throwable => Secret[T]): DeferredSecret[F, T]
def handleError(f: Throwable => Secret[T]): Secret.Deferred[F, T]

/** Handle the error of the acquisition of the secret value */
def handleErrorWith(f: Throwable => F[Secret[T]]): DeferredSecret[F, T]
def handleErrorWith(f: Throwable => F[Secret[T]]): Secret.Deferred[F, T]

object DeferredSecret:

/** Create a DeferredSecret from a `F[T]`.
*
* The function is called every time the DeferredSecret is used.
*/
def apply[F[_]: MonadThrow, T: SecretStrategy](acquire: => F[T])(using Hasher): DeferredSecret[F, T] =
def apply[F[_]: MonadThrow, T: SecretStrategy](acquire: => F[T])(using Hasher): Secret.Deferred[F, T] =
DeferredSecret.fromSecret[F, T](acquire.map(Secret(_)))

/** Create a pure and constant DeferredSecret */
def pure[F[_]: MonadThrow, T: SecretStrategy](t: T)(using Hasher): DeferredSecret[F, T] =
def pure[F[_]: MonadThrow, T: SecretStrategy](t: T)(using Hasher): Secret.Deferred[F, T] =
DeferredSecret(t.pure[F])

/** Create a failed DeferredSecret which always fails with the specified error */
def failed[F[_]: MonadThrow, T](e: Throwable): DeferredSecret[F, T] =
def failed[F[_]: MonadThrow, T](e: Throwable): Secret.Deferred[F, T] =
DeferredSecret.fromSecret(MonadThrow[F].raiseError(e))

/** Create a DeferredSecret that reads the specified environment variable every time it is used. */
def fromEnv[F[_]: MonadThrow](name: String)(using SecretStrategy[String], Hasher): DeferredSecret[F, String] =
def fromEnv[F[_]: MonadThrow](name: String)(using SecretStrategy[String], Hasher): Secret.Deferred[F, String] =
DeferredSecret.fromSecret(Secret.fromEnv[F](name))

/** Create a DeferredSecret from a Secret.
*
* The acquire function is called every time you use the DeferredSecret.
*/
def fromSecret[F[_]: MonadThrow, T](_acquire: => F[Secret[T]]): DeferredSecret[F, T] =
new DeferredSecret[F, T]:
def fromSecret[F[_]: MonadThrow, T](_acquire: => F[Secret[T]]): Secret.Deferred[F, T] =
new Secret.Deferred[F, T]:

private[secret] def acquire = _acquire

Expand All @@ -100,14 +100,14 @@ object DeferredSecret:
override def evalUseRaw[U](f: Secret[T] => F[U]): F[U] =
acquire.flatMap(f)

override def map[U: SecretStrategy](f: T => U)(using Hasher): DeferredSecret[F, U] =
override def map[U: SecretStrategy](f: T => U)(using Hasher): Secret.Deferred[F, U] =
DeferredSecret.fromSecret(acquire.map(_.map(f)))

override def flatMap[U: SecretStrategy](f: T => DeferredSecret[F, U])(using Hasher): DeferredSecret[F, U] =
override def flatMap[U: SecretStrategy](f: T => Secret.Deferred[F, U])(using Hasher): Secret.Deferred[F, U] =
DeferredSecret.fromSecret(evalUse(f(_).acquire))

override def handleError(f: Throwable => Secret[T]): DeferredSecret[F, T] =
override def handleError(f: Throwable => Secret[T]): Secret.Deferred[F, T] =
handleErrorWith(f.andThen(_.pure[F]))

override def handleErrorWith(f: Throwable => F[Secret[T]]): DeferredSecret[F, T] =
override def handleErrorWith(f: Throwable => F[Secret[T]]): Secret.Deferred[F, T] =
DeferredSecret.fromSecret(acquire.handleErrorWith(f))
11 changes: 7 additions & 4 deletions core/src/main/scala/com/geirolz/secret/OneShotSecret.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import com.geirolz.secret.util.{Hasher, Location}
* type of the secret
*/
final class OneShotSecret[T] private[secret] (vault: Vault[T]) extends SecretApi[T](vault)
object OneShotSecret extends SecretCompanionApi[OneShotSecret]:
object OneShotSecret extends SecretCompanionApi[Secret.OneShot]:

private[secret] def fromVault[T](vault: Vault[T]): Secret.OneShot[T] =
new OneShotSecret(vault)

/** Create a destroyed secret */
override def destroyed[T](location: Location = Location.unknown): OneShotSecret[T] =
new OneShotSecret[T](Vault.destroyed[T](location))
override def destroyed[T](location: Location = Location.unknown): Secret.OneShot[T] =
fromVault[T](Vault.destroyed[T](location))

/** Create a new `OneShotSecret` with the specified value.
*
Expand All @@ -30,4 +33,4 @@ object OneShotSecret extends SecretCompanionApi[OneShotSecret]:
override def apply[T](value: => T, collectDestructionLocation: Boolean = true)(using
strategy: SecretStrategy[T],
hasher: Hasher
): OneShotSecret[T] = new OneShotSecret(Vault[T](value, collectDestructionLocation))
): Secret.OneShot[T] = fromVault(Vault[T](value, collectDestructionLocation))
6 changes: 3 additions & 3 deletions core/src/main/scala/com/geirolz/secret/Secret.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ abstract sealed class Secret[T] private (vault: Vault[T]) extends SecretApi[T](v
final def flatMap[U: SecretStrategy](f: T => Secret[U])(using Hasher): Secret[U] =
transform(_.euse(f))

/** [[OneShotSecret]] version of this secret */
final def asOneShot: OneShotSecret[T] =
new OneShotSecret(vault)
/** [[Secret.OneShot]] version of this secret */
final def asOneShot: Secret.OneShot[T] =
Secret.oneShot.fromVault(vault)

/** Apply `f` with the de-obfuscated value WITHOUT destroying it.
*
Expand Down
62 changes: 33 additions & 29 deletions core/src/test/scala/com/geirolz/secret/DeferredSecretSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import scala.util.Try

class DeferredSecretSuite extends munit.CatsEffectSuite:

test("DeferredSecret should be evaluated every time use is called with IO") {
test("Secret.Deferred should be evaluated every time use is called with IO") {
var counter = 0
val secret: DeferredSecret[IO, Int] =
DeferredSecret(IO {
counter += 1
1
}).map(_ + 1)
.flatMap(v => DeferredSecret.pure(v + 1))
val secret: Secret.Deferred[IO, Int] =
Secret
.deferred(IO {
counter += 1
1
})
.map(_ + 1)
.flatMap(v => Secret.deferred.pure(v + 1))
.handleError(_ => Secret(0))
.handleErrorWith(_ => IO(Secret(0)))

Expand All @@ -26,14 +28,16 @@ class DeferredSecretSuite extends munit.CatsEffectSuite:
assert(counter == 3)
}

test("DeferredSecret should be evaluated every time use is called with Try") {
test("Secret.Deferred should be evaluated every time use is called with Try") {
var counter = 0
val secret: DeferredSecret[Try, Int] =
DeferredSecret(Try {
counter += 1
1
}).map(_ + 1)
.flatMap(v => DeferredSecret.pure(v + 1))
val secret: Secret.Deferred[Try, Int] =
Secret
.deferred(Try {
counter += 1
1
})
.map(_ + 1)
.flatMap(v => Secret.deferred.pure(v + 1))
.handleError(_ => Secret(0))
.handleErrorWith(_ => Try(Secret(0)))

Expand All @@ -46,32 +50,32 @@ class DeferredSecretSuite extends munit.CatsEffectSuite:
assert(counter == 3)
}

test("DeferredSecret.pure should always return the same value") {
val secret: DeferredSecret[Try, String] = DeferredSecret.pure("hello")
test("Secret.deferred.pure should always return the same value") {
val secret: Secret.Deferred[Try, String] = Secret.deferred.pure("hello")
secret.use((v: String) => assertEquals(v, "hello"))
}

test("DeferredSecret.failure should always return a failure") {
val ex = new Exception("error")
val secret: DeferredSecret[Try, Int] = DeferredSecret.failed[Try, Int](ex)
test("Secret.deferred.failure should always return a failure") {
val ex = new Exception("error")
val secret: Secret.Deferred[Try, Int] = Secret.deferred.failed[Try, Int](ex)
assert(secret.use(_ => ()).isFailure)
}

test("DeferredSecret.map should transform the value") {
val secret: DeferredSecret[Try, Int] = DeferredSecret.pure(1)
val result = secret.map(_ + 1)
test("Secret.deferred.map should transform the value") {
val secret: Secret.Deferred[Try, Int] = Secret.deferred.pure(1)
val result = secret.map(_ + 1)
result.use((v: Int) => assertEquals(v, 2))
}

test("DeferredSecret.flatMap should transform the value") {
val secret: DeferredSecret[Try, Int] = DeferredSecret.pure(1)
val result = secret.flatMap(v => DeferredSecret.pure(v + 1))
test("Secret.deferred.flatMap should transform the value") {
val secret: Secret.Deferred[Try, Int] = Secret.deferred.pure(1)
val result = secret.flatMap(v => Secret.deferred.pure(v + 1))
result.use((v: Int) => assertEquals(v, 2))
}

test("DeferredSecret.flatMap should propagate the failure") {
val ex = new Exception("error")
val secret: DeferredSecret[Try, Int] = DeferredSecret.failed[Try, Int](ex)
val result = secret.flatMap(v => DeferredSecret.pure(v + 1))
test("Secret.deferred.flatMap should propagate the failure") {
val ex = new Exception("error")
val secret: Secret.Deferred[Try, Int] = Secret.deferred.failed[Try, Int](ex)
val result = secret.flatMap(v => Secret.deferred.pure(v + 1))
assert(result.use(_ => ()).isFailure)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.geirolz.secret.testing

import com.geirolz.secret.{OneShotSecret, Secret}
import com.geirolz.secret.Secret
import com.geirolz.secret.internal.SecretApi
import com.geirolz.secret.strategy.SecretStrategy
import com.geirolz.secret.util.Hasher
Expand All @@ -21,10 +21,10 @@ object SecretBuilder:
hasher: Hasher
): Secret[T] = Secret(value, collectDestructionLocation)

val oneShotSecret: SecretBuilder[OneShotSecret] =
new SecretBuilder[OneShotSecret]:
val oneShotSecret: SecretBuilder[Secret.OneShot] =
new SecretBuilder[Secret.OneShot]:
override val name: String = "OneShotSecret"
override def apply[T](value: => T, collectDestructionLocation: Boolean = true)(using
strategy: SecretStrategy[T],
hasher: Hasher
): OneShotSecret[T] = OneShotSecret(value, collectDestructionLocation)
): Secret.OneShot[T] = Secret.oneShot(value, collectDestructionLocation)
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package com.geirolz.secret.catsxml.hashed
import cats.xml.codec.Encoder
import com.geirolz.secret.internal.SecretApi
import com.geirolz.secret.strategy.SecretStrategy
import com.geirolz.secret.{OneShotSecret, Secret}
import com.geirolz.secret.Secret

export com.geirolz.secret.catsxml.given_Decoder_Secret
export com.geirolz.secret.catsxml.given_Decoder_OneShotSecret
export com.geirolz.secret.catsxml.given_Decoder_OneShot

given [S[X] <: SecretApi[X], T]: Encoder[S[T]] =
Encoder.encodeString.contramap(_.hashed)
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package com.geirolz.secret.catsxml

import cats.xml.codec.{Decoder, Encoder}
import com.geirolz.secret.internal.SecretApi
import com.geirolz.secret.{OneShotSecret, Secret}
import com.geirolz.secret.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](_))
given [T: Decoder: SecretStrategy]: Decoder[Secret.OneShot[T]] =
Decoder[T].map(Secret.oneShot[T](_))

given [S[X] <: SecretApi[X], T]: Encoder[S[T]] =
Encoder.encodeString.contramap(_.toString)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.geirolz.secret.catsxml
import cats.xml.codec.Decoder.Result
import cats.xml.{Xml, XmlData}
import cats.xml.syntax.*
import com.geirolz.secret.{secretTag, OneShotSecret, Secret}
import com.geirolz.secret.{secretTag, Secret}

class SecretCatsXmlSuite extends munit.FunSuite:

Expand All @@ -14,9 +14,9 @@ class SecretCatsXmlSuite extends munit.FunSuite:
result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}

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

result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}
Expand All @@ -28,9 +28,9 @@ class SecretCatsXmlSuite extends munit.FunSuite:
assert(result == Xml.string(secretTag))
}

test("OneShotSecret should be encoded to json") {
val secret: OneShotSecret[String] = OneShotSecret("secret_value")
val result: Xml = secret.toXml
test("Secret.OneShot should be encoded to json") {
val secret: Secret.OneShot[String] = Secret.oneShot("secret_value")
val result: Xml = secret.toXml

assert(result == Xml.string(secretTag))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.geirolz.secret.catsxml.hashed
import cats.xml.codec.Decoder.Result
import cats.xml.syntax.*
import cats.xml.{Xml, XmlData}
import com.geirolz.secret.{secretTag, OneShotSecret, Secret}
import com.geirolz.secret.{secretTag, Secret}

class SecretCatsXmlHashedSuite extends munit.FunSuite:

Expand All @@ -14,9 +14,9 @@ class SecretCatsXmlHashedSuite extends munit.FunSuite:
result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}

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

result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}
Expand All @@ -28,9 +28,9 @@ class SecretCatsXmlHashedSuite extends munit.FunSuite:
assert(result == Xml.string(secret.hashed))
}

test("OneShotSecret should be encoded to json") {
val secret: OneShotSecret[String] = OneShotSecret("secret_value")
val result: Xml = secret.toXml
test("Secret.OneShot should be encoded to json") {
val secret: Secret.OneShot[String] = Secret.oneShot("secret_value")
val result: Xml = secret.toXml

assert(result == Xml.string(secret.hashed))
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.geirolz.secret.circe.hashed

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

export com.geirolz.secret.circe.given_Decoder_Secret
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.geirolz.secret.circe

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

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

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

given [S[X] <: SecretApi[X], T]: Encoder[S[T]] =
Encoder.encodeString.contramap(_.toString)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.geirolz.secret.circe

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

Expand All @@ -13,9 +13,9 @@ class SecretCirceSuite extends munit.FunSuite:
result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}

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

result.toOption.get.euseAndDestroy(v => assert(v == "secret_value"))
}
Expand All @@ -27,9 +27,9 @@ class SecretCirceSuite extends munit.FunSuite:
assert(result == Json.fromString(secretTag))
}

test("OneShotSecret should be encoded to json") {
val secret: OneShotSecret[String] = OneShotSecret("secret_value")
val result: Json = secret.asJson
test("Secret.OneShot should be encoded to json") {
val secret: Secret.OneShot[String] = Secret.oneShot("secret_value")
val result: Json = secret.asJson

assert(result == Json.fromString(secretTag))
}
Loading

0 comments on commit ee294d4

Please sign in to comment.