@@ -55,37 +55,37 @@ def _generate_allocation_id(feature_flag_value: Dict[str, JSON]) -> Optional[str
55
55
56
56
allocation : Optional [JSON ] = feature_flag_value .get ("allocation" )
57
57
58
- if allocation :
59
- # Seed
60
- allocation_id = f"seed={ allocation .get ('seed' , '' )} "
58
+ if not allocation :
59
+ return None
61
60
62
- # DefaultWhenEnabled
63
- if "default_when_enabled" in allocation :
64
- allocated_variants .append (allocation .get ("default_when_enabled" ))
61
+ # Seed
62
+ allocation_id = f"seed={ allocation .get ('seed' , '' )} "
65
63
66
- allocation_id += f"\n default_when_enabled={ allocation .get ('default_when_enabled' , '' )} "
64
+ # DefaultWhenEnabled
65
+ if "default_when_enabled" in allocation :
66
+ allocated_variants .append (allocation .get ("default_when_enabled" ))
67
67
68
- # Percentile
69
- allocation_id += "\n percentiles="
68
+ allocation_id += f"\n default_when_enabled={ allocation .get ('default_when_enabled' , '' )} "
70
69
71
- percentile = allocation .get ("percentile" )
70
+ # Percentile
71
+ allocation_id += "\n percentiles="
72
72
73
- if percentile :
74
- percentile_allocations = sorted (
75
- (x for x in percentile if x .get ("from" ) != x .get ("to" )),
76
- key = lambda x : x .get ("from" ),
77
- )
73
+ percentile = allocation .get ("percentile" )
78
74
79
- for percentile_allocation in percentile_allocations :
80
- if "variant" in percentile_allocation :
81
- allocated_variants .append (percentile_allocation .get ("variant" ))
75
+ if percentile :
76
+ percentile_allocations = sorted (
77
+ (x for x in percentile if x .get ("from" ) != x .get ("to" )),
78
+ key = lambda x : x .get ("from" ),
79
+ )
82
80
83
- allocation_id += ";" .join (
84
- f"{ pa .get ('from' )} ," f"{ base64 .b64encode (pa .get ('variant' ).encode ()).decode ()} ," f"{ pa .get ('to' )} "
85
- for pa in percentile_allocations
86
- )
87
- else :
88
- allocation_id = "seed=\n default_when_enabled=\n percentiles="
81
+ for percentile_allocation in percentile_allocations :
82
+ if "variant" in percentile_allocation :
83
+ allocated_variants .append (percentile_allocation .get ("variant" ))
84
+
85
+ allocation_id += ";" .join (
86
+ f"{ pa .get ('from' )} ," f"{ base64 .b64encode (pa .get ('variant' ).encode ()).decode ()} ," f"{ pa .get ('to' )} "
87
+ for pa in percentile_allocations
88
+ )
89
89
90
90
if not allocated_variants and (not allocation or not allocation .get ("seed" )):
91
91
return None
@@ -95,18 +95,22 @@ def _generate_allocation_id(feature_flag_value: Dict[str, JSON]) -> Optional[str
95
95
96
96
variants_value = feature_flag_value .get ("variants" )
97
97
if variants_value and (isinstance (variants_value , list ) or all (isinstance (v , dict ) for v in variants_value )):
98
- if allocated_variants :
99
- if isinstance (variants_value , list ) and all (isinstance (v , dict ) for v in variants_value ):
100
- sorted_variants : List [Dict [str , Any ]] = sorted (
101
- (v for v in variants_value if v .get ("name" ) in allocated_variants ),
102
- key = lambda v : v .get ("name" ),
103
- )
104
-
105
- for v in sorted_variants :
106
- allocation_id += f"{ base64 .b64encode (v .get ('name' , '' ).encode ()).decode ()} ,"
107
- if "configuration_value" in v :
108
- allocation_id += f"{ json .dumps (v .get ('configuration_value' , '' ), separators = (',' , ':' ))} "
109
- allocation_id += ";"
98
+ if (
99
+ allocated_variants
100
+ and isinstance (variants_value , list )
101
+ and all (isinstance (v , dict ) for v in variants_value )
102
+ ):
103
+ sorted_variants : List [Dict [str , Any ]] = sorted (
104
+ (v for v in variants_value if v .get ("name" ) in allocated_variants ),
105
+ key = lambda v : v .get ("name" ),
106
+ )
107
+
108
+ for v in sorted_variants :
109
+ allocation_id += f"{ base64 .b64encode (v .get ('name' , '' ).encode ()).decode ()} ,"
110
+ if "configuration_value" in v :
111
+ allocation_id += f"{ json .dumps (v .get ('configuration_value' , '' ), separators = (',' , ':' ))} "
112
+ allocation_id += ";"
113
+ if sorted_variants :
110
114
allocation_id = allocation_id [:- 1 ]
111
115
112
116
# Create a sha256 hash of the allocation_id
0 commit comments