Skip to content

Commit

Permalink
feat!: Add exclude from summary field to flag (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Jul 3, 2024
1 parent 5e4e969 commit ca935a3
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,12 @@ mod tests {
let event = &recorder.events.borrow()[0];
assert_eq!("flagWithSatisfiedPrereq", event.target_flag_key);
assert_eq!("prereq", event.prerequisite_flag.key);
assert!(event.prerequisite_flag.exclude_from_summaries);

let event = &recorder.events.borrow()[1];
assert_eq!("flagWithNestedPrereq", event.target_flag_key);
assert_eq!("flagWithSatisfiedPrereq", event.prerequisite_flag.key);
assert!(!event.prerequisite_flag.exclude_from_summaries);
}

#[test]
Expand Down
46 changes: 36 additions & 10 deletions src/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,27 @@ pub struct Flag {

/// Contains migration-related flag parameters. If this flag is for migration purposes, this
/// property is guaranteed to be set.
#[serde(default, skip_serializing_if = "is_default_migration_settings")]
#[serde(
default,
rename = "migration",
skip_serializing_if = "is_default_migration_settings"
)]
pub migration_settings: Option<MigrationFlagParameters>,

/// Controls the rate at which feature and debug events are emitted from the SDK for this
/// particular flag. If this value is not defined, it is assumed to be 1.
///
/// LaunchDarkly may affect this flag to prevent poorly performing applications from adversely
/// LaunchDarkly may modify this value to prevent poorly performing applications from adversely
/// affecting upstream service health.
#[serde(default, skip_serializing_if = "is_default_ratio")]
pub sampling_ratio: Option<u64>,
pub sampling_ratio: Option<u32>,

/// Determines whether or not this flag will be excluded from the event summarization process.
///
/// LaunchDarkly may change this value to prevent poorly performing applications from adversely
/// affecting upstream service health.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub exclude_from_summaries: bool,
}

