LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/misc/json_parser - json_parser.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 70.0 % 60 42
Test Date: 2026-08-04 10:52:23 Functions: 80.0 % 5 4

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : #include <climits>
      11              : #include "json_parser.h"
      12              : #include "adapter_error_manager_pub.h"
      13              : 
      14              : namespace Hccl {
      15              : 
      16         4059 : std::string GetJsonProperty(const nlohmann::json &obj, const char *propName, bool required)
      17              : {
      18         4059 :     if (propName == nullptr) {
      19            1 :         std::string msg = StringFormat("[%s] propName is nullptr", __func__);
      20            1 :         THROW<NullPtrException>(msg);
      21            1 :     }
      22         4058 :     if (!required) {
      23         1190 :         return obj.value<std::string>(propName, "");
      24              :     }
      25              : 
      26         3463 :     if (!obj.contains(propName)) {
      27            3 :         RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
      28              :             std::vector<std::string>({std::string(propName)}));
      29            6 :         THROW<InvalidParamsException>(StringFormat("[Get][JsonProperty] json object does not contain property[%s].",
      30              :             propName));
      31              :     }
      32         3460 :     std::string value = obj.at(propName).get<std::string>();
      33         3460 :     return value;
      34         3460 : }
      35              : 
      36         4097 : u32 GetJsonPropertyUInt(const nlohmann::json &obj, const char *propName, bool required, u32 defaultValue)
      37              : {
      38         4097 :     if (!required) {
      39          194 :         return obj.value<u32>(propName, defaultValue);
      40              :     }
      41              : 
      42         3903 :     if (!obj.contains(propName)) {
      43            7 :         RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
      44              :             std::vector<std::string>({std::string(propName)}));
      45           14 :         THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertyUInt] json object does not contain property[%s].",
      46              :             propName));
      47              :     }
      48              : 
      49         3896 :     s64 value = obj.at(propName).get<s64>();
      50         3895 :     if (value < 0 || value > UINT32_MAX) {
      51            3 :         RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
      52              :             std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ UINT32_MAX"}));
      53            3 :         THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertyUInt]errNo[0x%016llx]:json object "
      54              :                                         "property value of Name[%s] should be an unsigned 32-bit integer but acutally not!",
      55              :                                         HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
      56              :     }
      57         3892 :     return static_cast<u32>(value);
      58            0 : }
      59              : 
      60            2 : s32 GetJsonPropertySInt(const nlohmann::json &obj, const char *propName, bool required, s32 defaultValue)
      61              : {
      62            2 :     if (!required) {
      63            0 :         return obj.value<s32>(propName, defaultValue);
      64              :     }
      65              : 
      66            2 :     if (!obj.contains(propName)) {
      67            1 :         RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
      68              :             std::vector<std::string>({std::string(propName)}));
      69            2 :         THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertySInt] json object does not contain property[%s].",
      70              :             propName));
      71              :     }
      72              : 
      73            1 :     s64 value = obj.at(propName).get<s64>();
      74            1 :     if (value < INT_MIN || value > INT_MAX) {
      75            1 :         RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
      76              :             std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ INT_MAX"}));
      77            2 :         THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertySInt]errNo[0x%016llx]:json object "
      78              :                                                 "property value of Name[%s] is not signed number!",
      79              :                                                 HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
      80              :     }
      81            0 :     HCCL_INFO("[Get][JsonPropertySInt]json object property value of Name [%s] is [%d]", propName,
      82              :                             static_cast<s32>(value));
      83            0 :     return static_cast<s32>(value);
      84            0 : }
      85              : 
      86         3238 : void GetJsonPropertyList(const nlohmann::json &obj, const char *propName, nlohmann::json &listObj)
      87              : {
      88         3238 :     if (!obj.contains(propName)) {
      89            1 :         RPT_INPUT_ERR(true, "EI0017", std::vector<std::string>({"config"}),
      90              :             std::vector<std::string>({std::string(propName)}));
      91            2 :         THROW<InvalidParamsException>(StringFormat("[Get][JsonPropertyList] json object does not contain property[%s].",
      92              :             propName));
      93              :     }
      94              : 
      95         3237 :     listObj = obj.at(propName);
      96         3237 :     if (!listObj.is_array()) {
      97            1 :         RPT_INPUT_ERR(true, "EI0014", std::vector<std::string>({ "value", "variable" ,"expect" }),
      98              :             std::vector<std::string>({std::string(listObj.type_name()), std::string(propName), "array"}));
      99            1 :         THROW<InvalidParamsException>(StringFormat("[Get][GetJsonPropertyList]errNo[0x%016llx]:json object "
     100              :                                                 "property value of Name \"%s\" is not list!",
     101              :                                                 HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
     102              :     }
     103         3236 : }
     104              : 
     105              : 
     106            0 : void JsonParser::ParseFileToJson(const std::string &filePath, nlohmann::json &parseInformation) const
     107              : {
     108              :     // 校验文件是否存在
     109            0 :     char resolvedPath[PATH_MAX] = {0};
     110            0 :     if (realpath(filePath.c_str(), resolvedPath) == nullptr) {
     111            0 :         RPT_INPUT_ERR(true, "EI0004", std::vector<std::string>({"error_reason", "ranktable_path"}), 
     112              :             std::vector<std::string>({filePath, "The rankTable file path does not exist, the permission is insufficient, or the JSON format is incorrect."}));
     113            0 :         THROW<InvalidParamsException>(
     114            0 :             StringFormat("[Get][RanktableRealPath]errNo[0x%016llx] path %s is not a valid real path",
     115              :                          HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), filePath.c_str()));
     116              :     }
     117              : 
     118            0 :     HCCL_INFO("waiting for json file load complete");
     119            0 :     std::ifstream infoFile(resolvedPath, std::ifstream::in);
     120            0 :     if (!infoFile) {
     121            0 :         THROW<InternalException>(StringFormat("[Read][File]errNo[0x%016llx],open file %s failed",
     122              :                                               HCOM_ERROR_CODE(HcclResult::HCCL_E_OPEN_FILE_FAILURE), resolvedPath));
     123              :     }
     124              : 
     125            0 :     ParseInformation(parseInformation, infoFile);
     126            0 :     infoFile.close();
     127            0 : }
     128              : 
     129              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1