|
|
|
@@ -1,5 +1,5 @@ |
|
|
|
/** |
|
|
|
* Copyright 2021 Huawei Technologies Co., Ltd |
|
|
|
* Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved. |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@@ -58,24 +58,51 @@ Status Message2Operator::ParseOperatorAttrs(const google::protobuf::Message *mes |
|
|
|
return SUCCESS; |
|
|
|
} |
|
|
|
|
|
|
|
void Message2Operator::ParseBaseTypeField(const google::protobuf::Reflection *reflection, |
|
|
|
const google::protobuf::Message *message, |
|
|
|
const google::protobuf::FieldDescriptor *field, ge::Operator &ops) { |
|
|
|
switch (field->cpp_type()) { |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_INT32: { |
|
|
|
int32_t value = reflection->GetInt32(*message, field); |
|
|
|
GELOGD("Parse result(%s : %d)", field->name().c_str(), value); |
|
|
|
(void)ops.SetAttr(field->name().c_str(), value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: { |
|
|
|
uint32_t value = reflection->GetUInt32(*message, field); |
|
|
|
GELOGD("Parse result(%s : %u)", field->name().c_str(), value); |
|
|
|
(void)ops.SetAttr(field->name().c_str(), value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_INT64: { |
|
|
|
int64_t value = reflection->GetInt64(*message, field); |
|
|
|
GELOGD("Parse result(%s : %ld)", field->name().c_str(), value); |
|
|
|
(void)ops.SetAttr(field->name().c_str(), value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: { |
|
|
|
float value = reflection->GetFloat(*message, field); |
|
|
|
GELOGD("Parse result(%s : %f)", field->name().c_str(), value); |
|
|
|
(void)ops.SetAttr(field->name().c_str(), value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: { |
|
|
|
bool value = reflection->GetBool(*message, field); |
|
|
|
GELOGD("Parse result(%s : %d)", field->name().c_str(), value); |
|
|
|
(void)ops.SetAttr(field->name().c_str(), value); |
|
|
|
break; |
|
|
|
} |
|
|
|
default: { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Status Message2Operator::ParseField(const google::protobuf::Reflection *reflection, |
|
|
|
const google::protobuf::Message *message, |
|
|
|
const google::protobuf::FieldDescriptor *field, int depth, ge::Operator &ops) { |
|
|
|
GELOGD("Start to parse field: %s.", field->name().c_str()); |
|
|
|
ParseBaseTypeField(reflection, message, field, ops); |
|
|
|
switch (field->cpp_type()) { |
|
|
|
#define CASE_FIELD_TYPE(cpptype, method, valuetype, logtype) \ |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_##cpptype: { \ |
|
|
|
valuetype value = reflection->Get##method(*message, field); \ |
|
|
|
GELOGD("Parse result(%s : %" #logtype ")", field->name().c_str(), value); \ |
|
|
|
(void)ops.SetAttr(field->name().c_str(), value); \ |
|
|
|
break; \ |
|
|
|
} |
|
|
|
CASE_FIELD_TYPE(INT32, Int32, int32_t, d); |
|
|
|
CASE_FIELD_TYPE(UINT32, UInt32, uint32_t, u); |
|
|
|
CASE_FIELD_TYPE(INT64, Int64, int64_t, ld); |
|
|
|
CASE_FIELD_TYPE(FLOAT, Float, float, f); |
|
|
|
CASE_FIELD_TYPE(BOOL, Bool, bool, d); |
|
|
|
#undef CASE_FIELD_TYPE |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_ENUM: { |
|
|
|
GE_CHECK_NOTNULL(reflection->GetEnum(*message, field)); |
|
|
|
int value = reflection->GetEnum(*message, field)->number(); |
|
|
|
@@ -108,6 +135,70 @@ Status Message2Operator::ParseField(const google::protobuf::Reflection *reflecti |
|
|
|
return SUCCESS; |
|
|
|
} |
|
|
|
|
|
|
|
void Message2Operator::ParseRepeatedBaseTypeField(const google::protobuf::Reflection *reflection, |
|
|
|
const google::protobuf::Message *message, |
|
|
|
const google::protobuf::FieldDescriptor *field, |
|
|
|
ge::Operator &ops) { |
|
|
|
switch (field->cpp_type()) { |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_INT32: { |
|
|
|
std::vector<int32_t> attr_value; |
|
|
|
for (int i = 0; i < field_size; i++) { |
|
|
|
int32_t value = reflection->GetRepeatedInt32(*message, field, i); |
|
|
|
attr_value.push_back(value); |
|
|
|
} |
|
|
|
(void)ops.SetAttr(field->name().c_str(), attr_value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_UINT32: { |
|
|
|
std::vector<uint32_t> attr_value; |
|
|
|
for (int i = 0; i < field_size; i++) { |
|
|
|
uint32_t value = reflection->GetRepeatedUInt32(*message, field, i); |
|
|
|
attr_value.push_back(value); |
|
|
|
} |
|
|
|
(void)ops.SetAttr(field->name().c_str(), attr_value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_INT64: { |
|
|
|
std::vector<int64_t> attr_value; |
|
|
|
for (int i = 0; i < field_size; i++) { |
|
|
|
int64_t value = reflection->GetRepeatedInt64(*message, field, i); |
|
|
|
attr_value.push_back(value); |
|
|
|
} |
|
|
|
(void)ops.SetAttr(field->name().c_str(), attr_value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT: { |
|
|
|
std::vector<float> attr_value; |
|
|
|
for (int i = 0; i < field_size; i++) { |
|
|
|
float value = reflection->GetRepeatedFloat(*message, field, i); |
|
|
|
attr_value.push_back(value); |
|
|
|
} |
|
|
|
(void)ops.SetAttr(field->name().c_str(), attr_value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_BOOL: { |
|
|
|
std::vector<bool> attr_value; |
|
|
|
for (int i = 0; i < field_size; i++) { |
|
|
|
bool value = reflection->GetRepeatedBool(*message, field, i); |
|
|
|
attr_value.push_back(value); |
|
|
|
} |
|
|
|
(void)ops.SetAttr(field->name().c_str(), attr_value); |
|
|
|
break; |
|
|
|
} |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_STRING: { |
|
|
|
std::vector<string> attr_value; |
|
|
|
for (int i = 0; i < field_size; i++) { |
|
|
|
string value = reflection->GetRepeatedString(*message, field, i); |
|
|
|
attr_value.push_back(value); |
|
|
|
} |
|
|
|
(void)ops.SetAttr(field->name().c_str(), attr_value); |
|
|
|
break; |
|
|
|
} |
|
|
|
default: { |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Status Message2Operator::ParseRepeatedField(const google::protobuf::Reflection *reflection, |
|
|
|
const google::protobuf::Message *message, |
|
|
|
const google::protobuf::FieldDescriptor *field, |
|
|
|
@@ -120,24 +211,8 @@ Status Message2Operator::ParseRepeatedField(const google::protobuf::Reflection * |
|
|
|
return FAILED; |
|
|
|
} |
|
|
|
|
|
|
|
ParseRepeatedBaseTypeField(reflection, message, field, ops); |
|
|
|
switch (field->cpp_type()) { |
|
|
|
#define CASE_FIELD_TYPE_REPEATED(cpptype, method, valuetype) \ |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_##cpptype: { \ |
|
|
|
std::vector<valuetype> attr_value; \ |
|
|
|
for (int i = 0; i < field_size; i++) { \ |
|
|
|
valuetype value = reflection->GetRepeated##method(*message, field, i); \ |
|
|
|
attr_value.push_back(value); \ |
|
|
|
} \ |
|
|
|
(void)ops.SetAttr(field->name().c_str(), attr_value); \ |
|
|
|
break; \ |
|
|
|
} |
|
|
|
CASE_FIELD_TYPE_REPEATED(INT32, Int32, int32_t); |
|
|
|
CASE_FIELD_TYPE_REPEATED(UINT32, UInt32, uint32_t); |
|
|
|
CASE_FIELD_TYPE_REPEATED(INT64, Int64, int64_t); |
|
|
|
CASE_FIELD_TYPE_REPEATED(FLOAT, Float, float); |
|
|
|
CASE_FIELD_TYPE_REPEATED(BOOL, Bool, bool); |
|
|
|
CASE_FIELD_TYPE_REPEATED(STRING, String, string); |
|
|
|
#undef CASE_FIELD_TYPE_REPEATED |
|
|
|
case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { |
|
|
|
nlohmann::json message_json; |
|
|
|
Pb2Json::RepeatedMessage2Json(*message, field, reflection, std::set<string>(), message_json[field->name()], |
|
|
|
|