Skip to content

Commit 816d598

Browse files
authored
fix comments
1 parent 3006aa9 commit 816d598

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

Documentation/API.md

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ message ParentMessage {
638638

639639
If you want to update only the `name` field of `ParentMessage`
640640
and the `childName` field within `ChildMessage`,
641-
you would define a `FieldMask` as follows:
641+
you would use a `FieldMask` as follows:
642642

643643
```swift
644644
let fieldMask = Google_Protobuf_FieldMask.with {
@@ -748,50 +748,61 @@ For example, consider a message with the following structure:
748748

749749
```protobuf
750750
message ExampleMessage {
751+
752+
message NestedMessage {
753+
string baz = 1;
754+
string qux = 2;
755+
}
756+
751757
string foo = 1;
752758
string bar = 2;
759+
NestedMessage nested = 3;
753760
}
754761
```
755762

756763
Assume we have two instances of `ExampleMessage`:
757764

758765
```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+
}
765772

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+
}
768781
```
769782

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:
771786

772787
```swift
788+
let fieldMask = Google_Protobuf_FieldMask.with {
789+
$0.paths = ["bar", "nested.qux"]
790+
}
773791
try message1.merge(from: message2, fieldMask: fieldMask)
774792
```
775793

776794
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`.
787801

788802
### Trimming a Message
789803

790804
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.
795806

796807
Consider the `ExampleMessage` structure from the previous example.
797808
Suppose you have an instance of `ExampleMessage` initialized as follows:
@@ -804,7 +815,7 @@ let message = ExampleMessage.with {
804815
```
805816

806817
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:
808819

809820
```swift
810821
let fieldMask = Google_Protobuf_FieldMask.with { $0.paths = ["bar"] }
@@ -819,14 +830,7 @@ message.trim(keeping: fieldMask)
819830
After this operation, the `bar` field in `message` will still have the value `"bar"`,
820831
while the `foo` field will be cleared, resetting to its default value (an empty string,
821832
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.
830834

831835
## Aside: proto2 vs. proto3
832836

0 commit comments

Comments
 (0)