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

Commit d0729d1

Browse files
committed
some progress
1 parent 57aff7f commit d0729d1

File tree

9 files changed

+648
-254
lines changed

9 files changed

+648
-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
@@ -22,7 +22,7 @@
2222
/// Libraries may also want to provide an extension, offering the values that users are expected to reach for
2323
/// using the following pattern:
2424
///
25-
/// extension BaggageContext {
25+
/// extension BaggageContextProtocol {
2626
/// var testID: TestIDKey.Value {
2727
/// get {
2828
/// self[TestIDKey.self]
@@ -31,7 +31,7 @@
3131
/// }
3232
/// }
3333
/// }
34-
public struct BaggageContext {
34+
public struct BaggageContext: BaggageContextProtocol {
3535
private var _storage = [AnyBaggageContextKey: ValueContainer]()
3636

3737
/// Create an empty `BaggageContext`.
@@ -64,10 +64,34 @@ public struct BaggageContext {
6464

6565
extension BaggageContext: CustomStringConvertible {
6666
public var description: String {
67-
"\(Self.self)(keys: \(self._storage.map(\.key.name)))"
67+
"\(Self.self)(keys: \(self._storage.map { $0.key.name }))"
6868
}
6969
}
7070

71+
public protocol BaggageContextProtocol {
72+
/// Provides type-safe access to the baggage's values.
73+
///
74+
/// Rather than using this subscript directly, users are encouraged to offer a convenience accessor to their values,
75+
/// using the following pattern:
76+
///
77+
/// extension BaggageContextProtocol {
78+
/// var testID: TestIDKey.Value {
79+
/// get {
80+
/// self[TestIDKey.self]
81+
/// } set {
82+
/// self[TestIDKey.self] = newValue
83+
/// }
84+
/// }
85+
/// }
86+
subscript<Key: BaggageContextKey>(_ key: Key.Type) -> Key.Value? { get set }
87+
88+
/// Iterates over the baggage context's contents invoking the callback one-by one.
89+
func forEach(_ callback: (AnyBaggageContextKey, Any) -> Void)
90+
}
91+
92+
// ==== ------------------------------------------------------------------------
93+
// MARK: Baggage keys
94+
7195
/// `BaggageContextKey`s are used as keys in a `BaggageContext`. Their associated type `Value` gurantees type-safety.
7296
/// To give your `BaggageContextKey` an explicit name you may override the `name` property.
7397
public protocol BaggageContextKey {
@@ -108,3 +132,36 @@ extension AnyBaggageContextKey: Hashable {
108132
hasher.combine(ObjectIdentifier(self.keyType))
109133
}
110134
}
135+
136+
// ==== ----------------------------------------------------------------------------------------------------------------
137+
// MARK: Framework Context Protocols
138+
139+
public protocol BaggageContextCarrier: BaggageContextProtocol {
140+
var baggage: BaggageContext { get set }
141+
}
142+
143+
extension BaggageContextCarrier {
144+
public subscript<Key: BaggageContextKey>(baggageKey: Key.Type) -> Key.Value? {
145+
get {
146+
self.baggage[baggageKey]
147+
} set {
148+
self.baggage[baggageKey] = newValue
149+
}
150+
}
151+
152+
public func forEach(_ callback: (AnyBaggageContextKey, Any) -> Void) {
153+
self.baggage.forEach(callback)
154+
}
155+
}
156+
157+
/// A baggage itself also is a carrier of "itself".
158+
extension BaggageContext: BaggageContextCarrier {
159+
public var baggage: BaggageContext {
160+
get {
161+
self
162+
}
163+
set {
164+
self = newValue
165+
}
166+
}
167+
}

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)