* @brief Transform dry-run: compile + apply transform() to a list of values. */
| 6059 | * @brief Transform dry-run: compile + apply transform() to a list of values. |
| 6060 | */ |
| 6061 | API::CommandResponse API::Handlers::ProjectHandler::transformDryRun(const QString& id, |
| 6062 | const QJsonObject& params) |
| 6063 | { |
| 6064 | if (!params.contains(QStringLiteral("code"))) |
| 6065 | return CommandResponse::makeError( |
| 6066 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: code")); |
| 6067 | |
| 6068 | if (!params.contains(QStringLiteral("language"))) |
| 6069 | return CommandResponse::makeError( |
| 6070 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: language")); |
| 6071 | |
| 6072 | if (!params.contains(QStringLiteral("values"))) |
| 6073 | return CommandResponse::makeError( |
| 6074 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: values")); |
| 6075 | |
| 6076 | const auto code = params.value(QStringLiteral("code")).toString(); |
| 6077 | const auto language = params.value(QStringLiteral("language")).toInt(); |
| 6078 | const auto values = params.value(QStringLiteral("values")).toArray(); |
| 6079 | |
| 6080 | QString wrapped; |
| 6081 | if (language == 1) { |
| 6082 | wrapped = code |
| 6083 | + QStringLiteral("\n\nfunction parse(frame)\n" |
| 6084 | " local v = tonumber(frame)\n" |
| 6085 | " if v == nil then v = frame end\n" |
| 6086 | " local out = transform(v)\n" |
| 6087 | " return { tostring(out) }\n" |
| 6088 | "end\n"); |
| 6089 | } else { |
| 6090 | wrapped = code |
| 6091 | + QStringLiteral("\n\nfunction parse(frame) {\n" |
| 6092 | " var v = parseFloat(frame);\n" |
| 6093 | " if (isNaN(v)) v = frame;\n" |
| 6094 | " return [String(transform(v))];\n" |
| 6095 | "}\n"); |
| 6096 | } |
| 6097 | |
| 6098 | auto engine = makeScriptEngine(language); |
| 6099 | if (!engine->loadScript(wrapped, 0, false)) |
| 6100 | return CommandResponse::makeError( |
| 6101 | id, |
| 6102 | ErrorCode::ExecutionError, |
| 6103 | QStringLiteral("Transform failed to compile or define transform(value)")); |
| 6104 | |
| 6105 | QJsonArray outputs; |
| 6106 | for (const auto& v : values) { |
| 6107 | QString sample; |
| 6108 | if (v.isDouble()) |
| 6109 | sample = QString::number(SerialStudio::toDouble(v), 'g', 17); |
| 6110 | else |
| 6111 | sample = v.toString(); |
| 6112 | |
| 6113 | const auto rows = engine->parseString(sample); |
| 6114 | if (rows.isEmpty() || rows.first().isEmpty()) { |
| 6115 | outputs.append(QJsonValue::Null); |
| 6116 | continue; |
| 6117 | } |
| 6118 |
nothing calls this directly
no test coverage detected