-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_features.rs
1134 lines (1059 loc) · 37.2 KB
/
client_features.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#[cfg(feature = "hashes")]
use base64::Engine;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::{cmp::Ordering, collections::BTreeMap};
#[cfg(feature = "openapi")]
use utoipa::{IntoParams, ToSchema};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;
#[cfg(feature = "hashes")]
use xxhash_rust::xxh3::xxh3_128;
use crate::{Deduplicate, Merge, Upsert};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
#[serde(rename_all = "camelCase")]
pub struct Query {
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<Vec<String>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub projects: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name_prefix: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub environment: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub inline_segment_constraints: Option<bool>,
}
#[derive(Serialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Operator {
NotIn,
In,
StrEndsWith,
StrStartsWith,
StrContains,
NumEq,
NumGt,
NumGte,
NumLt,
NumLte,
DateAfter,
DateBefore,
SemverEq,
SemverLt,
SemverGt,
Unknown(String),
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[cfg_attr(feature = "openapi", derive(ToSchema, IntoParams))]
#[cfg_attr(feature = "openapi", into_params(style = Form, parameter_in = Query))]
#[serde(rename_all = "camelCase")]
pub struct Context {
#[serde(default)]
#[serde(
deserialize_with = "stringify_numbers_and_booleans",
skip_serializing_if = "Option::is_none"
)]
pub user_id: Option<String>,
#[serde(default)]
#[serde(
deserialize_with = "stringify_numbers_and_booleans",
skip_serializing_if = "Option::is_none"
)]
pub session_id: Option<String>,
#[serde(default)]
#[serde(
deserialize_with = "stringify_numbers_and_booleans",
skip_serializing_if = "Option::is_none"
)]
pub environment: Option<String>,
#[serde(default)]
#[serde(
deserialize_with = "stringify_numbers_and_booleans",
skip_serializing_if = "Option::is_none"
)]
pub app_name: Option<String>,
#[serde(default)]
#[serde(
deserialize_with = "stringify_numbers_and_booleans",
skip_serializing_if = "Option::is_none"
)]
pub current_time: Option<String>,
#[serde(default)]
#[serde(
deserialize_with = "stringify_numbers_and_booleans",
skip_serializing_if = "Option::is_none"
)]
pub remote_address: Option<String>,
#[serde(default)]
#[serde(
deserialize_with = "stringify_numbers_and_booleans_remove_nulls_and_non_strings",
serialize_with = "optional_ordered_map",
skip_serializing_if = "Option::is_none"
)]
#[cfg_attr(feature = "openapi", param(style = Form, explode = false, value_type = Object))]
pub properties: Option<HashMap<String, String>>,
}
// I know this looks silly but it's also important for two reasons:
// The first is that the client spec tests have a test case that has a context defined like:
// {
// "properties": {
// "someValue": null
// }
// }
// Passing around an Option<HashMap<String, Option<String>>> is awful and unnecessary, we should scrub ingested data
// before trying to execute our logic, so we scrub out those empty values instead, they do nothing useful for us.
// The second reason is that we can't shield the Rust code from consumers using the FFI layers and potentially doing
// exactly the same thing in languages that allow it. They should not do that. But if they do we have enough information
// to understand the intent of the executed code clearly and there's no reason to fail.
// This also maps numbers + booleans to strings, and disregards other types without failing
fn stringify_numbers_and_booleans_remove_nulls_and_non_strings<'de, D>(
deserializer: D,
) -> Result<Option<HashMap<String, String>>, D::Error>
where
D: Deserializer<'de>,
{
let props: Option<HashMap<String, Option<Value>>> = Option::deserialize(deserializer)?;
Ok(props.map(|props| {
props
.into_iter()
.filter_map(|(k, v)| match v {
Some(Value::String(s)) => {
if s.is_empty() {
None
} else {
Some((k, s))
}
},
Some(Value::Number(n)) => Some((k, n.to_string())),
Some(Value::Bool(b)) => Some((k, b.to_string())),
_ => None,
})
.collect()
}))
}
// Allowing a looser deserialization for the contexts properties to match Unleash behavior
fn stringify_numbers_and_booleans<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let prop: Option<Value> = Option::deserialize(deserializer)?;
Ok(match prop {
Some(Value::String(s)) => {
if s.is_empty() {
None
} else {
Some(s)
}
}
Some(Value::Number(n)) => Some(n.to_string()),
Some(Value::Bool(b)) => Some(b.to_string()),
_ => None,
})
}
///
/// We need this to ensure that ClientFeatures gets a deterministic serialization.
fn optional_ordered_map<S>(
value: &Option<HashMap<String, String>>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match value {
Some(m) => {
let ordered: BTreeMap<_, _> = m.iter().collect();
ordered.serialize(serializer)
}
None => serializer.serialize_none(),
}
}
impl Default for Context {
fn default() -> Self {
Self {
user_id: None,
session_id: None,
environment: None,
current_time: None,
app_name: None,
remote_address: None,
properties: Some(HashMap::new()),
}
}
}
impl<'de> Deserialize<'de> for Operator {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(match s.as_str() {
"NOT_IN" => Operator::NotIn,
"IN" => Operator::In,
"STR_ENDS_WITH" => Operator::StrEndsWith,
"STR_STARTS_WITH" => Operator::StrStartsWith,
"STR_CONTAINS" => Operator::StrContains,
"NUM_EQ" => Operator::NumEq,
"NUM_GT" => Operator::NumGt,
"NUM_GTE" => Operator::NumGte,
"NUM_LT" => Operator::NumLt,
"NUM_LTE" => Operator::NumLte,
"DATE_AFTER" => Operator::DateAfter,
"DATE_BEFORE" => Operator::DateBefore,
"SEMVER_EQ" => Operator::SemverEq,
"SEMVER_LT" => Operator::SemverLt,
"SEMVER_GT" => Operator::SemverGt,
_ => Operator::Unknown(s),
})
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct Constraint {
pub context_name: String,
pub operator: Operator,
#[serde(default)]
pub case_insensitive: bool,
#[serde(default)]
pub inverted: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub values: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub enum WeightType {
Fix,
Variable,
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct Strategy {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub sort_order: Option<i32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub segments: Option<Vec<i32>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub constraints: Option<Vec<Constraint>>,
#[serde(
serialize_with = "optional_ordered_map",
skip_serializing_if = "Option::is_none"
)]
pub parameters: Option<HashMap<String, String>>,
#[serde(serialize_with = "serialize_option_vec")]
pub variants: Option<Vec<StrategyVariant>>,
}
fn serialize_option_vec<S, T>(value: &Option<Vec<T>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Serialize,
{
match value {
Some(ref v) => v.serialize(serializer),
None => Vec::<T>::new().serialize(serializer),
}
}
impl PartialEq for Strategy {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.sort_order == other.sort_order
&& self.segments == other.segments
&& self.constraints == other.constraints
&& self.parameters == other.parameters
}
}
impl PartialOrd for Strategy {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Strategy {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.sort_order.cmp(&other.sort_order) {
Ordering::Equal => self.name.cmp(&other.name),
ord => ord,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct Override {
pub context_name: String,
pub values: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct Payload {
#[serde(rename = "type")]
pub payload_type: String,
pub value: String,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct Variant {
pub name: String,
pub weight: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub weight_type: Option<WeightType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stickiness: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub payload: Option<Payload>,
#[serde(skip_serializing_if = "Option::is_none")]
pub overrides: Option<Vec<Override>>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct StrategyVariant {
pub name: String,
pub weight: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub payload: Option<Payload>,
#[serde(skip_serializing_if = "Option::is_none")]
pub stickiness: Option<String>,
}
impl PartialOrd for Variant {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Variant {
fn cmp(&self, other: &Self) -> Ordering {
self.name.cmp(&other.name)
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct Segment {
pub id: i32,
pub constraints: Vec<Constraint>,
}
impl PartialEq for Segment {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl PartialOrd for Segment {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Segment {
fn cmp(&self, other: &Self) -> Ordering {
self.id.cmp(&other.id)
}
}
impl Hash for Segment {
fn hash<H: Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct FeatureDependency {
pub feature: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub variants: Option<Vec<String>>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, Default)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct ClientFeature {
pub name: String,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub feature_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_seen_at: Option<DateTime<Utc>>,
pub enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub stale: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub impression_data: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub project: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub strategies: Option<Vec<Strategy>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub variants: Option<Vec<Variant>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub dependencies: Option<Vec<FeatureDependency>>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
#[serde(rename_all = "camelCase")]
pub struct Meta {
pub etag: Option<String>,
pub revision_id: Option<usize>,
pub query_hash: Option<String>,
}
impl Merge for ClientFeatures {
fn merge(self, other: Self) -> Self {
let mut features = self.features.merge(other.features);
features.sort();
let segments = match (self.segments, other.segments) {
(Some(mut s), Some(o)) => {
s.extend(o);
Some(s.deduplicate())
}
(Some(s), None) => Some(s),
(None, Some(o)) => Some(o),
(None, None) => None,
};
ClientFeatures {
version: self.version.max(other.version),
features,
segments: segments.map(|mut s| {
s.sort();
s
}),
query: self.query.or(other.query),
meta: other.meta.or(self.meta),
}
}
}
impl Upsert for ClientFeatures {
fn upsert(self, other: Self) -> Self {
let mut features = self.features.upsert(other.features);
features.sort();
let segments = match (self.segments, other.segments) {
(Some(s), Some(mut o)) => {
o.extend(s);
Some(o.deduplicate())
}
(Some(s), None) => Some(s),
(None, Some(o)) => Some(o),
(None, None) => None,
};
ClientFeatures {
version: self.version.max(other.version),
features,
segments: segments.map(|mut s| {
s.sort();
s
}),
query: self.query.or(other.query),
meta: other.meta.or(self.meta),
}
}
}
impl PartialOrd for ClientFeature {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ClientFeature {
fn cmp(&self, other: &Self) -> Ordering {
self.name.cmp(&other.name)
}
}
impl PartialEq for ClientFeature {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Hash for ClientFeature {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct ClientFeatures {
pub version: u32,
pub features: Vec<ClientFeature>,
#[serde(skip_serializing_if = "Option::is_none")]
pub segments: Option<Vec<Segment>>,
pub query: Option<Query>,
#[serde(skip_serializing_if = "Option::is_none")]
pub meta: Option<Meta>,
}
#[cfg(feature = "hashes")]
impl ClientFeatures {
///
/// Returns a base64 encoded xx3_128 hash for the json representation of ClientFeatures
///
pub fn xx3_hash(&self) -> Result<String, serde_json::Error> {
serde_json::to_string(self)
.map(|s| xxh3_128(s.as_bytes()))
.map(|xxh_hash| base64::prelude::BASE64_URL_SAFE.encode(xxh_hash.to_le_bytes()))
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(tag = "type", rename_all = "kebab-case")]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub enum DeltaEvent {
/// Event for a feature update.
FeatureUpdated {
#[serde(rename = "eventId")]
event_id: u32,
feature: ClientFeature,
},
/// Event for a feature removal.
#[serde(rename_all = "camelCase")]
FeatureRemoved {
event_id: u32,
feature_name: String,
project: String,
},
/// Event for a segment update.
SegmentUpdated {
#[serde(rename = "eventId")]
event_id: u32,
segment: Segment,
},
/// Event for a segment removal.
#[serde(rename_all = "camelCase")]
SegmentRemoved { event_id: u32, segment_id: i32 },
/// Hydration event for features and segments.
Hydration {
#[serde(rename = "eventId")]
event_id: u32,
features: Vec<ClientFeature>,
segments: Vec<Segment>,
},
}
impl DeltaEvent {
pub fn get_event_id(&self) -> u32 {
match self {
DeltaEvent::FeatureUpdated { event_id, .. }
| DeltaEvent::FeatureRemoved { event_id, .. }
| DeltaEvent::SegmentUpdated { event_id, .. }
| DeltaEvent::SegmentRemoved { event_id, .. }
| DeltaEvent::Hydration { event_id, .. } => *event_id,
}
}
}
/// Schema for delta updates of feature configurations.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "openapi", derive(ToSchema))]
pub struct ClientFeaturesDelta {
/// A list of delta events.
pub events: Vec<DeltaEvent>,
}
impl ClientFeatures {
/// Modifies the current ClientFeatures instance by applying the events.
pub fn apply_delta(&mut self, delta: &ClientFeaturesDelta) {
self.apply_delta_events(delta);
}
/// Returns a new ClientFeatures instance with the events applied.
pub fn create_from_delta(delta: &ClientFeaturesDelta) -> ClientFeatures {
let mut client_features = ClientFeatures::default();
client_features.apply_delta_events(delta);
client_features
}
fn apply_delta_events(&mut self, delta: &ClientFeaturesDelta) {
let segments = &mut self.segments;
let features = &mut self.features;
for event in &delta.events {
match event {
DeltaEvent::FeatureUpdated { feature, .. } => {
if let Some(existing) = features.iter_mut().find(|f| f.name == feature.name) {
*existing = feature.clone();
} else {
features.push(feature.clone());
}
}
DeltaEvent::FeatureRemoved { feature_name, .. } => {
features.retain(|f| f.name != *feature_name);
}
DeltaEvent::SegmentUpdated { segment, .. } => {
let segments_list = segments.get_or_insert_with(Vec::new);
if let Some(existing) = segments_list.iter_mut().find(|s| s.id == segment.id) {
*existing = segment.clone();
} else {
segments_list.push(segment.clone());
}
}
DeltaEvent::SegmentRemoved { segment_id, .. } => {
if let Some(segments_list) = segments {
segments_list.retain(|s| s.id != *segment_id);
}
}
DeltaEvent::Hydration {
features: new_features,
segments: new_segments,
..
} => {
*features = new_features.clone();
*segments = Some(new_segments.clone());
}
}
}
features.sort();
}
}
impl Default for ClientFeatures {
fn default() -> Self {
Self {
version: 2,
features: vec![],
segments: None,
query: None,
meta: None,
}
}
}
impl From<ClientFeaturesDelta> for ClientFeatures {
fn from(value: ClientFeaturesDelta) -> Self {
ClientFeatures::create_from_delta(&value)
}
}
impl From<&ClientFeaturesDelta> for ClientFeatures {
fn from(value: &ClientFeaturesDelta) -> Self {
ClientFeatures::create_from_delta(value)
}
}
#[cfg(test)]
mod tests {
use crate::{
client_features::{ClientFeature, ClientFeaturesDelta},
Merge, Upsert,
};
use serde_json::{from_reader, to_string};
use serde_qs::Config;
use std::{fs::File, io::BufReader, path::PathBuf};
use super::{ClientFeatures, Constraint, DeltaEvent, Operator, Segment, Strategy};
use crate::client_features::Context;
use test_case::test_case;
#[derive(Debug)]
pub enum EdgeError {
SomethingWentWrong,
}
#[test]
pub fn can_deserialize_numbers_to_strings() {
let json = serde_json::json!({
"context": {
"userId": 123123,
"sessionId": false,
"environment": {
"aKey": "aValue",
},
"appName": "name",
"currentTime": null,
"properties": {
"someValue": 123,
"otherValue": null,
"anotherValue": {
"someKey": 123,
},
"boolProp": true,
}
},
});
let context: Context = serde_json::from_value(json["context"].clone()).unwrap();
assert_eq!(context.user_id.unwrap(), "123123");
assert_eq!(context.session_id.unwrap(), "false");
assert_eq!(context.app_name.unwrap(), "name");
assert_eq!(context.current_time.is_none(), true);
assert_eq!(context.environment.is_none(), true);
assert_eq!(context.remote_address.is_none(), true);
assert_eq!(
context
.properties
.clone()
.unwrap()
.get("someValue")
.unwrap(),
"123"
);
assert_eq!(
context.properties.clone().unwrap().get("boolProp").unwrap(),
"true"
);
assert!(!context
.properties
.clone()
.unwrap()
.contains_key("otherValue"));
assert!(!context
.properties
.clone()
.unwrap()
.contains_key("anotherValue"));
}
#[test]
pub fn ordering_is_stable_for_constraints() {
let c1 = Constraint {
context_name: "acontext".into(),
operator: super::Operator::DateAfter,
case_insensitive: true,
inverted: false,
values: Some(vec![]),
value: None,
};
let c2 = Constraint {
context_name: "acontext".into(),
operator: super::Operator::DateBefore,
case_insensitive: false,
inverted: false,
values: None,
value: Some("value".into()),
};
let c3 = Constraint {
context_name: "bcontext".into(),
operator: super::Operator::NotIn,
case_insensitive: false,
inverted: false,
values: None,
value: None,
};
let mut v = vec![c3.clone(), c1.clone(), c2.clone()];
v.sort();
assert_eq!(v, vec![c1, c2, c3]);
}
fn read_file(path: PathBuf) -> Result<BufReader<File>, EdgeError> {
File::open(path)
.map_err(|_| EdgeError::SomethingWentWrong)
.map(BufReader::new)
}
#[test_case("./examples/features_with_variantType.json".into() ; "features with variantType")]
#[test_case("./examples/15-global-constraints.json".into(); "global-constraints")]
pub fn client_features_parsing_is_stable(path: PathBuf) {
let client_features: ClientFeatures =
serde_json::from_reader(read_file(path).unwrap()).unwrap();
let to_string = serde_json::to_string(&client_features).unwrap();
let reparsed_to_string: ClientFeatures = serde_json::from_str(to_string.as_str()).unwrap();
assert_eq!(client_features, reparsed_to_string);
}
#[cfg(feature = "hashes")]
#[test_case("./examples/features_with_variantType.json".into() ; "features with variantType")]
#[cfg(feature = "hashes")]
#[test_case("./examples/15-global-constraints.json".into(); "global-constraints")]
pub fn client_features_hashing_is_stable(path: PathBuf) {
let client_features: ClientFeatures =
serde_json::from_reader(read_file(path.clone()).unwrap()).unwrap();
let second_read: ClientFeatures =
serde_json::from_reader(read_file(path).unwrap()).unwrap();
let first_hash = client_features.xx3_hash().unwrap();
let second_hash = client_features.xx3_hash().unwrap();
assert_eq!(first_hash, second_hash);
let first_hash_from_second_read = second_read.xx3_hash().unwrap();
assert_eq!(first_hash, first_hash_from_second_read);
}
#[test]
fn merging_two_client_features_takes_both_feature_sets() {
let client_features_one = ClientFeatures {
version: 2,
features: vec![
ClientFeature {
name: "feature1".into(),
..ClientFeature::default()
},
ClientFeature {
name: "feature2".into(),
..ClientFeature::default()
},
],
segments: None,
query: None,
meta: None,
};
let client_features_two = ClientFeatures {
version: 2,
features: vec![ClientFeature {
name: "feature3".into(),
..ClientFeature::default()
}],
segments: None,
query: None,
meta: None,
};
let merged = client_features_one.merge(client_features_two);
assert_eq!(merged.features.len(), 3);
}
#[test]
fn upserting_client_features_prioritizes_new_data_but_keeps_uniques() {
let client_features_one = ClientFeatures {
version: 2,
features: vec![
ClientFeature {
name: "feature1".into(),
..ClientFeature::default()
},
ClientFeature {
name: "feature2".into(),
..ClientFeature::default()
},
],
segments: None,
query: None,
meta: None,
};
let mut updated_strategies = client_features_one.clone();
let updated_feature_one_with_strategy = ClientFeature {
name: "feature1".into(),
strategies: Some(vec![Strategy {
name: "default".into(),
sort_order: Some(124),
segments: None,
constraints: None,
parameters: None,
variants: None,
}]),
..ClientFeature::default()
};
let feature_the_third = ClientFeature {
name: "feature3".into(),
strategies: Some(vec![Strategy {
name: "default".into(),
sort_order: Some(124),
segments: None,
constraints: None,
parameters: None,
variants: None,
}]),
..ClientFeature::default()
};
updated_strategies.features = vec![updated_feature_one_with_strategy, feature_the_third];
let updated_features = client_features_one.upsert(updated_strategies);
let client_features = updated_features.features;
assert_eq!(client_features.len(), 3);
let updated_feature_one = client_features
.iter()
.find(|f| f.name == "feature1")
.unwrap();
assert_eq!(updated_feature_one.strategies.as_ref().unwrap().len(), 1);
assert!(client_features.iter().any(|f| f.name == "feature3"));
assert!(client_features.iter().any(|f| f.name == "feature2"));
}
#[test]
pub fn can_parse_properties_map_from_get_query_string() {
let config = Config::new(5, false);
let query_string =
"userId=123123&properties[email]=test@test.com&properties%5BcompanyId%5D=bricks&properties%5Bhello%5D=world";
let context: Context = config
.deserialize_str(query_string)
.expect("Could not parse query string");
assert_eq!(context.user_id, Some("123123".to_string()));
let prop_map = context.properties.unwrap();
assert_eq!(prop_map.len(), 3);
assert!(prop_map.contains_key("companyId"));
assert!(prop_map.contains_key("hello"));
assert!(prop_map.contains_key("email"));
}
#[test_case("./examples/delta_base.json".into(), "./examples/delta_patch.json".into(); "Base and delta")]
pub fn can_take_delta_updates(base: PathBuf, delta: PathBuf) {
let base_delta: ClientFeaturesDelta = from_reader(read_file(base).unwrap()).unwrap();
let mut features = ClientFeatures {
version: 2,
features: vec![],
segments: None,
query: None,
meta: None,
};
features.apply_delta(&base_delta);
assert_eq!(features.features.len(), 3);
let delta: ClientFeaturesDelta = from_reader(read_file(delta).unwrap()).unwrap();
features.apply_delta(&delta);
assert_eq!(features.features.len(), 2);
}
#[test_case("./examples/delta_base.json".into(), "./examples/delta_patch.json".into(); "Base and delta")]
pub fn validate_delta_updates(base_path: PathBuf, delta_path: PathBuf) {
let base_delta: ClientFeaturesDelta = from_reader(read_file(base_path).unwrap()).unwrap();
let mut updated_features = ClientFeatures::create_from_delta(&base_delta);
let expected_feature_count = base_delta
.events
.iter()
.filter(|event| matches!(event, DeltaEvent::FeatureUpdated { .. }))
.count();
assert_eq!(updated_features.features.len(), expected_feature_count);
let delta_update: ClientFeaturesDelta =
from_reader(read_file(delta_path).unwrap()).unwrap();
updated_features.apply_delta(&delta_update);
let mut sorted_delta_features: Vec<ClientFeature> = delta_update
.events
.iter()
.filter_map(|event| {
if let DeltaEvent::FeatureUpdated { feature, .. } = event {
Some(feature.clone())
} else {
None
}
})
.collect();
sorted_delta_features.sort();
let serialized_delta_updates = to_string(&sorted_delta_features).unwrap();
let serialized_final_features = to_string(&updated_features.features).unwrap();
assert_eq!(serialized_delta_updates, serialized_final_features);
}
#[test]
pub fn when_strategy_variants_is_none_default_to_empty_vec() {
let client_features = ClientFeatures {
version: 2,
features: vec![ClientFeature {
name: "feature1".into(),
strategies: Some(vec![Strategy {
name: "default".into(),
sort_order: Some(124),
segments: None,
constraints: None,
parameters: None,
variants: None,
}]),
..ClientFeature::default()
}],
segments: None,
query: None,
meta: None,
};
let client_features_json = serde_json::to_string(&client_features).unwrap();
let client_features_parsed: ClientFeatures =
serde_json::from_str(&client_features_json).unwrap();
let strategy = client_features_parsed
.features
.first()
.unwrap()
.strategies
.as_ref()
.unwrap()
.first()
.unwrap();
assert_eq!(strategy.variants.as_ref().unwrap().len(), 0);