Skip to content

Commit bff6477

Browse files
authored
[refact] Refact/models (#61)
Following #56, this PR moves the `FeatureSnapshot` and `PropertySnapshot` inside `Configuration` (closer to JSON data), this way the json-models doesn't leak into the application. --------- Signed-off-by: Javier G. Sogo <[email protected]>
1 parent 812cc68 commit bff6477

File tree

7 files changed

+200
-200
lines changed

7 files changed

+200
-200
lines changed

src/client/app_configuration_http.rs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,7 @@ impl AppConfigurationClient for AppConfigurationClientHttp {
176176
}
177177

178178
fn get_feature(&self, feature_id: &str) -> Result<FeatureSnapshot> {
179-
let config_snapshot = self.latest_config_snapshot.lock()?;
180-
181-
// Get the feature from the snapshot
182-
let feature = config_snapshot.get_feature(feature_id)?;
183-
184-
// Get the segment rules that apply to this feature
185-
let segments = config_snapshot.get_segments_for_segment_rules(&feature.segment_rules);
186-
187-
// Integrity DB check: all segment_ids should be available in the snapshot
188-
if feature.segment_rules.len() != segments.len() {
189-
return Err(ConfigurationAccessError::MissingSegments {
190-
resource_id: feature_id.to_string(),
191-
}
192-
.into());
193-
}
194-
195-
Ok(FeatureSnapshot::new(feature.clone(), segments))
179+
self.latest_config_snapshot.lock()?.get_feature(feature_id)
196180
}
197181

198182
fn get_feature_proxy<'a>(&'a self, feature_id: &str) -> Result<FeatureProxy<'a>> {
@@ -214,23 +198,9 @@ impl AppConfigurationClient for AppConfigurationClientHttp {
214198
}
215199

216200
fn get_property(&self, property_id: &str) -> Result<PropertySnapshot> {
217-
let config_snapshot = self.latest_config_snapshot.lock()?;
218-
219-
// Get the property from the snapshot
220-
let property = config_snapshot.get_property(property_id)?;
221-
222-
// Get the segment rules that apply to this property
223-
let segments = config_snapshot.get_segments_for_segment_rules(&property.segment_rules);
224-
225-
// Integrity DB check: all segment_ids should be available in the snapshot
226-
if property.segment_rules.len() != segments.len() {
227-
return Err(ConfigurationAccessError::MissingSegments {
228-
resource_id: property_id.to_string(),
229-
}
230-
.into());
231-
}
232-
233-
Ok(PropertySnapshot::new(property.clone(), segments))
201+
self.latest_config_snapshot
202+
.lock()?
203+
.get_property(property_id)
234204
}
235205

236206
fn get_property_proxy(&self, property_id: &str) -> Result<PropertyProxy> {

src/client/app_configuration_offline.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,7 @@ impl AppConfigurationClient for AppConfigurationOffline {
6565
}
6666

6767
fn get_feature(&self, feature_id: &str) -> Result<FeatureSnapshot> {
68-
// Get the feature from the snapshot
69-
let feature = self.config_snapshot.get_feature(feature_id)?;
70-
71-
// Get the segment rules that apply to this feature
72-
let segments = self
73-
.config_snapshot
74-
.get_segments_for_segment_rules(&feature.segment_rules);
75-
76-
Ok(FeatureSnapshot::new(feature.clone(), segments))
68+
self.config_snapshot.get_feature(feature_id)
7769
}
7870

7971
fn get_feature_proxy<'a>(&'a self, feature_id: &str) -> Result<FeatureProxy<'a>> {
@@ -88,15 +80,7 @@ impl AppConfigurationClient for AppConfigurationOffline {
8880
}
8981

9082
fn get_property(&self, property_id: &str) -> Result<PropertySnapshot> {
91-
// Get the property from the snapshot
92-
let property = self.config_snapshot.get_property(property_id)?;
93-
94-
// Get the segment rules that apply to this feature
95-
let segments = self
96-
.config_snapshot
97-
.get_segments_for_segment_rules(&property.segment_rules);
98-
99-
Ok(PropertySnapshot::new(property.clone(), segments))
83+
self.config_snapshot.get_property(property_id)
10084
}
10185

