Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/segments/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ pub const NOT_EQUAL: &str = "NOT_EQUAL";
pub const REGEX: &str = "REGEX";
pub const PERCENTAGE_SPLIT: &str = "PERCENTAGE_SPLIT";
pub const MODULO: &str = "MODULO";
pub const IS_SET: &str = "IS_SET";
pub const IS_NOT_SET: &str = "IS_NOT_SET";
53 changes: 49 additions & 4 deletions src/segments/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,15 @@ fn traits_match_segment_rule(
})
.all(|result| result == true);
}

fn traits_match_segment_condition(
identity_traits: &Vec<identities::Trait>,
condition: &SegmentCondition,
segment_id: &str,
identity_id: &str,
) -> bool {
if condition.operator == constants::PERCENTAGE_SPLIT {
let float_value: f32 = condition.value.parse().unwrap();
let float_value: f32 = condition.value.as_ref().unwrap().parse().unwrap();
return get_hashed_percentage_for_object_ids(vec![segment_id, identity_id], 1)
<= float_value;
}
Expand All @@ -82,11 +83,55 @@ fn traits_match_segment_condition(
.iter()
.filter(|identity_trait| identity_trait.trait_key == property)
.next();
match identity_trait {
Some(_trait) => condition.matches_trait_value(&_trait.trait_value),
None => false,
if condition.operator == constants::IS_SET {
return identity_trait.is_some();
}

if condition.operator == constants::IS_NOT_SET {
return identity_trait.is_none();
}

if identity_trait.is_some() {
return condition.matches_trait_value(&identity_trait.unwrap().trait_value);
}

return false;
}
None => false,
}
}

#[cfg(test)]
mod tests {
use crate::types::FlagsmithValue;

use super::*;
use rstest::*;

#[rstest]
#[case(constants::IS_SET, "foo", "foo", true)]
#[case(constants::IS_SET, "foo", "bar", false)]
#[case(constants::IS_NOT_SET, "foo", "foo", false)]
#[case(constants::IS_NOT_SET, "foo", "bar", true)]
fn trait_matches_segmnt_condition_is_and_is_not(
#[case] operator: &str,
#[case] property: &str,
#[case] trait_key: &str,
#[case] expected_result: bool,
) {
let condition = SegmentCondition {
property: Some(property.to_string()),
operator: operator.to_string(),
value: None,
};
let traits = vec![identities::Trait {
trait_key: trait_key.to_string(),
trait_value: FlagsmithValue {
value: "".to_string(),
value_type: crate::types::FlagsmithValueType::None,
},
}];
let result = traits_match_segment_condition(&traits, &condition, "1", "1");
assert_eq!(result, expected_result);
}
}
34 changes: 19 additions & 15 deletions src/segments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,42 @@ pub mod evaluator;
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct SegmentCondition {
pub operator: String,
pub value: String,
pub value: Option<String>,
#[serde(rename = "property_")]
pub property: Option<String>,
}

