Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/1145/is set is not set segment operators #75

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion flagsmith-engine/segments/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export const NOT_CONTAINS = 'NOT_CONTAINS';
export const NOT_EQUAL = 'NOT_EQUAL';
export const REGEX = 'REGEX';
export const PERCENTAGE_SPLIT = 'PERCENTAGE_SPLIT';
export const IS_SET = 'IS_SET';
export const IS_NOT_SET = 'IS_NOT_SET';

export const CONDITION_OPERATORS = {
EQUAL,
Expand All @@ -27,5 +29,7 @@ export const CONDITION_OPERATORS = {
NOT_CONTAINS,
NOT_EQUAL,
REGEX,
PERCENTAGE_SPLIT
PERCENTAGE_SPLIT,
IS_SET,
IS_NOT_SET
};
32 changes: 24 additions & 8 deletions flagsmith-engine/segments/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { EnvironmentModel } from '../environments/models';
import { IdentityModel } from '../identities/models';
import { TraitModel } from '../identities/traits/models';
import { getHashedPercentateForObjIds } from '../utils/hashing';
import { PERCENTAGE_SPLIT } from './constants';
import { PERCENTAGE_SPLIT, IS_SET, IS_NOT_SET } from './constants';
import { SegmentConditionModel, SegmentModel, SegmentRuleModel } from './models';
import constants_1 from "../../build/flagsmith-engine/segments/constants";

export function getIdentitySegments(
environment: EnvironmentModel,
Expand Down Expand Up @@ -55,18 +56,33 @@ function traitsMatchSegmentRule(
);
}

function traitsMatchSegmentCondition(
export function traitsMatchSegmentCondition(
identityTraits: TraitModel[],
condition: SegmentConditionModel,
segmentId: number | string,
identityId: number | string
): boolean {
if (condition.operator == PERCENTAGE_SPLIT) {
return getHashedPercentateForObjIds([segmentId, identityId]) <= parseFloat(condition.value);
return getHashedPercentateForObjIds([segmentId, identityId]) <= parseFloat(<string>condition.value);
EdsnLoor marked this conversation as resolved.
Show resolved Hide resolved
} else if (condition.operator === IS_SET || condition.operator === IS_NOT_SET) {
return handleTraitExistenceConditions (condition, identityTraits);
} else {
const traits = identityTraits.filter(t => t.traitKey === condition.property_);const trait = traits.length > 0 ? traits[0] : undefined;
return trait ? condition.matchesTraitValue(trait.traitValue) : false;
}

const traits = identityTraits.filter(t => t.traitKey === condition.property_);
const trait = traits.length > 0 ? traits[0] : undefined;

EdsnLoor marked this conversation as resolved.
Show resolved Hide resolved
return trait ? condition.matchesTraitValue(trait.traitValue) : false;
}
function handleTraitExistenceConditions (condition: SegmentConditionModel, identityTraits: TraitModel[] ) {
let traitKeysArray: string[]=[]
identityTraits.map(function (e){
let objectKey = Object.keys (e);
traitKeysArray.push(objectKey[0])
})
if (condition.operator === IS_SET && condition.property_ != undefined) {
return traitKeysArray.includes(condition.property_)
}
else
{
// @ts-ignore
return !(traitKeysArray.includes(condition.property_))
}
}
30 changes: 19 additions & 11 deletions flagsmith-engine/segments/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,26 @@ export const matchingFunctions = {
[CONDITION_OPERATORS.NOT_EQUAL]: (thisValue: any, otherValue: any) => thisValue != otherValue,
[CONDITION_OPERATORS.CONTAINS]: (thisValue: any, otherValue: any) =>
otherValue.includes(thisValue),
[CONDITION_OPERATORS.IS_SET]: (thisValue: any, otherValue: any) => thisValue == otherValue,
[CONDITION_OPERATORS.IS_NOT_SET]: (thisValue: any, otherValue: any) => thisValue != otherValue,
};

export const semverMatchingFunction = {
...matchingFunctions,
[CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) => semver.eq(thisValue, otherValue),
[CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) => semver.gt(otherValue, thisValue),
[CONDITION_OPERATORS.EQUAL]: (thisValue: any, otherValue: any) =>
EdsnLoor marked this conversation as resolved.
Show resolved Hide resolved
semver.eq(thisValue, otherValue),
[CONDITION_OPERATORS.GREATER_THAN]: (thisValue: any, otherValue: any) =>
semver.gt(otherValue, thisValue),
[CONDITION_OPERATORS.GREATER_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
semver.gte(otherValue, thisValue),
[CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) => semver.gt(thisValue, otherValue),
[CONDITION_OPERATORS.LESS_THAN]: (thisValue: any, otherValue: any) =>
semver.gt(thisValue, otherValue),
[CONDITION_OPERATORS.LESS_THAN_INCLUSIVE]: (thisValue: any, otherValue: any) =>
semver.gte(thisValue, otherValue),
}
semver.gte(thisValue, otherValue)
};

export const getMatchingFunctions = (semver: boolean) => (semver ? semverMatchingFunction : matchingFunctions);
export const getMatchingFunctions = (semver: boolean) =>
semver ? semverMatchingFunction : matchingFunctions;

export class SegmentConditionModel {
EXCEPTION_OPERATOR_METHODS: { [key: string]: string } = {
Expand All @@ -48,10 +54,10 @@ export class SegmentConditionModel {
};

operator: string;
value: string;
property_: string | undefined;
value: string | null | undefined;
property_: string | null | undefined;

constructor(operator: string, value: string, property?: string) {
constructor(operator: string, value?: string | null | undefined, property?: string | null | undefined) {
this.operator = operator;
this.value = value;
this.property_ = property;
Expand All @@ -60,10 +66,12 @@ export class SegmentConditionModel {
matchesTraitValue(traitValue: any) {
const evaluators: { [key: string]: CallableFunction } = {
evaluateNotContains: (traitValue: any) => {
return !traitValue.includes(this.value);
return !traitValue.includes(this.value)
EdsnLoor marked this conversation as resolved.
Show resolved Hide resolved
},
evaluateRegex: (traitValue: any) => {
return !!traitValue.match(new RegExp(this.value));
if (this.value != null || this.value != undefined){
EdsnLoor marked this conversation as resolved.
Show resolved Hide resolved
return !!traitValue.match(new RegExp(this.value))
}
}
};

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sdk/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ export class Flags {
isFeatureEnabled(featureName: string): boolean {
return this.getFlag(featureName).enabled;
}
}
}
24 changes: 24 additions & 0 deletions tests/engine/unit/segments/segment_operator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
CONDITION_OPERATORS,
} from '../../../../flagsmith-engine/segments/constants';
import {
SegmentConditionModel,
} from '../../../../flagsmith-engine/segments/models';
import {traitsMatchSegmentCondition} from "../../../../flagsmith-engine/segments/evaluators";

const conditionMatchCases: [string, string | null | undefined, string | null | undefined, Array<any> ,boolean][] = [
[CONDITION_OPERATORS.IS_SET,'foo',null, [{}], false],
[CONDITION_OPERATORS.IS_SET, 'foo',undefined , [{foo:'bar'}], true],
[CONDITION_OPERATORS.IS_NOT_SET, 'foo', null,[{johnny: 'bravo'}, {'foo':'bar'}], false],
[CONDITION_OPERATORS.IS_NOT_SET, 'foo', undefined, [{}], true],
[CONDITION_OPERATORS.IS_SET, 'foo', null, [{johnny: 'bravo'}, {foo: 'bar'}], true]
];

test('test_segment_condition_matches_Trait', () => {
EdsnLoor marked this conversation as resolved.
Show resolved Hide resolved
for (const conditionTraits of conditionMatchCases) {
let segmentModel = new SegmentConditionModel(conditionTraits[0], conditionTraits[2], conditionTraits[1])
expect(
traitsMatchSegmentCondition (conditionTraits[3], segmentModel, 'any','any')
).toBe(conditionTraits[4]);
}
});