|
12 | 12 | // See the License for the specific language governing permissions and
|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 |
| -use std::sync::{Arc, Mutex}; |
16 |
| - |
17 |
| -use crate::{ |
18 |
| - client::cache::ConfigurationSnapshot, models, |
19 |
| - segment_evaluation::find_applicable_segment_rule_for_entity, |
20 |
| -}; |
| 15 | +use crate::Property; |
21 | 16 |
|
22 | 17 | use crate::entity::Entity;
|
23 | 18 |
|
24 |
| -use crate::errors::ConfigurationAccessError; |
25 |
| - |
26 |
| -const MISSING_PROPERTY_ERROR_MSG: &str = "The property should exist in the index. It should have been validated in `AppConfigurationClient::get_property()`."; |
| 19 | +use super::property_snapshot::PropertySnapshot; |
| 20 | +use super::AppConfigurationClient; |
27 | 21 |
|
28 |
| -/// A property in a collection and environment. Use the `get_property()` |
29 |
| -/// method of the `AppConfigurationClient` to create instances of properties. |
30 |
| -#[derive(Debug)] |
31 |
| -pub struct PropertyProxy { |
32 |
| - configuration_snapshot: Arc<Mutex<ConfigurationSnapshot>>, |
| 22 | +pub struct PropertyProxy<'a> { |
| 23 | + client: &'a AppConfigurationClient, |
33 | 24 | property_id: String,
|
34 | 25 | }
|
35 | 26 |
|
36 |
| -impl PropertyProxy { |
37 |
| - pub(crate) fn new( |
38 |
| - configuration_snapshot: Arc<Mutex<ConfigurationSnapshot>>, |
39 |
| - property_id: String, |
40 |
| - ) -> Self { |
41 |
| - PropertyProxy { |
42 |
| - configuration_snapshot, |
| 27 | +impl<'a> PropertyProxy<'a> { |
| 28 | + pub(crate) fn new(client: &'a AppConfigurationClient, property_id: String) -> Self { |
| 29 | + Self { |
| 30 | + client, |
43 | 31 | property_id,
|
44 | 32 | }
|
45 | 33 | }
|
46 | 34 |
|
47 |
| - /// Returns the name of the property. |
48 |
| - pub fn get_name(&self) -> String { |
49 |
| - self.configuration_snapshot |
50 |
| - .lock() |
51 |
| - .unwrap_or_else(|_| panic!("{}", ConfigurationAccessError::LockAcquisitionError)) |
52 |
| - .get_property(&self.property_id) |
53 |
| - .expect(MISSING_PROPERTY_ERROR_MSG) |
54 |
| - .name |
55 |
| - .clone() |
56 |
| - } |
57 |
| - |
58 |
| - /// Returns the value of the property as a `models::ConfigValue`. |
59 |
| - pub fn get_value(&self) -> models::ConfigValue { |
60 |
| - self.configuration_snapshot |
61 |
| - .lock() |
62 |
| - .unwrap_or_else(|_| panic!("{}", ConfigurationAccessError::LockAcquisitionError)) |
63 |
| - .get_property(&self.property_id) |
64 |
| - .expect(MISSING_PROPERTY_ERROR_MSG) |
65 |
| - .value |
66 |
| - .clone() |
67 |
| - } |
68 |
| - |
69 |
| - /// Returns the id of the property. |
70 |
| - pub fn get_id(&self) -> String { |
71 |
| - self.configuration_snapshot |
72 |
| - .lock() |
73 |
| - .unwrap_or_else(|_| panic!("{}", ConfigurationAccessError::LockAcquisitionError)) |
74 |
| - .get_property(&self.property_id) |
75 |
| - .expect(MISSING_PROPERTY_ERROR_MSG) |
76 |
| - .property_id |
77 |
| - .clone() |
| 35 | + pub fn snapshot(&self) -> crate::errors::Result<PropertySnapshot> { |
| 36 | + self.client.get_property(&self.property_id) |
78 | 37 | }
|
| 38 | +} |
79 | 39 |
|
80 |
| - /// Returns the data type as a member of the `models::ValueKind` enumeration. |
81 |
| - pub fn get_data_type(&self) -> models::ValueKind { |
82 |
| - self.configuration_snapshot |
83 |
| - .lock() |
84 |
| - .unwrap_or_else(|_| panic!("{}", ConfigurationAccessError::LockAcquisitionError)) |
85 |
| - .get_property(&self.property_id) |
86 |
| - .expect(MISSING_PROPERTY_ERROR_MSG) |
87 |
| - .kind |
| 40 | +impl<'a> Property for PropertyProxy<'a> { |
| 41 | + fn get_id(&self) -> &str { |
| 42 | + &self.property_id |
88 | 43 | }
|
89 | 44 |
|
90 |
| - /// Gets the `Some(data_format)` if the feature data type is |
91 |
| - /// `models::ValueKind::STRING`, or `None` otherwise. |
92 |
| - pub fn get_data_format(&self) -> Option<String> { |
93 |
| - self.configuration_snapshot |
94 |
| - .lock() |
95 |
| - .unwrap_or_else(|_| panic!("{}", ConfigurationAccessError::LockAcquisitionError)) |
96 |
| - .get_property(&self.property_id) |
97 |
| - .expect(MISSING_PROPERTY_ERROR_MSG) |
98 |
| - .format |
99 |
| - .clone() |
| 45 | + fn get_name(&self) -> crate::errors::Result<String> { |
| 46 | + self.client.get_property(&self.property_id)?.get_name() |
100 | 47 | }
|
101 | 48 |
|
102 |
| - /// Returns the targeting rules for the property. I.e.: what value to |
103 |
| - /// associate with an entity, and under what circumstances it applies. |
104 |
| - pub fn get_targeting_rules(&self) -> Vec<models::TargetingRule> { |
105 |
| - self.configuration_snapshot |
106 |
| - .lock() |
107 |
| - .unwrap_or_else(|_| panic!("{}", ConfigurationAccessError::LockAcquisitionError)) |
108 |
| - .get_property(&self.property_id) |
109 |
| - .expect(MISSING_PROPERTY_ERROR_MSG) |
110 |
| - .segment_rules |
111 |
| - .clone() |
| 49 | + fn get_data_type(&self) -> crate::errors::Result<crate::models::ValueKind> { |
| 50 | + self.client.get_property(&self.property_id)?.get_data_type() |
112 | 51 | }
|
113 | 52 |
|
114 |
| - /// Evaluates the property for `entity` and returns the evaluation as a |
115 |
| - /// `models::ConfigValue`. |
116 |
| - pub fn get_current_value(&self, entity: &impl Entity) -> models::ConfigValue { |
117 |
| - self.evaluate_feature_for_entity(entity) |
| 53 | + fn get_value_default(&self) -> crate::errors::Result<crate::models::ConfigValue> { |
| 54 | + self.client |
| 55 | + .get_property(&self.property_id)? |
| 56 | + .get_value_default() |
118 | 57 | }
|
119 | 58 |
|
120 |
| - fn evaluate_feature_for_entity(&self, entity: &impl Entity) -> models::ConfigValue { |
121 |
| - let segment_rule = find_applicable_segment_rule_for_entity( |
122 |
| - &self |
123 |
| - .configuration_snapshot |
124 |
| - .lock() |
125 |
| - .unwrap_or_else(|e| panic!("Failed to acquire configuration snapshot lock: {e}")) |
126 |
| - .segments, |
127 |
| - self.get_targeting_rules().into_iter(), |
128 |
| - entity, |
129 |
| - ) |
130 |
| - .unwrap_or_else(|e| panic!("Failed to evaluate segment rules: {e}")); |
131 |
| - if let Some(segment_rule) = segment_rule { |
132 |
| - self.resolve_value(&segment_rule) |
133 |
| - } else { |
134 |
| - self.get_value() |
135 |
| - } |
136 |
| - } |
137 |
| - |
138 |
| - fn resolve_value(&self, segment_rule: &models::TargetingRule) -> models::ConfigValue { |
139 |
| - if segment_rule.value.is_default() { |
140 |
| - self.get_value() |
141 |
| - } else { |
142 |
| - segment_rule.value.clone() |
143 |
| - } |
| 59 | + fn get_value(&self, entity: &impl Entity) -> crate::errors::Result<super::value::Value> { |
| 60 | + self.client |
| 61 | + .get_property(&self.property_id)? |
| 62 | + .get_value(entity) |
144 | 63 | }
|
145 | 64 | }
|
0 commit comments