Skip to content

Commit

Permalink
feat(pluto): hide pluto codable implementation for messages and did
Browse files Browse the repository at this point in the history
  • Loading branch information
goncalo-frade-iohk committed Dec 6, 2022
1 parent dd9f924 commit 85180b4
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Core/Sources/Helpers/JSONDecoder+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
public extension JSONDecoder {
static func didComm() -> JSONDecoder {
let decoder = JSONDecoder()
decoder.dataDecodingStrategy = .deferredToData
decoder.dataDecodingStrategy = .base64
decoder.keyDecodingStrategy = .convertFromSnakeCase
return decoder
}
Expand Down
4 changes: 2 additions & 2 deletions Core/Sources/Helpers/JSONEncoder+Helper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import Foundation
public extension JSONEncoder {
static func didComm() -> JSONEncoder {
let encoder = JSONEncoder()
encoder.dataEncodingStrategy = .deferredToData
encoder.dataEncodingStrategy = .base64
encoder.keyEncodingStrategy = .convertToSnakeCase
encoder.outputFormatting = .prettyPrinted
encoder.outputFormatting = .withoutEscapingSlashes
return encoder
}
}
24 changes: 17 additions & 7 deletions Pluto/Sources/Helpers/DID+Codable.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import Domain
import Foundation

extension DID: Codable {
struct CodableDID: Codable {
enum CodingKeys: String, CodingKey {
case schema
case method
case methodId
}

public func encode(to encoder: Encoder) throws {
let did: DID

init(did: DID) {
self.did = did
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(schema, forKey: .schema)
try container.encode(method, forKey: .method)
try container.encode(methodId, forKey: .methodId)
try container.encode(did.schema, forKey: .schema)
try container.encode(did.method, forKey: .method)
try container.encode(did.methodId, forKey: .methodId)
}

public init(from decoder: Decoder) throws {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let schema = try container.decode(String.self, forKey: .schema)
let method = try container.decode(DIDMethod.self, forKey: .method)
let methodId = try container.decode(DIDMethodId.self, forKey: .methodId)

self.init(schema: schema, method: method, methodId: methodId)
self.init(did: .init(
schema: schema,
method: method,
methodId: methodId
))
}
}
46 changes: 26 additions & 20 deletions Pluto/Sources/Helpers/Message+Codable.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Domain
import Foundation

extension Message: Codable {
struct CodableMessage: Codable {
enum CodingKeys: String, CodingKey {
case id
case piuri
Expand All @@ -18,24 +18,30 @@ extension Message: Codable {
case body
}

public func encode(to encoder: Encoder) throws {
let message: Message

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(piuri, forKey: .piuri)
try container.encode(body, forKey: .body)
try container.encode(extraHeaders, forKey: .extraHeaders)
try container.encode(createdTime, forKey: .createdTime)
try container.encode(expiresTimePlus, forKey: .expiresTimePlus)
try container.encode(attachments, forKey: .attachments)
try container.encode(ack, forKey: .ack)
try from.map { try container.encode($0, forKey: .from) }
try to.map { try container.encode($0, forKey: .to) }
try fromPrior.map { try container.encode($0, forKey: .fromPrior) }
try thid.map { try container.encode($0, forKey: .thid) }
try pthid.map { try container.encode($0, forKey: .pthid) }
try container.encode(message.id, forKey: .id)
try container.encode(message.piuri, forKey: .piuri)
try container.encode(message.body, forKey: .body)
try container.encode(message.extraHeaders, forKey: .extraHeaders)
try container.encode(message.createdTime, forKey: .createdTime)
try container.encode(message.expiresTimePlus, forKey: .expiresTimePlus)
try container.encode(message.attachments, forKey: .attachments)
try container.encode(message.ack, forKey: .ack)
try message.from.map { try container.encode(CodableDID(did: $0), forKey: .from) }
try message.to.map { try container.encode(CodableDID(did: $0), forKey: .to) }
try message.fromPrior.map { try container.encode($0, forKey: .fromPrior) }
try message.thid.map { try container.encode($0, forKey: .thid) }
try message.pthid.map { try container.encode($0, forKey: .pthid) }
}

init(message: Message) {
self.message = message
}

public init(from decoder: Decoder) throws {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let id = try container.decode(String.self, forKey: .id)
let piuri = try container.decode(String.self, forKey: .piuri)
Expand All @@ -45,13 +51,13 @@ extension Message: Codable {
let expiresTimePlus = try container.decode(Date.self, forKey: .expiresTimePlus)
let attachments = try container.decode([AttachmentDescriptor].self, forKey: .attachments)
let ack = try container.decode([String].self, forKey: .ack)
let from = try? container.decode(DID.self, forKey: .from)
let to = try? container.decode(DID.self, forKey: .to)
let from = try? container.decode(CodableDID.self, forKey: .from).did
let to = try? container.decode(CodableDID.self, forKey: .to).did
let fromPrior = try? container.decode(String.self, forKey: .fromPrior)
let thid = try? container.decode(String.self, forKey: .thid)
let pthid = try? container.decode(String.self, forKey: .pthid)

self.init(
self.init(message: .init(
id: id,
piuri: piuri,
from: from,
Expand All @@ -65,6 +71,6 @@ extension Message: Codable {
thid: thid,
pthid: pthid,
ack: ack
)
))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ extension CDMessageDAO: MessageProvider {

private extension CDMessage {
func toDomain() throws -> Message {
return try JSONDecoder().decode(Message.self, from: dataJson)
return try JSONDecoder().decode(CodableMessage.self, from: dataJson).message
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private extension CDMessage {
self.from = msg.from?.string
self.to = msg.to?.string
self.type = msg.piuri
self.dataJson = try JSONEncoder().encode(msg)
self.dataJson = try JSONEncoder().encode(CodableMessage(message: msg))
self.createdTime = msg.createdTime
self.pair = pair
}
Expand Down
2 changes: 1 addition & 1 deletion PrismAgent/Tests/HandshakeRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class HandshakeRequestTests: XCTestCase {
XCTAssertEqual(message.from, request.from)
XCTAssertEqual(message.to, request.to)
XCTAssertEqual(message.thid, request.thid)
let decodedBody = try JSONDecoder().decode(HandshakeRequest.Body.self, from: message.body)
let decodedBody = try JSONDecoder.didComm().decode(HandshakeRequest.Body.self, from: message.body)
XCTAssertEqual(decodedBody, request.body)
}

Expand Down
73 changes: 73 additions & 0 deletions PrismAgent/Tests/Helper/Message+Testing.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,78 @@
@testable import Domain
import Foundation

import Domain
import Foundation

extension Message: Codable {
enum CodingKeys: String, CodingKey {
case id
case piuri
case from
case to
case fromPrior
case extraHeaders
case createdTime
case expiresTimePlus
case attachments
case thid
case pthid
case ack
case body
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(piuri, forKey: .piuri)
try container.encode(body.base64UrlEncodedString(), forKey: .body)
try container.encode(extraHeaders, forKey: .extraHeaders)
try container.encode(createdTime, forKey: .createdTime)
try container.encode(expiresTimePlus, forKey: .expiresTimePlus)
try container.encode(attachments, forKey: .attachments)
try container.encode(ack, forKey: .ack)
try from.map { try container.encode($0.string, forKey: .from) }
try to.map { try container.encode($0.string, forKey: .to) }
try fromPrior.map { try container.encode($0, forKey: .fromPrior) }
try thid.map { try container.encode($0, forKey: .thid) }
try pthid.map { try container.encode($0, forKey: .pthid) }
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let id = try container.decode(String.self, forKey: .id)
let piuri = try container.decode(String.self, forKey: .piuri)
let body = try container.decode(String.self, forKey: .body)
let extraHeaders = try container.decode([String: String].self, forKey: .extraHeaders)
let createdTime = try container.decode(Date.self, forKey: .createdTime)
let expiresTimePlus = try container.decode(Date.self, forKey: .expiresTimePlus)
let attachments = try container.decode([AttachmentDescriptor].self, forKey: .attachments)
let ack = try container.decode([String].self, forKey: .ack)
let from = try? container.decode(String.self, forKey: .from)
let to = try? container.decode(String.self, forKey: .to)
let fromPrior = try? container.decode(String.self, forKey: .fromPrior)
let thid = try? container.decode(String.self, forKey: .thid)
let pthid = try? container.decode(String.self, forKey: .pthid)

self.init(
id: id,
piuri: piuri,
from: try from.map { try DID(string: $0) },
to: try to.map { try DID(string: $0) },
fromPrior: fromPrior,
body: Data(fromBase64URL: body)!,
extraHeaders: extraHeaders,
createdTime: createdTime,
expiresTimePlus: expiresTimePlus,
attachments: attachments,
thid: thid,
pthid: pthid,
ack: ack
)
}
}


extension Message: Equatable {
public static func == (lhs: Domain.Message, rhs: Domain.Message) -> Bool {
lhs.id == rhs.id && lhs.piuri == rhs.piuri
Expand Down
2 changes: 2 additions & 0 deletions PrismAgent/Tests/ProposeCredentialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ final class ProposeCredentialTests: XCTestCase {
let toDID = DID(index: 1)
let validProposeCredential = ProposeCredential(
body: .init(
goalCode: "Test1",
comment: "Test1",
credentialPreview: .init(
attributes: [
.init(
Expand Down
6 changes: 4 additions & 2 deletions PrismAgent/Tests/RequestCredentialTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ final class RequestCredentialTests: XCTestCase {
let toDID = DID(index: 1)
let validRequestCredential = RequestCredential(
body: .init(
goalCode: "test1",
comment: "test1",
formats: [
.init(
attachId: "test1",
format: "test")
]
),
attachments: [],
attachments: [
],
thid: "1",
from: fromDID,
to: toDID
)
let requestMessage = try validRequestCredential.makeMessage()

let testRequestCredential = try RequestCredential(fromMessage: requestMessage)
XCTAssertEqual(validRequestCredential, testRequestCredential)
}
Expand Down

0 comments on commit 85180b4

Please sign in to comment.