Skip to content

Commit 283b2c5

Browse files
committed
feat(did): add missing conformances for identifiable and CustomStringConvertible
1 parent d002e88 commit 283b2c5

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

Sources/DIDCore/DID/DID.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public struct DID: Sendable, Hashable, Equatable {
3838
}
3939
}
4040

41+
extension DID: Identifiable {
42+
public var id: String {
43+
description
44+
}
45+
}
46+
4147
extension DID: ExpressibleByStringLiteral {
4248

4349
public init(stringLiteral value: String) {

Sources/DIDCore/DID/DIDUrl.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ public struct DIDUrl: Sendable, Hashable, Equatable {
7070
}
7171
}
7272

73+
extension DIDUrl: Identifiable {
74+
public var id: String {
75+
description
76+
}
77+
}
78+
7379
extension DIDUrl: ExpressibleByStringLiteral {
7480
public init(stringLiteral value: String) {
7581
guard let didUrl = DIDUrl(from: value) else {
@@ -78,3 +84,13 @@ extension DIDUrl: ExpressibleByStringLiteral {
7884
self = didUrl
7985
}
8086
}
87+
88+
extension DIDUrl: CustomStringConvertible {
89+
public var description: String {
90+
guard var url = URLComponents(string: did.description) else { return "" }
91+
if let path { url.path = path }
92+
url.queryItems = queries.map { .init(name: $0, value: $1) }
93+
url.fragment = fragment
94+
return url.string ?? ""
95+
}
96+
}

Sources/DIDCore/DIDDocument/DIDDocument.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ extension DIDDocument.VerificationMethod: Codable {
201201
}
202202
}
203203

204+
extension DIDDocument: Identifiable {}
205+
extension DIDDocument.VerificationMethod: Identifiable {}
206+
extension DIDDocument.Service: Identifiable {}
207+
204208
extension VerificationMaterialFormat {
205209

206210
init(fromKey: String) throws {

Tests/DIDCoreTests/DIDUrlParsingTests.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ final class DIDUrlParsingTests: XCTestCase {
1818
XCTAssertTrue(didUrl1?.queries.isEmpty ?? false)
1919
XCTAssertNil(didUrl1?.path)
2020
XCTAssertNil(didUrl1?.fragment)
21+
XCTAssertEqual("did:example:123456789abcdefghi", didUrl1)
2122

2223
let example2 = "did:example:123456/path"
2324
let didUrl2 = DIDUrl(from: example2)
@@ -27,6 +28,7 @@ final class DIDUrlParsingTests: XCTestCase {
2728
XCTAssertTrue(didUrl2?.queries.isEmpty ?? false)
2829
XCTAssertEqual(didUrl2?.path, "/path")
2930
XCTAssertNil(didUrl2?.fragment)
31+
XCTAssertEqual("did:example:123456/path", didUrl2)
3032

3133
let example3 = "did:example:123456?versionId=1"
3234
let didUrl3 = DIDUrl(from: example3)
@@ -36,6 +38,7 @@ final class DIDUrlParsingTests: XCTestCase {
3638
XCTAssertEqual(didUrl3?.queries["versionId"], "1")
3739
XCTAssertNil(didUrl3?.path)
3840
XCTAssertNil(didUrl3?.fragment)
41+
XCTAssertEqual("did:example:123456?versionId=1", didUrl3)
3942

4043
let example4 = "did:example:123#public-key-0"
4144
let didUrl4 = DIDUrl(from: example4)
@@ -45,6 +48,7 @@ final class DIDUrlParsingTests: XCTestCase {
4548
XCTAssertTrue(didUrl4?.queries.isEmpty ?? false)
4649
XCTAssertNil(didUrl4?.path)
4750
XCTAssertEqual(didUrl4?.fragment, "public-key-0")
51+
XCTAssertEqual("did:example:123#public-key-0", didUrl4)
4852

4953
let example5 = "did:example:123?service=agent&relativeRef=/credentials#degree"
5054
let didUrl5 = DIDUrl(from: example5)
@@ -55,5 +59,6 @@ final class DIDUrlParsingTests: XCTestCase {
5559
XCTAssertEqual(didUrl5?.queries["relativeRef"], "/credentials")
5660
XCTAssertNil(didUrl5?.path)
5761
XCTAssertEqual(didUrl5?.fragment, "degree")
62+
XCTAssertEqual("did:example:123?service=agent&relativeRef=/credentials#degree", didUrl5)
5863
}
5964
}

0 commit comments

Comments
 (0)