/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of 
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef AIR_INC_FRAMEWORK_COMMON_PROTO_UTIL_H_
#define AIR_INC_FRAMEWORK_COMMON_PROTO_UTIL_H_

#include <sstream>
#include <string>

#include "google/protobuf/text_format.h"
#include "ge/ge_error_codes.h"
#include "graph/types.h"

namespace ge {
/**
 * @ingroup ge_common
 * @brief Converts RepeatedField to String.
 * @param [in] rpd_field  RepeatedField
 * @return string
 */
template <typename T>
GE_FUNC_VISIBILITY std::string ToString(const google::protobuf::RepeatedField<T> &rpd_field) {
  std::stringstream ss;
  ss << "[";
  for (const T x : rpd_field) {
    ss << x;
    ss << ", ";
  }
  // Delete the two extra characters at the end of the line.
  std::string str = ss.str().substr(0U, ss.str().length() - 2U);
  str += "]";
  return str;
}

/**
 *  @ingroup ge_common
 *  @brief RepeatedPtrField->String
 *  @param [in] const rpd_field  RepeatedPtrField
 *  @return String
 */
template <typename T>
GE_FUNC_VISIBILITY std::string ToString(const google::protobuf::RepeatedPtrField<T> &rpd_ptr_field) {
  std::stringstream ss;
  ss << "[";
  for (const T &x : rpd_ptr_field) {
    ss << x;
    ss << ", ";
  }
  std::string str_ret = ss.str().substr(0U, ss.str().length() - 2U);
  str_ret += "]";
  return str_ret;
}

/**
 * @ingroup ge_common
 * @brief Reads the proto structure from an array.
 * @param [in] data proto data to be read
 * @param [in] size proto data size
 * @param [out] proto Memory for storing the proto file
 * @return true success
 * @return false fail
 */
GE_FUNC_VISIBILITY bool ReadProtoFromArray(const void *const data, const int32_t size,
                                           google::protobuf::Message *const proto);

/**
 * @ingroup ge_common
 * @brief Reads the proto file in the text format.
 * @param [in] file path of proto file
 * @param [out] message Memory for storing the proto file
 * @return true success
 * @return false fail
 */
GE_FUNC_VISIBILITY bool ReadProtoFromText(const char_t *const file, google::protobuf::Message *const message);
}  // namespace ge
#endif  // AIR_INC_FRAMEWORK_COMMON_PROTO_UTIL_H_