@@ -16,6 +16,11 @@ use std::collections::{HashMap, HashSet};
16
16
17
17
use crate :: errors:: { ConfigurationAccessError , Result } ;
18
18
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 ;
19
24
20
25
/// Represents all the configuration data needed for the client to perform
21
26
/// feature/propery evaluation.
@@ -28,22 +33,63 @@ pub(crate) struct Configuration {
28
33
}
29
34
30
35
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 {
34
40
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 ( ) ,
35
51
}
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
+ ) )
38
68
}
39
69
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 {
43
74
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 ( ) ,
44
85
}
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 ) )
47
93
}
48
94
49
95
/// Constructs the Configuration, by consuming and filtering data in exchange format
0 commit comments