Skip to content

Commit 5757e40

Browse files
committed
EnvironmentValuesHolder is now AnyObject constrained
Otherwise the subscript can't write due to immutability concerns.
1 parent 33340a6 commit 5757e40

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

Sources/MacroCore/EnvironmentValues.swift

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Macro
44
//
55
// Created by Helge Hess.
6-
// Copyright © 2020 ZeeZide GmbH. All rights reserved.
6+
// Copyright © 2020-2025 ZeeZide GmbH. All rights reserved.
77
//
88

99
/**
@@ -13,43 +13,47 @@
1313
* "")
1414
*
1515
* Example:
16-
*
17-
* enum LoginUserEnvironmentKey: EnvironmentKey {
18-
* static let defaultValue = ""
19-
* }
16+
* ```swift
17+
* enum LoginUserEnvironmentKey: EnvironmentKey {
18+
* static let defaultValue = ""
19+
* }
20+
* ```
2021
*
2122
* In addition to the key definition, one usually declares an accessor to the
2223
* respective environment holder, for example the `IncomingMessage`:
24+
* ```swift
25+
* extension IncomingMessage {
2326
*
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+
* ```
3133
*
3234
* 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+
* ```
3841
*
3942
* 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+
* ```
4448
*
4549
* To add a shorter name for environment dumps, implement the `loggingKey`
4650
* 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+
* ```
5357
*/
5458
public protocol EnvironmentKey {
5559

@@ -144,7 +148,15 @@ public extension EnvironmentValues {
144148
}
145149
}
146150

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.
148160

149161
var environment : EnvironmentValues { set get }
150162

0 commit comments

Comments
 (0)