Skip to content

Commit d745581

Browse files
committed
Merge branch 'develop' into main
2 parents 933ad74 + f952fc6 commit d745581

File tree

4 files changed

+33
-48
lines changed

4 files changed

+33
-48
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ let package = Package(
1212

1313
dependencies: [
1414
.package(url: "https://github.com/Macro-swift/Macro.git",
15-
from: "0.6.0"),
15+
from: "0.6.2"),
1616
.package(url: "https://github.com/Macro-swift/MacroExpress.git",
17-
from: "0.5.7"),
17+
from: "0.6.0"),
1818
.package(url: "https://github.com/AlwaysRightInstitute/SwiftXmlRpc.git",
1919
from: "0.8.5")
2020
],

Sources/MacroXmlRpc/BodyParser.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ public extension bodyParser {
3636
* This plays well w/ other body parsers. If no other parser was active,
3737
* it will fill `request.body` as `.text`.
3838
*/
39-
@inlinable
4039
static func xmlRpcCall() -> Middleware {
4140
return { req, res, next in
42-
if req.extra[xmlRpcRequestKey] != nil { return next() } // parsed already
41+
if req[XmlRpcBodyKey.self] != nil { return next() } // parsed already
4342

4443
func registerCallInLogger() {
4544
guard let call = req.xmlRpcCall else { return }
@@ -104,25 +103,22 @@ public extension bodyParser {
104103
}
105104
}
106105
}
107-
108106
}
109107

110-
@usableFromInline
111-
let xmlRpcRequestKey = "macro.xmlrpc.body-parser"
108+
internal enum XmlRpcBodyKey: EnvironmentKey {
109+
static let defaultValue : bodyParser.XmlRpcBodyParserBody? = nil
110+
static let loggingKey = "xmlrpc.body"
111+
}
112112

113113
public extension IncomingMessage {
114114

115115
/**
116116
* Returns the XML-RPC body parsed by e.g. `bodyParser.xmlRpcCall`. It is
117117
* only filled when the middleware executed, otherwise it returns `.invalid`.
118118
*/
119-
@inlinable
120119
var xmlRpcBody: bodyParser.XmlRpcBodyParserBody {
121-
set { extra[xmlRpcRequestKey] = newValue }
122-
get {
123-
return (extra[xmlRpcRequestKey] as? bodyParser.XmlRpcBodyParserBody)
124-
?? .invalid
125-
}
120+
set { environment[XmlRpcBodyKey.self] = newValue }
121+
get { return environment[XmlRpcBodyKey.self] ?? .invalid }
126122
}
127123

128124
/**

Sources/MacroXmlRpc/Introspection.swift

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,59 +148,49 @@ fileprivate extension IncomingMessage {
148148
}
149149
}
150150

151-
@usableFromInline
152-
let xmlRpcMethodsRequestKey = "macro.xmlrpc.method-names"
153-
@usableFromInline
154-
let xmlRpcHelpsRequestKey = "macro.xmlrpc.method-helps"
155-
@usableFromInline
156-
let xmlRpcMethodSignaturesRequestKey = "macro.xmlrpc.method-signatures"
151+
private enum MethodNames: EnvironmentKey {
152+
static let defaultValue : Set<String> = []
153+
static let loggingKey = "xmlrpc.names"
154+
}
155+
private enum MethodHelps: EnvironmentKey {
156+
static let defaultValue : [ String : String ] = [:]
157+
static let loggingKey = "xmlrpc.helps"
158+
}
159+
private enum MethodSignatures: EnvironmentKey {
160+
static let defaultValue : [ String : [ [ XmlRpc.Value.ValueType ] ] ] = [:]
161+
static let loggingKey = "xmlrpc.signatues"
162+
}
157163

158164
extension IncomingMessage {
159165

160166
fileprivate var knownXmlRpcMethodNames : Set<String> {
161-
return (extra[xmlRpcMethodsRequestKey] as? Set<String>) ?? []
167+
return environment[MethodNames.self]
162168
}
163169

164170
@usableFromInline
165171
func addKnownXmlRpcMethod(_ methodName: String) {
166-
if var methods = extra.removeValue(forKey: xmlRpcMethodsRequestKey)
167-
as? Set<String>
168-
{
169-
methods.insert(methodName)
170-
extra[xmlRpcMethodsRequestKey] = methods
171-
}
172-
else {
173-
extra[xmlRpcMethodsRequestKey] = Set([ methodName ])
174-
}
172+
environment[MethodNames.self].insert(methodName)
175173
}
176174

177175
@usableFromInline
178-
func addHelp(_ help: String, for method: String) {
179-
var helps = extra.removeValue(forKey: xmlRpcHelpsRequestKey)
180-
as? [ String : String ]
181-
?? [:]
182-
helps[method] = help
183-
extra[xmlRpcHelpsRequestKey] = helps
176+
func addHelp(_ help: String, for methodName: String) {
177+
environment[MethodHelps.self][methodName] = help
184178
}
185179

186180
@usableFromInline
187181
func addSignature(_ signature: [ XmlRpc.Value.ValueType ],
188182
for method: String)
189183
{
190-
var values = extra.removeValue(forKey: xmlRpcMethodSignaturesRequestKey)
191-
as? [ String : [ [ XmlRpc.Value.ValueType ] ] ]
192-
?? [:]
184+
var values = environment[MethodSignatures.self]
193185
values[method, default: []].append(signature)
194-
extra[xmlRpcMethodSignaturesRequestKey] = values
186+
environment[MethodSignatures.self] = values
195187
}
196188

197189
fileprivate var helps : [ String : String ] {
198-
return (extra[xmlRpcHelpsRequestKey] as? [ String : String ]) ?? [:]
190+
return environment[MethodHelps.self]
199191
}
200192
fileprivate var signatures : [ String : [ [ XmlRpc.Value.ValueType ] ] ] {
201-
return (extra[xmlRpcMethodSignaturesRequestKey]
202-
as? [ String : [ [ XmlRpc.Value.ValueType ] ] ])
203-
?? [:]
193+
return environment[MethodSignatures.self]
204194
}
205195
}
206196

Sources/MacroXmlRpc/Middleware.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,15 @@ public extension xmlrpc {
6767
return res.send("Call to XML-RPC function failed.")
6868
}
6969
}
70-
70+
7171
/* Body parser is not active */
72-
if req.extra[xmlRpcRequestKey] == nil,
73-
typeIs(req, [ "text/xml" ]) != nil
74-
{
72+
if req.environment[XmlRpcBodyKey.self] == nil, req.is("text/xml") {
7573
req.log.notice("Use of XML-RPC middleware w/o a bodyParser")
7674
let parser = bodyParser.xmlRpcCall()
7775
do {
7876
try parser(req, res) { (args: Any...) in
79-
assert(req.extra[xmlRpcRequestKey] != nil) // at least .invalid!
77+
assert(req.environment[XmlRpcBodyKey.self] != nil)
78+
// at least .invalid!
8079
process()
8180
}
8281
}

0 commit comments

Comments
 (0)