@@ -638,7 +638,7 @@ message ParentMessage {
638
638
639
639
If you want to update only the `name` field of `ParentMessage`
640
640
and the `childName` field within `ChildMessage`,
641
- you would define a `FieldMask` as follows:
641
+ you would use a `FieldMask` as follows:
642
642
643
643
```swift
644
644
let fieldMask = Google_Protobuf_FieldMask.with {
@@ -748,50 +748,61 @@ For example, consider a message with the following structure:
748
748
749
749
```protobuf
750
750
message ExampleMessage {
751
+
752
+ message NestedMessage {
753
+ string baz = 1 ;
754
+ string qux = 2 ;
755
+ }
756
+
751
757
string foo = 1 ;
752
758
string bar = 2 ;
759
+ NestedMessage nested = 3 ;
753
760
}
754
761
```
755
762
756
763
Assume we have two instances of `ExampleMessage`:
757
764
758
765
```swift
759
- let message1: ExampleMessage = .with { $0 . foo = " foo1 " }
760
- let message2 : ExampleMessage = . with { $0 .bar = " bar2 " }
761
- ```
762
-
763
- To merge `message2` into `message1` but only update the `bar` field,
764
- you can define a `Google_Protobuf_FieldMask` like this :
766
+ let message1: ExampleMessage = .with {
767
+ $0 .foo = " foo1 "
768
+ $0 . nested = . with {
769
+ $0 . baz = " baz1 "
770
+ }
771
+ }
765
772
766
- ```swift
767
- let fieldMask = Google_Protobuf_FieldMask.with { $0 .paths = [" bar" ] }
773
+ let message2: ExampleMessage = .with {
774
+ $0 .foo = " foo2"
775
+ $0 .bar = " bar2"
776
+ $0 .nested = .with {
777
+ $0 .baz = " baz2"
778
+ $0 .qux = " qux2"
779
+ }
780
+ }
768
781
```
769
782
770
- Then, you apply the merge:
783
+ To merge `message2` into `message1` but only update the `bar` field
784
+ and `qux` field of `nested`, you can use a `Google_Protobuf_FieldMask`
785
+ like this:
771
786
772
787
```swift
788
+ let fieldMask = Google_Protobuf_FieldMask.with {
789
+ $0 .paths = [" bar" , " nested.qux" ]
790
+ }
773
791
try message1.merge (from : message2, fieldMask : fieldMask)
774
792
```
775
793
776
794
After this operation, `message1.bar ` will have the value `" bar2" ` from `message2`,
777
- while `message1.foo` remains `" foo1" `. The `merge` function operates in - place,
778
- meaning it directly modifies `message1`.
779
- This targeted approach is beneficial when handling partial updates,
780
- as it prevents unintended changes to other fields.
781
- Proper configuration of the `fieldMask` is essential to ensure that only the desired
782
- fields are updated.
783
- Additionally, since the `merge` function may throw errors if the operation fails,
784
- it's important to handle these exceptions appropriately.
785
- This API is a powerful tool for managing partial updates in protocol buffer messages,
786
- providing developers with precise control over the merging process.
795
+ and `message1.nested .qux ` will have the value `" qux2" ` from `message2`,
796
+ while `message1.foo` and `message1.nested.baz` remain `" foo1" ` and `" baz1" `.
797
+ Be aware that including `" nested" ` in the FieldMask paths will cause all fields
798
+ within `message1.nested ` to be updated from `message2` (including `baz` and `qux`),
799
+ whereas adding `" nested.qux" ` only affects the `qux` field in the `nested` message.
800
+ The `merge` function operates in - place, meaning it directly modifies `message1`.
787
801
788
802
### Trimming a Message
789
803
790
804
The `trim (keeping: )` function allows developers to retain only specific
791
- fields in a protocol buffer message while clearing the rest.
792
- This function is particularly useful when you want to ensure that only certain
793
- fields are preserved in a message, effectively " trimming" the message
794
- to contain just the necessary data.
805
+ fields in a protocol buffer message while clearing the rest.
795
806
796
807
Consider the `ExampleMessage` structure from the previous example.
797
808
Suppose you have an instance of `ExampleMessage` initialized as follows:
@@ -804,7 +815,7 @@ let message = ExampleMessage.with {
804
815
```
805
816
806
817
If you want to trim this message so that only the `bar` field retains its value,
807
- you can define a `Google_Protobuf_FieldMask` like this:
818
+ you can use a `Google_Protobuf_FieldMask` like this:
808
819
809
820
```swift
810
821
let fieldMask = Google_Protobuf_FieldMask.with { $0 .paths = [" bar" ] }
@@ -819,14 +830,7 @@ message.trim(keeping: fieldMask)
819
830
After this operation, the `bar` field in `message` will still have the value `" bar" `,
820
831
while the `foo` field will be cleared, resetting to its default value (an empty string,
821
832
in this case ). The `trim (keeping: )` function is performed in - place, meaning it directly
822
- modifies the original message.
823
- This function is ideal for scenarios where it's necessary to remove all but a few
824
- specified fields, ensuring that the resulting message contains only the data you want to keep.
825
- This precise control over the message structure can be essential when working with
826
- large or complex messages, where only a subset of the data is relevant for a
827
- particular operation or transmission. The `trim` function enhances the flexibility
828
- of message management in Swift Protobuf, making it easier to handle scenarios
829
- where selective field retention is required .
833
+ modifies the original message.
830
834
831
835
## Aside: proto2 vs. proto3
832
836
0 commit comments