Skip to content

Commit

Permalink
[STRATCONN-3772] Add new operator "number_equals" for number (#2030)
Browse files Browse the repository at this point in the history
* Add == operator in destination subscription package

* Update/add new unit tests for the operator

* limit == to just numeric values

* update test cases STRATCONN-3772

* Add operator == in the AST level

* Modified = to only store strings

* Modified = to only store strings: update test cases

* add == operator for traits AND CONTEXT

* Add stringify on fqlExpression

* Change operator name to number_equals

* Change operator name to number_equals

* Change operator name to number_equals

* Refactor branching statements on parse-fq.ts
  • Loading branch information
itsarijitray authored Jun 4, 2024
1 parent 524fc6c commit 5887a12
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,46 @@ test('should handle valid ast', () => {
expect(generateFql(ast)).toEqual('properties.value = "x"')
})

test('should number_equals ast', () => {
const ast: Subscription = {
type: 'group',
operator: 'and',
children: [
{
type: 'event-property',
name: 'value',
operator: 'number_equals',
value: '123'
}
]
}

expect(generateFql(ast)).toEqual('properties.value = 123')
})

test('should string equal (=) ast', () => {
const ast: Subscription = {
type: 'group',
operator: 'and',
children: [
{
type: 'event-property',
name: 'value',
operator: 'number_equals',
value: '123'
},
{
type: 'event-property',
name: 'label',
operator: '=',
value: '456'
}
]
}

expect(generateFql(ast)).toEqual('properties.value = 123 and properties.label = "456"')
})

test('should handle ast with multiple childs (or condition)', () => {
const ast: Subscription = {
type: 'group',
Expand Down Expand Up @@ -95,7 +135,7 @@ test('should handle field paths with non-regular values and escape properly', ()
{
type: 'event-trait',
name: 'property dos',
operator: '=',
operator: 'number_equals',
value: 2
}
]
Expand Down
24 changes: 22 additions & 2 deletions packages/destination-subscriptions/src/__tests__/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,34 @@ test('operators - equals (numbers)', () => {
]
}

// Since action tester UI only supports string type input, we assume the value in the ast is a string for now (i.e. 123 !== "123")
expect(validate(ast, { properties: { value: 123 } })).toEqual(false)

expect(validate(ast, { properties: { value: '123' } })).toEqual(true)
expect(validate(ast, { properties: { value: 0 } })).toEqual(false)
}
})

test('operators - number_equals (numbers)', () => {
for (const value of ['123', 123]) {
const ast = {
type: 'group',
operator: 'and',
children: [
{
type: 'event-property',
name: 'value',
operator: 'number_equals',
value
}
]
}

expect(validate(ast, { properties: { value: 123 } })).toEqual(true)

expect(validate(ast, { properties: { value: '123' } })).toEqual(false)
expect(validate(ast, { properties: { value: 0 } })).toEqual(false)
}
})

test('operators - not equals (strings)', () => {
const ast = {
type: 'group',
Expand Down
2 changes: 2 additions & 0 deletions packages/destination-subscriptions/src/generate-fql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const fqlExpression = (name: string, operator: Operator, value: string | boolean
case '<=':
case '>=':
return `${name} ${operator} ${Number(value)}`
case 'number_equals':
return `${name} = ${Number(value)}`
default:
return `${name} ${operator} ${stringifyValue(value)}`
}
Expand Down
22 changes: 22 additions & 0 deletions packages/destination-subscriptions/src/parse-fql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ const parse = (tokens: Token[]): Condition => {
const isFalse = operatorToken.value === '=' && valueToken.value === 'false'
const isExists = operatorToken.value === '!=' && valueToken.value === 'null'
const isNotExists = operatorToken.value === '=' && valueToken.value === 'null'
const isNumberEquals = operatorToken.value === '=' && valueToken.type === 'number'

if (conditionType === 'event') {
nodes.push({
Expand Down Expand Up @@ -276,6 +277,13 @@ const parse = (tokens: Token[]): Condition => {
name: token.value.replace(/^(properties)\./, ''),
operator: 'is_false'
})
} else if (isNumberEquals) {
nodes.push({
type: 'event-property',
name: token.value.replace(/^(properties)\./, ''),
operator: 'number_equals',
value: getTokenValue(valueToken)
})
} else {
nodes.push({
type: 'event-property',
Expand Down Expand Up @@ -309,6 +317,13 @@ const parse = (tokens: Token[]): Condition => {
name: token.value.replace(/^(traits)\./, ''),
operator: 'is_false'
})
} else if (isNumberEquals) {
nodes.push({
type: 'event-trait',
name: token.value.replace(/^(traits)\./, ''),
operator: 'number_equals',
value: getTokenValue(valueToken)
})
} else {
nodes.push({
type: 'event-trait',
Expand Down Expand Up @@ -342,6 +357,13 @@ const parse = (tokens: Token[]): Condition => {
name: token.value.replace(/^(context)\./, ''),
operator: 'is_false'
})
} else if (isNumberEquals) {
nodes.push({
type: 'event-context',
name: token.value.replace(/^(context)\./, ''),
operator: 'number_equals',
value: getTokenValue(valueToken)
})
} else {
nodes.push({
type: 'event-context',
Expand Down
1 change: 1 addition & 0 deletions packages/destination-subscriptions/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type Operator =
| '<='
| '>'
| '>='
| 'number_equals'
| 'contains'
| 'not_contains'
| 'starts_with'
Expand Down
2 changes: 2 additions & 0 deletions packages/destination-subscriptions/src/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ const validateValue = (actual: unknown, operator: Operator, expected?: string |
switch (operator) {
case '=':
return actual === String(expected)
case 'number_equals':
return typeof actual === 'number' && Number(actual) === Number(expected)
case '!=':
return actual !== String(expected)
case '<':
Expand Down

0 comments on commit 5887a12

Please sign in to comment.