|
| 1 | +# Understanding the generated code |
| 2 | + |
| 3 | +Understand what code is generated by `protoc-gen-grpc-swift` from a `.proto` |
| 4 | +file and how to use it. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The gRPC Swift Protobuf package provides a plugin to the Protocol Buffers |
| 9 | +Compiler (`protoc`) called `protoc-gen-grpc-swift`. The plugin is responsible |
| 10 | +for generating the gRPC specific code for services defined in a `.proto` file. |
| 11 | + |
| 12 | +### Package namespace |
| 13 | + |
| 14 | +Most `.proto` files contain a `package` directive near the start of the file |
| 15 | +describing the namespace it belongs to. Here's an example: |
| 16 | + |
| 17 | +```proto |
| 18 | +package foo.bar.v1; |
| 19 | +``` |
| 20 | + |
| 21 | +The package name "foo.bar.v1" is important as it is used as a prefix for |
| 22 | +generated types. The default behaviour is to replace periods with underscores |
| 23 | +and to capitalize each word and add a trailing underscore. For this package the |
| 24 | +prefix is "Foo\_Bar\_V1\_". If you don't declare a package then the prefix will be |
| 25 | +the empty string. |
| 26 | + |
| 27 | +You can override the prefix by setting the `swift_prefix` option: |
| 28 | + |
| 29 | +```proto |
| 30 | +option swift_prefix = "FooBarV1"; |
| 31 | +
|
| 32 | +package foo.bar.v1; |
| 33 | +``` |
| 34 | + |
| 35 | +The prefix for types in this file would be "FooBarV1" instead of "Foo\_Bar\_V1\_". |
| 36 | + |
| 37 | +### Service namespace |
| 38 | + |
| 39 | +For each service declared in your `.proto` file, gRPC will generate a caseless |
| 40 | +`enum` which is a namespace holding the generated protocols and types. The name |
| 41 | +of this `enum` is `{PREFIX}{SERVICE}` where `{PREFIX}` is as described in the |
| 42 | +previous section and `{SERVICE}` is the name of the service as declared in the |
| 43 | +`.proto` file. |
| 44 | + |
| 45 | +As an example the following definition creates a service namespace `enum` called |
| 46 | +`Foo_Bar_V1_BazService` (the `{PREFIX}` is "Foo_Bar_V1_" and `{SERVICE}` is |
| 47 | +"BazService"): |
| 48 | + |
| 49 | +```proto |
| 50 | +package foo.bar.v1; |
| 51 | +
|
| 52 | +service BazService { |
| 53 | + // ... |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +Code generated for each service falls into three categories: |
| 58 | + |
| 59 | +1. Service metadata, |
| 60 | +2. Service code, and |
| 61 | +3. Client code. |
| 62 | + |
| 63 | +#### Service metadata |
| 64 | + |
| 65 | +gRPC generates metadata for each service including a descriptor identifying the |
| 66 | +fully qualified name of the service and information about each method in the |
| 67 | +service. You likely won't need to interact directly with this information but |
| 68 | +it's available should you need to. |
| 69 | + |
| 70 | +#### Service code |
| 71 | + |
| 72 | +Within a service namespace gRPC generates three service protocols: |
| 73 | + |
| 74 | +1. `StreamingServiceProtocol`, |
| 75 | +2. `ServiceProtocol`, and |
| 76 | +3. `SimpleServiceProtocol`. |
| 77 | + |
| 78 | +The full name of each protocol includes the service namespace. |
| 79 | + |
| 80 | +> Example: |
| 81 | +> |
| 82 | +> For the `BazService` in the `foo.bar.v1` package the protocols would be: |
| 83 | +> |
| 84 | +> - `Foo_Bar_V1_BazService.StreamingServiceProtocol`, |
| 85 | +> - `Foo_Bar_V1_BazService.ServiceProtocol`, and |
| 86 | +> - `Foo_Bar_V1_BazService.SimpleServiceProtocol`. |
| 87 | +
|
| 88 | +Each of these protocols mirror the `service` defined in your `.proto` file with |
| 89 | +one requirement per RPC. To implement your service you must implement one of |
| 90 | +these protocols. |
| 91 | + |
| 92 | +The protocols form a hierarchy with `StreamingServiceProtocol` at the bottom and |
| 93 | +`SimpleServiceProtocol` at the top. `ServiceProtocol` refines |
| 94 | +`StreamingServiceProtocol`, and `SimpleServiceProtocol` refines |
| 95 | +`ServiceProtocol` (and `StreamingServiceProtocol` in turn). |
| 96 | + |
| 97 | +The `StreamingServiceProtocol` implements each RPC as if it were a bidirectional |
| 98 | +streaming RPC. This gives you the most flexibility at the cost of a harder to |
| 99 | +implement API. It also puts the responsibility on you to ensure that each RPC |
| 100 | +sends and receives the correct number of messages. |
| 101 | + |
| 102 | +The `ServiceProtocol` enforces that the correct number of messages are sent and |
| 103 | +received via its API. It also allows you to read request metadata and send both |
| 104 | +initial and trailing metadata. The request and response types for these |
| 105 | +requirements are in terms of `ServerRequest` and `ServerResponse`. |
| 106 | + |
| 107 | +The `SimpleServiceProtocol` also enforces the correct number of messages are |
| 108 | +sent and received via its API. However, it isn't defined in terms of |
| 109 | +`ServerRequest` or `ServerResponse` so it doesn't allow you access metadata. |
| 110 | +This limitation allows it to have the simplest API and is preferred if you don't |
| 111 | +need access to metadata. |
| 112 | + |
| 113 | +| Protocol | Enforces number of messages | Access to metadata |
| 114 | +|----------------------------|-----------------------------|------------------- |
| 115 | +| `StreamingServiceProtocol` | ✗ | ✓ |
| 116 | +| `ServiceProtocol` | ✓ | ✓ |
| 117 | +| `SimpleServiceProtocol` | ✓ | ✗ |
| 118 | + |
| 119 | +#### Client code |
| 120 | + |
| 121 | +gRPC generates two types for the client within a service namespace: |
| 122 | + |
| 123 | +1. `ClientProtocol`, and |
| 124 | +2. `Client`. |
| 125 | + |
| 126 | +Like the service code, the full name includes the namespace. |
| 127 | + |
| 128 | +> Example: |
| 129 | +> |
| 130 | +> For the `BazService` in the `foo.bar.v1` package the client types would be: |
| 131 | +> |
| 132 | +> - `Foo_Bar_V1_BazService.ClientProtocol`, and |
| 133 | +> - `Foo_Bar_V1_BazService.Client`. |
| 134 | +
|
| 135 | +The `ClientProtocol` defines one requirement for each RPC in terms of |
| 136 | +`ClientRequest` and `ClientResponse`. You don't need to implement the protocol |
| 137 | +as `Client` provides a concrete implementation. |
| 138 | + |
| 139 | +gRPC also generates extensions on `ClientProtocol` to provide more ergonomic |
| 140 | +APIs. These include versions which provide default arguments for various |
| 141 | +parameters (like the message serializer and deserializers; call options and |
| 142 | +response handler) and versions which don't use `ClientRequest` and |
| 143 | +`ClientResponse` directly. |
0 commit comments