11import { engineParser } from './Engine' ;
22import { thenable } from '../utils/promise/thenable' ;
3- import { EXCEPTION , SPLIT_NOT_FOUND } from '../utils/labels' ;
3+ import { EXCEPTION , NO_CONDITION_MATCH , SPLIT_NOT_FOUND } from '../utils/labels' ;
44import { CONTROL } from '../utils/constants' ;
55import { ISplit , MaybeThenable } from '../dtos/types' ;
66import { IStorageAsync , IStorageSync } from '../storages/types' ;
@@ -10,24 +10,29 @@ import { ILogger } from '../logger/types';
1010import { returnSetsUnion , setToArray } from '../utils/lang/sets' ;
1111import { WARN_FLAGSET_WITHOUT_FLAGS } from '../logger/constants' ;
1212
13- const treatmentException = {
13+ const EVALUATION_EXCEPTION = {
1414 treatment : CONTROL ,
1515 label : EXCEPTION ,
1616 config : null
1717} ;
1818
19+ let EVALUATION_NOT_FOUND = {
20+ treatment : CONTROL ,
21+ label : SPLIT_NOT_FOUND ,
22+ config : null
23+ } ;
24+
1925function treatmentsException ( splitNames : string [ ] ) {
2026 const evaluations : Record < string , IEvaluationResult > = { } ;
2127 splitNames . forEach ( splitName => {
22- evaluations [ splitName ] = treatmentException ;
28+ evaluations [ splitName ] = EVALUATION_EXCEPTION ;
2329 } ) ;
2430 return evaluations ;
2531}
2632
27- // @TODO : test cases with no key
2833export function evaluateFeature (
2934 log : ILogger ,
30- key : SplitIO . SplitKey | undefined ,
35+ key : SplitIO . SplitKey ,
3136 splitName : string ,
3237 attributes : SplitIO . Attributes | undefined ,
3338 storage : IStorageSync | IStorageAsync ,
@@ -39,7 +44,7 @@ export function evaluateFeature(
3944 parsedSplit = storage . splits . getSplit ( splitName ) ;
4045 } catch ( e ) {
4146 // Exception on sync `getSplit` storage. Not possible ATM with InMemory and InLocal storages.
42- return treatmentException ;
47+ return EVALUATION_EXCEPTION ;
4348 }
4449
4550 if ( thenable ( parsedSplit ) ) {
@@ -53,7 +58,7 @@ export function evaluateFeature(
5358 ) ) . catch (
5459 // Exception on async `getSplit` storage. For example, when the storage is redis or
5560 // pluggable and there is a connection issue and we can't retrieve the split to be evaluated
56- ( ) => treatmentException
61+ ( ) => EVALUATION_EXCEPTION
5762 ) ;
5863 }
5964
@@ -140,21 +145,17 @@ export function evaluateFeaturesByFlagSets(
140145
141146function getEvaluation (
142147 log : ILogger ,
143- key : SplitIO . SplitKey | undefined ,
148+ key : SplitIO . SplitKey ,
144149 splitJSON : ISplit | null ,
145150 attributes : SplitIO . Attributes | undefined ,
146151 storage : IStorageSync | IStorageAsync ,
147152 options ?: SplitIO . EvaluationOptions ,
148153) : MaybeThenable < IEvaluationResult > {
149- let evaluation : MaybeThenable < IEvaluationResult > = {
150- treatment : CONTROL ,
151- label : SPLIT_NOT_FOUND ,
152- config : null
153- } ;
154+
154155
155156 if ( splitJSON ) {
156157 const split = engineParser ( log , splitJSON , storage ) ;
157- evaluation = split . getTreatment ( key , attributes , evaluateFeature ) ;
158+ const evaluation = split . getTreatment ( key , attributes , evaluateFeature ) ;
158159
159160 // If the storage is async and the evaluated flag uses segments or dependencies, evaluation is thenable
160161 if ( thenable ( evaluation ) ) {
@@ -172,9 +173,11 @@ function getEvaluation(
172173 // @ts -expect-error impressionsDisabled is not exposed in the public typings yet.
173174 evaluation . impressionsDisabled = options ?. impressionsDisabled || splitJSON . impressionsDisabled ;
174175 }
176+
177+ return evaluation ;
175178 }
176179
177- return evaluation ;
180+ return EVALUATION_NOT_FOUND ;
178181}
179182
180183function getEvaluations (
@@ -208,3 +211,35 @@ function getEvaluations(
208211
209212 return thenables . length > 0 ? Promise . all ( thenables ) . then ( ( ) => result ) : result ;
210213}
214+
215+ export function evaluateDefaultTreatment (
216+ splitName : string ,
217+ storage : IStorageSync | IStorageAsync ,
218+ ) : MaybeThenable < IEvaluationResult > {
219+ let parsedSplit ;
220+
221+ try {
222+ parsedSplit = storage . splits . getSplit ( splitName ) ;
223+ } catch ( e ) {
224+ return EVALUATION_EXCEPTION ;
225+ }
226+
227+ return thenable ( parsedSplit ) ?
228+ parsedSplit . then ( getDefaultTreatment ) . catch ( ( ) => EVALUATION_EXCEPTION ) :
229+ getDefaultTreatment ( parsedSplit ) ;
230+ }
231+
232+ function getDefaultTreatment (
233+ splitJSON : ISplit | null ,
234+ ) : MaybeThenable < IEvaluationResult > {
235+ if ( splitJSON ) {
236+ return {
237+ treatment : splitJSON . defaultTreatment ,
238+ label : NO_CONDITION_MATCH , // "default rule"
239+ config : splitJSON . configurations && splitJSON . configurations [ splitJSON . defaultTreatment ] || null ,
240+ changeNumber : splitJSON . changeNumber
241+ } ;
242+ }
243+
244+ return EVALUATION_NOT_FOUND ;
245+ }
0 commit comments