impl SegmentCondition {
pub fn matches_trait_value(&self, trait_value: &FlagsmithValue) -> bool {
if self.operator.as_str() == constants::MODULO {
return self.modulo_operations(trait_value, &self.value);
return self.modulo_operations(trait_value, &self.value.as_ref().unwrap());
}
return match trait_value.value_type {
FlagsmithValueType::Integer => {
let trait_value: i64 = trait_value.value.parse().unwrap();
let segment_condition_value: i64 = self.value.clone().parse().unwrap();
let segment_condition_value: i64 = self.value.as_ref().unwrap().parse().unwrap();

self.number_operations(trait_value, segment_condition_value)
}
FlagsmithValueType::Float => {
let trait_value: f64 = trait_value.value.parse().unwrap();
let segment_condition_value: f64 = self.value.clone().parse().unwrap();
let segment_condition_value: f64 = self.value.as_ref().unwrap().parse().unwrap();
self.number_operations(trait_value, segment_condition_value)
}
FlagsmithValueType::String if self.value.ends_with(":semver") => {
FlagsmithValueType::String if self.value.as_ref().unwrap().ends_with(":semver") => {
let trait_value = Version::parse(&trait_value.value).unwrap();
let segment_condition_value =
Version::parse(&self.value.clone()[..self.value.len() - 7]).unwrap();
let segment_condition_value = Version::parse(
&self.value.as_ref().unwrap()[..self.value.as_ref().unwrap().len() - 7],
)
.unwrap();
self.semver_operations(trait_value, segment_condition_value)
}
FlagsmithValueType::String => self.string_operations(&trait_value.value, &self.value),
FlagsmithValueType::String => {
self.string_operations(&trait_value.value, &self.value.as_ref().unwrap())
}
FlagsmithValueType::Bool => {
let trait_value: bool = trait_value.value.parse().unwrap();
let segment_condition_value: bool = self.value.clone().parse().unwrap();
let segment_condition_value: bool = self.value.clone().unwrap().parse().unwrap();
self.bool_operations(trait_value, segment_condition_value)
}
_ => false,
Expand All @@ -50,10 +54,10 @@ impl SegmentCondition {
match self.operator.as_str() {
constants::EQUAL => trait_value == segment_value,
constants::NOT_EQUAL => trait_value != segment_value,
constants::CONTAINS => trait_value.contains(&self.value),
constants::NOT_CONTAINS => !trait_value.contains(&self.value),
constants::CONTAINS => trait_value.contains(segment_value),
constants::NOT_CONTAINS => !trait_value.contains(segment_value),
constants::REGEX => {
let re = Regex::new(&self.value).unwrap();
let re = Regex::new(segment_value).unwrap();
re.is_match(&trait_value)
}
_ => false,
Expand Down Expand Up @@ -321,7 +325,7 @@ mod tests {
};
let segment_condition = SegmentCondition {
operator: operator.to_string(),
value: value.to_string(),
value: Some(value.to_string()),
property: Some("foo".to_string()),
};
assert_eq!(segment_condition.matches_trait_value(&trait_value), result)
Expand Down Expand Up @@ -489,7 +493,7 @@ mod tests {
};
let segment_condition = SegmentCondition {
operator: operator.to_string(),
value: value.to_string(),
value: Some(value.to_string()),
property: Some("foo".to_string()),
};
assert_eq!(segment_condition.matches_trait_value(&trait_value), result)
Expand Down Expand Up @@ -517,7 +521,7 @@ mod tests {
};
let segment_condition = SegmentCondition {
operator: constants::MODULO.to_string(),
value: value.to_string(),
value: Some(value.to_string()),
property: Some("foo".to_string()),
};
assert_eq!(segment_condition.matches_trait_value(&trait_value), result)
Expand Down
22 changes: 11 additions & 11 deletions tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn segment_single_condition() -> segments::Segment {
conditions: vec![segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_1.to_string()),
value: TRAIT_VALUE_1.to_string(),
value: Some(TRAIT_VALUE_1.to_string()),
}],
}],
feature_states: vec![],
Expand All @@ -96,12 +96,12 @@ pub fn segment_multiple_conditions_all() -> segments::Segment {
segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_1.to_string()),
value: TRAIT_VALUE_1.to_string(),
value: Some(TRAIT_VALUE_1.to_string()),
},
segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_2.to_string()),
value: TRAIT_VALUE_2.to_string(),
value: Some(TRAIT_VALUE_2.to_string()),
},
],
}],
Expand All @@ -120,12 +120,12 @@ pub fn segment_multiple_conditions_any() -> segments::Segment {
segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_1.to_string()),
value: TRAIT_VALUE_1.to_string(),
value: Some(TRAIT_VALUE_1.to_string()),
},
segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_2.to_string()),
value: TRAIT_VALUE_2.to_string(),
value: Some(TRAIT_VALUE_2.to_string()),
},
],
}],
Expand All @@ -149,12 +149,12 @@ pub fn segment_nested_rules_all() -> segments::Segment {
segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_1.to_string()),
value: TRAIT_VALUE_1.to_string(),
value: Some(TRAIT_VALUE_1.to_string()),
},
segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_2.to_string()),
value: TRAIT_VALUE_2.to_string(),
value: Some(TRAIT_VALUE_2.to_string()),
},
],
}
Expand All @@ -165,7 +165,7 @@ pub fn segment_nested_rules_all() -> segments::Segment {
conditions: vec![segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_3.to_string()),
value: TRAIT_VALUE_3.to_string(),
value: Some(TRAIT_VALUE_3.to_string()),
}],
}),
],
Expand All @@ -183,7 +183,7 @@ pub fn segment_conditions_and_nested_rules() -> segments::Segment {
conditions: vec![segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_1.to_string()),
value: TRAIT_VALUE_1.to_string(),
value: Some(TRAIT_VALUE_1.to_string()),
}],
rules: vec![
Box::new({
Expand All @@ -193,7 +193,7 @@ pub fn segment_conditions_and_nested_rules() -> segments::Segment {
conditions: vec![segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_2.to_string()),
value: TRAIT_VALUE_2.to_string(),
value: Some(TRAIT_VALUE_2.to_string()),
}],
}
}),
Expand All @@ -204,7 +204,7 @@ pub fn segment_conditions_and_nested_rules() -> segments::Segment {
conditions: vec![segments::SegmentCondition {
operator: constants::EQUAL.to_string(),
property: Some(TRAIT_KEY_3.to_string()),
value: TRAIT_VALUE_3.to_string(),
value: Some(TRAIT_VALUE_3.to_string()),
}],
}
}),
Expand Down