Skip to content

Commit

Permalink
Add cats xml encoders
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed Jul 3, 2024
1 parent c369913 commit 4d82415
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
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)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.geirolz.secret.catsxml

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

Expand All @@ -9,3 +10,6 @@ given [T: Decoder: SecretStrategy]: Decoder[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)
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package com.geirolz.secret.catsxml

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

class SecretCatsXmlSuite extends munit.FunSuite:

Expand All @@ -19,3 +20,17 @@ class SecretCatsXmlSuite extends munit.FunSuite:

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(secretTag))
}

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

assert(result == Xml.string(secretTag))
}
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))
}

0 comments on commit 4d82415

Please sign in to comment.