Skip to content

Commit 2a0fdfc

Browse files
committed
Allow providing additional context inline to isEnabled
1 parent eb87858 commit 2a0fdfc

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

ModuleConfig.cfc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ component {
1111
"contextProvider": "DefaultContextProvider@unleashsdk",
1212
"apiURL": getSystemSetting( "UNLEASH_API_URL" ),
1313
"apiToken": getSystemSetting( "UNLEASH_API_TOKEN" ),
14-
"cacheTimeout": createTimeSpan( 0, 0, 0, 1 )
14+
"cacheTimeout": createTimeSpan( 0, 0, 0, 10 )
1515
};
1616
}
1717

@@ -24,15 +24,15 @@ component {
2424
bodyFormat = "json",
2525
headers = {
2626
"Authorization": settings.apiToken,
27-
"Content-Type": "application/json"
27+
"Content-Type": "application/json"
2828
}
2929
);
3030

3131
binder.map( "UnleashSDK@unleashsdk" )
32-
.to( "#moduleMapping#.models.UnleashSDK" )
32+
.to( "#moduleMapping#.models.UnleashSDK" );
3333

3434
binder.map( "@unleashsdk" )
35-
.toDSL( "UnleashSDK@unleashsdk" )
35+
.toDSL( "UnleashSDK@unleashsdk" );
3636
}
3737

3838
}

models/UnleashSDK.cfc

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ component singleton accessors="true" {
1414
"applicationHostname" : "ApplicationHostnameStrategy@unleashsdk"
1515
};
1616

17-
public boolean function isEnabled( required string name, boolean defaultValue = false ) {
17+
public boolean function isEnabled(
18+
required string name,
19+
struct additionalContext = {},
20+
boolean defaultValue = false
21+
) {
1822
var feature = getFeature( arguments.name );
1923
if ( isNull( feature ) ) {
2024
return arguments.defaultValue;
@@ -24,27 +28,37 @@ component singleton accessors="true" {
2428
return false;
2529
}
2630

31+
var context = getContext( arguments.additionalContext );
32+
33+
if ( feature.strategies.isEmpty() ) {
34+
return true;
35+
}
36+
2737
for ( var strategyData in feature.strategies ) {
2838
var strategy = getStrategy( strategyData.name );
2939
if ( isNull( strategy ) ) {
3040
return false;
3141
}
3242

3343
param strategyData.constraints = [];
34-
if ( !satisfiesConstraints( strategyData.constraints ) ) {
44+
if ( !satisfiesConstraints( strategyData.constraints, context ) ) {
3545
continue;
3646
}
3747

3848
param strategyData.parameters = {};
39-
if ( !strategy.isEnabled( strategyData.parameters, getContext() ) ) {
40-
return false;
49+
if ( strategy.isEnabled( strategyData.parameters, context ) ) {
50+
return true;
4151
}
4252
}
4353

44-
return true;
54+
return false;
4555
}
4656

47-
public boolean function isDisabled( required string name, boolean defaultValue = false ) {
57+
public boolean function isDisabled(
58+
required string name,
59+
struct additionalContext = {},
60+
boolean defaultValue = false
61+
) {
4862
return !isEnabled( argumentCollection = arguments );
4963
}
5064

@@ -130,25 +144,25 @@ component singleton accessors="true" {
130144
return variables.client.get( "/client/features" ).json().features;
131145
}
132146

133-
private boolean function satisfiesConstraints( required array constraints ) {
147+
private boolean function satisfiesConstraints( required array constraints, required struct context ) {
134148
for ( var constraint in arguments.constraints ) {
135-
if ( !satisfiesConstraint( constraint ) ) {
149+
if ( !satisfiesConstraint( constraint, arguments.context ) ) {
136150
return false;
137151
}
138152
}
139153
return true;
140154
}
141155

142-
private boolean function satisfiesConstraint( required struct constraint ) {
143-
var context = getContext();
156+
private boolean function satisfiesConstraint( required struct constraint, required struct context ) {
144157
var contextValue = context[ arguments.constraint.contextName ];
145158
var valuePresent = arrayContainsNoCase( arguments.constraint.values, contextValue );
146159
return arguments.constraint.operator == "IN" ? valuePresent : !valuePresent;
147160
}
148161

149-
private struct function getContext() {
162+
private struct function getContext( struct additionalContext = {} ) {
150163
param request.unleashContext = generateContext();
151-
return request.unleashContext;
164+
structAppend( arguments.additionalContext, request.unleashContext, false );
165+
return arguments.additionalContext;
152166
}
153167

154168
private struct function generateContext() {

0 commit comments

Comments
 (0)