Skip to content

Commit 249b066

Browse files
author
Pouya Yarandi
committed
Change path contain and canonical algorithm
1 parent af441d1 commit 249b066

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

Sources/SwiftProtobuf/Google_Protobuf_FieldMask+Extensions.swift

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,12 @@ extension Google_Protobuf_FieldMask {
242242
public var canonical: Google_Protobuf_FieldMask {
243243
var mask = Google_Protobuf_FieldMask()
244244
let sortedPaths = self.paths.sorted()
245-
var set = Set<String>()
246245
for path in sortedPaths {
247-
if !contains(path, in: set) {
248-
set.insert(path)
246+
if let lastPath = mask.paths.last {
247+
if path != lastPath, !path.hasPrefix("\(lastPath).") {
248+
mask.paths.append(path)
249+
}
250+
} else {
249251
mask.paths.append(path)
250252
}
251253
}
@@ -314,7 +316,12 @@ extension Google_Protobuf_FieldMask {
314316
/// - Parameter path: Path to be checked.
315317
/// - Returns: Boolean determines is path covered.
316318
public func contains(_ path: String) -> Bool {
317-
contains(path, in: pathsSet)
319+
for _path in paths {
320+
if path.hasPrefix("\(_path).") || _path == path {
321+
return true
322+
}
323+
}
324+
return false
318325
}
319326

320327
// Set containing paths of FieldMask
@@ -328,18 +335,6 @@ extension Google_Protobuf_FieldMask {
328335
comps[0...$0].joined(separator: ".")
329336
}
330337
}
331-
332-
private func contains(
333-
_ path: String,
334-
in set: Set<String>
335-
) -> Bool {
336-
for level in levels(path: path) {
337-
if set.contains(level) {
338-
return true
339-
}
340-
}
341-
return false
342-
}
343338
}
344339

345340
extension Google_Protobuf_FieldMask {

Tests/SwiftProtobufTests/Test_FieldMask.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,17 @@ final class Test_FieldMask: XCTestCase, PBTestHelpers {
206206
// Checks canonincal form of field mask.
207207
// 1. Sub-message with parent in the paths should be excluded.
208208
// 2. Canonincal form should be sorted.
209-
// 3. More nested levels of paths.
209+
// 3. More nested levels of paths with duplicates.
210+
// 4. Two siblings with their parent should be excluded.
210211
func testCanonicalFieldMask() {
211212
let m1 = Google_Protobuf_FieldMask(protoPaths: ["a.b", "a", "b"])
212213
XCTAssertEqual(m1.canonical.paths, ["a", "b"])
213214
let m2 = Google_Protobuf_FieldMask(protoPaths: ["b", "a"])
214215
XCTAssertEqual(m2.canonical.paths, ["a", "b"])
215-
let m3 = Google_Protobuf_FieldMask(protoPaths: ["c", "a.b.c", "a.b", "a.b.c.d"])
216+
let m3 = Google_Protobuf_FieldMask(protoPaths: ["c", "a.b.c", "a.b", "a.b", "a.b.c.d"])
216217
XCTAssertEqual(m3.canonical.paths, ["a.b", "c"])
218+
let m4 = Google_Protobuf_FieldMask(protoPaths: ["a.c", "a", "a.b"])
219+
XCTAssertEqual(m4.canonical.paths, ["a"])
217220
}
218221

219222
// Checks `addPath` func of fieldMask with:

0 commit comments

Comments
 (0)