Skip to content
This repository was archived by the owner on Apr 23, 2021. It is now read-only.

Commit 3bdd07f

Browse files
committed
some progress
1 parent 5e0610b commit 3bdd07f

File tree

9 files changed

+620
-254
lines changed

9 files changed

+620
-254
lines changed

Package.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ let package = Package(
1010
"Baggage"
1111
]
1212
),
13-
.library(name: "BaggageHolder",
13+
.library(name: "BaggageLogging",
1414
targets: [
15-
"BaggageHolder"
15+
"BaggageLogging"
1616
]
1717
),
1818
],
@@ -27,7 +27,7 @@ let package = Package(
2727
),
2828

2929
.target(
30-
name: "BaggageHolder",
30+
name: "BaggageLogging",
3131
dependencies: [
3232
"Baggage",
3333
.product(name: "Logging", package: "swift-log"),
@@ -42,10 +42,10 @@ let package = Package(
4242
]
4343
),
4444
.testTarget(
45-
name: "BaggageHolderTests",
45+
name: "BaggageLoggingTests",
4646
dependencies: [
4747
"Baggage",
48-
"BaggageHolder"
48+
"BaggageLogging"
4949
]
5050
)
5151
]

Sources/Baggage/BaggageContext.swift

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
/// Libraries may also want to provide an extension, offering the values that users are expected to reach for
3636
/// using the following pattern:
3737
///
38-
/// extension BaggageContext {
38+
/// extension BaggageContextProtocol {
3939
/// var testID: TestIDKey.Value {
4040
/// get {
4141
/// self[TestIDKey.self]
@@ -44,7 +44,7 @@
4444
/// }
4545
/// }
4646
/// }
47-
public struct BaggageContext {
47+
public struct BaggageContext: BaggageContextProtocol {
4848
private var _storage = [AnyBaggageContextKey: ValueContainer]()
4949

5050
/// Create an empty `BaggageContext`.
@@ -77,10 +77,34 @@ public struct BaggageContext {
7777

7878
extension BaggageContext: CustomStringConvertible {
7979
public var description: String {
80-
"\(Self.self)(keys: \(self._storage.map(\.key.name)))"
80+
"\(Self.self)(keys: \(self._storage.map { $0.key.name }))"
8181
}
8282
}
8383

84+
public protocol BaggageContextProtocol {
85+
/// Provides type-safe access to the baggage's values.
86+
///
87+
/// Rather than using this subscript directly, users are encouraged to offer a convenience accessor to their values,
88+
/// using the following pattern:
89+
///
90+
/// extension BaggageContextProtocol {
91+
/// var testID: TestIDKey.Value {
92+
/// get {
93+
/// self[TestIDKey.self]
94+
/// } set {
95+
/// self[TestIDKey.self] = newValue
96+
/// }
97+
/// }
98+
/// }
99+
subscript<Key: BaggageContextKey>(_ key: Key.Type) -> Key.Value? { get set }
100+
101+
/// Iterates over the baggage context's contents invoking the callback one-by one.
102+
func forEach(_ callback: (AnyBaggageContextKey, Any) -> Void)
103+
}
104+
105+
// ==== ------------------------------------------------------------------------
106+
// MARK: Baggage keys
107+
84108
/// `BaggageContextKey`s are used as keys in a `BaggageContext`. Their associated type `Value` gurantees type-safety.
85109
/// To give your `BaggageContextKey` an explicit name you may override the `name` property.
86110
public protocol BaggageContextKey {
@@ -121,3 +145,36 @@ extension AnyBaggageContextKey: Hashable {
121145
hasher.combine(ObjectIdentifier(self.keyType))
122146
}
123147
}
148+
149+
// ==== ----------------------------------------------------------------------------------------------------------------
150+
// MARK: Framework Context Protocols
151+
152+
public protocol BaggageContextCarrier: BaggageContextProtocol {
153+
var baggage: BaggageContext { get set }
154+
}
155+
156+
extension BaggageContextCarrier {
157+
public subscript<Key: BaggageContextKey>(baggageKey: Key.Type) -> Key.Value? {
158+
get {
159+
self.baggage[baggageKey]
160+
} set {
161+
self.baggage[baggageKey] = newValue
162+
}
163+
}
164+
165+
public func forEach(_ callback: (AnyBaggageContextKey, Any) -> Void) {
166+
self.baggage.forEach(callback)
167+
}
168+
}
169+
170+
/// A baggage itself also is a carrier of "itself".
171+
extension BaggageContext: BaggageContextCarrier {
172+
public var baggage: BaggageContext {
173+
get {
174+
self
175+
}
176+
set {
177+
self = newValue
178+
}
179+
}
180+
}

Sources/BaggageHolder/BaggageContext+Logging.swift

Lines changed: 0 additions & 93 deletions
This file was deleted.

Sources/BaggageHolder/Context.swift

Lines changed: 0 additions & 93 deletions
This file was deleted.

0 commit comments

Comments
 (0)