Skip to content

Commit

Permalink
fixing followup issue not resolves with #55 to fully resolve #54
Browse files Browse the repository at this point in the history
  • Loading branch information
heckj committed Jul 30, 2023
1 parent 1303f4d commit ad56019
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
21 changes: 13 additions & 8 deletions Sources/Automerge/Codable/Encoding/AutomergeEncoderImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,28 @@ final class AutomergeEncoderImpl {
}
}
case .Index:
precondition(highestUnkeyedIndexWritten != nil)
guard let highestUnkeyedIndexWritten else {
return
var highestIndexWritten: Int64 = -1
if let highestUnkeyedIndexWritten {
// If highestUnkeyedIndexWritten is nil, then a list/array was encoded
// with no items within it.
highestIndexWritten = Int64(highestUnkeyedIndexWritten)
}
// Remove index elements that exist in this objectId beyond
// the max written during encode. (allow arrays to 'shrink')
var highestAutomergeIndex = document.length(obj: objectIdForContainer) - 1
while highestAutomergeIndex > highestUnkeyedIndexWritten {
let lengthOfAutomergeContainer = document.length(obj: objectIdForContainer)
if lengthOfAutomergeContainer > 0 {
var highestAutomergeIndex = Int64(lengthOfAutomergeContainer - 1)
// Remove index elements that exist in this objectId beyond
// the max written during encode. (allow arrays to 'shrink')
while highestAutomergeIndex > highestIndexWritten {
do {
try document.delete(obj: objectIdForContainer, index: highestAutomergeIndex)
try document.delete(obj: objectIdForContainer, index: UInt64(highestAutomergeIndex))
highestAutomergeIndex -= 1
} catch {
fatalError(
"Unable to delete index position \(highestAutomergeIndex) during post-encode cleanup: \(error)"
)
}
}
}
case .Value:
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@ final class AutomergeArrayEncodeDecodeTests: XCTestCase {
// ("StructWithArray(names: ["one", "one", "two"])") is not equal to
// ("StructWithArray(names: ["one"])")
}

func testEmptyArrayEncode() throws {
// illustrates https://github.com/automerge/automerge-swift/issues/54

struct StructWithArray: Codable, Equatable {
var names: [String] = []
}

let encoder = AutomergeEncoder(doc: doc)
let decoder = AutomergeDecoder(doc: doc)
var sample = StructWithArray()
try encoder.encode(sample)

sample.names.append("one")
sample.names.append("two")
try encoder.encode(sample)
let replica = try decoder.decode(StructWithArray.self)
XCTAssertEqual(replica, sample)

_ = sample.names.popLast()
_ = sample.names.popLast()
try encoder.encode(sample)
let secondReplica = try decoder.decode(StructWithArray.self)
XCTAssertEqual(secondReplica, sample)
// XCTAssertEqual failed:
// ("StructWithArray(names: ["one", "one", "two"])") is not equal to
// ("StructWithArray(names: ["one"])")
}
}

0 comments on commit ad56019

Please sign in to comment.