Skip to content

Commit

Permalink
feat(flags): rust implementation of a bunch of things (#24957)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
dmarticus and github-actions[bot] committed Sep 24, 2024
1 parent d4c86a9 commit c940fd5
Show file tree
Hide file tree
Showing 9 changed files with 1,309 additions and 362 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ plugin-transpiler/dist
.dlt
*.db
# Ignore any log files that happen to be present
*.log
*.log
# pyright config (keep this until we have a standardized one)
pyrightconfig.json
33 changes: 31 additions & 2 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/feature-flags/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ uuid = { workspace = true }
base64.workspace = true
flate2.workspace = true
common-alloc = { path = "../common/alloc" }
strum = { version = "0.26", features = ["derive"] }
health = { path = "../common/health" }
common-metrics = { path = "../common/metrics" }
tower = { workspace = true }
Expand Down
101 changes: 101 additions & 0 deletions rust/feature-flags/src/feature_flag_match_reason.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use std::cmp::Ordering;
use strum::EnumString;

#[derive(Debug, Clone, PartialEq, Eq, EnumString)]
pub enum FeatureFlagMatchReason {
#[strum(serialize = "super_condition_value")]
SuperConditionValue,
#[strum(serialize = "condition_match")]
ConditionMatch,
#[strum(serialize = "no_condition_match")]
NoConditionMatch,
#[strum(serialize = "out_of_rollout_bound")]
OutOfRolloutBound,
#[strum(serialize = "no_group_type")]
NoGroupType,
}

impl FeatureFlagMatchReason {
pub fn score(&self) -> i32 {
match self {
FeatureFlagMatchReason::SuperConditionValue => 4,
FeatureFlagMatchReason::ConditionMatch => 3,
FeatureFlagMatchReason::NoGroupType => 2,
FeatureFlagMatchReason::OutOfRolloutBound => 1,
FeatureFlagMatchReason::NoConditionMatch => 0,
}
}
}

impl PartialOrd for FeatureFlagMatchReason {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for FeatureFlagMatchReason {
fn cmp(&self, other: &Self) -> Ordering {
self.score().cmp(&other.score())
}
}

impl std::fmt::Display for FeatureFlagMatchReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
FeatureFlagMatchReason::SuperConditionValue => "super_condition_value",
FeatureFlagMatchReason::ConditionMatch => "condition_match",
FeatureFlagMatchReason::NoConditionMatch => "no_condition_match",
FeatureFlagMatchReason::OutOfRolloutBound => "out_of_rollout_bound",
FeatureFlagMatchReason::NoGroupType => "no_group_type",
}
)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ordering() {
let reasons = vec![
FeatureFlagMatchReason::NoConditionMatch,
FeatureFlagMatchReason::OutOfRolloutBound,
FeatureFlagMatchReason::NoGroupType,
FeatureFlagMatchReason::ConditionMatch,
FeatureFlagMatchReason::SuperConditionValue,
];

let mut sorted_reasons = reasons.clone();
sorted_reasons.sort();

assert_eq!(sorted_reasons, reasons);
}

#[test]
fn test_display() {
assert_eq!(
FeatureFlagMatchReason::SuperConditionValue.to_string(),
"super_condition_value"
);
assert_eq!(
FeatureFlagMatchReason::ConditionMatch.to_string(),
"condition_match"
);
assert_eq!(
FeatureFlagMatchReason::NoConditionMatch.to_string(),
"no_condition_match"
);
assert_eq!(
FeatureFlagMatchReason::OutOfRolloutBound.to_string(),
"out_of_rollout_bound"
);
assert_eq!(
FeatureFlagMatchReason::NoGroupType.to_string(),
"no_group_type"
);
}
}
8 changes: 8 additions & 0 deletions rust/feature-flags/src/flag_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ impl FeatureFlag {
.clone()
.map_or(vec![], |m| m.variants)
}

pub fn get_payload(&self, match_val: &str) -> Option<serde_json::Value> {
self.filters.payloads.as_ref().and_then(|payloads| {
payloads
.as_object()
.and_then(|obj| obj.get(match_val).cloned())
})
}
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down
Loading

0 comments on commit c940fd5

Please sign in to comment.