Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

duplicate IDs and repository create #82

Merged
merged 1 commit into from
May 10, 2024
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
12 changes: 12 additions & 0 deletions Sources/AutomergeRepo/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ public enum Errors: Sendable {
self.msg = msg
}
}

/// The ID of the document already exists within the repository
public struct DuplicateID: Sendable, LocalizedError {
public var id: DocumentId
public var errorDescription: String? {
"The ID of the document \(id) already exists within the repository."
}

public init(id: DocumentId) {
self.id = id
}
}
}
9 changes: 9 additions & 0 deletions Sources/AutomergeRepo/Repo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,9 @@ public final class Repo {
/// - Returns: The Automerge document.
/// - Parameter id: The Id of the Automerge document.
public func create(id: DocumentId) async throws -> DocHandle {
if let existing = handles[id] {
throw Errors.DuplicateID(id: id)
}
let handle = InternalDocHandle(id: id, isNew: true, initialValue: Document())
handles[handle.id] = handle
docHandlePublisher.send(handle.snapshot())
Expand All @@ -480,6 +483,9 @@ public final class Repo {
/// - Parameter doc: The Automerge document to use for the new, shared document
/// - Returns: The Automerge document.
public func create(doc: Document, id: DocumentId? = nil) async throws -> DocHandle {
if let providedId = id, let existing = handles[providedId] {
throw Errors.DuplicateID(id: providedId)
}
let creationId = id ?? DocumentId()
let handle = InternalDocHandle(id: creationId, isNew: true, initialValue: doc)
handles[handle.id] = handle
Expand All @@ -492,6 +498,9 @@ public final class Repo {
/// - Parameter data: The data to load as an Automerge document for the new, shared document.
/// - Returns: The Automerge document.
public func create(data: Data, id: DocumentId? = nil) async throws -> DocHandle {
if let providedId = id, let existing = handles[providedId] {
throw Errors.DuplicateID(id: providedId)
}
let creationId = id ?? DocumentId()
let handle = try InternalDocHandle(id: creationId, isNew: true, initialValue: Document(data))
handles[handle.id] = handle
Expand Down
Loading