From 5c8a9e51bfdfc8a35f28c0df111e8b440d3b08af Mon Sep 17 00:00:00 2001 From: Vlad Velicu Date: Mon, 18 Nov 2024 15:15:36 +0000 Subject: [PATCH] change/add copy api and simple test --- Objective-C/CBLMutableDocument.h | 6 ++++++ Objective-C/CBLMutableDocument.mm | 4 ++++ Objective-C/Tests/DocumentTest.m | 16 ++++++++++++++++ Swift/MutableDocument.swift | 8 ++++++++ Swift/Tests/DocumentTest.swift | 13 +++++++++++++ 5 files changed, 47 insertions(+) diff --git a/Objective-C/CBLMutableDocument.h b/Objective-C/CBLMutableDocument.h index 5d9f82dbe..b021b5361 100644 --- a/Objective-C/CBLMutableDocument.h +++ b/Objective-C/CBLMutableDocument.h @@ -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 diff --git a/Objective-C/CBLMutableDocument.mm b/Objective-C/CBLMutableDocument.mm index c626b0ff7..ee474caba 100644 --- a/Objective-C/CBLMutableDocument.mm +++ b/Objective-C/CBLMutableDocument.mm @@ -119,6 +119,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 { diff --git a/Objective-C/Tests/DocumentTest.m b/Objective-C/Tests/DocumentTest.m index 84244ea07..602cd14ca 100644 --- a/Objective-C/Tests/DocumentTest.m +++ b/Objective-C/Tests/DocumentTest.m @@ -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 */ diff --git a/Swift/MutableDocument.swift b/Swift/MutableDocument.swift index fc17ba3fc..7dfa1ff4b 100644 --- a/Swift/MutableDocument.swift +++ b/Swift/MutableDocument.swift @@ -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, diff --git a/Swift/Tests/DocumentTest.swift b/Swift/Tests/DocumentTest.swift index 025ee1800..0f9460560 100644 --- a/Swift/Tests/DocumentTest.swift +++ b/Swift/Tests/DocumentTest.swift @@ -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