Skip to content

Commit

Permalink
Merge pull request #11 from coryfklein/secret-type-field-addition
Browse files Browse the repository at this point in the history
Secret type field addition
  • Loading branch information
doriordan authored Jul 30, 2016
2 parents e34ef1b + fa99ae2 commit dcfe343
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
3 changes: 2 additions & 1 deletion client/src/main/scala/skuber/Secret.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ case class Secret(
val kind: String ="Secret",
override val apiVersion: String = v1,
val metadata: ObjectMeta,
data: Map[String, Array[Byte]] = Map())
data: Map[String, Array[Byte]] = Map(),
val `type`: String = "")
extends ObjectResource {

def add(key: String, is: InputStream) : Unit = {
Expand Down
19 changes: 15 additions & 4 deletions client/src/main/scala/skuber/json/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ package object format {
implicit vReads: Reads[V], vWrites: Writes[V],
omitEmpty: Boolean=true) : OFormat[Map[String,V]] =
path.formatNullable[Map[String,V]].inmap[Map[String,V]](_.getOrElse(emptyM[V]), m => if (omitEmpty && m.isEmpty) None else Some(m))


def formatMaybeEmptyByteArrayMap(
implicit vReads: Reads[Map[String, Array[Byte]]], vWrites: Writes[Map[String, Array[Byte]]],
omitEmpty: Boolean=true) : OFormat[Map[String,Array[Byte]]] =
path.formatNullable[Map[String,Array[Byte]]].inmap[Map[String,Array[Byte]]](_.getOrElse(emptyM[Array[Byte]]), m => if (omitEmpty && m.isEmpty) None else Some(m))

// Boolean: the empty value is 'false'
def formatMaybeEmptyBoolean(omitEmpty: Boolean=true) : OFormat[Boolean] =
path.formatNullable[Boolean].inmap[Boolean](_.getOrElse(false), b => if (omitEmpty && !b) None else Some(b))
Expand Down Expand Up @@ -626,11 +631,17 @@ package object format {
implicit val base64Format: Format[Array[Byte]] = (
JsPath.format[String].inmap(s => Base64.decodeBase64(s), (bytes: Array[Byte]) => Base64.encodeBase64String(bytes))
)

import skuber.Secret

implicit val mapStringByteArrayFormat: Format[Map[String,Array[Byte]]] =
JsPath.format[Map[String,String]]
.inmap(_.map({case (k,v) => k -> Base64.decodeBase64(v.getBytes)}),
(map: Map[String, Array[Byte]]) => map.map({case (k,v) => k -> Base64.encodeBase64String(v)}))

import skuber.Secret
implicit val secretFmt: Format[Secret] = (
objFormat and
(JsPath \ "data").formatMaybeEmptyMap[Array[Byte]]
(JsPath \ "data").formatMaybeEmptyByteArrayMap and
(JsPath \ "type").formatMaybeEmptyString()
)(Secret.apply _, unlift(Secret.unapply))

implicit val limitRangeItemTypeFmt: Format[LimitRange.ItemType.Type] = enumFormat(LimitRange.ItemType)
Expand Down
31 changes: 31 additions & 0 deletions client/src/test/scala/skuber/json/SecretSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package skuber.json

import org.specs2.mutable.Specification
import skuber.{ObjectMeta, Secret} // for unit-style testing
import format._

import play.api.libs.json._

/**
* @author Cory Klein
*/
class SecretSpec extends Specification {

"A Secret containing a byte array can symmetrically be written to json and the same value read back in" >> {
val dataBytes = "hello".getBytes
val secret = Secret(metadata = ObjectMeta("mySecret"), data = Map("key" -> dataBytes))
val resultBytes = Json.fromJson[Secret](Json.toJson(secret)).get.data("key")
dataBytes mustEqual resultBytes
}
"this can be done with an empty data map" >> {
val mySecret = Secret(metadata = ObjectMeta("mySecret"))
val json = Json.toJson(mySecret)
val readSecret = Json.fromJson[Secret](Json.toJson(mySecret)).get
mySecret mustEqual readSecret
}
"this can be done with the type member defined" >> {
val mySecret = Secret(metadata = ObjectMeta("mySecret"), `type` = "myType")
val readSecret = Json.fromJson[Secret](Json.toJson(mySecret)).get
mySecret mustEqual readSecret
}
}

0 comments on commit dcfe343

Please sign in to comment.