|
2 | 2 |
|
3 | 3 | #include <ydb-cpp-sdk/src/library/yql/public/issue/protos/issue_severity.pb.h> |
4 | 4 |
|
5 | | -#include <ydb-cpp-sdk/library/resource/resource.h> |
6 | | -#include <ydb-cpp-sdk/library/string_utils/misc/misc.h> |
7 | | - |
8 | | -#include <google/protobuf/descriptor.h> |
9 | | -#include <google/protobuf/repeated_field.h> |
10 | | -#include <google/protobuf/text_format.h> |
11 | | -#include <google/protobuf/message.h> |
12 | | - |
13 | | -#include <ydb-cpp-sdk/util/generic/singleton.h> |
14 | | -#include <ydb-cpp-sdk/util/generic/yexception.h> |
15 | | -#include <ydb-cpp-sdk/util/string/subst.h> |
16 | | - |
17 | | -#ifdef _win_ |
18 | | -#ifdef GetMessage |
19 | | -#undef GetMessage |
20 | | -#endif |
21 | | -#endif |
22 | | - |
23 | 5 | namespace NYql { |
24 | 6 |
|
25 | | -using TIssueCode = ui32; |
| 7 | +using TIssueCode = uint32_t; |
26 | 8 | using ESeverity = NYql::TSeverityIds::ESeverityId; |
27 | 9 | const TIssueCode DEFAULT_ERROR = 0; |
28 | 10 | const TIssueCode UNEXPECTED_ERROR = 1; |
29 | 11 |
|
30 | | -inline std::string SeverityToString(ESeverity severity) |
31 | | -{ |
32 | | - auto ret = NYql::TSeverityIds::ESeverityId_Name(severity); |
33 | | - return ret.empty() ? "Unknown" : NUtils::ToTitle(ret.substr(2)); //remove prefix "S_" |
34 | | -} |
35 | | - |
36 | | -template <typename T> |
37 | | -inline std::string IssueCodeToString(TIssueCode id) { |
38 | | - auto ret = T::EIssueCode_Name(static_cast<typename T::EIssueCode>(id)); |
39 | | - if (!ret.empty()) { |
40 | | - SubstGlobal(ret, '_', ' '); |
41 | | - return to_title(ret); |
42 | | - } else { |
43 | | - return "Unknown"; |
44 | | - } |
45 | | -} |
46 | | - |
47 | | -template<typename TProto, const char* ResourceName> |
48 | | -class TIssueId { |
49 | | - TProto ProtoIssues_; |
50 | | - std::unordered_map<TIssueCode, NYql::TSeverityIds::ESeverityId> IssuesMap_; |
51 | | - std::unordered_map<TIssueCode, std::string> IssuesFormatMap_; |
52 | | - |
53 | | - const google::protobuf::Descriptor* GetProtoDescriptor() const { |
54 | | - auto ret = ProtoIssues_.GetDescriptor(); |
55 | | - Y_ENSURE(ret != nullptr, "Bad proto file"); |
56 | | - return ret; |
57 | | - } |
58 | | - |
59 | | - bool CheckSeverityNameFormat(const std::string& name) const { |
60 | | - if (name.size() > 2 && name.substr(0,2) == "S_") { |
61 | | - return true; |
62 | | - } |
63 | | - return false; |
64 | | - } |
65 | | - |
66 | | -public: |
67 | | - ESeverity GetSeverity(TIssueCode id) const { |
68 | | - auto it = IssuesMap_.find(id); |
69 | | - Y_ENSURE(it != IssuesMap_.end(), "Unknown issue id: " |
70 | | - << id << "(" << IssueCodeToString<TProto>(id) << ")"); |
71 | | - return it->second; |
72 | | - } |
73 | | - |
74 | | - std::string GetMessage(TIssueCode id) const { |
75 | | - auto it = IssuesFormatMap_.find(id); |
76 | | - Y_ENSURE(it != IssuesFormatMap_.end(), "Unknown issue id: " |
77 | | - << id << "(" << IssueCodeToString<TProto>(id) << ")"); |
78 | | - return it->second; |
79 | | - } |
80 | | - |
81 | | - TIssueId() { |
82 | | - auto configData = NResource::Find(std::string_view(ResourceName)); |
83 | | - if (!::google::protobuf::TextFormat::ParseFromString(configData, &ProtoIssues_)) { |
84 | | - Y_ENSURE(false, "Bad format of protobuf data file, resource: " << ResourceName); |
85 | | - } |
86 | | - |
87 | | - auto sDesc = TSeverityIds::ESeverityId_descriptor(); |
88 | | - for (int i = 0; i < sDesc->value_count(); i++) { |
89 | | - const auto& name = sDesc->value(i)->name(); |
90 | | - Y_ENSURE(CheckSeverityNameFormat(name), |
91 | | - "Wrong severity name: " << name << ". Severity must starts with \"S_\" prefix"); |
92 | | - } |
93 | | - |
94 | | - for (const auto& x : ProtoIssues_.ids()) { |
95 | | - auto rv = IssuesMap_.insert(std::make_pair(x.code(), x.severity())); |
96 | | - Y_ENSURE(rv.second, "Duplicate issue code found, code: " |
97 | | - << static_cast<int>(x.code()) |
98 | | - << "(" << IssueCodeToString<TProto>(x.code()) <<")"); |
99 | | - } |
100 | | - |
101 | | - // Check all IssueCodes have mapping to severity |
102 | | - auto eDesc = TProto::EIssueCode_descriptor(); |
103 | | - for (int i = 0; i < eDesc->value_count(); i++) { |
104 | | - auto it = IssuesMap_.find(eDesc->value(i)->number()); |
105 | | - Y_ENSURE(it != IssuesMap_.end(), "IssueCode: " |
106 | | - << eDesc->value(i)->name() |
107 | | - << " is not found in protobuf data file"); |
108 | | - } |
109 | | - |
110 | | - for (const auto& x : ProtoIssues_.ids()) { |
111 | | - auto rv = IssuesFormatMap_.insert(std::make_pair(x.code(), x.format())); |
112 | | - Y_ENSURE(rv.second, "Duplicate issue code found, code: " |
113 | | - << static_cast<int>(x.code()) |
114 | | - << "(" << IssueCodeToString<TProto>(x.code()) <<")"); |
115 | | - } |
116 | | - } |
117 | | -}; |
118 | | - |
119 | | -template<typename TProto, const char* ResourceName> |
120 | | -inline ESeverity GetSeverity(TIssueCode id) { |
121 | | - return Singleton<TIssueId<TProto, ResourceName>>()->GetSeverity(id); |
122 | | -} |
123 | | - |
124 | | -template<typename TProto, const char* ResourceName> |
125 | | -inline std::string GetMessage(TIssueCode id) { |
126 | | - return Singleton<TIssueId<TProto, ResourceName>>()->GetMessage(id); |
127 | | -} |
| 12 | +std::string SeverityToString(ESeverity severity); |
128 | 13 |
|
129 | 14 | } |
0 commit comments