-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Circe and Cats XML encoders (#33)
* Add circe encoder * Add cats xml encoders
- Loading branch information
Showing
8 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
integrations/cats-xml/src/main/scala/com/geirolz/secret/catsxml/hashed/implicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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} | ||
|
||
export com.geirolz.secret.catsxml.given_Decoder_Secret | ||
export com.geirolz.secret.catsxml.given_Decoder_OneShotSecret | ||
|
||
given [S[X] <: SecretApi[X], T]: Encoder[S[T]] = | ||
Encoder.encodeString.contramap(_.hashed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
.../cats-xml/src/test/scala/com/geirolz/secret/catsxml/hashed/SecretCatsXmlHashedSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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} | ||
|
||
class SecretCatsXmlHashedSuite 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")) | ||
} | ||
|
||
test("Secret should be encoded to json") { | ||
val secret: Secret[String] = Secret("secret_value") | ||
val result: Xml = secret.toXml | ||
|
||
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 | ||
|
||
assert(result == Xml.string(secret.hashed)) | ||
} |
12 changes: 12 additions & 0 deletions
12
integrations/circe/src/main/scala/com/geirolz/secret/circe/hashed/implicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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 io.circe.{Decoder, Encoder} | ||
|
||
export com.geirolz.secret.circe.given_Decoder_Secret | ||
export com.geirolz.secret.circe.given_Decoder_OneShotSecret | ||
|
||
given [S[X] <: SecretApi[X], T]: Encoder[S[T]] = | ||
Encoder.encodeString.contramap(_.hashed) |
6 changes: 5 additions & 1 deletion
6
integrations/circe/src/main/scala/com/geirolz/secret/circe/implicits.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package com.geirolz.secret.circe | ||
|
||
import com.geirolz.secret.internal.SecretApi | ||
import com.geirolz.secret.{OneShotSecret, Secret} | ||
import com.geirolz.secret.strategy.SecretStrategy | ||
import io.circe.Decoder | ||
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 [S[X] <: SecretApi[X], T]: Encoder[S[T]] = | ||
Encoder.encodeString.contramap(_.toString) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...rations/circe/src/test/scala/com/geirolz/secret/circe/hashed/SecretCirceHashedSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.geirolz.secret.circe.hashed | ||
|
||
import com.geirolz.secret.{secretTag, OneShotSecret, Secret} | ||
import io.circe.Json | ||
import io.circe.syntax.* | ||
|
||
class SecretCirceHashedSuite 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")) | ||
} | ||
|
||
test("Secret should be encoded to json") { | ||
val secret: Secret[String] = Secret("secret_value") | ||
val result: Json = secret.asJson | ||
|
||
assert(result == Json.fromString(secret.hashed)) | ||
} | ||
|
||
test("OneShotSecret should be encoded to json") { | ||
val secret: OneShotSecret[String] = OneShotSecret("secret_value") | ||
val result: Json = secret.asJson | ||
|
||
assert(result == Json.fromString(secret.hashed)) | ||
} |