Skip to content

Commit aae790b

Browse files
author
Pouya Yarandi
committed
Replace GetPathDecoder with PathVisitor
1 parent dcfa442 commit aae790b

File tree

6 files changed

+563
-383
lines changed

6 files changed

+563
-383
lines changed

Sources/SwiftProtobuf/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ add_library(SwiftProtobuf
2626
FieldTag.swift
2727
FieldTypes.swift
2828
field_mask.pb.swift
29-
GetPathDecoder.swift
3029
Google_Protobuf_Any+Extensions.swift
3130
Google_Protobuf_Any+Registry.swift
3231
Google_Protobuf_Duration+Extensions.swift
@@ -58,11 +57,12 @@ add_library(SwiftProtobuf
5857
Message.swift
5958
MessageExtension.swift
6059
NameMap.swift
60+
PathDecoder.swift
61+
PathVisitor.swift
6162
ProtobufAPIVersionCheck.swift
6263
ProtobufMap.swift
6364
ProtoNameProviding.swift
6465
SelectiveVisitor.swift
65-
SetPathDecoder.swift
6666
SimpleExtensionMap.swift
6767
source_context.pb.swift
6868
StringUtils.swift

Sources/SwiftProtobuf/GetPathDecoder.swift

Lines changed: 0 additions & 328 deletions
This file was deleted.

Sources/SwiftProtobuf/Message+FieldMask.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ extension Message {
2727
return message.hasPath(path: path)
2828
}
2929

30+
internal mutating func hasPath(path: String) -> Bool {
31+
do {
32+
try set(path: path, value: nil, mergeOption: .init())
33+
return true
34+
} catch let error as PathDecodingError {
35+
return error != .pathNotFound
36+
} catch {
37+
return false
38+
}
39+
}
40+
3041
internal mutating func isPathValid(
3142
_ path: String
3243
) -> Bool {
@@ -54,22 +65,20 @@ extension Message {
5465
/// Merges fields specified in a FieldMask into another message.
5566
///
5667
/// - Parameters:
57-
/// - source: Message should be merged to the original one.
68+
/// - source: Message that should be merged to the original one.
5869
/// - fieldMask: FieldMask specifies which fields should be merged.
5970
public mutating func merge(
6071
with source: Self,
6172
fieldMask: Google_Protobuf_FieldMask,
6273
mergeOption: Google_Protobuf_FieldMask.MergeOptions = .init()
6374
) throws {
64-
var source = source
65-
var pathToValueMap: [String: Any?] = [:]
75+
var visitor = PathVisitor<Self>()
76+
try source.traverse(visitor: &visitor)
77+
let values = visitor.values
6678
for path in fieldMask.paths {
67-
pathToValueMap[path] = try source.get(path: path)
68-
}
69-
for (path, value) in pathToValueMap {
7079
try? set(
7180
path: path,
72-
value: value,
81+
value: values[path],
7382
mergeOption: mergeOption
7483
)
7584
}

0 commit comments

Comments
 (0)