From c722e8368c9aae31b91e4e57d3ccead34c26bddf Mon Sep 17 00:00:00 2001 From: 4eb0da <4eb0da@yandex-team.com> Date: Tue, 1 Jul 2025 19:53:33 +0300 Subject: [PATCH] Fix boolean expressions commit_hash:e44dac4a5891c15da87b0d44fad541d370f32e3b --- client/web/divkit/src/expressions/utils.ts | 5 ++- client/web/divkit/src/expressions/variable.ts | 12 +++++- .../operations_comparison_boolean.json | 42 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/client/web/divkit/src/expressions/utils.ts b/client/web/divkit/src/expressions/utils.ts index b5d2418a3..9283149fe 100644 --- a/client/web/divkit/src/expressions/utils.ts +++ b/client/web/divkit/src/expressions/utils.ts @@ -5,7 +5,7 @@ import { walk } from './walk'; import { parseColor, type ParsedColor } from '../utils/correctColor'; import { padLeft } from '../utils/padLeft'; import { MAX_INT, MIN_INT, toBigInt } from './bigint'; -import { NUMBER } from './const'; +import { BOOLEAN, NUMBER } from './const'; export function valToInternal(val: EvalValue): EvalValue { if (val.type === 'url' || val.type === 'color') { @@ -213,6 +213,9 @@ export function convertJsValueToDivKit(ctx: EvalContext, val: unknown, evalType: if (jsType === 'string' && evalType === 'url') { checkUrl(val); } + if (jsType === 'boolean' && evalType === BOOLEAN) { + val = val ? 1 : 0; + } return { type: evalType, diff --git a/client/web/divkit/src/expressions/variable.ts b/client/web/divkit/src/expressions/variable.ts index 3550766cb..efe6ebbc7 100644 --- a/client/web/divkit/src/expressions/variable.ts +++ b/client/web/divkit/src/expressions/variable.ts @@ -4,6 +4,7 @@ import type { EvalValue } from './eval'; import { parseColor } from '../utils/correctColor'; import { bigIntZero, toBigInt } from './bigint'; import { checkUrl } from './utils'; +import { BOOLEAN } from './const'; export type VariableType = 'string' | 'number' | 'integer' | 'boolean' | 'color' | 'url' | 'dict' | 'array'; export type VariableValue = string | number | bigint | boolean | null | undefined | object | unknown[]; @@ -362,8 +363,15 @@ export function defaultValueByType(type: keyof typeof TYPE_TO_CLASS): VariableVa } export function variableToValue(variable: Variable): EvalValue { + const type = variable.getType(); + let value = variable.getValue(); + + if (type === BOOLEAN) { + value = value ? 1 : 0; + } + return { - type: variable.getType(), - value: variable.getValue() + type, + value }; } diff --git a/test_data/expression_test_data/operations_comparison_boolean.json b/test_data/expression_test_data/operations_comparison_boolean.json index afe3af2f2..4203c2cf7 100644 --- a/test_data/expression_test_data/operations_comparison_boolean.json +++ b/test_data/expression_test_data/operations_comparison_boolean.json @@ -103,6 +103,48 @@ "web", "flutter" ] + }, + { + "expression": "@{d.getBoolean('b') == false}", + "expected": { + "type": "boolean", + "value": true + }, + "variables": [ + { + "type": "dict", + "value": { + "b": false + }, + "name": "d" + } + ], + "platforms": [ + "android", + "ios", + "web", + "flutter" + ] + }, + { + "expression": "@{(1 >= 2) == a}", + "expected": { + "type": "boolean", + "value": true + }, + "variables": [ + { + "type": "boolean", + "value": false, + "name": "a" + } + ], + "platforms": [ + "android", + "ios", + "web", + "flutter" + ] } ] }