Skip to content

Commit 263fb81

Browse files
authored
Refactor yql issue lib (#272)
1 parent afed51b commit 263fb81

File tree

13 files changed

+43
-125
lines changed

13 files changed

+43
-125
lines changed

examples/basic_example/basic_example.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "basic_example.h"
22

3+
#include <ydb-cpp-sdk/util/string/cast.h>
4+
35
#include <src/util/folder/pathsplit.h>
46

57
#include <format>

examples/pagination/pagination.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "pagination.h"
22

3+
#include <ydb-cpp-sdk/util/string/cast.h>
4+
35
#include <src/util/folder/pathsplit.h>
46

57
using namespace NYdb;

examples/ttl/util.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
#include <ydb-cpp-sdk/client/table/table.h>
44

5-
#include <src/util/folder/pathsplit.h>
65
#include <ydb-cpp-sdk/util/generic/yexception.h>
6+
#include <ydb-cpp-sdk/util/string/cast.h>
7+
8+
#include <src/util/folder/pathsplit.h>
79

810
namespace NExample {
911

include/ydb-cpp-sdk/library/yql/public/issue/yql_issue.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
#include "yql_issue_id.h"
44

5+
#include <ydb-cpp-sdk/util/generic/ptr.h>
6+
#include <ydb-cpp-sdk/util/generic/yexception.h>
57
#include <ydb-cpp-sdk/util/system/types.h>
68
#include <ydb-cpp-sdk/util/str_stl.h>
9+
710
#include <google/protobuf/message.h>
811

12+
#include <optional>
913
#include <vector>
1014
#include <sstream>
1115
#include <string>

include/ydb-cpp-sdk/library/yql/public/issue/yql_issue_id.h

Lines changed: 2 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,128 +2,13 @@
22

33
#include <ydb-cpp-sdk/src/library/yql/public/issue/protos/issue_severity.pb.h>
44

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-
235
namespace NYql {
246

25-
using TIssueCode = ui32;
7+
using TIssueCode = uint32_t;
268
using ESeverity = NYql::TSeverityIds::ESeverityId;
279
const TIssueCode DEFAULT_ERROR = 0;
2810
const TIssueCode UNEXPECTED_ERROR = 1;
2911

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);
12813

12914
}

src/client/impl/ydb_internal/plain_status/status.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
#include <ydb-cpp-sdk/client/types/status_codes.h>
66

7-
#include <src/api/protos/ydb_operation.pb.h>
8-
7+
#include <ydb-cpp-sdk/library/grpc/client/grpc_client_low.h>
98
#include <ydb-cpp-sdk/library/yql/public/issue/yql_issue.h>
109

11-
#include <ydb-cpp-sdk/library/grpc/client/grpc_client_low.h>
10+
#include <ydb-cpp-sdk/util/string/subst.h>
11+
12+
#include <src/api/protos/ydb_operation.pb.h>
1213

1314
namespace NYdb {
1415

src/client/impl/ydb_internal/retry/retry.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "retry.h"
2+
23
#include <ydb-cpp-sdk/util/random/random.h>
34
#include <ydb-cpp-sdk/client/retry/retry.h>
5+
6+
#include <ydb-cpp-sdk/util/string/subst.h>
7+
48
#include <src/client/common_client/impl/iface.h>
59

610
#include <cmath>

src/client/impl/ydb_internal/retry/retry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
#include <ydb-cpp-sdk/library/threading/future/core/fwd.h>
88
#include <ydb-cpp-sdk/util/datetime/base.h>
9-
#include <src/util/datetime/cputimer.h>
109
#include <ydb-cpp-sdk/util/generic/ptr.h>
1110
#include <ydb-cpp-sdk/util/system/types.h>
11+
#include <ydb-cpp-sdk/util/string/cast.h>
1212

1313
#include <functional>
1414
#include <memory>

src/client/types/operation/operation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
#include <ydb-cpp-sdk/client/types/operation/operation.h>
22

3-
#include <src/api/protos/ydb_operation.pb.h>
43
#include <ydb-cpp-sdk/client/types/status/status.h>
54

5+
#include <ydb-cpp-sdk/util/stream/str.h>
6+
7+
#include <src/api/protos/ydb_operation.pb.h>
8+
69
namespace NYdb {
710

811

src/client/types/status/status.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <ydb-cpp-sdk/client/types/exceptions/exceptions.h>
88

9+
#include <ydb-cpp-sdk/util/string/cast.h>
10+
911
namespace NYdb {
1012

1113
class TStatus::TImpl {

src/library/yql/public/issue/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ target_link_libraries(yql-public-issue PUBLIC
1515
)
1616

1717
target_sources(yql-public-issue PRIVATE
18-
yql_issue.cpp
18+
yql_issue_id.cpp
1919
yql_issue_message.cpp
20+
yql_issue.cpp
2021
)
2122

2223
_ydb_sdk_install_targets(TARGETS yql-public-issue)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <ydb-cpp-sdk/library/yql/public/issue/yql_issue_id.h>
2+
3+
#include <ydb-cpp-sdk/library/string_utils/helpers/helpers.h>
4+
5+
namespace NYql {
6+
7+
std::string SeverityToString(ESeverity severity) {
8+
auto ret = NYql::TSeverityIds::ESeverityId_Name(severity);
9+
return ret.empty() ? "Unknown" : NUtils::ToTitle(ret.substr(2)); //remove prefix "S_"
10+
}
11+
12+
};

src/library/yql/public/issue/yql_issue_message.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <ydb-cpp-sdk/util/stream/output.h>
88
#include <src/util/string/join.h>
99

10-
#include <tuple>
10+
#include <google/protobuf/text_format.h>
1111

1212
namespace NYql {
1313

0 commit comments

Comments
 (0)