Skip to content

Commit cfa6743

Browse files
glbrnttgjcairo
andauthored
Update tutorials and docs (#2070)
Co-authored-by: Gus Cairo <[email protected]>
1 parent 1fd34e7 commit cfa6743

36 files changed

+225
-260
lines changed

.spi.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
version: 1
22
builder:
33
configs:
4-
- documentation_targets: [GRPC, GRPCReflectionService, protoc-gen-grpc-swift, GRPCCore]
4+
- documentation_targets: [GRPCCore, GRPCCodeGen]
55
swift_version: 6.0
6+
- documentation_targets: [GRPCInProcessTransport]
7+
swift_version: 6.0
8+
# Don't include @_exported types from GRPCCore
9+
custom_documentation_parameters: [--exclude-extended-types]

README.md

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,47 @@
11
# gRPC Swift
22

3-
This repository contains a gRPC code generator and runtime libraries for Swift.
4-
You can read more about gRPC on the [gRPC project's website][grpcio].
5-
6-
## Versions
7-
8-
gRPC Swift is currently undergoing active development to take full advantage of
9-
Swift's native concurrency features. The culmination of this work will be a new
10-
major version, v2.x. Pre-release versions will be available in the near future.
11-
12-
In the meantime, v1.x is available and still supported. You can read more about
13-
it on the [Swift Package Index][spi-grpc-swift-main].
14-
15-
## Security
16-
17-
Please see [SECURITY.md](SECURITY.md).
18-
19-
## License
20-
21-
gRPC Swift is released under the same license as [gRPC][gh-grpc], repeated in
22-
[LICENSE](LICENSE).
23-
24-
## Contributing
25-
26-
Please get involved! See our [guidelines for contributing](CONTRIBUTING.md).
3+
This repository contains a gRPC implementation for Swift. You can read more
4+
about gRPC on the [gRPC project's website][grpcio].
5+
6+
> gRPC Swift v2.x is under active development on the `main` branch and takes
7+
> full advantage of Swift's native concurrency features.
8+
>
9+
> v1.x is still supported and maintained on the `release/1.x` branch.
10+
11+
- 📚 **Documentation** and **tutorials** are available on the [Swift Package Index][spi-grpc-swift]
12+
- 💻 **Examples** are available in the [Examples](Examples) directory
13+
- 🚀 **Contributions** are welcome, please see [CONTRIBUTING.md](CONTRIBUTING.md)
14+
- 🪪 **License** is Apache 2.0, repeated in [LICENSE](License)
15+
- 🔒 **Security** issues should be reported via the process in [SECURITY.md](SECURITY.md)
16+
17+
## Quick Start
18+
19+
The following snippet contains a Swift Package manifest to use gRPC Swift v2.x with
20+
the SwiftNIO based transport and SwiftProtobuf serialization:
21+
22+
```swift
23+
// swift-tools-version: 6.0
24+
import PackageDescription
25+
26+
let package = Package(
27+
name: "foo-package",
28+
platforms: [.macOS("15.0")],
29+
dependencies: [
30+
.package(url: "https://github.com/grpc/grpc-swift-nio-transport.git", from: "1.0.0-alpha.1"),
31+
.package(url: "https://github.com/grpc/grpc-swift-protobuf.git", from: "1.0.0-alpha.1"),
32+
],
33+
targets: [
34+
.executableTarget(
35+
name: "bar-target",
36+
dependencies: [
37+
.product(name: "GRPCNIOTransportHTTP2", package: "grpc-swift-nio-transport"),
38+
.product(name: "GRPCProtobuf", package: "grpc-swift-protobuf"),
39+
]
40+
)
41+
]
42+
)
43+
```
2744

2845
[gh-grpc]: https://github.com/grpc/grpc
2946
[grpcio]: https://grpc.io
30-
[spi-grpc-swift-main]: https://swiftpackageindex.com/grpc/grpc-swift/main/documentation/grpccore
47+
[spi-grpc-swift]: https://swiftpackageindex.com/grpc/grpc-swift/documentation

Sources/GRPCCore/Call/Server/ServerInterceptor.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@
2323
///
2424
/// Interceptors are registered with the server apply to all RPCs. If you need to modify the
2525
/// behavior of an interceptor on a per-RPC basis then you can use the
26-
/// ``ServerInterceptorContext/descriptor`` to determine which RPC is being called and
26+
/// ``ServerContext/descriptor`` to determine which RPC is being called and
2727
/// conditionalise behavior accordingly.
2828
///
29-
/// - TODO: Update example and documentation to show how to register an interceptor.
30-
///
3129
/// ## RPC filtering
3230
///
3331
/// A common use of server-side interceptors is to filter requests from clients. Interceptors can

Sources/GRPCCore/Documentation.docc/Articles/Generating-stubs.md

Lines changed: 8 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,14 @@
22

33
Learn how to generate stubs for gRPC Swift from a service defined using the Protocol Buffers IDL.
44

5-
## Overview
6-
7-
There are two approaches to generating stubs from Protocol Buffers:
8-
9-
1. With the Swift Package Manager build plugin, or
10-
2. With the Protocol Buffers compiler (`protoc`).
11-
12-
The following sections describe how and when to use each.
13-
14-
### Using the Swift Package Manager build plugin
15-
16-
You can generate stubs at build time by using `GRPCSwiftPlugin` which is a build plugin for the
17-
Swift Package Manager. Using it means that you don't have to manage the generation of
18-
stubs with separate tooling, or check the generated stubs into your source repository.
19-
20-
The build plugin will generate gRPC stubs for you by building `protoc-gen-grpc-swift` (more details
21-
in the following section) for you and invoking `protoc`. Because of the implicit
22-
dependency on `protoc` being made available by the system `GRPCSwiftPlugin` isn't suitable for use
23-
in:
24-
25-
- Library packages, or
26-
- Environments where `protoc` isn't available.
27-
28-
> `GRPCSwiftPlugin` _only_ generates gRPC stubs, it doesn't generate messages. You must generate
29-
> messages in addition to the gRPC Stubs. The [Swift Protobuf](https://github.com/apple/swift-protobuf)
30-
> project provides an equivalent build plugin, `SwiftProtobufPlugin`, for this.
31-
32-
#### Configuring the build plugin
33-
34-
You can configure which stubs `GRPCSwiftPlugin` generates and how via a configuration file. This
35-
must be called `grpc-swift-config.json` and can be placed anywhere in the source directory for your
36-
target.
37-
38-
A config file for the plugin is made up of a number of `protoc` invocations. Each invocation
39-
describes the inputs to `protoc` as well as any options.
40-
41-
The following is a list of options which can be applied to each invocation object:
42-
- `protoFiles`, an array of strings where each string is the path to an input `.proto` file
43-
_relative to `grpc-swift-config.json`_.
44-
- `visibility`, a string describing the access level of the generated stub (must be one
45-
of `"public"`, `"internal"`, or `"package"`). If not specified then stubs are generated as
46-
`internal`.
47-
- `server`, a boolean indicating whether server stubs should be generated. Defaults to `true` if
48-
not specified.
49-
- `client`, a boolean indicating whether client stubs should be generated. Defaults to `true` if
50-
not specified.
51-
- `_V2`, a boolean indicated whether the generated stubs should be for v2.x. Defaults to `false` if
52-
not specified.
53-
54-
> The `GRPCSwiftPlugin` build plugin is currently shared between gRPC Swift v1.x and v2.x. To
55-
> generate stubs for v2.x you _must_ set `_V2` to `true` in your config.
56-
>
57-
> This option will be deprecated and removed once v2.x has been released.
58-
59-
#### Finding protoc
60-
61-
The build plugin requires a copy of the `protoc` binary to be available. To resolve which copy of
62-
the binary to use, `GRPCSwiftPlugin` will look at the following in order:
63-
64-
1. The exact path specified in the `protocPath` property in `grpc-swift-config.json`, if present.
65-
2. The exact path specified in the `PROTOC_PATH` environment variable, if set.
66-
3. The first `protoc` binary found in your `PATH` environment variable.
67-
68-
#### Using the build plugin from Xcode
69-
70-
Xcode doesn't have access to your `PATH` so in order to use `GRPCSwiftPlugin` with Xcode you must
71-
either set `protocPath` in your `grpc-swift-config.json` or explicitly set `PROTOC_PATH` when
72-
opening Xcode.
73-
74-
You can do this by running:
75-
76-
```sh
77-
env PROTOC_PATH=/path/to/protoc xed /path/to/your-project
78-
```
79-
80-
Note that Xcode must _not_ be open before running this command.
81-
82-
#### Example configuration
83-
84-
We recommend putting your config and `.proto` files in a directory called `Protos` within your
85-
target. Here's an example package structure:
86-
87-
```
88-
MyPackage
89-
├── Package.swift
90-
└── Sources
91-
└── MyTarget
92-
└── Protos
93-
├── foo
94-
│   └── bar
95-
│   ├── baz.proto
96-
│   └── buzz.proto
97-
└── grpc-swift-config.json
98-
```
99-
100-
If you wanted the generated stubs from `baz.proto` to be `public`, and to only generate a client
101-
for `buzz.proto` then the `grpc-swift-config` could look like this:
102-
103-
```json
104-
{
105-
"invocations": [
106-
{
107-
"_V2": true,
108-
"protoFiles": ["foo/bar/baz.proto"],
109-
"visibility": "public"
110-
},
111-
{
112-
"_V2": true,
113-
"protoFiles": ["foo/bar/buzz.proto"],
114-
"server": false
115-
}
116-
]
117-
}
118-
```
119-
120-
### Using protoc
5+
## Using protoc
1216

1227
If you've used Protocol Buffers before then generating gRPC Swift stubs should be simple. If you're
1238
unfamiliar with Protocol Buffers then you should get comfortable with the concepts before
1249
continuing; the [Protocol Buffers website](https://protobuf.dev/) is a great place to start.
12510

126-
gRPC Swift provides `protoc-gen-grpc-swift`, a program which is a plugin for the Protocol Buffers
127-
compiler, `protoc`.
11+
The [`grpc-swift-protobuf`](https://github.com/grpc/grpc-swift-protobuf) package provides
12+
`protoc-gen-grpc-swift`, a program which is a plugin for the Protocol Buffers compiler, `protoc`.
12813

12914
> `protoc-gen-grpc-swift` only generates gRPC stubs, it doesn't generate messages. You must use
13015
> `protoc-gen-swift` to generate messages in addition to gRPC Stubs.
@@ -160,22 +45,16 @@ protoc \
16045
--grpc-swift_out=.
16146
```
16247

163-
#### Generator options
48+
### Generator options
16449

16550
| Name | Possible Values | Default | Description |
16651
|---------------------------|--------------------------------------------|------------|----------------------------------------------------------|
167-
| `_V2` | `True`, `False` | `False` | Whether stubs are generated for gRPC Swift v2.x |
16852
| `Visibility` | `Public`, `Package`, `Internal` | `Internal` | Access level for generated stubs |
16953
| `Server` | `True`, `False` | `True` | Generate server stubs |
17054
| `Client` | `True`, `False` | `True` | Generate client stubs |
17155
| `FileNaming` | `FullPath`, `PathToUnderscore`, `DropPath` | `FullPath` | How generated source files should be named. (See below.) |
17256
| `ProtoPathModuleMappings` | | | Path to module map `.asciipb` file. (See below.) |
173-
| `AccessLevelOnImports` | `True`, `False` | `True` | Whether imports should have explicit access levels. |
174-
175-
> The `protoc-gen-grpc-swift` binary is currently shared between gRPC Swift v1.x and v2.x. To
176-
> generate stubs for v2.x you _must_ specify `_V2=True`.
177-
>
178-
> This option will be deprecated and removed once v2.x has been released.
57+
| `UseAccessLevelOnImports` | `True`, `False` | `False` | Whether imports should have explicit access levels. |
17958

18059
The `FileNaming` option has three possible values, for an input of `foo/bar/baz.proto` the following
18160
output file will be generated:
@@ -188,12 +67,12 @@ allows you to specify a mapping from `.proto` files to the Swift module they are
18867
allows the code generator to add appropriate imports to your generated stubs. This is described in
18968
more detail in the [SwiftProtobuf documentation](https://github.com/apple/swift-protobuf/blob/main/Documentation/PLUGIN.md).
19069

191-
#### Building the plugin
70+
### Building the plugin
19271

19372
> The version of `protoc-gen-grpc-swift` you use mustn't be newer than the version of
194-
> the `grpc-swift` you're using.
73+
> the `grpc-swift-protobuf` you're using.
19574
196-
If your package depends on `grpc-swift` then you can get a copy of `protoc-gen-grpc-swift`
75+
If your package depends on `grpc-swift-protobuf` then you can get a copy of `protoc-gen-grpc-swift`
19776
by building it directly:
19877

19978
```console

Sources/GRPCCore/Documentation.docc/Documentation.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,38 @@ A gRPC library for Swift written natively in Swift.
77
88
## Package structure
99

10-
gRPC Swift is made up of a number of modules, each of which is documented separately. However this
11-
module – ``GRPCCore`` – includes higher level documentation such as tutorials. The following list
12-
contains products of this package:
13-
14-
- ``GRPCCore`` contains core types and abstractions and is the 'base' module for the project.
15-
- `GRPCInProcessTransport` contains an implementation of an in-process transport.
16-
- `GRPCHTTP2TransportNIOPosix` provides client and server implementations of HTTP/2 transports built
17-
on top of SwiftNIO's POSIX Sockets abstractions.
18-
- `GRPCHTTP2TransportNIOTransportServices` provides client and server implementations of HTTP/2
19-
transports built on top of SwiftNIO's Network.framework abstraction, `NIOTransportServices`.
20-
- `GRPCProtobuf` provides serialization and deserialization components for `SwiftProtobuf`.
10+
gRPC Swift is distributed across multiple Swift packages. These are:
11+
12+
- `grpc-swift` (this package) containing core gRPC abstractions and an in-process transport.
13+
- GitHub repository: [`grpc/grpc-swift`](https://github.com/grpc/grpc-swift)
14+
- Documentation: hosted on the [Swift Package
15+
Index](https://swiftpackageindex.com/grpc/grpc-swift/documentation)
16+
- `grpc-swift-nio-transport` contains high-performance HTTP/2 transports built on top
17+
of [SwiftNIO](https://github.com/apple/swift-nio).
18+
- GitHub repository: [`grpc/grpc-swift-nio-transport`](https://github.com/grpc/grpc-swift-nio-transport)
19+
- Documentation: hosted on the [Swift Package
20+
Index](https://swiftpackageindex.com/grpc/grpc-swift-nio-transport/documentation)
21+
- `grpc-swift-protobuf` contains runtime serialization components to interoperate with
22+
[SwiftProtobuf](https://github.com/apple/swift-protobuf) as well as a plugin for the Protocol
23+
Buffers compiler, `protoc`.
24+
- GitHub repository: [`grpc/grpc-swift-protobuf`](https://github.com/grpc/grpc-swift-protobuf)
25+
- Documentation: hosted on the [Swift Package
26+
Index](https://swiftpackageindex.com/grpc/grpc-swift-protobuf/documentation)
27+
- `grpc-swift-extras` contains optional runtime components and integrations with other packages.
28+
- GitHub repository: [`grpc/grpc-swift-extras`](https://github.com/grpc/grpc-swift-extras)
29+
- Documentation: hosted on the [Swift Package
30+
Index](https://swiftpackageindex.com/grpc/grpc-swift-extras/documentation)
31+
32+
This package, and this module (``GRPCCore``) in particular, include higher level documentation such
33+
as tutorials.
34+
35+
## Modules in this package
36+
37+
- ``GRPCCore`` (this module) contains core abstractions, currency types and runtime components
38+
for gRPC Swift.
39+
- `GRPCInProcessTransport` contains an in-process implementation of the ``ClientTransport`` and
40+
``ServerTransport`` protocols.
41+
- `GRPCodeGen` contains components for building a code generator.
2142

2243
## Topics
2344

Sources/GRPCCore/Documentation.docc/Tutorials/Hello-World/Hello-World.tutorial

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
git clone https://github.com/grpc/grpc-swift
2222
```
2323

24-
The rest of the tutorial assumes that your current working directory is the cloned `grpc-swift`
25-
directory.
24+
You then need to change directory to the `Examples/hello-world` directory of the cloned
25+
repository. The rest of the tutorial assumes this is the current working directory.
2626
}
2727

2828
@Section(title: "Run a gRPC application") {
@@ -60,8 +60,7 @@
6060

6161
@Steps {
6262
@Step {
63-
Open `HelloWorld.proto` in the `Examples/v2/hello-world` directory to see how the
64-
service is defined.
63+
Open `HelloWorld.proto` in to see how the service is defined.
6564

6665
@Code(name: "HelloWorld.proto", file: "hello-world-sec03-step01.proto")
6766
}
@@ -77,10 +76,10 @@
7776

7877
@Section(title: "Update and run the application") {
7978
You need to regenerate the stubs as the service definition has changed. To do this run the
80-
following command from the root of the checked out repository:
79+
following command from the _root of the checked out repository_:
8180

8281
```console
83-
Protos/generate.sh
82+
dev/protos/generate.sh
8483
```
8584

8685
To learn how to generate stubs check out the <doc:Generating-stubs> article.
@@ -90,7 +89,7 @@
9089

9190
@Steps {
9291
@Step {
93-
Open `Serve.swift` in the `Examples/v2/hello-world/Subcommands` directory.
92+
Open `Serve.swift` in the `Subcommands` directory.
9493

9594
@Code(name: "Serve.swift", file: "hello-world-sec04-step01.swift")
9695
}
@@ -102,8 +101,7 @@
102101
}
103102

104103
@Step {
105-
Let's update the client now. Open `Greet.swift` in the
106-
`Examples/v2/hello-world/Subcommands` directory.
104+
Let's update the client now. Open `Greet.swift` in the `Subcommands` directory.
107105

108106
@Code(name: "Greet.swift", file: "hello-world-sec04-step03.swift")
109107
}

Sources/GRPCCore/Documentation.docc/Tutorials/Hello-World/Resources/hello-world-sec04-step01.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
struct Greeter: Helloworld_GreeterServiceProtocol {
22
func sayHello(
3-
request: ServerRequest.Single<Helloworld_HelloRequest>
3+
request: ServerRequest.Single<Helloworld_HelloRequest>,
4+
context: ServerContext
45
) async throws -> ServerResponse.Single<Helloworld_HelloReply> {
56
var reply = Helloworld_HelloReply()
67
let recipient = request.message.name.isEmpty ? "stranger" : request.message.name

0 commit comments

Comments
 (0)