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: 76.2 % 63 48
Test Date: 2026-08-17 10:19:35 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          783 : std::string GetJsonProperty(const nlohmann::json& obj, const char* propName, bool required)
      17              : {
      18          783 :     if (propName == nullptr) {
      19            1 :         std::string msg = StringFormat("[%s] propName is nullptr", __func__);
      20            1 :         THROW<NullPtrException>(msg);
      21            1 :     }
      22          782 :     if (!required) {
      23           28 :         return obj.value<std::string>(propName, "");
      24              :     }
      25              : 
      26          768 :     if (!obj.contains(propName)) {
      27            8 :         RPT_INPUT_ERR(
      28              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      29            1 :         THROW<InvalidParamsException>(
      30            3 :             StringFormat("[Get][JsonProperty] json object does not contain property[%s].", propName));
      31              :     }
      32          767 :     std::string value = obj.at(propName).get<std::string>();
      33          767 :     return value;
      34          769 : }
      35              : 
      36          500 : u32 GetJsonPropertyUInt(const nlohmann::json& obj, const char* propName, bool required, u32 defaultValue)
      37              : {
      38          500 :     if (!required) {
      39           84 :         return obj.value<u32>(propName, defaultValue);
      40              :     }
      41              : 
      42          416 :     if (!obj.contains(propName)) {
      43            8 :         RPT_INPUT_ERR(
      44              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      45            1 :         THROW<InvalidParamsException>(
      46            3 :             StringFormat("[Get][JsonPropertyUInt] json object does not contain property[%s].", propName));
      47              :     }
      48              : 
      49          415 :     s64 value = obj.at(propName).get<s64>();
      50          415 :     if (value < 0 || value > UINT32_MAX) {
      51           14 :         RPT_INPUT_ERR(
      52              :             true, "EI0014", std::vector<std::string>({"value", "variable", "expect"}),
      53              :             std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ UINT32_MAX"}));
      54            1 :         THROW<InvalidParamsException>(StringFormat(
      55              :             "[Get][JsonPropertyUInt]errNo[0x%016llx]:json object "
      56              :             "property value of Name[%s] should be an unsigned 32-bit integer but acutally not!",
      57              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
      58              :     }
      59          414 :     return static_cast<u32>(value);
      60            4 : }
      61              : 
      62            2 : s32 GetJsonPropertySInt(const nlohmann::json& obj, const char* propName, bool required, s32 defaultValue)
      63              : {
      64            2 :     if (!required) {
      65            0 :         return obj.value<s32>(propName, defaultValue);
      66              :     }
      67              : 
      68            2 :     if (!obj.contains(propName)) {
      69            8 :         RPT_INPUT_ERR(
      70              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      71            1 :         THROW<InvalidParamsException>(
      72            3 :             StringFormat("[Get][JsonPropertySInt] json object does not contain property[%s].", propName));
      73              :     }
      74              : 
      75            1 :     s64 value = obj.at(propName).get<s64>();
      76            1 :     if (value < INT_MIN || value > INT_MAX) {
      77           14 :         RPT_INPUT_ERR(
      78              :             true, "EI0014", std::vector<std::string>({"value", "variable", "expect"}),
      79              :             std::vector<std::string>({std::to_string(value), std::string(propName), "0 ~ INT_MAX"}));
      80            2 :         THROW<InvalidParamsException>(StringFormat(
      81              :             "[Get][JsonPropertySInt]errNo[0x%016llx]:json object "
      82              :             "property value of Name[%s] is not signed number!",
      83              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
      84              :     }
      85            0 :     HCCL_INFO(
      86              :         "[Get][JsonPropertySInt]json object property value of Name [%s] is [%d]", propName, static_cast<s32>(value));
      87            0 :     return static_cast<s32>(value);
      88            5 : }
      89              : 
      90          521 : void GetJsonPropertyList(const nlohmann::json& obj, const char* propName, nlohmann::json& listObj)
      91              : {
      92          521 :     if (!obj.contains(propName)) {
      93            8 :         RPT_INPUT_ERR(
      94              :             true, "EI0017", std::vector<std::string>({"config"}), std::vector<std::string>({std::string(propName)}));
      95            1 :         THROW<InvalidParamsException>(
      96            3 :             StringFormat("[Get][JsonPropertyList] json object does not contain property[%s].", propName));
      97              :     }
      98              : 
      99          520 :     listObj = obj.at(propName);
     100          520 :     if (!listObj.is_array()) {
     101           14 :         RPT_INPUT_ERR(
     102              :             true, "EI0014", std::vector<std::string>({"value", "variable", "expect"}),
     103              :             std::vector<std::string>({std::string(listObj.type_name()), std::string(propName), "array"}));
     104            1 :         THROW<InvalidParamsException>(StringFormat(
     105              :             "[Get][GetJsonPropertyList]errNo[0x%016llx]:json object "
     106              :             "property value of Name \"%s\" is not list!",
     107              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), propName));
     108              :     }
     109          523 : }
     110              : 
     111            0 : void JsonParser::ParseFileToJson(const std::string& filePath, nlohmann::json& parseInformation) const
     112              : {
     113              :     // 校验文件是否存在
     114            0 :     char resolvedPath[PATH_MAX] = {0};
     115            0 :     if (realpath(filePath.c_str(), resolvedPath) == nullptr) {
     116            0 :         RPT_INPUT_ERR(
     117              :             true, "EI0004", std::vector<std::string>({"error_reason", "ranktable_path"}),
     118              :             std::vector<std::string>(
     119              :                 {filePath, "The rankTable file path does not exist, the permission is insufficient, or the JSON format "
     120              :                            "is incorrect."}));
     121            0 :         THROW<InvalidParamsException>(StringFormat(
     122              :             "[Get][RanktableRealPath]errNo[0x%016llx] path %s is not a valid real path",
     123              :             HCOM_ERROR_CODE(HcclResult::HCCL_E_PARA), filePath.c_str()));
     124              :     }
     125              : 
     126            0 :     HCCL_INFO("waiting for json file load complete");
     127            0 :     std::ifstream infoFile(resolvedPath, std::ifstream::in);
     128            0 :     if (!infoFile) {
     129            0 :         THROW<InternalException>(StringFormat(
     130              :             "[Read][File]errNo[0x%016llx],open file %s failed", HCOM_ERROR_CODE(HcclResult::HCCL_E_OPEN_FILE_FAILURE),
     131              :             resolvedPath));
     132              :     }
     133              : 
     134            0 :     ParseInformation(parseInformation, infoFile);
     135            0 :     infoFile.close();
     136            0 : }
     137              : 
     138              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1