Skip to content

Commit

Permalink
fix: Allow for negation of segment match clauses. (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Aug 14, 2023
1 parent e57716f commit d8e469a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ describe('when evaluating user equivalent contexts for segments', () => {
expect(res.detail.value).toBe(false);
});

it('can negate a segment match op', async () => {
const segment = {
key: 'test',
included: ['foo'],
version: 1,
};
const evaluator = new Evaluator(basicPlatform, new TestQueries({ segments: [segment] }));
const flag = makeFlagWithSegmentMatch(segment);
flag.rules[0].clauses![0].negate = true;
const user = { key: 'bar' };
const res = await evaluator.evaluate(flag, Context.fromLDContext(user));
expect(res.detail.value).toBe(true);
});

it.each([basicUser, basicSingleKindUser, basicMultiKindUser])(
'matches segment with user who is both included and excluded',
async (context) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/sdk-server/src/evaluation/Evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ErrorKinds from './ErrorKinds';
import EvalResult from './EvalResult';
import evalTargets from './evalTargets';
import makeBigSegmentRef from './makeBigSegmentRef';
import matchClauseWithoutSegmentOperations from './matchClause';
import matchClauseWithoutSegmentOperations, { maybeNegate } from './matchClause';
import matchSegmentTargets from './matchSegmentTargets';
import { Queries } from './Queries';
import Reasons from './Reasons';
Expand Down Expand Up @@ -296,7 +296,7 @@ export default class Evaluator {
return new MatchError(errorResult);
}

return new Match(match);
return new Match(maybeNegate(clause, match));
}
// This is after segment matching, which does not use the reference.
if (!clause.attributeReference.isValid) {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/sdk-server/src/evaluation/matchClause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Context } from '@launchdarkly/js-sdk-common';
import { Clause } from './data/Clause';
import Operators from './Operations';

function maybeNegate(clause: Clause, value: boolean): boolean {
export function maybeNegate(clause: Clause, value: boolean): boolean {
if (clause.negate) {
return !value;
}
Expand Down

0 comments on commit d8e469a

Please sign in to comment.