Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

CBL-5736: Implement copy for MutableDocument and test #3340

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions Objective-C/CBLMutableDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ NS_ASSUME_NONNULL_BEGIN
json: (NSString*)json
error: (NSError**)error;

/**
Returns a mutable copy of the document.
@return The CBLMutableDocument object.
*/
- (CBLMutableDocument*) copy;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be wrong as it has been awhile. I don't remember if we need to explicity define this as the copy is a part of NSObject protocol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've searched for it and found that is part of NSObject now. I've tried it before in CBL and copy didnt work (the reason I've filed the ticket in the first place...)

@end

NS_ASSUME_NONNULL_END
4 changes: 4 additions & 0 deletions Objective-C/CBLMutableDocument.mm
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ - (CBLMutableDocument*) mutableCopyWithZone: (NSZone *)zone {
return [[CBLMutableDocument alloc] initAsCopyWithDocument: self dict: _dict];
}

- (CBLMutableDocument*) copy {
return [self mutableCopyWithZone: nil];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember if the default copy calls mutableCopyWithZone or not. Can you double check?

}

#pragma mark - CBLMutableDictionary

- (void) setValue: (nullable id)value forKey: (NSString*)key {
Expand Down
16 changes: 16 additions & 0 deletions Objective-C/Tests/DocumentTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,22 @@ - (void) testDocumentResaveInAnotherCollection {
}];
}

- (void) testMutableDocumentCopy {
NSError* err;
CBLCollection* defaultCollection = [self.db defaultCollection: &err];

CBLMutableDocument* doc = [[CBLMutableDocument alloc] initWithID: @"doc1"];
[doc setString: @"first" forKey: @"one"];
CBLMutableDocument* docCopy = [doc copy];
AssertEqual([docCopy stringForKey:@"one"], @"first");

[doc setString: @"second" forKey: @"two"];
Assert([defaultCollection saveDocument:doc error: &err]);
docCopy = [[[defaultCollection documentWithID: @"doc1" error: &err] toMutable] copy];
AssertEqual([docCopy stringForKey:@"one"], @"first");
AssertEqual([docCopy stringForKey:@"two"], @"second");
}

#pragma mark - Timestamp & Revision history

/** https://github.com/couchbaselabs/couchbase-lite-api/blob/master/spec/tests/T0005-Version-Vector.md */
Expand Down
8 changes: 8 additions & 0 deletions Swift/MutableDocument.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,18 @@ public final class MutableDocument : Document, MutableDictionaryProtocol {
/// Returns the same MutableDocument object.
///
/// - Returns: The MutableDocument object.
@available(*, deprecated, message: "Use mutableDocument.copy() instead.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't deprecate this as toMutable() is our cross-platform API.

public override func toMutable() -> MutableDocument {
return self;
}

/// Returns the same MutableDocument object.
///
/// - Returns: The MutableDocument object.
public func copy() -> MutableDocument {
return self;
}

// MARK: Type Setters

/// Set a value for the given key. Allowed value types are Array, Date, Dictionary,
Expand Down
13 changes: 13 additions & 0 deletions Swift/Tests/DocumentTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,19 @@ class DocumentTest: CBLTestCase {
}
}

func testCopy() throws {
let doc = MutableDocument(id: "doc1")
doc.setValue("first", forKey: "one")
var docCopy = doc.copy()
assert(docCopy.value(forKey: "one") as! String == "first")

doc.setValue("second", forKey: "two")
try defaultCollection!.save(document: doc)
docCopy = try defaultCollection!.document(id: "doc1")!.toMutable().copy()
assert(docCopy.value(forKey: "one") as! String == "first")
assert(docCopy.value(forKey: "two") as! String == "second")
}

// MARK: toJSONTimestamp & Revision history

// https://github.com/couchbaselabs/couchbase-lite-api/blob/master/spec/tests/T0005-Version-Vector.md
Expand Down
Loading