Skip to content

Commit 37f1872

Browse files
author
Pouya Yarandi
committed
Implement merge option
1 parent 095dde2 commit 37f1872

File tree

3 files changed

+122
-63
lines changed

3 files changed

+122
-63
lines changed

Sources/SwiftProtobuf/Message+FieldMask.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ extension Message {
3434
}
3535
}
3636

37+
public enum MergeOption: Equatable {
38+
case replaceRepeatedFields
39+
}
40+
3741
extension Message {
3842

3943
/// Merges fields specified in a FieldMask into another message.
@@ -43,16 +47,23 @@ extension Message {
4347
/// - fieldMask: FieldMask specifies which fields should be merged.
4448
public mutating func merge(
4549
to source: Self,
46-
fieldMask: Google_Protobuf_FieldMask
50+
fieldMask: Google_Protobuf_FieldMask,
51+
mergeOptions: [MergeOption] = []
4752
) throws {
4853
var source = source
4954
var copy = self
5055
var pathToValueMap: [String: Any?] = [:]
56+
let replaceRepeatedFields = mergeOptions
57+
.contains(.replaceRepeatedFields)
5158
for path in fieldMask.paths {
5259
pathToValueMap[path] = try source.get(path: path)
5360
}
5461
for (path, value) in pathToValueMap {
55-
try copy.set(path: path, value: value)
62+
try copy.set(
63+
path: path,
64+
value: value,
65+
replaceRepeatedFields: replaceRepeatedFields
66+
)
5667
}
5768
self = copy
5869
}

0 commit comments

Comments
 (0)