1
1
component singleton accessors = " true" {
2
2
3
3
property name = " client" inject = " UnleashHyperClient@unleashsdk" ;
4
+ property name = " cache" inject = " cachebox:default" ;
5
+ property name = " log" inject = " logbox:logger:{this}" ;
6
+
7
+ variables .strategies = {
8
+ " default" : " DefaultStrategy" ,
9
+ " userWithId" : " UserWithIdStrategy" ,
10
+ " flexibleRollout" : " FlexibleRolloutStrategy" ,
11
+ " remoteAddress" : " RemoteAddressStrategy" ,
12
+ " applicationHostname" : " ApplicationHostnameStrategy"
13
+ };
4
14
5
15
public boolean function isEnabled ( required string name , boolean defaultValue = false ) {
6
16
var feature = findFeature ( arguments .name );
@@ -12,9 +22,39 @@ component singleton accessors="true" {
12
22
return false ;
13
23
}
14
24
25
+ for ( var strategyData in feature .strategies ) {
26
+ var strategy = getStrategy ( strategyData .name );
27
+ if ( isNull ( strategy ) ) {
28
+ return false ;
29
+ }
30
+
31
+ param strategyData .constraints = [];
32
+ if ( ! strategy .satisfiesConstraints ( strategyData .constraints ) ) {
33
+ continue ;
34
+ }
35
+
36
+ param strategyData .parameters = {};
37
+ if ( ! strategy .isEnabled ( strategyData .parameters ) ) {
38
+ return false ;
39
+ }
40
+ }
41
+
15
42
return true ;
16
43
}
17
44
45
+ private any function getStrategy ( required string name ) {
46
+ if ( ! variables .strategies .keyExists ( arguments .name ) ) {
47
+ log .warn ( " No Unleash strategy found for [#arguments .name #]" );
48
+ return javacast ( " null" , " " );
49
+ }
50
+
51
+ if ( isSimpleValue ( variables .strategies [ arguments .name ] ) ) {
52
+ variables .strategies [ arguments .name ] = new " unleashsdk.models.strategies.#variables .strategies [ arguments .name ] #" ();
53
+ }
54
+
55
+ return variables .strategies [ arguments .name ];
56
+ }
57
+
18
58
public struct function createFeature (
19
59
required string name ,
20
60
required string description ,
@@ -54,6 +94,12 @@ component singleton accessors="true" {
54
94
} );
55
95
}
56
96
97
+ public array function getFeatures () {
98
+ return cache .getOrSet ( " unleashsdk-features" , function () {
99
+ return fetchFeatures ();
100
+ }, createTimespan ( 0 , 0 , 10 , 0 ) );
101
+ }
102
+
57
103
private array function fetchFeatures () {
58
104
return variables .client .get ( " /client/features" ).json ().features ;
59
105
}
0 commit comments