Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-api-guidelines.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion Sources/AtprotoTypes/Atproto/ATURI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down
9 changes: 6 additions & 3 deletions Sources/AtprotoTypes/Atproto/CID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 }
}
}

Expand All @@ -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)
}
}
4 changes: 0 additions & 4 deletions Sources/AtprotoTypes/Atproto/Handle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ extension Atproto {
rawValue = knownValue
}

var string: String {
rawValue
}

public enum Errors: Error, LocalizedError {
case tooLong
case badHandle
Expand Down
2 changes: 1 addition & 1 deletion Sources/AtprotoTypes/Atproto/PLC/PLCLogVerifying.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/GetRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions Sources/AtprotoTypes/Lexicon/Com/Atproto/Sync/GetBlob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AtprotoTypes/LexiconTypes/Union.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions Sources/AtprotoTypes/XRPC/ResponseParsing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 9 additions & 2 deletions Sources/AtprotoTypes/XRPC/XRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
[]
}

Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Sources/AtprotoTypesMocks/Atproto/DiDDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/AtprotoTypesMocks/Lexicon/Com/Atproto/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/AtprotoTypesVerify/CAR/CARv1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/AtprotoTypesVerify/ContentIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/AtprotoTypesVerify/DAGCBOR/DAGCBORValue+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "\"}"
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/AtprotoTypesVerify/PLC/PLCVerifyError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions Sources/AtprotoTypesVerifyMocks/PLCFixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
])
}

Expand All @@ -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)),
])
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/AtprotoTypesTests/ATProtoDIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Tests/AtprotoTypesTests/CIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

}
2 changes: 1 addition & 1 deletion Tests/AtprotoTypesTests/PutRecordSwapEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Tests/AtprotoTypesVerifyTests/CARTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
}
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/AtprotoTypesVerifyTests/ContentIdentifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion Tests/AtprotoTypesVerifyTests/MerkleSearchTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)")
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/AtprotoTypesVerifyTests/RepoProofVerifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down Expand Up @@ -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,
Expand Down
Loading