-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fixes make_tag script to include swift-source * Adds GleanPlumbHelpers.swift
- Loading branch information
Tarik Eshaq
authored
Feb 16, 2022
1 parent
a276a45
commit 29772c8
Showing
3 changed files
with
74 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import Foundation | ||
|
||
/** | ||
* Instances of this class are useful for implementing a messaging service based upon | ||
* Nimbus. | ||
* | ||
* The message helper is designed to help string interpolation and JEXL evalutaiuon against the context | ||
* of the attrtibutes Nimbus already knows about. | ||
* | ||
* App-specific, additional context can be given at creation time. | ||
* | ||
* The helpers are designed to evaluate multiple messages at a time, however: since the context may change | ||
* over time, the message helper should not be stored for long periods. | ||
*/ | ||
public protocol GleanPlumbProtocol { | ||
func createMessageHelper() throws -> GleanPlumbMessageHelper | ||
func createMessageHelper(additionalContext: [String: Any]) throws -> GleanPlumbMessageHelper | ||
func createMessageHelper<T: Encodable>(additionalContext: T) throws -> GleanPlumbMessageHelper | ||
} | ||
|
||
/** | ||
* A helper object to make working with Strings uniform across multiple implementations of the messaging | ||
* system. | ||
* | ||
* This object provides access to a JEXL evaluator which runs against the same context as provided by | ||
* Nimbus targeting. | ||
* | ||
* It should also provide a similar function for String substitution, though this scheduled for EXP-2159. | ||
*/ | ||
public class GleanPlumbMessageHelper { | ||
private let targetingHelper: NimbusTargetingHelperProtocol | ||
private let stringHelper: NimbusStringHelperProtocol | ||
|
||
init(targetingHelper: NimbusTargetingHelperProtocol, stringHelper: NimbusStringHelperProtocol) { | ||
self.targetingHelper = targetingHelper | ||
self.stringHelper = stringHelper | ||
} | ||
|
||
public func evalJexl(expression: String) throws -> Bool { | ||
try targetingHelper.evalJexl(expression: expression) | ||
} | ||
|
||
public func getUuid(template: String) -> String? { | ||
stringHelper.getUuid(template: template) | ||
} | ||
|
||
public func stringFormat(template: String, uuid: String?) -> String { | ||
stringHelper.stringFormat(template: template, uuid: uuid) | ||
} | ||
} | ||
|
||
// MARK: Dummy implementations | ||
|
||
internal class AlwaysFalseTargetingHelper: NimbusTargetingHelperProtocol { | ||
public func evalJexl(expression _: String) throws -> Bool { | ||
false | ||
} | ||
} | ||
|
||
internal class NonStringHelper: NimbusStringHelperProtocol { | ||
public func getUuid(template _: String) -> String? { | ||
nil | ||
} | ||
|
||
public func stringFormat(template: String, uuid _: String?) -> String { | ||
template | ||
} | ||
} |