From 082d4ebe899b6efbf64e47aaeddaf5a91f11d33d Mon Sep 17 00:00:00 2001 From: 4eb0da <4eb0da@yandex-team.com> Date: Thu, 16 May 2024 13:52:50 +0300 Subject: [PATCH] toString for arrays and dicts 201c4203e883ff3f85e3d0d5cefdc023896546a1 --- client/web/divkit/src/expressions/funcs/std.ts | 17 +++++++++++++++-- .../function_signatures_to_string.json | 6 ++++-- .../functions_to_string.json | 6 ++++-- test_data/expression_test_data/methods.json | 6 ++++-- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/client/web/divkit/src/expressions/funcs/std.ts b/client/web/divkit/src/expressions/funcs/std.ts index 17a134b26..bc9977a94 100644 --- a/client/web/divkit/src/expressions/funcs/std.ts +++ b/client/web/divkit/src/expressions/funcs/std.ts @@ -1,6 +1,8 @@ import type { + ArrayValue, BooleanValue, ColorValue, + DictValue, EvalContext, EvalValue, IntegerValue, @@ -10,14 +12,21 @@ import type { } from '../eval'; import type { VariableType, VariableValue } from '../variable'; import { registerFunc, registerMethod } from './funcs'; -import { BOOLEAN, COLOR, INTEGER, NUMBER, STRING, URL } from '../const'; +import { ARRAY, BOOLEAN, COLOR, DICT, INTEGER, NUMBER, STRING, URL } from '../const'; import { transformColorValue, valToString } from '../utils'; import { MAX_INT, MIN_INT, toBigInt } from '../bigint'; function toString( _ctx: EvalContext, - arg: IntegerValue | NumberValue | BooleanValue | ColorValue | UrlValue | StringValue + arg: IntegerValue | NumberValue | BooleanValue | ColorValue | UrlValue | StringValue | ArrayValue | DictValue ): EvalValue { + if (arg.type === ARRAY || arg.type === DICT) { + return { + type: STRING, + value: JSON.stringify(arg.value) + }; + } + return { type: STRING, value: valToString(arg) @@ -191,6 +200,8 @@ export function registerStd(): void { registerFunc('toString', [COLOR], toString); registerFunc('toString', [URL], toString); registerFunc('toString', [STRING], toString); + registerFunc('toString', [ARRAY], toString); + registerFunc('toString', [DICT], toString); registerFunc('toNumber', [INTEGER], toNumber); registerFunc('toNumber', [STRING], toNumber); @@ -224,4 +235,6 @@ export function registerStd(): void { registerMethod('toString', [COLOR], toString); registerMethod('toString', [URL], toString); registerMethod('toString', [STRING], toString); + registerMethod('toString', [ARRAY], toString); + registerMethod('toString', [DICT], toString); } diff --git a/test_data/expression_test_data/function_signatures_to_string.json b/test_data/expression_test_data/function_signatures_to_string.json index 3703f0856..f76c75e70 100644 --- a/test_data/expression_test_data/function_signatures_to_string.json +++ b/test_data/expression_test_data/function_signatures_to_string.json @@ -12,7 +12,8 @@ ], "result_type": "string", "platforms": [ - "ios" + "ios", + "web" ] }, { @@ -61,7 +62,8 @@ ], "result_type": "string", "platforms": [ - "ios" + "ios", + "web" ] }, { diff --git a/test_data/expression_test_data/functions_to_string.json b/test_data/expression_test_data/functions_to_string.json index a637d1368..43cf39f44 100644 --- a/test_data/expression_test_data/functions_to_string.json +++ b/test_data/expression_test_data/functions_to_string.json @@ -238,7 +238,8 @@ } ], "platforms": [ - "ios" + "ios", + "web" ] }, { @@ -263,7 +264,8 @@ } ], "platforms": [ - "ios" + "ios", + "web" ] }, { diff --git a/test_data/expression_test_data/methods.json b/test_data/expression_test_data/methods.json index 853bdc24e..90bebee23 100644 --- a/test_data/expression_test_data/methods.json +++ b/test_data/expression_test_data/methods.json @@ -113,7 +113,8 @@ } ], "platforms": [ - "ios" + "ios", + "web" ] }, { @@ -133,7 +134,8 @@ } ], "platforms": [ - "ios" + "ios", + "web" ] }, {