@@ -27,6 +27,17 @@ extension Message {
27
27
return message. hasPath ( path: path)
28
28
}
29
29
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
+
30
41
internal mutating func isPathValid(
31
42
_ path: String
32
43
) -> Bool {
@@ -54,22 +65,20 @@ extension Message {
54
65
/// Merges fields specified in a FieldMask into another message.
55
66
///
56
67
/// - Parameters:
57
- /// - source: Message should be merged to the original one.
68
+ /// - source: Message that should be merged to the original one.
58
69
/// - fieldMask: FieldMask specifies which fields should be merged.
59
70
public mutating func merge(
60
71
with source: Self ,
61
72
fieldMask: Google_Protobuf_FieldMask ,
62
73
mergeOption: Google_Protobuf_FieldMask . MergeOptions = . init( )
63
74
) 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
66
78
for path in fieldMask. paths {
67
- pathToValueMap [ path] = try source. get ( path: path)
68
- }
69
- for (path, value) in pathToValueMap {
70
79
try ? set (
71
80
path: path,
72
- value: value ,
81
+ value: values [ path ] ,
73
82
mergeOption: mergeOption
74
83
)
75
84
}
0 commit comments