|
| 1 | +// |
| 2 | +// TypedRoutes.swift |
| 3 | +// MacroXmlRpc |
| 4 | +// |
| 5 | +// Created by Helge Hess. |
| 6 | +// Copyright © 2020 ZeeZide GmbH. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import MacroExpress |
| 10 | +import XmlRpc |
| 11 | + |
| 12 | +public protocol IntrospectibleXmlRpcValue: XmlRpcValueRepresentable { |
| 13 | + static var xmlRpcValueType : XmlRpc.Value.ValueType { get } |
| 14 | +} |
| 15 | + |
| 16 | +public extension RouteKeeper { |
| 17 | + |
| 18 | + // TODO: To finish up introspection, this needs to be decoupled from |
| 19 | + // `synchronousCall`. |
| 20 | + |
| 21 | + @inlinable |
| 22 | + @discardableResult |
| 23 | + func rpc<A1, R>(_ methodName: String, execute: @escaping ( A1 ) throws -> R) |
| 24 | + -> Self |
| 25 | + where A1 : IntrospectibleXmlRpcValue, |
| 26 | + R : IntrospectibleXmlRpcValue |
| 27 | + { |
| 28 | + post(xmlrpc.synchronousCall(methodName) { call in |
| 29 | + guard call.parameters.count == 1, |
| 30 | + let a1 = A1(xmlRpcValue: call.parameters[0]) |
| 31 | + else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") } |
| 32 | + return try execute(a1) |
| 33 | + }) |
| 34 | + } |
| 35 | + |
| 36 | + @inlinable |
| 37 | + @discardableResult |
| 38 | + func rpc<A1, A2, R>(_ methodName: String, |
| 39 | + execute: @escaping ( A1, A2 ) throws -> R) |
| 40 | + -> Self |
| 41 | + where A1 : IntrospectibleXmlRpcValue, |
| 42 | + A2 : IntrospectibleXmlRpcValue, |
| 43 | + R : IntrospectibleXmlRpcValue |
| 44 | + { |
| 45 | + post(xmlrpc.synchronousCall(methodName) { call in |
| 46 | + guard call.parameters.count == 2, |
| 47 | + let a1 = A1(xmlRpcValue: call.parameters[0]), |
| 48 | + let a2 = A2(xmlRpcValue: call.parameters[1]) |
| 49 | + else { throw XmlRpc.Fault(code: 400, reason: "Invalid parameters") } |
| 50 | + return try execute(a1, a2) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +// MARK: - IntrospectibleXmlRpcValue types |
| 57 | + |
| 58 | +extension String: IntrospectibleXmlRpcValue { |
| 59 | + @inlinable |
| 60 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .string } |
| 61 | +} |
| 62 | + |
| 63 | +extension Int: IntrospectibleXmlRpcValue { |
| 64 | + @inlinable |
| 65 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .int } |
| 66 | +} |
| 67 | + |
| 68 | +extension Double: IntrospectibleXmlRpcValue { |
| 69 | + @inlinable |
| 70 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .double } |
| 71 | +} |
| 72 | + |
| 73 | +extension Bool: IntrospectibleXmlRpcValue { |
| 74 | + @inlinable |
| 75 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .bool } |
| 76 | +} |
| 77 | + |
| 78 | +extension Collection where Element : IntrospectibleXmlRpcValue { |
| 79 | + @inlinable |
| 80 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .array } |
| 81 | +} |
| 82 | + |
| 83 | +extension Array : IntrospectibleXmlRpcValue |
| 84 | + where Element : IntrospectibleXmlRpcValue |
| 85 | +{ |
| 86 | +} |
| 87 | +extension Set : IntrospectibleXmlRpcValue |
| 88 | + where Element : IntrospectibleXmlRpcValue |
| 89 | +{ |
| 90 | +} |
| 91 | + |
| 92 | +extension Dictionary : IntrospectibleXmlRpcValue |
| 93 | + where Key == String, |
| 94 | + Value : IntrospectibleXmlRpcValue |
| 95 | +{ |
| 96 | + @inlinable |
| 97 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .dictionary } |
| 98 | +} |
| 99 | + |
| 100 | +#if canImport(Foundation) |
| 101 | + import struct Foundation.URL |
| 102 | + import struct Foundation.DateComponents |
| 103 | + |
| 104 | + extension DateComponents: IntrospectibleXmlRpcValue { |
| 105 | + @inlinable |
| 106 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .dateTime } |
| 107 | + } |
| 108 | + |
| 109 | + extension URL: IntrospectibleXmlRpcValue { |
| 110 | + @inlinable |
| 111 | + public static var xmlRpcValueType : XmlRpc.Value.ValueType { .string } |
| 112 | + } |
| 113 | +#endif // canImport(Foundation) |
0 commit comments