Skip to content

Commit af441d1

Browse files
author
Pouya Yarandi
committed
Remove copy mechanism in some functions
1 parent 1c94065 commit af441d1

File tree

4 files changed

+29
-47
lines changed

4 files changed

+29
-47
lines changed

Sources/SwiftProtobuf/GetPathDecoder.swift

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Sources/SwiftProtobuf/GetPathDecoder.swift - Path decoder (Getter)
22
//
3-
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
3+
// Copyright (c) 2014 - 2023 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
66
// See LICENSE.txt for license information:
@@ -16,7 +16,7 @@ import Foundation
1616

1717
struct GetPathDecoder<T: Message>: Decoder {
1818

19-
private let path: String
19+
private let nextPath: [String]
2020
private var number: Int?
2121

2222
private var _value: Any?
@@ -30,19 +30,11 @@ struct GetPathDecoder<T: Message>: Decoder {
3030
_hasPath
3131
}
3232

33-
internal init(path: String) {
34-
self.path = path
35-
if let firstPathComponent {
36-
self.number = T.number(for: firstPathComponent)
33+
internal init(path: [String]) {
34+
if let firstComponent = path.first {
35+
self.number = T.number(for: firstComponent)
3736
}
38-
}
39-
40-
private var firstPathComponent: String? {
41-
path.components(separatedBy: ".").first
42-
}
43-
44-
private var nextPath: String {
45-
path.components(separatedBy: ".").dropFirst().joined(separator: ".")
37+
self.nextPath = .init(path.dropFirst())
4638
}
4739

4840
mutating func handleConflictingOneOf() throws {}
@@ -309,17 +301,17 @@ struct GetPathDecoder<T: Message>: Decoder {
309301
}
310302

311303
extension Message {
312-
func `get`(path: String) throws -> Any? {
313-
var copy = self
314-
var decoder = GetPathDecoder<Self>(path: path)
315-
try copy.decodeMessage(decoder: &decoder)
304+
mutating func `get`(path: String) throws -> Any? {
305+
let _path = path.components(separatedBy: ".")
306+
var decoder = GetPathDecoder<Self>(path: _path)
307+
try decodeMessage(decoder: &decoder)
316308
return decoder.value
317309
}
318310

319-
func hasPath(path: String) -> Bool {
320-
var copy = self
321-
var decoder = GetPathDecoder<Self>(path: path)
322-
try? copy.decodeMessage(decoder: &decoder)
311+
mutating func hasPath(path: String) -> Bool {
312+
let _path = path.components(separatedBy: ".")
313+
var decoder = GetPathDecoder<Self>(path: _path)
314+
try? decodeMessage(decoder: &decoder)
323315
return decoder.hasPath
324316
}
325317
}

Sources/SwiftProtobuf/Google_Protobuf_FieldMask+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ extension Google_Protobuf_FieldMask {
351351
public func isValid<M: Message & _ProtoNameProviding>(
352352
for messageType: M.Type
353353
) -> Bool {
354-
let message = M()
354+
var message = M()
355355
return paths.allSatisfy { path in
356356
message.isPathValid(path)
357357
}

Sources/SwiftProtobuf/Message+FieldMask.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Sources/SwiftProtobuf/Message+FieldMask.swift - Message field mask extensions
22
//
3-
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
3+
// Copyright (c) 2014 - 2023 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
66
// See LICENSE.txt for license information:
@@ -23,10 +23,11 @@ extension Message {
2323
public static func isPathValid(
2424
_ path: String
2525
) -> Bool {
26-
Self().hasPath(path: path)
26+
var message = Self()
27+
return message.hasPath(path: path)
2728
}
2829

29-
internal func isPathValid(
30+
internal mutating func isPathValid(
3031
_ path: String
3132
) -> Bool {
3233
hasPath(path: path)
@@ -44,6 +45,7 @@ extension Message {
4445
to source: Self,
4546
fieldMask: Google_Protobuf_FieldMask
4647
) throws {
48+
var source = source
4749
var copy = self
4850
var pathToValueMap: [String: Any?] = [:]
4951
for path in fieldMask.paths {

Sources/SwiftProtobuf/SetPathDecoder.swift

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Sources/SwiftProtobuf/SetPathDecoder.swift - Path decoder (Setter)
22
//
3-
// Copyright (c) 2014 - 2016 Apple Inc. and the project authors
3+
// Copyright (c) 2014 - 2023 Apple Inc. and the project authors
44
// Licensed under Apache License v2.0 with Runtime Library Exception
55
//
66
// See LICENSE.txt for license information:
@@ -35,29 +35,16 @@ extension Message {
3535

3636
struct SetPathDecoder<T: Message>: Decoder {
3737

38-
private let path: String
3938
private let value: Any?
4039
private var number: Int?
40+
private let nextPath: [String]
4141

42-
init(path: String, value: Any?) {
43-
self.path = path
44-
self.value = value
45-
if let firstPathComponent {
46-
self.number = T.number(for: firstPathComponent)
42+
init(path: [String], value: Any?) {
43+
if let firstComponent = path.first {
44+
self.number = T.number(for: firstComponent)
4745
}
48-
}
49-
50-
var firstPathComponent: String? {
51-
return path
52-
.components(separatedBy: ".")
53-
.first
54-
}
55-
56-
var nextPath: String {
57-
return path
58-
.components(separatedBy: ".")
59-
.dropFirst()
60-
.joined(separator: ".")
46+
self.nextPath = .init(path.dropFirst())
47+
self.value = value
6148
}
6249

6350
func _value<V>(as: V.Type) throws -> V {
@@ -337,7 +324,8 @@ struct SetPathDecoder<T: Message>: Decoder {
337324

338325
extension Message {
339326
mutating func `set`(path: String, value: Any?) throws {
340-
var decoder = SetPathDecoder<Self>(path: path, value: value)
327+
let _path = path.components(separatedBy: ".")
328+
var decoder = SetPathDecoder<Self>(path: _path, value: value)
341329
try decodeMessage(decoder: &decoder)
342330
}
343331
}

0 commit comments

Comments
 (0)