Skip to content

Commit 095dde2

Browse files
author
Pouya Yarandi
committed
Fix intersect and subtract
1 parent 795b22a commit 095dde2

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

Sources/SwiftProtobuf/Google_Protobuf_FieldMask+Extensions.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,10 @@ extension Google_Protobuf_FieldMask {
282282
) -> Google_Protobuf_FieldMask {
283283
let set = mask.pathsSet
284284
var _paths: [String] = []
285-
for path in paths where set.contains(path) {
285+
var _buffer = Set<String>()
286+
for path in paths where set.contains(path)
287+
&& !_buffer.contains(path) {
288+
_buffer.insert(path)
286289
_paths.append(path)
287290
}
288291
return .with { mask in
@@ -300,7 +303,10 @@ extension Google_Protobuf_FieldMask {
300303
) -> Google_Protobuf_FieldMask {
301304
let set = mask.pathsSet
302305
var _paths: [String] = []
303-
for path in paths where !set.contains(path) {
306+
var _buffer = Set<String>()
307+
for path in paths where !set.contains(path)
308+
&& !_buffer.contains(path) {
309+
_buffer.insert(path)
304310
_paths.append(path)
305311
}
306312
return .with { mask in

Tests/SwiftProtobufTests/Test_FieldMask.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
279279
let m7 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
280280
let m8 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
281281
XCTAssertEqual(m7.union(m8).paths, ["a", "b"])
282+
283+
let m9 = Google_Protobuf_FieldMask(protoPaths: ["a", "a"])
284+
let m10 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
285+
XCTAssertEqual(m9.union(m10).paths, ["a", "b"])
282286
}
283287

284288
// Checks `intersect` func of fieldMask.
@@ -298,6 +302,10 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
298302
let m7 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
299303
let m8 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
300304
XCTAssertEqual(m7.intersect(m8).paths, ["a", "b"])
305+
306+
let m9 = Google_Protobuf_FieldMask(protoPaths: ["a", "a"])
307+
let m10 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
308+
XCTAssertEqual(m9.intersect(m10).paths, ["a"])
301309
}
302310

303311
// Checks `substract` func of fieldMask.
@@ -317,6 +325,10 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
317325
let m7 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
318326
let m8 = Google_Protobuf_FieldMask(protoPaths: ["a", "b"])
319327
XCTAssertEqual(m7.subtract(m8).paths, [])
328+
329+
let m9 = Google_Protobuf_FieldMask(protoPaths: ["a", "a"])
330+
let m10 = Google_Protobuf_FieldMask(protoPaths: ["b"])
331+
XCTAssertEqual(m9.subtract(m10).paths, ["a"])
320332
}
321333

322334
}

0 commit comments

Comments
 (0)