|
3 | 3 | // Macro |
4 | 4 | // |
5 | 5 | // Created by Helge Hess. |
6 | | -// Copyright © 2020 ZeeZide GmbH. All rights reserved. |
| 6 | +// Copyright © 2020-2025 ZeeZide GmbH. All rights reserved. |
7 | 7 | // |
8 | 8 |
|
9 | 9 | /** |
|
13 | 13 | * "") |
14 | 14 | * |
15 | 15 | * Example: |
16 | | - * |
17 | | - * enum LoginUserEnvironmentKey: EnvironmentKey { |
18 | | - * static let defaultValue = "" |
19 | | - * } |
| 16 | + * ```swift |
| 17 | + * enum LoginUserEnvironmentKey: EnvironmentKey { |
| 18 | + * static let defaultValue = "" |
| 19 | + * } |
| 20 | + * ``` |
20 | 21 | * |
21 | 22 | * In addition to the key definition, one usually declares an accessor to the |
22 | 23 | * respective environment holder, for example the `IncomingMessage`: |
| 24 | + * ```swift |
| 25 | + * extension IncomingMessage { |
23 | 26 | * |
24 | | - * extension IncomingMessage { |
25 | | - * |
26 | | - * var loginUser : String { |
27 | | - * set { self[LoginUserEnvironmentKey.self] = newValue } |
28 | | - * get { self[LoginUserEnvironmentKey.self] } |
29 | | - * } |
30 | | - * } |
| 27 | + * var loginUser : String { |
| 28 | + * set { self[LoginUserEnvironmentKey.self] = newValue } |
| 29 | + * get { self[LoginUserEnvironmentKey.self] } |
| 30 | + * } |
| 31 | + * } |
| 32 | + * ``` |
31 | 33 | * |
32 | 34 | * It can then be used like: |
33 | | - * |
34 | | - * app.use { req, res, next in |
35 | | - * console.log("active user:", req.loginUser) |
36 | | - * next() |
37 | | - * } |
| 35 | + * ```swift |
| 36 | + * app.use { req, res, next in |
| 37 | + * console.log("active user:", req.loginUser) |
| 38 | + * next() |
| 39 | + * } |
| 40 | + * ``` |
38 | 41 | * |
39 | 42 | * If the value really is optional, it can be declared an optional: |
40 | | - * |
41 | | - * enum DatabaseConnectionEnvironmentKey: EnvironmentKey { |
42 | | - * static let defaultValue : DatabaseConnection? |
43 | | - * } |
| 43 | + * ```swift |
| 44 | + * enum DatabaseConnectionEnvironmentKey: EnvironmentKey { |
| 45 | + * static let defaultValue : DatabaseConnection? |
| 46 | + * } |
| 47 | + * ``` |
44 | 48 | * |
45 | 49 | * To add a shorter name for environment dumps, implement the `loggingKey` |
46 | 50 | * property: |
47 | | - * |
48 | | - * enum DatabaseConnectionEnvironmentKey: EnvironmentKey { |
49 | | - * static let defaultValue : DatabaseConnection? = nil |
50 | | - * static let loggingKey = "db" |
51 | | - * } |
52 | | - * |
| 51 | + * ``` |
| 52 | + * enum DatabaseConnectionEnvironmentKey: EnvironmentKey { |
| 53 | + * static let defaultValue : DatabaseConnection? = nil |
| 54 | + * static let loggingKey = "db" |
| 55 | + * } |
| 56 | + * ``` |
53 | 57 | */ |
54 | 58 | public protocol EnvironmentKey { |
55 | 59 |
|
@@ -144,7 +148,15 @@ public extension EnvironmentValues { |
144 | 148 | } |
145 | 149 | } |
146 | 150 |
|
147 | | -public protocol EnvironmentValuesHolder { |
| 151 | +/** |
| 152 | + * Some object that can hold an environment. |
| 153 | + * |
| 154 | + * Current implementors: |
| 155 | + * - `IncomingMessage` |
| 156 | + * - `OutgoingMessage` |
| 157 | + */ |
| 158 | +public protocol EnvironmentValuesHolder: AnyObject { |
| 159 | + // Note: This is AnyObject to allow subscript modifications. |
148 | 160 |
|
149 | 161 | var environment : EnvironmentValues { set get } |
150 | 162 |
|
|
0 commit comments