|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import Foundation |
| 6 | + |
| 7 | +/** |
| 8 | + * Instances of this class are useful for implementing a messaging service based upon |
| 9 | + * Nimbus. |
| 10 | + * |
| 11 | + * The message helper is designed to help string interpolation and JEXL evalutaiuon against the context |
| 12 | + * of the attrtibutes Nimbus already knows about. |
| 13 | + * |
| 14 | + * App-specific, additional context can be given at creation time. |
| 15 | + * |
| 16 | + * The helpers are designed to evaluate multiple messages at a time, however: since the context may change |
| 17 | + * over time, the message helper should not be stored for long periods. |
| 18 | + */ |
| 19 | +public protocol GleanPlumbProtocol { |
| 20 | + func createMessageHelper() throws -> GleanPlumbMessageHelper |
| 21 | + func createMessageHelper(additionalContext: [String: Any]) throws -> GleanPlumbMessageHelper |
| 22 | + func createMessageHelper<T: Encodable>(additionalContext: T) throws -> GleanPlumbMessageHelper |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * A helper object to make working with Strings uniform across multiple implementations of the messaging |
| 27 | + * system. |
| 28 | + * |
| 29 | + * This object provides access to a JEXL evaluator which runs against the same context as provided by |
| 30 | + * Nimbus targeting. |
| 31 | + * |
| 32 | + * It should also provide a similar function for String substitution, though this scheduled for EXP-2159. |
| 33 | + */ |
| 34 | +public class GleanPlumbMessageHelper { |
| 35 | + private let targetingHelper: NimbusTargetingHelperProtocol |
| 36 | + private let stringHelper: NimbusStringHelperProtocol |
| 37 | + |
| 38 | + init(targetingHelper: NimbusTargetingHelperProtocol, stringHelper: NimbusStringHelperProtocol) { |
| 39 | + self.targetingHelper = targetingHelper |
| 40 | + self.stringHelper = stringHelper |
| 41 | + } |
| 42 | + |
| 43 | + public func evalJexl(expression: String) throws -> Bool { |
| 44 | + try targetingHelper.evalJexl(expression: expression) |
| 45 | + } |
| 46 | + |
| 47 | + public func getUuid(template: String) -> String? { |
| 48 | + stringHelper.getUuid(template: template) |
| 49 | + } |
| 50 | + |
| 51 | + public func stringFormat(template: String, uuid: String?) -> String { |
| 52 | + stringHelper.stringFormat(template: template, uuid: uuid) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// MARK: Dummy implementations |
| 57 | + |
| 58 | +internal class AlwaysFalseTargetingHelper: NimbusTargetingHelperProtocol { |
| 59 | + public func evalJexl(expression _: String) throws -> Bool { |
| 60 | + false |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +internal class NonStringHelper: NimbusStringHelperProtocol { |
| 65 | + public func getUuid(template _: String) -> String? { |
| 66 | + nil |
| 67 | + } |
| 68 | + |
| 69 | + public func stringFormat(template: String, uuid _: String?) -> String { |
| 70 | + template |
| 71 | + } |
| 72 | +} |
0 commit comments