-
Notifications
You must be signed in to change notification settings - Fork 279
[AC-154] Adds basic nimbus flag building to ads-client #7551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
thesuzerain
wants to merge
3
commits into
main
Choose a base branch
from
ads-client-adds-nimbus-flags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+108
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
| use std::collections::HashMap; | ||
|
|
||
| // At the current moment, nimbus usage exclusively inside of rust components is not fully realized. | ||
| // Therefore, for now we will accept a series of string flags passed in from each surface (that are connected to nimbus). | ||
| // The ads-client can then branch behavior based on the passed flags. | ||
| #[derive(Clone, Default)] | ||
| pub struct NimbusFlags { | ||
| flags: HashMap<NimbusFlag, bool>, | ||
| } | ||
|
|
||
| impl NimbusFlags { | ||
| // Parse and store flags in constructor. | ||
| // Allow unrecognized flags to be read as `Unknown`, so that extra irrelevant flags can be easily passed. | ||
| pub fn new(flags: HashMap<String, bool>) -> NimbusFlags { | ||
| NimbusFlags { | ||
| flags: flags | ||
| .into_iter() | ||
| .map(|(k, v)| (NimbusFlag::from_string(&k), v)) | ||
| .collect(), | ||
| } | ||
| } | ||
|
|
||
| pub fn check_flag(&self, flag: &NimbusFlag) -> bool { | ||
| *self.flags.get(flag).unwrap_or(&false) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Hash, PartialEq, Eq)] | ||
| pub enum NimbusFlag { | ||
| // `ads-client.async-enabled` | ||
| AsyncEnabled, | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not used in this PR, obviously, but it will be the first flag we use (and will be used imminently in the other async PR). Just wanted to have an actual example, but if we want to avoid that as it's not used in this PR yet, I can delete. |
||
|
|
||
| // This allows unrecognized flags to be passed (parsed as `Unknown`) | ||
| Unknown(String), | ||
|
|
||
| #[cfg(test)] | ||
| Test, | ||
| } | ||
|
|
||
| impl NimbusFlag { | ||
| pub fn from_string(s: &str) -> NimbusFlag { | ||
| match s { | ||
| "ads-client.async-enabled" => NimbusFlag::AsyncEnabled, | ||
| #[cfg(test)] | ||
| "test" => NimbusFlag::Test, | ||
| s => NimbusFlag::Unknown(s.to_string()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| mod tests { | ||
| #[test] | ||
| fn nimbus_test_flag_active_on_test() { | ||
| let nimbus_flag_off = std::sync::Arc::new(crate::MozAdsClientBuilder::new()) | ||
| .environment(crate::MozAdsEnvironment::Test) | ||
| .build(); | ||
| assert!( | ||
| !nimbus_flag_off.nimbus_test_flag(), | ||
| "Nimbus `Test` flag should be disabled if no flags are passed" | ||
| ); | ||
| let mut flags = std::collections::HashMap::new(); | ||
| flags.insert("test".to_string(), true); | ||
| let nimbus_flag_on = std::sync::Arc::new(crate::MozAdsClientBuilder::new()) | ||
| .environment(crate::MozAdsEnvironment::Test) | ||
| .nimbus_flags(flags) | ||
| .build(); | ||
| assert!( | ||
| nimbus_flag_on.nimbus_test_flag(), | ||
| "Nimbus `Test` flag should be enabled if no flag is passed" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense to include at the MozAdsClient level given that it's surface-passed flags, and they may be referenced at this level as well. (if we want an early branch in the FFI layer for example of which of two wholly different internal functions to call- eg: recordimpression sync vs fire-and-forget)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I pictured that nimbus flags fully end at this layer actually and they are translated into whatever they actually mean do the client at the time we construct/call the client.
So in the async case this is something like not storing the
NimbusFlag.AsyncEnabled, but just constructing eitherAsyncAdsClientorSyncAdsClient. If they were not as dramatic as needing separate implementations then they could be translated into an option to theAdsClientconstructor options.For other situations like per-request flags they would be translated into some request option and have no reason to ever be stored at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think there is a confusion,
MozAdsClientandAdsClientare the same.MozAdsClientis only the uniffi version of it that encapsulate surface API for better tracking of breacking changes but it should not hold any feature so flags should go toAdsClientstruct.