You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Google_Protobuf_FieldMask` contains a list of field paths, whereeach path is a string that identifies a particular field or nested field in the message. This is particularly useful for selectively updating or trimming messages.
621
+
622
+
```protobuf
623
+
message FieldMask {
624
+
// The set of field mask paths.
625
+
repeated string paths =1;
626
+
}
627
+
```
618
628
619
629
## Extensions
620
630
@@ -696,6 +706,69 @@ Descriptor objects. It is something that could get revisited in the future,
696
706
but will need careful consideration; the bloat/size issues isof the most
697
707
concern because of Swift's common use for mobile applications.
698
708
709
+
## FieldMask Utilities
710
+
711
+
### Merging Two Messages
712
+
713
+
The `merge(from:fieldMask:)` function in Swift Protobuf enables developers to selectively merge fields from one message into another, guided by a `Google_Protobuf_FieldMask`. This method is particularly useful when you need to update only specific fields in a message without affecting others. The `merge` function is available as a method on `Message` types and requires two parameters: the source message (`from`) containing the data to merge and the `fieldMask` that specifies which fields should be updated.
714
+
715
+
For example, consider a message with the following structure:
716
+
717
+
```protobuf
718
+
message ExampleMessage {
719
+
string foo =1;
720
+
string bar =2;
721
+
}
722
+
```
723
+
724
+
Assume we have two instances of `ExampleMessage`:
725
+
726
+
```swift
727
+
let message1: ExampleMessage = .with { $0.foo="foo1" }
728
+
let message2: ExampleMessage = .with { $0.bar="bar2" }
729
+
```
730
+
731
+
To merge `message2` into `message1` but only update the `bar` field, you can define a `Google_Protobuf_FieldMask` like this:
732
+
733
+
```swift
734
+
let fieldMask = Google_Protobuf_FieldMask.with { $0.paths= ["bar"] }
After this operation, `message1.bar` will have the value `"bar2"` from `message2`, while `message1.foo` remains `"foo1"`. The `merge` function operates in-place, meaning it directly modifies `message1`. This targeted approach is beneficial when handling partial updates, as it prevents unintended changes to other fields. Proper configuration of the `fieldMask` is essential to ensure that only the desired fields are updated. Additionally, since the `merge` function may throw errors if the operation fails, it's important to handle these exceptions appropriately. This API is a powerful tool for managing partial updates inprotocol buffer messages, providing developers with precise control over the merging process.
744
+
745
+
### Trimming a Message
746
+
747
+
The `trim(keeping:)` function allows developers to retain only specific fields in a protocol buffer message while clearing the rest. This function is particularly useful when you want to ensure that only certain fields are preserved in a message, effectively "trimming" the message to contain just the necessary data.
748
+
749
+
Consider the `ExampleMessage` structure from the previous example. Suppose you have an instance of `ExampleMessage` initialized as follows:
750
+
751
+
```swift
752
+
let message = ExampleMessage.with {
753
+
$0.foo="foo"
754
+
$0.bar="bar"
755
+
}
756
+
```
757
+
758
+
If you want to trim this message so that only the `bar` field retains its value, you can define a `Google_Protobuf_FieldMask` like this:
759
+
760
+
```swift
761
+
let fieldMask = Google_Protobuf_FieldMask.with { $0.paths= ["bar"] }
762
+
```
763
+
764
+
Then, you apply the `trim` function:
765
+
766
+
```swift
767
+
message.trim(keeping: fieldMask)
768
+
```
769
+
770
+
After this operation, the `bar` field in `message` will still have the value `"bar"`, while the `foo` field will be cleared, resetting to its defaultvalue (an empty string, in this case). The `trim(keeping:)` function is performed in-place, meaning it directly modifies the original message. This function is ideal for scenarios where it's necessary to remove all but a few specified fields, ensuring that the resulting message contains only the data you want to keep. This precise control over the message structure can be essential when working with large or complex messages, where only a subset of the data is relevant for a particular operation or transmission. The `trim` function enhances the flexibility of message management in Swift Protobuf, making it easier to handle scenarios where selective field retention isrequired.
771
+
699
772
## Aside: proto2 vs. proto3
700
773
701
774
The terms *proto2* and *proto3* refer to two different dialects of the proto
0 commit comments