Skip to content

Commit

Permalink
Add tupled method to convert EnvVar to Pair of Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
amanjpro committed Aug 26, 2020
1 parent fc4a2f5 commit c802bd5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/scala/models/EnvVar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ sealed trait EnvVar {
type T <: AnyRef
def name: String
def value: T
def tupled: (String, String)
}
object EnvVar {
private[this] val pattern = """secure\((.*)\)""".r
Expand All @@ -33,11 +34,12 @@ object EnvVar {
}
}

case class SecureEnvVar private(name: String, value: Seq[Char]) extends EnvVar {
private[models] case class SecureEnvVar(name: String, value: Seq[Char]) extends EnvVar {
type T = Seq[Char]
def tupled: (String, String) = (name, value.mkString(""))
}

object SecureEnvVar {
private[models] object SecureEnvVar {
val HIDDEN_PASSWORD = "****"
implicit val secureEnvVarEncoder: Encoder[SecureEnvVar] =
new Encoder[SecureEnvVar] {
Expand All @@ -60,10 +62,11 @@ object SecureEnvVar {
}
}

case class PlainEnvVar private(name: String, value: String) extends EnvVar {
private[models] case class PlainEnvVar(name: String, value: String) extends EnvVar {
type T = String
def tupled: (String, String) = (name, value)
}
object PlainEnvVar {
private[models] object PlainEnvVar {
implicit val plainEnvVarEncoder: Encoder[PlainEnvVar] =
new Encoder[PlainEnvVar] {
final def apply(v: PlainEnvVar): Json = Json.obj(
Expand Down
17 changes: 17 additions & 0 deletions src/test/scala/models/EnvVarSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,22 @@ class EnvVarSpec() extends Matchers with AnyWordSpecLike {
}
}

"EnvVar.tupled" must {
"work for secure variables" in {
val (name, value) = ("username", "secure(Homa)")
val origin = EnvVar(name, value)
val expected = (name, "Homa")
val actual = origin.tupled
actual shouldBe expected
}

"work for plain variables" in {
val (name, value) = ("username", "Homa")
val origin = EnvVar(name, value)
val expected = (name, value)
val actual = origin.tupled
actual shouldBe expected
}
}
}

0 comments on commit c802bd5

Please sign in to comment.