From 1bc88fb5206e42ae288864bf3789acfec904fc50 Mon Sep 17 00:00:00 2001 From: wendel Date: Wed, 29 Apr 2026 16:07:54 +0800 Subject: [PATCH] delete argmapping Signed-off-by: wendel Co-Authored-By: Agent Change-Id: I4ce7839b9f96ee3405c53b39bd7338340fb7899f --- .../services/climgr/include/tool_util.h | 70 +- .../services/climgr/src/tool_util.cpp | 712 +----------------- services/common/include/hilog_tag_wrapper.h | 2 +- .../tool_util_test/tool_util_test.cpp | 138 ---- 4 files changed, 17 insertions(+), 905 deletions(-) diff --git a/cli_tool_framework/services/climgr/include/tool_util.h b/cli_tool_framework/services/climgr/include/tool_util.h index 8240f2c652..530a670dd1 100644 --- a/cli_tool_framework/services/climgr/include/tool_util.h +++ b/cli_tool_framework/services/climgr/include/tool_util.h @@ -52,9 +52,6 @@ public: static void TransferToCmdParam(const ToolInfo &toolInfo, const AAFwk::WantParams &args, std::string &cmdLine); - // Path utilities (public for testing) - static std::vector SplitPathBySeparator(const std::string &path, const std::string &separator); - private: static bool GetBundleInfoByTokenId(AccessToken::AccessTokenID tokenId, AppExecFwk::BundleInfo &bundleInfo); @@ -63,12 +60,8 @@ private: // Helper methods for type validation static bool ValidateParamType(const sptr &value, const std::string &expectedType, const nlohmann::json &propertySchema, const std::string &key = ""); - static bool ValidateNestedObject(const AAFwk::WantParams &nestedParams, - const nlohmann::json &objectSchema, const std::string &parentKey); static bool ValidateArrayType(const sptr &value, const nlohmann::json &propertySchema, const std::string &key); - static bool ValidateObjectType(const sptr &value, - const nlohmann::json &propertySchema, const std::string &key); static bool ValidateArrayItems(sptr arrayObj, const nlohmann::json &itemsSchema, const std::string &key); static bool ValidateBasicType(const sptr &value, const std::string &expectedType); @@ -77,77 +70,16 @@ private: static bool IsIntegerType(const sptr &value); static bool IsNumberType(const sptr &value); static bool IsArrayType(const sptr &value); - static bool IsObjectType(const sptr &value); - - // Helper methods for argument mapping - static void ApplyFlagMapping(const std::string &templates, const AAFwk::WantParams &args, std::string &cmdLine); - static void ApplyPositionalMapping(const std::string &order, const AAFwk::WantParams &args, std::string &cmdLine); - static void ApplyFlattenedMapping(const std::string &separator, const std::string &templates, - const AAFwk::WantParams &args, std::string &cmdLine); - static void ApplyJsonStringMapping(const std::string &templates, const AAFwk::WantParams &args, - std::string &cmdLine); - static std::string FormatTemplate(const std::string &tmpl, const std::string &value); // Helper methods for mode processing (extracted to reduce nesting depth) - static void ProcessArrayExpansion(const sptr &value, const std::string &tmpl, + static void ProcessArrayExpansion(const std::string &key, const sptr &value, std::string &cmdLine); - static void ProcessJsonStringTemplate(const std::string &key, const sptr &value, - const nlohmann::json &templateValue, std::string &cmdLine); - static void ProcessBooleanTemplate(const std::string &key, const sptr &value, - const nlohmann::json &templateValue, std::string &cmdLine); - static void ProcessFlattenedTemplate(const std::string &flattenedKey, const nlohmann::json &templateValue, - const std::string &separator, const AAFwk::WantParams &args, std::string &cmdLine); - - // JSON conversion helper - static std::string ConvertValueToJson(const std::string &key, const sptr &value); - - // Nested path query helper for flattened mapping - static sptr QueryNestedValue(const AAFwk::WantParams &args, - const std::string &path, const std::string &separator); - - // Helper method for nested path traversal - static sptr QueryNestedPath(const AAFwk::WantParams &args, - const std::vector &pathSegments, const std::string &separator); - static sptr QueryNextLevel(const sptr ¤tValue, - const std::string &nextSegment, const std::string &separator); - - // Helper methods for path query (extracted to reduce QueryNestedValue length) - static sptr TryDirectLookup(const AAFwk::WantParams &args, - const std::string &path); - static sptr TryNestedPathTraversal(const AAFwk::WantParams &args, - const std::string &path, const std::string &separator); - - // WantParams to JSON conversion helper for nested objects - static std::string WantParamsToJson(const AAFwk::WantParams &wantParams); - - // Core parameter processing logic (extracted for reuse) - static void ApplyFlagModeLogic(const sptr &value, - const nlohmann::json &templateValue, std::string &cmdLine); // Type conversion helpers // GetParamStringValue: only supports basic types (bool, int, long, float, double, string) - // GetParamArrayValue: supports single-level arrays with basic type elements - // GetParamJsonValue: converts to JSON format (supports single-level arrays) // Note: Nested arrays and byte/char/short types are not supported static std::string GetParamStringValue(const sptr &value); - static std::string GetParamJsonValue(const sptr &value); static bool GetParamBoolValue(const sptr &value, bool &result); - static bool GetParamArrayValue(const sptr &value, std::vector &result); - - // Low-level helper methods for code reuse - static bool ExtractWantParams(const sptr &value, AAFwk::WantParams &wantParams); - static std::string EscapeJsonString(const std::string &str); - static void IterateIArray(sptr arrayObj, - std::function&)> elementHandler); - static std::string BuildJsonArrayFromIArray(sptr arrayObj, - std::function&)> elementConverter); - - // Type-specific JSON conversion helpers (extracted to reduce GetParamJsonValue length) - static std::string ConvertWantParamsToJson(const sptr &value); - static std::string ConvertArrayToJson(const sptr &value); - static std::string ConvertStringToJson(const sptr &value); - static std::string ConvertBooleanToJson(const sptr &value); - static std::string ConvertNumericToJson(const sptr &value); }; } // namespace CliTool diff --git a/cli_tool_framework/services/climgr/src/tool_util.cpp b/cli_tool_framework/services/climgr/src/tool_util.cpp index dd1c2328fc..c54db2a7f9 100644 --- a/cli_tool_framework/services/climgr/src/tool_util.cpp +++ b/cli_tool_framework/services/climgr/src/tool_util.cpp @@ -162,6 +162,7 @@ bool ToolUtil::GenerateSandboxConfig(const std::string &challenge, AccessToken:: config["callerPid"] = IPCSkeleton::GetCallingPid(); config["gid"] = bundleInfo.gid; config["appId"] = bundleInfo.appId; + config["bundleName"] = bundleInfo.name; sandboxConfig = config.dump(); bundleName = bundleInfo.name; TAG_LOGE(AAFwkTag::CLI_TOOL, "sandboxConfig: %{public}s", sandboxConfig.c_str()); @@ -209,208 +210,31 @@ void ToolUtil::TransferToCmdParam(const ToolInfo &toolInfo, const AAFwk::WantPar TAG_LOGI(AAFwkTag::CLI_TOOL, "Not has arg"); return; } - if (toolInfo.argMapping == nullptr) { - TAG_LOGW(AAFwkTag::CLI_TOOL, "argMapping is nullptr"); - return; - } - - // Apply mapping based on type - switch (toolInfo.argMapping->type) { - case ArgMappingType::FLAG: - ApplyFlagMapping(toolInfo.argMapping->templates, args, cmdLine); - break; - case ArgMappingType::POSITIONAL: - ApplyPositionalMapping(toolInfo.argMapping->order, args, cmdLine); - break; - case ArgMappingType::FLATTENED: - ApplyFlattenedMapping(toolInfo.argMapping->separator, toolInfo.argMapping->templates, args, cmdLine); - break; - case ArgMappingType::JSONSTRING: - ApplyJsonStringMapping(toolInfo.argMapping->templates, args, cmdLine); - break; - default: - TAG_LOGW(AAFwkTag::CLI_TOOL, "Unknown argMapping type"); - break; - } -} - -void ToolUtil::ApplyFlagMapping(const std::string &templates, const AAFwk::WantParams &args, std::string &cmdLine) -{ - if (templates.empty()) { - TAG_LOGW(AAFwkTag::CLI_TOOL, "Flag templates is empty"); - return; - } - - nlohmann::json templatesJson = nlohmann::json::parse(templates, nullptr, false); - if (templatesJson.is_discarded() || !templatesJson.is_object()) { - TAG_LOGE(AAFwkTag::CLI_TOOL, "Failed to parse templates JSON"); - return; - } - for (const auto &[key, value] : args.GetParams()) { - if (!templatesJson.contains(key)) { + if (value == nullptr) { continue; } - auto &templateValue = templatesJson[key]; - // Apply FLAG mode logic for this parameter - ApplyFlagModeLogic(value, templateValue, cmdLine); - } -} - -void ToolUtil::ApplyPositionalMapping(const std::string &order, const AAFwk::WantParams &args, std::string &cmdLine) -{ - if (order.empty()) { - TAG_LOGW(AAFwkTag::CLI_TOOL, "Positional order is empty"); - return; - } - - nlohmann::json orderJson = nlohmann::json::parse(order, nullptr, false); - if (orderJson.is_discarded() || !orderJson.is_array()) { - TAG_LOGE(AAFwkTag::CLI_TOOL, "Failed to parse order JSON"); - return; - } - - for (const auto &key : orderJson) { - if (!key.is_string()) { - continue; - } - std::string keyStr = key.get(); - auto it = args.GetParams().find(keyStr); - if (it != args.GetParams().end()) { - std::string strValue = GetParamStringValue(it->second); - if (!strValue.empty()) { - cmdLine += " " + strValue; + if (IsBooleanType(value)) { + bool boolValue = false; + if (GetParamBoolValue(value, boolValue) && boolValue) { + cmdLine += " --" + key; } - } - } -} - -void ToolUtil::ApplyFlattenedMapping(const std::string &separator, const std::string &templates, - const AAFwk::WantParams &args, std::string &cmdLine) -{ - std::string sep = separator.empty() ? "." : separator; - - nlohmann::json templatesJson = nlohmann::json::parse(templates, nullptr, false); - if (templatesJson.is_discarded() || !templatesJson.is_object()) { - TAG_LOGE(AAFwkTag::CLI_TOOL, "Failed to parse templates JSON"); - return; - } - - // Process each template key (which contains the flattened path) - for (const auto &templateIt : templatesJson.items()) { - const std::string &flattenedKey = templateIt.key(); - const auto &templateValue = templateIt.value(); - - // Query nested value and process in one step to reduce depth - ProcessFlattenedTemplate(flattenedKey, templateValue, sep, args, cmdLine); - } -} - -void ToolUtil::ApplyJsonStringMapping(const std::string &templates, const AAFwk::WantParams &args, std::string &cmdLine) -{ - nlohmann::json templatesJson = nlohmann::json::parse(templates, nullptr, false); - if (templatesJson.is_discarded() || !templatesJson.is_object()) { - TAG_LOGE(AAFwkTag::CLI_TOOL, "Failed to parse templates JSON"); - return; - } - - for (const auto &[key, value] : args.GetParams()) { - if (!templatesJson.contains(key)) { continue; } - auto &templateValue = templatesJson[key]; + if (IsArrayType(value)) { + ProcessArrayExpansion(key, value, cmdLine); + continue; + } - if (templateValue.is_string()) { - ProcessJsonStringTemplate(key, value, templateValue, cmdLine); - } else if (templateValue.is_object()) { - ProcessBooleanTemplate(key, value, templateValue, cmdLine); + std::string strValue = GetParamStringValue(value); + if (!strValue.empty()) { + cmdLine += " --" + key + " " + strValue; } } } -std::string ToolUtil::FormatTemplate(const std::string &tmpl, const std::string &value) -{ - std::string result = tmpl; - size_t pos = result.find("{value}"); - constexpr int32_t valueSize = 7; - if (pos != std::string::npos) { - result.replace(pos, valueSize, value); - } - pos = result.find("{json}"); - constexpr int32_t jsonSize = 6; - if (pos != std::string::npos) { - result.replace(pos, jsonSize, value); - } - return result; -} - -sptr ToolUtil::QueryNestedValue(const AAFwk::WantParams &args, - const std::string &path, const std::string &separator) -{ - if (path.empty()) { - return nullptr; - } - - // Try direct lookup first - sptr result = TryDirectLookup(args, path); - if (result != nullptr) { - return result; - } - - // Try nested path traversal - std::string sep = separator.empty() ? "." : separator; - return TryNestedPathTraversal(args, path, sep); -} - -sptr ToolUtil::TryDirectLookup(const AAFwk::WantParams &args, - const std::string &path) -{ - auto ¶ms = args.GetParams(); - auto it = params.find(path); - if (it != params.end()) { - TAG_LOGI(AAFwkTag::CLI_TOOL, "Found param with direct lookup: %{public}s", path.c_str()); - return it->second; - } - return nullptr; -} - -sptr ToolUtil::TryNestedPathTraversal(const AAFwk::WantParams &args, - const std::string &path, const std::string &separator) -{ - std::vector pathSegments = SplitPathBySeparator(path, separator); - constexpr int32_t segmentSize = 2; - if (pathSegments.size() < segmentSize) { - return nullptr; - } - - return QueryNestedPath(args, pathSegments, separator); -} - -std::string ToolUtil::WantParamsToJson(const AAFwk::WantParams &wantParams) -{ - nlohmann::json result = nlohmann::json::object(); - - for (const auto &[key, value] : wantParams.GetParams()) { - // Check if value is another WantParams (nested object) - AAFwk::WantParams nestedWantParams; - if (ExtractWantParams(value, nestedWantParams)) { - // Recursively convert nested WantParams to JSON - result[key] = nlohmann::json::parse(WantParamsToJson(nestedWantParams), nullptr, false); - } else if (value != nullptr) { - // Convert IInterface to JSON value using GetParamJsonValue - std::string valueJsonStr = GetParamJsonValue(value); - nlohmann::json valueJson = nlohmann::json::parse(valueJsonStr, nullptr, false); - if (!valueJson.is_discarded()) { - result[key] = valueJson; - } - } - } - - return result.dump(); -} - std::string ToolUtil::GetParamStringValue(const sptr &value) { if (value == nullptr) { @@ -475,130 +299,6 @@ std::string ToolUtil::GetParamStringValue(const sptr &value) return ""; } -std::string ToolUtil::GetParamJsonValue(const sptr &value) -{ - if (value == nullptr) { - return "null"; - } - - // Try WantParams (nested object) - should check before other types - std::string jsonStr = ConvertWantParamsToJson(value); - if (!jsonStr.empty()) { - return jsonStr; - } - - // Try Array - only single-level array support (no nested arrays) - jsonStr = ConvertArrayToJson(value); - if (!jsonStr.empty()) { - return jsonStr; - } - - // Try String - JSON strings are quoted - jsonStr = ConvertStringToJson(value); - if (!jsonStr.empty()) { - return jsonStr; - } - - // Try Boolean - JSON booleans are unquoted - jsonStr = ConvertBooleanToJson(value); - if (!jsonStr.empty()) { - return jsonStr; - } - - // Try Numeric types (int, long, float, double) - jsonStr = ConvertNumericToJson(value); - if (!jsonStr.empty()) { - return jsonStr; - } - - // If no type matches, return null - return "null"; -} - -std::string ToolUtil::ConvertWantParamsToJson(const sptr &value) -{ - AAFwk::WantParams wantParams; - if (ExtractWantParams(value, wantParams)) { - return WantParamsToJson(wantParams); - } - return ""; -} - -std::string ToolUtil::ConvertArrayToJson(const sptr &value) -{ - if (auto arrayObj = AAFwk::IArray::Query(value)) { - return BuildJsonArrayFromIArray(arrayObj, [](const sptr& element) { - return GetParamJsonValue(element); - }); - } - return ""; -} - -std::string ToolUtil::ConvertStringToJson(const sptr &value) -{ - if (auto strObj = AAFwk::IString::Query(value)) { - std::string strValue; - if (strObj->GetString(strValue) != ERR_OK) { - return "\"\""; - } - return "\"" + EscapeJsonString(strValue) + "\""; - } - return ""; -} - -std::string ToolUtil::ConvertBooleanToJson(const sptr &value) -{ - if (auto boolObj = AAFwk::IBoolean::Query(value)) { - bool boolValue = false; - if (boolObj->GetValue(boolValue) == ERR_OK) { - return boolValue ? "true" : "false"; - } - return "false"; - } - return ""; -} - -std::string ToolUtil::ConvertNumericToJson(const sptr &value) -{ - // Try Integer/Int - if (auto intObj = AAFwk::IInteger::Query(value)) { - int intValue = 0; - if (intObj->GetValue(intValue) == ERR_OK) { - return std::to_string(intValue); - } - return "0"; - } - - // Try Long - if (auto longObj = AAFwk::ILong::Query(value)) { - long longValue = 0; - if (longObj->GetValue(longValue) == ERR_OK) { - return std::to_string(longValue); - } - return "0"; - } - - // Try Float - if (auto floatObj = AAFwk::IFloat::Query(value)) { - float floatValue = 0.0f; - if (floatObj->GetValue(floatValue) == ERR_OK) { - return std::to_string(floatValue); - } - return "0.0"; - } - - // Try Double - if (auto doubleObj = AAFwk::IDouble::Query(value)) { - double doubleValue = 0.0; - if (doubleObj->GetValue(doubleValue) == ERR_OK) { - return std::to_string(doubleValue); - } - return "0.0"; - } - - return ""; -} - bool ToolUtil::GetParamBoolValue(const sptr &value, bool &result) { if (value == nullptr) { @@ -616,249 +316,7 @@ bool ToolUtil::GetParamBoolValue(const sptr &value, bool &res return false; } -bool ToolUtil::GetParamArrayValue(const sptr &value, std::vector &result) -{ - if (value == nullptr) { - return false; - } - - // Try to query as IArray - auto arrayValue = AAFwk::IArray::Query(value); - if (arrayValue == nullptr) { - return false; - } - - bool foundAny = false; - IterateIArray(arrayValue, [&result, &foundAny](const sptr& element) { - std::string elementStr = GetParamStringValue(element); - if (!elementStr.empty()) { - result.push_back(elementStr); - foundAny = true; - } - }); - - return foundAny; -} - -// ============================================================================ -// Helper methods for code reuse -// ============================================================================ - -bool ToolUtil::ExtractWantParams(const sptr &value, AAFwk::WantParams &wantParams) -{ - if (value == nullptr) { - return false; - } - - auto wantParamsWrapper = AAFwk::IWantParams::Query(value); - if (wantParamsWrapper == nullptr) { - return false; - } - - return wantParamsWrapper->GetValue(wantParams) == ERR_OK; -} - -std::string ToolUtil::EscapeJsonString(const std::string &str) -{ - std::string escaped = ""; - for (char c : str) { - if (c == '"') { - escaped += "\\\""; - } else if (c == '\\') { - escaped += "\\\\"; - } else if (c == '\n') { - escaped += "\\n"; - } else if (c == '\r') { - escaped += "\\r"; - } else if (c == '\t') { - escaped += "\\t"; - } else { - escaped += c; - } - } - return escaped; -} - -void ToolUtil::IterateIArray(sptr arrayObj, - std::function&)> elementHandler) -{ - if (arrayObj == nullptr) { - return; - } - - long arrayLength = 0; - if (arrayObj->GetLength(arrayLength) != ERR_OK || arrayLength <= 0) { - return; - } - - for (long i = 0; i < arrayLength; ++i) { - sptr elementValue; - if (arrayObj->Get(i, elementValue) != ERR_OK || elementValue == nullptr) { - continue; - } - - // Skip nested arrays - if (AAFwk::IArray::Query(elementValue) != nullptr) { - continue; - } - - elementHandler(elementValue); - } -} - -std::string ToolUtil::BuildJsonArrayFromIArray(sptr arrayObj, - std::function&)> elementConverter) -{ - if (arrayObj == nullptr) { - return "[]"; - } - - long arrayLength = 0; - if (arrayObj->GetLength(arrayLength) != ERR_OK || arrayLength <= 0) { - return "[]"; - } - - std::string result = "["; - bool firstElement = true; - - IterateIArray(arrayObj, [&result, &firstElement, &elementConverter](const sptr& element) { - if (!firstElement) { - result += ","; - } - result += elementConverter(element); - firstElement = false; - }); - - result += "]"; - return result; -} - -// ============================================================================ -// Core FLAG mode logic (extracted for reuse) -// ============================================================================ - -void ToolUtil::ApplyFlagModeLogic(const sptr &value, - const nlohmann::json &templateValue, std::string &cmdLine) -{ - if (value == nullptr) { - return; - } - - // Handle string template - if (templateValue.is_string()) { - std::string strValue = GetParamStringValue(value); - if (!strValue.empty()) { - std::string tmpl = templateValue.get(); - std::string formatted = FormatTemplate(tmpl, strValue); - cmdLine += " " + formatted; - } - return; - } - - // Handle boolean template with if_true/if_false - if (templateValue.is_object()) { - bool boolValue = false; - if (GetParamBoolValue(value, boolValue)) { - std::string flag = ""; - if (boolValue && templateValue.contains("if_true")) { - flag = templateValue["if_true"].get(); - } else if (!boolValue && templateValue.contains("if_false")) { - flag = templateValue["if_false"].get(); - } - - if (!flag.empty()) { - cmdLine += " " + flag; - } - } - return; - } - - // Handle array type - expand to multiple arguments - if (!templateValue.is_string()) { - return; - } - - std::string tmpl = templateValue.get(); - ProcessArrayExpansion(value, tmpl, cmdLine); -} - -void ToolUtil::ProcessJsonStringTemplate(const std::string &key, const sptr &value, - const nlohmann::json &templateValue, std::string &cmdLine) -{ - if (value == nullptr) { - TAG_LOGW(AAFwkTag::CLI_TOOL, "Param '%{public}s' value is null", key.c_str()); - return; - } - - std::string tmpl = templateValue.get(); - std::string jsonStr = ConvertValueToJson(key, value); - - // Apply template with JSON string - std::string formatted = FormatTemplate(tmpl, jsonStr); - cmdLine += " " + formatted; -} - -void ToolUtil::ProcessBooleanTemplate(const std::string &key, const sptr &value, - const nlohmann::json &templateValue, std::string &cmdLine) -{ - bool boolValue = false; - if (!GetParamBoolValue(value, boolValue)) { - return; - } - - std::string flag = ""; - if (boolValue && templateValue.contains("if_true")) { - flag = templateValue["if_true"].get(); - } else if (!boolValue && templateValue.contains("if_false")) { - flag = templateValue["if_false"].get(); - } - - if (!flag.empty()) { - cmdLine += " " + flag; - } -} - -std::string ToolUtil::ConvertValueToJson(const std::string &key, const sptr &value) -{ - // Check if value is a WantParams (nested object) - AAFwk::WantParams nestedWantParams; - if (ExtractWantParams(value, nestedWantParams)) { - // Recursively convert nested WantParams to JSON - std::string jsonStr = WantParamsToJson(nestedWantParams); - TAG_LOGI(AAFwkTag::CLI_TOOL, "Param '%{public}s' is nested WantParams, converted to: %{public}s", - key.c_str(), jsonStr.c_str()); - return jsonStr; - } - - // Convert IInterface to JSON string - std::string jsonStr = GetParamJsonValue(value); - TAG_LOGI(AAFwkTag::CLI_TOOL, "Param '%{public}s' converted to JSON: %{public}s", - key.c_str(), jsonStr.c_str()); - return jsonStr; -} - -void ToolUtil::ProcessFlattenedTemplate(const std::string &flattenedKey, const nlohmann::json &templateValue, - const std::string &separator, const AAFwk::WantParams &args, std::string &cmdLine) -{ - // Query nested value - simplifed version - sptr value = QueryNestedValue(args, flattenedKey, separator); - if (value == nullptr) { - return; - } - - // Process template based on type - if (templateValue.is_string()) { - std::string strValue = GetParamStringValue(value); - if (!strValue.empty()) { - std::string formatted = FormatTemplate(templateValue.get(), strValue); - cmdLine += " " + formatted; - } - } else if (templateValue.is_object()) { - ProcessBooleanTemplate(flattenedKey, value, templateValue, cmdLine); - } -} - -void ToolUtil::ProcessArrayExpansion(const sptr &value, const std::string &tmpl, +void ToolUtil::ProcessArrayExpansion(const std::string &key, const sptr &value, std::string &cmdLine) { auto arrayValue = AAFwk::IArray::Query(value); @@ -885,88 +343,11 @@ void ToolUtil::ProcessArrayExpansion(const sptr &value, const std::string elementStr = GetParamStringValue(elementValue); if (!elementStr.empty()) { - std::string formatted = FormatTemplate(tmpl, elementStr); - cmdLine += " " + formatted; + cmdLine += " --" + key + " " + elementStr; } } } -std::vector ToolUtil::SplitPathBySeparator(const std::string &path, const std::string &separator) -{ - std::vector pathSegments; - if (path.empty()) { - return pathSegments; - } - - std::string sep = separator.empty() ? "." : separator; - size_t start = 0; - size_t end = path.find(sep); - - while (end != std::string::npos) { - std::string segment = path.substr(start, end - start); - if (!segment.empty()) { - pathSegments.push_back(segment); - } - start = end + sep.length(); - end = path.find(sep, start); - } - std::string lastSegment = path.substr(start); - if (!lastSegment.empty()) { - pathSegments.push_back(lastSegment); - } - - return pathSegments; -} - -sptr ToolUtil::QueryNestedPath(const AAFwk::WantParams &args, - const std::vector &pathSegments, const std::string &separator) -{ - auto ¶ms = args.GetParams(); - - // First level: query from args - auto firstIt = params.find(pathSegments[0]); - if (firstIt == params.end()) { - TAG_LOGI(AAFwkTag::CLI_TOOL, "First level key not found: %{public}s", pathSegments[0].c_str()); - return nullptr; - } - - sptr currentValue = firstIt->second; - - // Nested levels: traverse through WantParams - for (size_t i = 1; i < pathSegments.size(); ++i) { - currentValue = QueryNextLevel(currentValue, pathSegments[i], separator); - if (currentValue == nullptr) { - TAG_LOGI(AAFwkTag::CLI_TOOL, "Nested key not found: %{public}s", pathSegments[i].c_str()); - return nullptr; - } - } - - TAG_LOGI(AAFwkTag::CLI_TOOL, "Found param with nested traversal: %{public}s", - pathSegments.size() > 1 ? pathSegments[0].c_str() : "N/A"); - return currentValue; -} - -sptr ToolUtil::QueryNextLevel(const sptr ¤tValue, - const std::string &nextSegment, const std::string &separator) -{ - if (currentValue == nullptr) { - return nullptr; - } - - AAFwk::WantParams nestedParams; - if (!ExtractWantParams(currentValue, nestedParams)) { - return nullptr; - } - - auto &nestedMap = nestedParams.GetParams(); - auto nestedIt = nestedMap.find(nextSegment); - if (nestedIt == nestedMap.end()) { - return nullptr; - } - - return nestedIt->second; -} - // ============================================================================ // Type validation helpers for ValidateInputSchemaProperties // ============================================================================ @@ -981,9 +362,6 @@ bool ToolUtil::ValidateParamType(const sptr &value, const std if (expectedType == "array") { return ValidateArrayType(value, propertySchema, key); } - if (expectedType == "object") { - return ValidateObjectType(value, propertySchema, key); - } return ValidateBasicType(value, expectedType); } @@ -1021,19 +399,6 @@ bool ToolUtil::ValidateArrayType(const sptr &value, return ValidateArrayItems(arrayObj, propertySchema["items"], key); } -bool ToolUtil::ValidateObjectType(const sptr &value, - const nlohmann::json &propertySchema, const std::string &key) -{ - if (!IsObjectType(value)) { - return false; - } - AAFwk::WantParams nestedParams; - if (!ExtractWantParams(value, nestedParams)) { - return false; - } - return ValidateNestedObject(nestedParams, propertySchema, key); -} - bool ToolUtil::ValidateArrayItems(sptr arrayObj, const nlohmann::json &itemsSchema, const std::string &key) { @@ -1060,47 +425,6 @@ bool ToolUtil::ValidateArrayItems(sptr arrayObj, return true; } -bool ToolUtil::ValidateNestedObject(const AAFwk::WantParams &nestedParams, - const nlohmann::json &objectSchema, const std::string &parentKey) -{ - if (!objectSchema.contains("properties") || !objectSchema["properties"].is_object()) { - TAG_LOGI(AAFwkTag::CLI_TOOL, "No nested properties defined for '%{public}s', skipping validation", - parentKey.c_str()); - return true; - } - auto nestedProperties = objectSchema["properties"]; - if (objectSchema.contains("required") && objectSchema["required"].is_array()) { - for (const auto &requiredProp : objectSchema["required"]) { - if (!requiredProp.is_string()) { - continue; - } - std::string requiredKey = requiredProp.get(); - std::string fullKey = parentKey + "." + requiredKey; - if (nestedParams.GetParams().find(requiredKey) == nestedParams.GetParams().end()) { - TAG_LOGE(AAFwkTag::CLI_TOOL, "Required nested property '%{public}s' not found", fullKey.c_str()); - return false; - } - } - } - for (const auto &[nestedKey, nestedValue] : nestedParams.GetParams()) { - std::string fullKey = parentKey + "." + nestedKey; - if (!nestedProperties.contains(nestedKey)) { - TAG_LOGE(AAFwkTag::CLI_TOOL, "Nested property '%{public}s' not found in schema", fullKey.c_str()); - return false; - } - auto &nestedPropertySchema = nestedProperties[nestedKey]; - if (nestedPropertySchema.contains("type")) { - std::string expectedType = nestedPropertySchema["type"].get(); - if (!ValidateParamType(nestedValue, expectedType, nestedPropertySchema, fullKey)) { - TAG_LOGE(AAFwkTag::CLI_TOOL, "Nested property '%{public}s' type mismatch, expected: %{public}s", - fullKey.c_str(), expectedType.c_str()); - return false; - } - } - } - return true; -} - bool ToolUtil::IsStringType(const sptr &value) { return AAFwk::IString::Query(value) != nullptr; @@ -1129,11 +453,5 @@ bool ToolUtil::IsArrayType(const sptr &value) return AAFwk::IArray::Query(value) != nullptr; } -bool ToolUtil::IsObjectType(const sptr &value) -{ - AAFwk::WantParams wantParams; - return ExtractWantParams(value, wantParams); -} - } // namespace CliTool } // namespace OHOS diff --git a/services/common/include/hilog_tag_wrapper.h b/services/common/include/hilog_tag_wrapper.h index 87a8a5a409..394d403a0b 100644 --- a/services/common/include/hilog_tag_wrapper.h +++ b/services/common/include/hilog_tag_wrapper.h @@ -87,6 +87,7 @@ enum class AAFwkLogTag : uint32_t { AUTOFILLMGR, EXTMGR, SER_ROUTER, + CLI_TOOL = SER_ROUTER, AUTO_STARTUP, STARTUP, RECOVERY, @@ -102,7 +103,6 @@ enum class AAFwkLogTag : uint32_t { APP_SERVICE_EXT, VERTICAL_PANEL, USER_CONTROLLER, - CLI_TOOL, END = 256, // N.B. never use it }; diff --git a/test/unittest/cli_tool_mgr/tool_util_test/tool_util_test.cpp b/test/unittest/cli_tool_mgr/tool_util_test/tool_util_test.cpp index 0e56acce40..5f455336df 100644 --- a/test/unittest/cli_tool_mgr/tool_util_test/tool_util_test.cpp +++ b/test/unittest/cli_tool_mgr/tool_util_test/tool_util_test.cpp @@ -1080,143 +1080,5 @@ HWTEST_F(ToolUtilTest, ValidateInputSchemaProperties_ArrayItems_0200, TestSize.L GTEST_LOG_(INFO) << "ToolUtil_ValidateInputSchemaProperties_ArrayItems_0200 end"; } -/** - * @tc.name: ToolUtil_SplitPathBySeparator_0100 - * @tc.desc: Test SplitPathBySeparator with normal path - * @tc.type: FUNC - */ -HWTEST_F(ToolUtilTest, SplitPathBySeparator_0100, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0100 start"; - - std::string path = "app.config.name"; - std::vector result = ToolUtil::SplitPathBySeparator(path, "."); - - EXPECT_EQ(result.size(), 3UL); - EXPECT_EQ(result[0], "app"); - EXPECT_EQ(result[1], "config"); - EXPECT_EQ(result[2], "name"); - - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0100 end"; -} - -/** - * @tc.name: ToolUtil_SplitPathBySeparator_0200 - * @tc.desc: Test SplitPathBySeparator with trailing separator - * @tc.type: FUNC - */ -HWTEST_F(ToolUtilTest, SplitPathBySeparator_0200, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0200 start"; - - std::string path = "app.config.name."; - std::vector result = ToolUtil::SplitPathBySeparator(path, "."); - - // Should filter out empty segment - EXPECT_EQ(result.size(), 3UL); - EXPECT_EQ(result[0], "app"); - EXPECT_EQ(result[1], "config"); - EXPECT_EQ(result[2], "name"); - - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0200 end"; -} - -/** - * @tc.name: ToolUtil_SplitPathBySeparator_0300 - * @tc.desc: Test SplitPathBySeparator with leading separator - * @tc.type: FUNC - */ -HWTEST_F(ToolUtilTest, SplitPathBySeparator_0300, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0300 start"; - - std::string path = ".app.config.name"; - std::vector result = ToolUtil::SplitPathBySeparator(path, "."); - - // Should filter out empty segment - EXPECT_EQ(result.size(), 3UL); - EXPECT_EQ(result[0], "app"); - EXPECT_EQ(result[1], "config"); - EXPECT_EQ(result[2], "name"); - - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0300 end"; -} - -/** - * @tc.name: ToolUtil_SplitPathBySeparator_0400 - * @tc.desc: Test SplitPathBySeparator with multiple consecutive separators - * @tc.type: FUNC - */ -HWTEST_F(ToolUtilTest, SplitPathBySeparator_0400, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0400 start"; - - std::string path = "app..config...name"; - std::vector result = ToolUtil::SplitPathBySeparator(path, "."); - - // Should filter out all empty segments - EXPECT_EQ(result.size(), 3UL); - EXPECT_EQ(result[0], "app"); - EXPECT_EQ(result[1], "config"); - EXPECT_EQ(result[2], "name"); - - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0400 end"; -} - -/** - * @tc.name: ToolUtil_SplitPathBySeparator_0500 - * @tc.desc: Test SplitPathBySeparator with empty string - * @tc.type: FUNC - */ -HWTEST_F(ToolUtilTest, SplitPathBySeparator_0500, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0500 start"; - - std::string path = ""; - std::vector result = ToolUtil::SplitPathBySeparator(path, "."); - - EXPECT_EQ(result.size(), 0UL); - - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0500 end"; -} - -/** - * @tc.name: ToolUtil_SplitPathBySeparator_0600 - * @tc.desc: Test SplitPathBySeparator with single segment - * @tc.type: FUNC - */ -HWTEST_F(ToolUtilTest, SplitPathBySeparator_0600, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0600 start"; - - std::string path = "app"; - std::vector result = ToolUtil::SplitPathBySeparator(path, "."); - - EXPECT_EQ(result.size(), 1UL); - EXPECT_EQ(result[0], "app"); - - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0600 end"; -} - -/** - * @tc.name: ToolUtil_SplitPathBySeparator_0700 - * @tc.desc: Test SplitPathBySeparator with custom separator - * @tc.type: FUNC - */ -HWTEST_F(ToolUtilTest, SplitPathBySeparator_0700, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0700 start"; - - std::string path = "app-config-name"; - std::vector result = ToolUtil::SplitPathBySeparator(path, "-"); - - EXPECT_EQ(result.size(), 3UL); - EXPECT_EQ(result[0], "app"); - EXPECT_EQ(result[1], "config"); - EXPECT_EQ(result[2], "name"); - - GTEST_LOG_(INFO) << "ToolUtil_SplitPathBySeparator_0700 end"; -} - } // namespace CliTool } // namespace OHOS