Skip to content

Commit 0803276

Browse files
authored
add documentation
1 parent 3a4c97d commit 0803276

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

Documentation/API.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,16 @@ public func -(lhs: Google_Protobuf_Timestamp, rhs: Google_Protobuf_Duration) ->
615615
public func +(lhs: Google_Protobuf_Timestamp, rhs: Google_Protobuf_Duration) -> Google_Protobuf_Timestamp
616616
```
617617

618+
## Google_Protobuf_FieldMask
619+
620+
The `Google_Protobuf_FieldMask` contains a list of field paths, where each 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+
```
618628

619629
## Extensions
620630

@@ -696,6 +706,69 @@ Descriptor objects. It is something that could get revisited in the future,
696706
but will need careful consideration; the bloat/size issues is of the most
697707
concern because of Swift's common use for mobile applications.
698708

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"] }
735+
```
736+
737+
Then, you apply the merge:
738+
739+
```swift
740+
try message1.merge(from: message2, fieldMask: fieldMask)
741+
```
742+
743+
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 in protocol 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 default value (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 is required.
771+
699772
## Aside: proto2 vs. proto3
700773

701774
The terms *proto2* and *proto3* refer to two different dialects of the proto

0 commit comments

Comments
 (0)