diff --git a/.changeset/swift-api-guidelines.md b/.changeset/swift-api-guidelines.md new file mode 100644 index 0000000..9738631 --- /dev/null +++ b/.changeset/swift-api-guidelines.md @@ -0,0 +1,5 @@ +--- +"@germ-network/atprototypes": minor +--- + +Align public API names with Swift conventions, including `queryItems` and `rawValue`, while retaining deprecated compatibility accessors where practical. `QueryParametrizable` conformers must now implement `queryItems`; existing calls to `asQueryItems()` remain available as a deprecated forwarding method. diff --git a/Package.resolved b/Package.resolved index 68f21d5..b796277 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,13 +1,13 @@ { - "originHash" : "e48cc519ddde5ca2192a0cd14745d22324c723f89809c97ab71b3216e086b858", + "originHash" : "7d23820ec152f17168b1c36aa8e0c6048f03a389cae013383abdf3f8d6891341", "pins" : [ { "identity" : "germconvenience", "kind" : "remoteSourceControl", "location" : "https://github.com/germ-network/GermConvenience.git", "state" : { - "revision" : "305053c5194bfd129a3feda8fe214ec33addee03", - "version" : "0.10.0" + "revision" : "6f9f44970d38889a02e8c3b2e51d0261c67237e2", + "version" : "0.15.0" } }, { diff --git a/Package.swift b/Package.swift index febb3c7..87d2598 100644 --- a/Package.swift +++ b/Package.swift @@ -24,8 +24,7 @@ let package = Package( .package(url: "https://github.com/swift-libp2p/swift-bases.git", from: "0.2.0"), .package( url: "https://github.com/germ-network/GermConvenience.git", - // 0.10.0 is its swift-crypto-5 release — the revision pin drops. - from: "0.10.0" + .upToNextMinor(from: "0.15.0") ), .package( url: "https://github.com/apple/swift-crypto.git", diff --git a/Sources/AtprotoTypes/Atproto/ATURI.swift b/Sources/AtprotoTypes/Atproto/ATURI.swift index 6a7f625..ca60091 100644 --- a/Sources/AtprotoTypes/Atproto/ATURI.swift +++ b/Sources/AtprotoTypes/Atproto/ATURI.swift @@ -52,7 +52,7 @@ extension Atproto { self.authority = try LexiconString.AtIdentifier( rawValue: String(components[0]) - ).tryUnwrap + ).unwrapped self.collection = components.count >= 2 ? NSID(rawValue: String(components[1])) diff --git a/Sources/AtprotoTypes/Atproto/CID.swift b/Sources/AtprotoTypes/Atproto/CID.swift index 4da73dc..9f50c53 100644 --- a/Sources/AtprotoTypes/Atproto/CID.swift +++ b/Sources/AtprotoTypes/Atproto/CID.swift @@ -12,7 +12,7 @@ import Foundation //https://atproto.com/specs/data-model#link-and-cid-formats extension Atproto { - public struct CID: Sendable, Hashable { + public struct CID: StringRepresentable { //todo: further parse components of the CID data such as the hash let bytes: Data @@ -34,11 +34,14 @@ extension Atproto { bytes = try Base32.decode(body) } - public var string: String { + public var rawValue: String { // CID is DASL-compatible (https://atproto.com/specs/data-model) // and DASL CID uses lowercase base-32 (https://dasl.ing/cid.html) "b" + Base32.encode(bytes, options: .letterCase(.lower), .pad(false)) } + + @available(*, deprecated, renamed: "rawValue") + public var string: String { rawValue } } } @@ -50,6 +53,6 @@ extension Atproto.CID: Codable { public func encode(to encoder: any Encoder) throws { var container = encoder.singleValueContainer() - try container.encode(self.string) + try container.encode(self.rawValue) } } diff --git a/Sources/AtprotoTypes/Atproto/Handle.swift b/Sources/AtprotoTypes/Atproto/Handle.swift index 92f08ad..aed8295 100644 --- a/Sources/AtprotoTypes/Atproto/Handle.swift +++ b/Sources/AtprotoTypes/Atproto/Handle.swift @@ -35,10 +35,6 @@ extension Atproto { rawValue = knownValue } - var string: String { - rawValue - } - public enum Errors: Error, LocalizedError { case tooLong case badHandle diff --git a/Sources/AtprotoTypes/Atproto/PLC/PLCLogVerifying.swift b/Sources/AtprotoTypes/Atproto/PLC/PLCLogVerifying.swift index 38670f0..ae7e20f 100644 --- a/Sources/AtprotoTypes/Atproto/PLC/PLCLogVerifying.swift +++ b/Sources/AtprotoTypes/Atproto/PLC/PLCLogVerifying.swift @@ -48,7 +48,7 @@ extension Atproto.PLC { ///in hand rather than normalized away. public init(cid claimed: String, operationJSON: String, nullified: Bool) throws { let parsed = try Atproto.CID(string: claimed) - guard parsed.string == claimed else { + guard parsed.rawValue == claimed else { throw Errors.nonCanonicalCID(claimed) } self.init(cid: parsed, operationJSON: operationJSON, nullified: nullified) diff --git a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Identity/ResolveHandle.swift b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Identity/ResolveHandle.swift index 637be80..a3354c9 100644 --- a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Identity/ResolveHandle.swift +++ b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Identity/ResolveHandle.swift @@ -29,7 +29,7 @@ extension Lexicon.Com.Atproto.Identity { self.handle = handle } - public func asQueryItems() -> [URLQueryItem] { + public var queryItems: [URLQueryItem] { [.init(name: "handle", value: handle.rawValue)] } } diff --git a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/GetRecord.swift b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/GetRecord.swift index e04dcd5..35b7753 100644 --- a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/GetRecord.swift +++ b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/GetRecord.swift @@ -58,7 +58,7 @@ extension Lexicon.Com.Atproto.Repo { self.cid = cid } - public func asQueryItems() -> [URLQueryItem] { + public var queryItems: [URLQueryItem] { var base: [URLQueryItem] = [ .init(name: "repo", value: repo.rawValue), .init( @@ -67,7 +67,7 @@ extension Lexicon.Com.Atproto.Repo { .init(name: "rkey", value: rkey.rawValue), ] if let cid { - base.append(.init(name: "cid", value: cid.string)) + base.append(.init(name: "cid", value: cid.rawValue)) } return base diff --git a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/ListRecords.swift b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/ListRecords.swift index ff52b73..0a4809e 100644 --- a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/ListRecords.swift +++ b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/ListRecords.swift @@ -56,7 +56,7 @@ extension Lexicon.Com.Atproto.Repo { } } - public func asQueryItems() -> [URLQueryItem] { + public var queryItems: [URLQueryItem] { var base: [URLQueryItem] = [ .init(name: "repo", value: repo.rawValue), .init( diff --git a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Sync/GetBlob.swift b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Sync/GetBlob.swift index cfd2df2..7a089fa 100644 --- a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Sync/GetBlob.swift +++ b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Sync/GetBlob.swift @@ -39,10 +39,10 @@ extension Lexicon.Com.Atproto.Sync { self.cid = cid } - public func asQueryItems() -> [URLQueryItem] { + public var queryItems: [URLQueryItem] { return [ .init(name: "did", value: did.rawValue), - .init(name: "cid", value: cid.string), + .init(name: "cid", value: cid.rawValue), ] } } diff --git a/Sources/AtprotoTypes/LexiconTypes/Strings/AtIdentifier.swift b/Sources/AtprotoTypes/LexiconTypes/Strings/AtIdentifier.swift index 1e0e2e8..c527592 100644 --- a/Sources/AtprotoTypes/LexiconTypes/Strings/AtIdentifier.swift +++ b/Sources/AtprotoTypes/LexiconTypes/Strings/AtIdentifier.swift @@ -20,7 +20,7 @@ extension LexiconString { //over the wire, passed as a string public var rawValue: String { switch self { - case .handle(let handle): handle.string + case .handle(let handle): handle.rawValue case .did(let did): did.rawValue } } diff --git a/Sources/AtprotoTypes/LexiconTypes/Union.swift b/Sources/AtprotoTypes/LexiconTypes/Union.swift index f3f8e9b..fc954b2 100644 --- a/Sources/AtprotoTypes/LexiconTypes/Union.swift +++ b/Sources/AtprotoTypes/LexiconTypes/Union.swift @@ -29,8 +29,8 @@ extension LexiconUnion { let container = try decoder.container(keyedBy: TypeHeader.self) let ref = try container.decode(Atproto.Ref.self, forKey: .type) - let type = try Self.members[ref].tryUnwrap( - LexionUnionError.unknownType(ref) + let type = try Self.members[ref].unwrap( + or: LexionUnionError.unknownType(ref) ) try self.init(object: try type.init(from: decoder)) diff --git a/Sources/AtprotoTypes/XRPC/ResponseParsing.swift b/Sources/AtprotoTypes/XRPC/ResponseParsing.swift index 2ab0ffb..17c91f9 100644 --- a/Sources/AtprotoTypes/XRPC/ResponseParsing.swift +++ b/Sources/AtprotoTypes/XRPC/ResponseParsing.swift @@ -120,10 +120,10 @@ extension Atproto.XRPC.ResponseParsing { private static func parseSuccess(body: Data) throws -> Output { switch Output.self { case is Data.Type: - return try (body as? Output).tryUnwrap + return try (body as? Output).unwrapped case is Data?.Type: let result = body.isEmpty ? nil : body - return try (result as? Output).tryUnwrap + return try (result as? Output).unwrapped default: return try JSONDecoder.atproto .decode(Output.self, from: body) diff --git a/Sources/AtprotoTypes/XRPC/XRPC.swift b/Sources/AtprotoTypes/XRPC/XRPC.swift index b628e7f..be04c58 100644 --- a/Sources/AtprotoTypes/XRPC/XRPC.swift +++ b/Sources/AtprotoTypes/XRPC/XRPC.swift @@ -41,7 +41,7 @@ extension Atproto { //In the XRPC lexicon, parameter is optional public struct EmptyParameters: QueryParametrizable { - public func asQueryItems() -> [URLQueryItem] { + public var queryItems: [URLQueryItem] { [] } @@ -71,5 +71,12 @@ extension Atproto.XRPC.EndpointId { } public protocol QueryParametrizable: Sendable { - func asQueryItems() -> [URLQueryItem] + var queryItems: [URLQueryItem] { get } +} + +extension QueryParametrizable { + @available(*, deprecated, renamed: "queryItems") + public func asQueryItems() -> [URLQueryItem] { + queryItems + } } diff --git a/Sources/AtprotoTypesMocks/Atproto/DiDDocument.swift b/Sources/AtprotoTypesMocks/Atproto/DiDDocument.swift index b1e3d54..b3b6b4a 100644 --- a/Sources/AtprotoTypesMocks/Atproto/DiDDocument.swift +++ b/Sources/AtprotoTypesMocks/Atproto/DiDDocument.swift @@ -48,7 +48,7 @@ extension Atproto.DIDDocument.Verified: Mockable { let didDoc = try Atproto.DIDDocument.mock() return try didDoc.verified( - expecting: didDoc.unverifiedHandle.tryUnwrap, + expecting: didDoc.unverifiedHandle.unwrapped, did: try .init(string: didDoc.id) ) } diff --git a/Sources/AtprotoTypesMocks/Lexicon/Com/Atproto/Repo.swift b/Sources/AtprotoTypesMocks/Lexicon/Com/Atproto/Repo.swift index 8ce3b85..07eddd1 100644 --- a/Sources/AtprotoTypesMocks/Lexicon/Com/Atproto/Repo.swift +++ b/Sources/AtprotoTypesMocks/Lexicon/Com/Atproto/Repo.swift @@ -13,7 +13,7 @@ extension Lexicon.Com.Atproto.Repo.GetRecord.Output: Mockable where Result: Mock public static func mock() throws -> Self { .init( uri: .mock(), - cid: Atproto.CID.mock().string, + cid: Atproto.CID.mock().rawValue, value: try .mock() ) } diff --git a/Sources/AtprotoTypesVerify/CAR/CARv1.swift b/Sources/AtprotoTypesVerify/CAR/CARv1.swift index 953ae9e..3a7074c 100644 --- a/Sources/AtprotoTypesVerify/CAR/CARv1.swift +++ b/Sources/AtprotoTypesVerify/CAR/CARv1.swift @@ -73,7 +73,7 @@ public struct CARv1: Sendable { let payload = Data(try reader.read(blockLength - cidLength)) guard cid.matches(block: payload) else { - throw Atproto.Repo.ProofError.blockCIDMismatch(cid.string) + throw Atproto.Repo.ProofError.blockCIDMismatch(cid.rawValue) } //a duplicate block is harmless: it hashed to the same CID, so it is //the same bytes @@ -84,7 +84,7 @@ public struct CARv1: Sendable { public func block(_ cid: ContentIdentifier) throws -> Data { guard let payload = blocks[cid.bytes] else { - throw Atproto.Repo.ProofError.missingBlock(cid.string) + throw Atproto.Repo.ProofError.missingBlock(cid.rawValue) } return payload } diff --git a/Sources/AtprotoTypesVerify/ContentIdentifier.swift b/Sources/AtprotoTypesVerify/ContentIdentifier.swift index a2f6549..64c3a85 100644 --- a/Sources/AtprotoTypesVerify/ContentIdentifier.swift +++ b/Sources/AtprotoTypesVerify/ContentIdentifier.swift @@ -51,10 +51,13 @@ public struct ContentIdentifier: Sendable, Hashable { } ///Base32 lower, `b`-prefixed, matching `Atproto.CID.string`. - public var string: String { + public var rawValue: String { "b" + Self.base32Lower(bytes) } + @available(*, deprecated, renamed: "rawValue") + public var string: String { rawValue } + ///base32 lower, unpadded, over raw bytes. /// ///A CID's base32 covers its framed bytes and carries the `b` multibase diff --git a/Sources/AtprotoTypesVerify/DAGCBOR/DAGCBORValue+JSON.swift b/Sources/AtprotoTypesVerify/DAGCBOR/DAGCBORValue+JSON.swift index 287c30c..e59af19 100644 --- a/Sources/AtprotoTypesVerify/DAGCBOR/DAGCBORValue+JSON.swift +++ b/Sources/AtprotoTypesVerify/DAGCBOR/DAGCBORValue+JSON.swift @@ -102,7 +102,7 @@ extension DAGCBORValue { let base64 = String(data.base64EncodedString().prefix { $0 != "=" }) out += "{\"/\":{\"bytes\":\"" + base64 + "\"}}" case .link(let cid): - out += "{\"/\":\"" + cid.string + "\"}" + out += "{\"/\":\"" + cid.rawValue + "\"}" } } diff --git a/Sources/AtprotoTypesVerify/PLC/PLCVerifyError.swift b/Sources/AtprotoTypesVerify/PLC/PLCVerifyError.swift index 363e517..fbbbdd3 100644 --- a/Sources/AtprotoTypesVerify/PLC/PLCVerifyError.swift +++ b/Sources/AtprotoTypesVerify/PLC/PLCVerifyError.swift @@ -76,9 +76,9 @@ extension Atproto.PLC { case .genesisIsNullified: "The genesis operation is nullified" case .genesisHasNonNilPrev: "The genesis operation carries a prev" case .cidMismatch(let claimed, let computed): - "Entry claims \(claimed.string), its operation hashes to \(computed.string)" + "Entry claims \(claimed.rawValue), its operation hashes to \(computed.rawValue)" case .chainBroken(let expected, let found): - "prev is \(found?.string ?? "null"), expected \(expected.string)" + "prev is \(found?.rawValue ?? "null"), expected \(expected.rawValue)" case .operationAfterTombstone: "A live operation follows a plc_tombstone" case .badSignatureLength(let length): diff --git a/Sources/AtprotoTypesVerifyMocks/PLCFixture.swift b/Sources/AtprotoTypesVerifyMocks/PLCFixture.swift index fda8b1d..ee8e016 100644 --- a/Sources/AtprotoTypesVerifyMocks/PLCFixture.swift +++ b/Sources/AtprotoTypesVerifyMocks/PLCFixture.swift @@ -79,7 +79,7 @@ public enum PLCFixture { ) }) ), - (key: "prev", value: prev.map { .string($0.string) } ?? .null), + (key: "prev", value: prev.map { .string($0.rawValue) } ?? .null), ]) } @@ -106,7 +106,7 @@ public enum PLCFixture { public static func unsignedTombstone(prev: Atproto.CID) -> DAGCBORValue { .map([ (key: "type", value: .string("plc_tombstone")), - (key: "prev", value: .string(prev.string)), + (key: "prev", value: .string(prev.rawValue)), ]) } diff --git a/Tests/AtprotoTypesTests/ATProtoDIDTests.swift b/Tests/AtprotoTypesTests/ATProtoDIDTests.swift index 0a6a7cb..9234f4c 100644 --- a/Tests/AtprotoTypesTests/ATProtoDIDTests.swift +++ b/Tests/AtprotoTypesTests/ATProtoDIDTests.swift @@ -34,7 +34,7 @@ struct AtprotoDIDTests { let did: Atproto.DID } let wrapped = try Wrapper( - did: .init(rawValue: Self.exampleDidString).tryUnwrap + did: .init(rawValue: Self.exampleDidString).unwrapped ) let encoded = try JSONEncoder().encode(wrapped) let decoded = try JSONSerialization.jsonObject(with: encoded) diff --git a/Tests/AtprotoTypesTests/CIDTests.swift b/Tests/AtprotoTypesTests/CIDTests.swift index 6bc0aca..ccba90d 100644 --- a/Tests/AtprotoTypesTests/CIDTests.swift +++ b/Tests/AtprotoTypesTests/CIDTests.swift @@ -13,7 +13,7 @@ struct CIDTests { @Test func testDecode() async throws { let input = "bafyreibmcnkzbxtciyydktbatjlpgh5hollpov4zvpwtaaq353vyzfbxwu" let parsed = try Atproto.CID(string: input) - #expect(parsed.string.lowercased() == input.lowercased()) + #expect(parsed.rawValue.lowercased() == input.lowercased()) } } diff --git a/Tests/AtprotoTypesTests/PutRecordSwapEncodingTests.swift b/Tests/AtprotoTypesTests/PutRecordSwapEncodingTests.swift index f2f3082..2dcd72a 100644 --- a/Tests/AtprotoTypesTests/PutRecordSwapEncodingTests.swift +++ b/Tests/AtprotoTypesTests/PutRecordSwapEncodingTests.swift @@ -54,7 +54,7 @@ struct PutRecordSwapEncodingTests { func recordEncodesCIDString() throws { let cid = try Atproto.CID(string: Self.cidString) let object = try Self.json(Self.schema(swap: .record(cid))) - #expect(object["swapRecord"] as? String == cid.string) + #expect(object["swapRecord"] as? String == cid.rawValue) } @Test diff --git a/Tests/AtprotoTypesVerifyTests/CARTests.swift b/Tests/AtprotoTypesVerifyTests/CARTests.swift index 6a248b4..3afa0e0 100644 --- a/Tests/AtprotoTypesVerifyTests/CARTests.swift +++ b/Tests/AtprotoTypesVerifyTests/CARTests.swift @@ -39,7 +39,7 @@ struct CARTests { bytes: DAGCBOREncoder.encode(.string("tampered")) ) - #expect(throws: Atproto.Repo.ProofError.blockCIDMismatch(real.cid.string)) { + #expect(throws: Atproto.Repo.ProofError.blockCIDMismatch(real.cid.rawValue)) { try CARv1(RepoFixture.car(root: real.cid, blocks: [swapped])) } } @@ -87,7 +87,7 @@ struct CARTests { RepoFixture.car(root: present.cid, blocks: [present]) ) - #expect(throws: Atproto.Repo.ProofError.missingBlock(absent.cid.string)) { + #expect(throws: Atproto.Repo.ProofError.missingBlock(absent.cid.rawValue)) { try archive.block(absent.cid) } } diff --git a/Tests/AtprotoTypesVerifyTests/ContentIdentifierTests.swift b/Tests/AtprotoTypesVerifyTests/ContentIdentifierTests.swift index 40b884b..45481f0 100644 --- a/Tests/AtprotoTypesVerifyTests/ContentIdentifierTests.swift +++ b/Tests/AtprotoTypesVerifyTests/ContentIdentifierTests.swift @@ -46,9 +46,9 @@ struct ContentIdentifierTests { codec: .dagCBOR, block: Data("some block".utf8) ) - #expect(cid.string.hasPrefix("bafyrei")) - #expect(cid.atprotoCID.string == cid.string) - #expect(try Atproto.CID(string: cid.string).string == cid.string) + #expect(cid.rawValue.hasPrefix("bafyrei")) + #expect(cid.atprotoCID.rawValue == cid.rawValue) + #expect(try Atproto.CID(string: cid.rawValue).rawValue == cid.rawValue) } @Test("matches() recomputes rather than trusting the link") diff --git a/Tests/AtprotoTypesVerifyTests/MerkleSearchTreeTests.swift b/Tests/AtprotoTypesVerifyTests/MerkleSearchTreeTests.swift index 52b9901..381c0c7 100644 --- a/Tests/AtprotoTypesVerifyTests/MerkleSearchTreeTests.swift +++ b/Tests/AtprotoTypesVerifyTests/MerkleSearchTreeTests.swift @@ -215,7 +215,7 @@ struct MerkleSearchTreeTests { RepoFixture.car(root: node.cid, blocks: [value, node]) ) - #expect(throws: Atproto.Repo.ProofError.missingBlock(orphan.cid.string)) { + #expect(throws: Atproto.Repo.ProofError.missingBlock(orphan.cid.rawValue)) { try MerkleSearchTree.find( key: Tree.key("a"), root: node.cid, diff --git a/Tests/AtprotoTypesVerifyTests/PLCOperationEncodingTests.swift b/Tests/AtprotoTypesVerifyTests/PLCOperationEncodingTests.swift index 535c15e..abc5514 100644 --- a/Tests/AtprotoTypesVerifyTests/PLCOperationEncodingTests.swift +++ b/Tests/AtprotoTypesVerifyTests/PLCOperationEncodingTests.swift @@ -26,7 +26,9 @@ struct PLCOperationEncodingTests { let bytes = DAGCBOREncoder.encode(value) let cid = try ContentIdentifier.compute( codec: .dagCBOR, block: bytes) - #expect(cid.atprotoCID == entry.cid, "\(name): \(entry.cid.string)") + #expect( + cid.atprotoCID == entry.cid, + "\(name): \(entry.cid.rawValue)") } } } diff --git a/Tests/AtprotoTypesVerifyTests/RepoProofVerifierTests.swift b/Tests/AtprotoTypesVerifyTests/RepoProofVerifierTests.swift index a83200c..bdb29d5 100644 --- a/Tests/AtprotoTypesVerifyTests/RepoProofVerifierTests.swift +++ b/Tests/AtprotoTypesVerifyTests/RepoProofVerifierTests.swift @@ -110,11 +110,11 @@ struct RepoProofVerifierTests { #expect(proof.rev == "3lbwqrstuvwxy") //the returned CID is the one recomputed from the block #expect( - try proof.cid.string + try proof.cid.rawValue == ContentIdentifier.compute( codec: .dagCBOR, block: proof.block - ).string + ).rawValue ) } @@ -291,7 +291,7 @@ struct RepoProofVerifierTests { blocks: [node, commitBlock] ) - #expect(throws: Atproto.Repo.ProofError.missingBlock(orphan.cid.string)) { + #expect(throws: Atproto.Repo.ProofError.missingBlock(orphan.cid.rawValue)) { try Atproto.Repo.Verifier().verifyRecordProof( car: car, did: scenario.did,