Skip to content

Commit

Permalink
added json handling for attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
mineme0110 committed Jan 24, 2023
1 parent 76eb759 commit 747dc84
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ object AgentCli extends ZIOAppDefault {
presentationAttachment = PresentationAttachment.build(
Some(Options(challenge = "somechallenge", domain = "somedomain"))
)
attachmentDescriptor = AttachmentDescriptor.buildBase64Attachment(payload =
presentationAttachment.asJson.noSpaces.getBytes()
)

// attachmentDescriptor = AttachmentDescriptor.buildBase64Attachment(payload =
// presentationAttachment.asJson.noSpaces.getBytes()
// )
attachmentDescriptor = AttachmentDescriptor.buildJsonAttachment(payload = presentationAttachment)
requestPresentation = RequestPresentation(
body = body,
attachments = Seq(attachmentDescriptor),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,37 @@ def json2Map(json: Json): Any = json match {
case _ => null // Impossible case but Json cases are private in circe ...
}

def mapValueToJson(obj: java.lang.Object): Json = {
obj match {
case null => Json.Null
case b: java.lang.Boolean => Json.fromBoolean(b)
case i: java.lang.Integer => Json.fromInt(i)
case d: java.lang.Double =>
Json.fromDouble(d).getOrElse(Json.fromDouble(0d).get)
case l: java.lang.Long => Json.fromLong(l)
case s: java.lang.String => Json.fromString(String.valueOf(s))
case array: com.nimbusds.jose.shaded.json.JSONArray => {
Json.fromValues(array.iterator().asScala.map(mapValueToJson).toList)
}
case joseObject: com.nimbusds.jose.shaded.json.JSONObject =>
Json.fromJsonObject {
JsonObject.fromMap(
joseObject
.asInstanceOf[java.util.Map[String, Object]]
.asScala
.toMap
.view
.mapValues(mapValueToJson)
.toMap
)
}
case any => {
println("*****NotImplemented***" + any.getClass().getCanonicalName() + "**********") // FIXME
???
}
}
}

given Conversion[AttachmentDescriptor, XAttachment] with {
def apply(attachment: AttachmentDescriptor): XAttachment = {
val id = attachment.id
Expand All @@ -82,8 +113,8 @@ given Conversion[XAttachment, AttachmentDescriptor] with {
val data: AttachmentData = attachment.getData().toJSONObject.asScala.toMap match {
case e if e contains ("json") =>
val aux = e("json")
println(aux.getClass().getCanonicalName()) // TODO
???
val x = aux.asInstanceOf[java.util.Map[String, Object]].asScala.toMap.view.mapValues(mapValueToJson)
JsonData(JsonObject.fromMap(x.toMap))
case e if e contains ("base64") =>
val tmp = e("base64").asInstanceOf[String] // ...
Base64(tmp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object LinkData {

}

final case class JsonData(data: JsonObject) extends AttachmentData
final case class JsonData(json: JsonObject) extends AttachmentData
object JsonData {
given Encoder[JsonData] = deriveEncoder[JsonData]
given Decoder[JsonData] = deriveDecoder[JsonData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private[this] trait ReadAttachmentsUtils {
case obj: JsonData =>
java.util.Base64
.getUrlEncoder()
.encode(obj.data.asJson.noSpaces.getBytes())
.encode(obj.json.asJson.noSpaces.getBytes())
})
maybeAttachament.map(formatName -> _)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class PresentationAttachmentSpec extends ZSuite {
val presentationAttachment =
PresentationAttachment(presentation_definition = presentationDefinition, options = Some(options))
val resultPresentationAttachment = presentationAttachment.asJson.deepDropNullValues
println(resultPresentationAttachment)
assertEquals(resultPresentationAttachment, expectedPresentationAttachmentJson)

}
Expand Down

0 comments on commit 747dc84

Please sign in to comment.