impl Versioned for Flag {
Expand All @@ -102,7 +113,7 @@ impl Versioned for Flag {
}

// Used strictly for serialization to determine if a ratio should be included in the JSON.
fn is_default_ratio(sampling_ratio: &Option<u64>) -> bool {
fn is_default_ratio(sampling_ratio: &Option<u32>) -> bool {
sampling_ratio.unwrap_or(1) == 1
}

Expand All @@ -122,7 +133,7 @@ pub struct MigrationFlagParameters {
/// read or write operation. This value can be controlled through the LaunchDarkly UI and
/// propagated downstream to the SDKs.
#[serde(skip_serializing_if = "is_default_ratio")]
pub check_ratio: Option<u64>,
pub check_ratio: Option<u32>,
}

impl MigrationFlagParameters {
Expand Down Expand Up @@ -361,6 +372,7 @@ impl Flag {
context_targets: vec![],
migration_settings: None,
sampling_ratio: None,
exclude_from_summaries: false,
}
}
}
Expand Down Expand Up @@ -597,7 +609,7 @@ mod tests {
asserting!("true for rule if rule.trackEvents is true")
.that(&flag.is_experimentation_enabled(&RuleMatch {
rule_index: 0,
rule_id: flag.rules.get(0).unwrap().id.clone(),
rule_id: flag.rules.first().unwrap().id.clone(),
in_experiment: false,
}))
.is_true();
Expand Down Expand Up @@ -648,30 +660,44 @@ mod tests {
assert!(!with_no_ratio.contains("\"samplingRatio\""));
}

#[test]
fn exclude_from_summaries_is_ignored_appropriately() {
let store = TestStore::new();
let mut flag = store.flag("flag").unwrap();

flag.exclude_from_summaries = true;
let with_exclude = serde_json::to_string_pretty(&flag).unwrap();
assert!(with_exclude.contains("\"excludeFromSummaries\": true"));

flag.exclude_from_summaries = false;
let without_exclude = serde_json::to_string_pretty(&flag).unwrap();
assert!(!without_exclude.contains("\"excludeFromSummaries\""));
}

#[test]
fn migration_settings_included_appropriately() {
let store = TestStore::new();
let mut flag = store.flag("flag").unwrap();

flag.migration_settings = None;
let without_migration_settings = serde_json::to_string_pretty(&flag).unwrap();
assert!(!without_migration_settings.contains("\"migrationSettings\""));
assert!(!without_migration_settings.contains("\"migration\""));

flag.migration_settings = Some(MigrationFlagParameters { check_ratio: None });
let without_empty_migration_settings = serde_json::to_string_pretty(&flag).unwrap();
assert!(!without_empty_migration_settings.contains("\"migrationSettings\""));
assert!(!without_empty_migration_settings.contains("\"migration\""));

flag.migration_settings = Some(MigrationFlagParameters {
check_ratio: Some(1),
});
let with_default_ratio = serde_json::to_string_pretty(&flag).unwrap();
assert!(!with_default_ratio.contains("\"migrationSettings\""));
assert!(!with_default_ratio.contains("\"migration\""));

flag.migration_settings = Some(MigrationFlagParameters {
check_ratio: Some(42),
});
let with_specific_ratio = serde_json::to_string_pretty(&flag).unwrap();
assert!(with_specific_ratio.contains("\"migrationSettings\": {"));
assert!(with_specific_ratio.contains("\"migration\": {"));
assert!(with_specific_ratio.contains("\"checkRatio\": 42"));
}
}
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ pub use segment::*;
pub use store::*;
pub use variation::*;

#[cfg(test)]
pub(crate) mod proptest_generators {
pub(crate) use crate::contexts::attribute_reference::proptest_generators::*;
pub(crate) use crate::contexts::context::proptest_generators::*;
pub(crate) use crate::rule::proptest_generators::*;
pub(crate) use crate::variation::proptest_generators::*;
}

/// Trait indicating that the item is versioned.
pub trait Versioned {
/// Retrieve the version for this item instance.
Expand All @@ -50,3 +42,11 @@ pub trait Versioned {
self.version() >= version
}
}

#[cfg(test)]
pub(crate) mod proptest_generators {
pub(crate) use crate::contexts::attribute_reference::proptest_generators::*;
pub(crate) use crate::contexts::context::proptest_generators::*;
pub(crate) use crate::rule::proptest_generators::*;
pub(crate) use crate::variation::proptest_generators::*;
}
2 changes: 1 addition & 1 deletion src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ mod tests {
proptest! {
#[test]
fn arbitrary_clause_serialization_rountrip(clause in any_clause()) {
let json = serde_json::to_value(&clause).expect("a clause should serialize");
let json = serde_json::to_value(clause).expect("a clause should serialize");
let parsed: Clause = serde_json::from_value(json.clone()).expect("a clause should parse");
assert_json_eq!(json, parsed);
}
Expand Down
2 changes: 1 addition & 1 deletion src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ mod tests {
proptest! {
#[test]
fn arbitrary_segment_rule_serialization_roundtrip(rule in any_segment_rule()) {
let json = serde_json::to_value(&rule).expect("an arbitrary segment rule should serialize");
let json = serde_json::to_value(rule).expect("an arbitrary segment rule should serialize");
let parsed: SegmentRule = serde_json::from_value(json.clone()).expect("an arbitrary segment rule should parse");
assert_json_eq!(json, parsed);
}
Expand Down
3 changes: 2 additions & 1 deletion src/test_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,8 @@ impl TestStore {
"usingEnvironmentId": true,
"usingMobileKey": true
},
"salt": "salty"
"salt": "salty",
"excludeFromSummaries": true
}"#).unwrap(),
"offPrereq".to_string() => serde_json::from_str(r#"{
"key": "offPrereq",
Expand Down
2 changes: 1 addition & 1 deletion src/variation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ mod tests {
proptest! {
#[test]
fn arbitrary_rollout_serialization_roundtrip(rollout in any_rollout()) {
let json = serde_json::to_value(&rollout).expect("a rollout should serialize");
let json = serde_json::to_value(rollout).expect("a rollout should serialize");
let parsed: Rollout = serde_json::from_value(json.clone()).expect("a rollout should parse");
assert_json_eq!(json, parsed);
}
Expand Down

0 comments on commit ca935a3

Please sign in to comment.