10286
fn get_property_proxy(&self, property_id: &str) -> Result<PropertyProxy> {

src/client/configuration.rs

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ use std::collections::{HashMap, HashSet};
1616

1717
use crate::errors::{ConfigurationAccessError, Result};
1818
use crate::models::{ConfigurationJson, Feature, Property, Segment, TargetingRule};
19+
use crate::segment_evaluation::SegmentRules;
20+
use crate::Error;
21+
22+
use super::feature_snapshot::FeatureSnapshot;
23+
use super::property_snapshot::PropertySnapshot;
1924

2025
/// Represents all the configuration data needed for the client to perform
2126
/// feature/propery evaluation.
@@ -28,22 +33,63 @@ pub(crate) struct Configuration {
2833
}
2934

3035
impl Configuration {
31-
pub fn get_feature(&self, feature_id: &str) -> Result<&Feature> {
32-
self.features.get(feature_id).ok_or_else(|| {
33-
ConfigurationAccessError::FeatureNotFound {
36+
pub fn get_feature(&self, feature_id: &str) -> Result<FeatureSnapshot> {
37+
// Get the feature from the snapshot
38+
let feature = self.features.get(feature_id).ok_or_else(|| {
39+
Error::ConfigurationAccessError(ConfigurationAccessError::FeatureNotFound {
3440
feature_id: feature_id.to_string(),
41+
})
42+
})?;
43+
44+
// Get the segment rules that apply to this feature
45+
let segments = self.get_segments_for_segment_rules(&feature.segment_rules);
46+
47+
// Integrity DB check: all segment_ids should be available in the snapshot
48+
if feature.segment_rules.len() != segments.len() {
49+
return Err(ConfigurationAccessError::MissingSegments {
50+
resource_id: feature_id.to_string(),
3551
}
36-
.into()
37-
})
52+
.into());
53+
}
54+
55+
let segment_rules =
56+
SegmentRules::new(segments, feature.segment_rules.clone(), feature.kind);
57+
let enabled_value = (feature.kind, feature.enabled_value.clone()).try_into()?;
58+
let disabled_value = (feature.kind, feature.disabled_value.clone()).try_into()?;
59+
Ok(FeatureSnapshot::new(
60+
feature.enabled,
61+
enabled_value,
62+
disabled_value,
63+
feature.rollout_percentage,
64+
&feature.name,
65+
feature_id,
66+
segment_rules,
67+
))
3868
}
3969

40-
pub fn get_property(&self, property_id: &str) -> Result<&Property> {
41-
self.properties.get(property_id).ok_or_else(|| {
42-
ConfigurationAccessError::PropertyNotFound {
70+
pub fn get_property(&self, property_id: &str) -> Result<PropertySnapshot> {
71+
// Get the property from the snapshot
72+
let property = self.properties.get(property_id).ok_or_else(|| {
73+
Error::ConfigurationAccessError(ConfigurationAccessError::PropertyNotFound {
4374
property_id: property_id.to_string(),
75+
})
76+
})?;
77+
78+
// Get the segment rules that apply to this property
79+
let segments = self.get_segments_for_segment_rules(&property.segment_rules);
80+
81+
// Integrity DB check: all segment_ids should be available in the snapshot
82+
if property.segment_rules.len() != segments.len() {
83+
return Err(ConfigurationAccessError::MissingSegments {
84+
resource_id: property_id.to_string(),
4485
}
45-
.into()
46-
})
86+
.into());
87+
}
88+
89+
let value = (property.kind, property.value.clone()).try_into()?;
90+
let segment_rules =
91+
SegmentRules::new(segments, property.segment_rules.clone(), property.kind);
92+
Ok(PropertySnapshot::new(value, segment_rules, &property.name))
4793
}
4894

4995
/// Constructs the Configuration, by consuming and filtering data in exchange format

src/client/feature_proxy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'a> FeatureProxy<'a> {
3939
}
4040
}
4141

42-
impl<'a> Feature for FeatureProxy<'a> {
42+
impl Feature for FeatureProxy<'_> {
4343
fn get_name(&self) -> crate::errors::Result<String> {
4444
self.client.get_feature(&self.feature_id)?.get_name()
4545
}

0 commit comments

Comments
 (0)