Skip to content

Commit

Permalink
change/add copy api and simple test
Browse files Browse the repository at this point in the history
  • Loading branch information
velicuvlad committed Nov 4, 2024
1 parent c2e3d73 commit b1a8221
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
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;
@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 @@ -120,6 +120,10 @@ - (CBLMutableDocument*) mutableCopyWithZone: (NSZone *)zone {
return [[CBLMutableDocument alloc] initAsCopyWithDocument: self dict: _dict];
}

- (CBLMutableDocument*) copy {
return [self mutableCopyWithZone: nil];
}

#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 clang diagnostic pop

@end
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.")
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 @@ -1855,4 +1855,17 @@ 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")
}
}
2 changes: 1 addition & 1 deletion vendor/couchbase-lite-core

0 comments on commit b1a8221

Please sign in to comment.