|
5 | 5 |
|
6 | 6 | namespace comm {
|
7 | 7 |
|
8 |
| -bool TelegramParser::tryExtractJsonValueStr(const std::string& jsonString, const std::string& key, std::optional<std::string>& result) |
| 8 | +std::optional<std::string> TelegramParser::tryExtractJsonValueStr(const std::string& jsonString, const std::string& key) |
9 | 9 | {
|
10 | 10 | // Find the position of the key
|
11 | 11 | size_t keyPos = jsonString.find("\"" + key + "\":");
|
12 | 12 |
|
13 | 13 | if (keyPos == std::string::npos) {
|
14 | 14 | // Key not found
|
15 |
| - return false; |
| 15 | + return std::nullopt; |
16 | 16 | }
|
17 | 17 |
|
18 | 18 | // Find the position of the value after the key
|
19 | 19 | size_t valuePosStart = jsonString.find("\"", keyPos + key.length() + std::string("\":\"").size());
|
20 | 20 |
|
21 | 21 | if (valuePosStart == std::string::npos) {
|
22 | 22 | // Value not found
|
23 |
| - return false; |
| 23 | + return std::nullopt; |
24 | 24 | }
|
25 | 25 |
|
26 | 26 | // Find the position of the closing quote for the value
|
27 | 27 | size_t valueEnd = jsonString.find("\"", valuePosStart + std::string("\"").size());
|
28 | 28 |
|
29 | 29 | if (valueEnd == std::string::npos) {
|
30 | 30 | // Closing quote not found
|
31 |
| - return false; |
| 31 | + return std::nullopt; |
32 | 32 | }
|
33 | 33 |
|
34 | 34 | // Extract the value substring
|
35 |
| - result = jsonString.substr(valuePosStart + 1, (valueEnd - valuePosStart) - 1); |
36 |
| - return true; |
| 35 | + return jsonString.substr(valuePosStart + 1, (valueEnd - valuePosStart) - 1); |
37 | 36 | }
|
38 | 37 |
|
39 | 38 | std::optional<int> TelegramParser::tryExtractFieldJobId(const std::string& message)
|
40 | 39 | {
|
41 |
| - std::optional<int> result; |
42 |
| - std::optional<std::string> strOpt; |
43 |
| - if (tryExtractJsonValueStr(message, comm::KEY_JOB_ID, strOpt)) { |
44 |
| - result = tryConvertToInt(strOpt.value()); |
| 40 | + if (std::optional<std::string> strOpt = tryExtractJsonValueStr(message, comm::KEY_JOB_ID)) { |
| 41 | + return tryConvertToInt(strOpt.value()); |
45 | 42 | }
|
46 |
| - return result; |
| 43 | + return std::nullopt; |
47 | 44 | }
|
48 | 45 |
|
49 | 46 | std::optional<int> TelegramParser::tryExtractFieldCmd(const std::string& message)
|
50 | 47 | {
|
51 |
| - std::optional<int> result; |
52 |
| - std::optional<std::string> strOpt; |
53 |
| - if (tryExtractJsonValueStr(message, comm::KEY_CMD, strOpt)) { |
54 |
| - result = tryConvertToInt(strOpt.value()); |
| 48 | + if (std::optional<std::string> strOpt = tryExtractJsonValueStr(message, comm::KEY_CMD)) { |
| 49 | + return tryConvertToInt(strOpt.value()); |
55 | 50 | }
|
56 |
| - return result; |
| 51 | + return std::nullopt; |
57 | 52 | }
|
58 | 53 |
|
59 |
| -bool TelegramParser::tryExtractFieldOptions(const std::string& message, std::optional<std::string>& result) |
| 54 | +std::optional<std::string> TelegramParser::tryExtractFieldOptions(const std::string& message) |
60 | 55 | {
|
61 |
| - return tryExtractJsonValueStr(message, comm::KEY_OPTIONS, result); |
| 56 | + return tryExtractJsonValueStr(message, comm::KEY_OPTIONS); |
62 | 57 | }
|
63 | 58 |
|
64 |
| -bool TelegramParser::tryExtractFieldData(const std::string& message, std::optional<std::string>& result) |
| 59 | +std::optional<std::string> TelegramParser::tryExtractFieldData(const std::string& message) |
65 | 60 | {
|
66 |
| - return tryExtractJsonValueStr(message, comm::KEY_DATA, result); |
| 61 | + return tryExtractJsonValueStr(message, comm::KEY_DATA); |
67 | 62 | }
|
68 | 63 |
|
69 | 64 | std::optional<int> TelegramParser::tryExtractFieldStatus(const std::string& message)
|
70 | 65 | {
|
71 |
| - std::optional<int> result; |
72 |
| - std::optional<std::string> strOpt; |
73 |
| - if (tryExtractJsonValueStr(message, comm::KEY_STATUS, strOpt)) { |
74 |
| - result = tryConvertToInt(strOpt.value()); |
| 66 | + if (std::optional<std::string> strOpt = tryExtractJsonValueStr(message, comm::KEY_STATUS)) { |
| 67 | + return tryConvertToInt(strOpt.value()); |
75 | 68 | }
|
76 |
| - return result; |
| 69 | + return std::nullopt; |
77 | 70 | }
|
78 | 71 |
|
79 | 72 | } // namespace comm
|
0 commit comments