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.
620
+
`Google_Protobuf_FieldMask` is used to specify which fields in
621
+
a protocol buffer message should be included in operations such as updates or merges.
622
+
It allows precise control over which parts of the message
623
+
are affected by defining a list of field paths.
624
+
625
+
For example, consider a protocol buffer message with nested fields:
621
626
622
627
```protobuf
623
-
message FieldMask {
624
-
// The set of field mask paths.
625
-
repeated string paths =1;
628
+
message ParentMessage {
629
+
string name =1;
630
+
ChildMessage child =2;
631
+
632
+
message ChildMessage {
633
+
string childName =1;
634
+
int32 age =2;
635
+
}
626
636
}
627
637
```
628
638
639
+
If you want to update only the `name` field of `ParentMessage`
640
+
and the `childName` field within `ChildMessage`,
641
+
you would define a `FieldMask` as follows:
642
+
643
+
```swift
644
+
let fieldMask = Google_Protobuf_FieldMask.with {
645
+
$0.paths= ["name", "child.childName"]
646
+
}
647
+
```
648
+
649
+
In this example, the `paths` list includes `"name"` to target
650
+
the `name` field in `ParentMessage` and `"child.childName"`
651
+
to target the `childName` field inside the nested `ChildMessage`.
652
+
This setup allows you to perform operations that affect
653
+
only these specified fields while leaving others unchanged.
654
+
629
655
## Extensions
630
656
631
657
Extensions are used to add additional properties to messages defined elsewhere.
@@ -710,7 +736,13 @@ concern because of Swift's common use for mobile applications.
710
736
711
737
### Merging Two Messages
712
738
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.
739
+
The `merge(from:fieldMask:)` function in Swift Protobuf enables developers
740
+
to selectively merge fields from one message into another, guided by a `Google_Protobuf_FieldMask`.
741
+
This method is particularly useful when you need to update only specific
742
+
fields in a message without affecting others.
743
+
The `merge` function is available as a method on `Message` types and requires two parameters:
744
+
the source message (`from`) containing the data to merge
745
+
and the `fieldMask` that specifies which fields should be updated.
714
746
715
747
For example, consider a message with the following structure:
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.
776
+
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 inprotocol buffer messages,
786
+
providing developers with precise control over the merging process.
744
787
745
788
### Trimming a Message
746
789
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.
790
+
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.
748
795
749
-
Consider the `ExampleMessage` structure from the previous example. Suppose you have an instance of `ExampleMessage` initialized as follows:
796
+
Consider the `ExampleMessage` structure from the previous example.
797
+
Suppose you have an instance of `ExampleMessage` initialized as follows:
750
798
751
799
```swift
752
800
let message = ExampleMessage.with {
@@ -755,7 +803,8 @@ let message = ExampleMessage.with {
755
803
}
756
804
```
757
805
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:
806
+
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:
759
808
760
809
```swift
761
810
let fieldMask = Google_Protobuf_FieldMask.with { $0.paths= ["bar"] }
@@ -767,7 +816,17 @@ Then, you apply the `trim` function:
767
816
message.trim(keeping: fieldMask)
768
817
```
769
818
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.
819
+
After this operation, the `bar` field in `message` will still have the value `"bar"`,
820
+
while the `foo` field will be cleared, resetting to its defaultvalue (an empty string,
821
+
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
0 commit comments