FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sysadmin/src/ProtobufTranslator.cpp at master · StarryInternet/sysadmin · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
StarryInternet
/
sysadmin
Public
Notifications
You must be signed in to change notification settings
Fork
8
Star
11
Code
Issues
10
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
sysadmin
/
src
/
ProtobufTranslator.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
243 lines (218 loc) · 6.96 KB
Breadcrumbs
sysadmin
/
src
/
ProtobufTranslator.cpp
Copy path
File metadata and controls
243 lines (218 loc) · 6.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#
include
"
ProtobufTranslator.h
"
#
include
"
decibel/Protobuf.h
"
#
include
<
log4cxx/logger.h
>
#
define
PROTO_LIST_TRANSLATE
(
PROTO_TYPE, LIST, VALUE
) \
for
(
int
i =
0
; i <
VALUE
.
PROTO_TYPE
##list().list_size(); i++) \
{ \
LIST
.
push_back
(
VALUE
.
PROTO_TYPE
##
list
().
list
(i)); \
}
namespace
{
log4cxx::LoggerPtr
spLogger
(log4cxx::Logger::getLogger(
"
ProtobufTranslator
"
));
}
ProtobufTranslator::ProtobufTranslator
()
{
}
ProtobufTranslator::~ProtobufTranslator
()
{
}
void
ProtobufTranslator::CopyConfigValue
(
const
ConfigValue& from, sysadminctl::ConfigValue* to)
{
if
(from.
Is
<
int32_t
>())
{
to->
set_int32val
(from.
Unpack
<
int32_t
>());
}
else
if
(from.
Is
<std::string>())
{
to->
set_strval
(from.
Unpack
<std::string>());
}
else
if
(from.
Is
<
bool
>())
{
to->
set_boolval
(from.
Unpack
<
bool
>());
}
else
if
(from.
Is
<std::vector<
int32_t
>>())
{
std::vector<
int32_t
> list = from.
Unpack
<std::vector<
int32_t
>>();
auto
proto_list = to->
mutable_int32list
();
for
(
const
auto
& item : list)
{
proto_list->
add_list
(item);
}
}
else
if
(from.
Is
<std::vector<
bool
>>())
{
std::vector<
bool
> list = from.
Unpack
<std::vector<
bool
>>();
auto
proto_list = to->
mutable_boollist
();
for
(
const
auto
& item : list)
{
proto_list->
add_list
(item);
}
}
else
if
(from.
Is
<std::vector<std::string>>())
{
std::vector<std::string> list = from.
Unpack
<std::vector<std::string>>();
auto
proto_list = to->
mutable_stringlist
();
for
(
const
auto
& item : list)
{
proto_list->
add_list
(item);
}
}
else
{
LOG4CXX_ERROR
(spLogger,
"
Type of value could not be determined:
"
<< from.
ToString
());
}
}
boost::optional<ConfigPair::Key>
ProtobufTranslator::TranslateToGet
(
const
sysadminctl::Command& message)
{
boost::optional<ConfigPair::Key> maybeReturn;
if
(!message.
has_get
())
{
return
maybeReturn;
}
maybeReturn =
ConfigPair::Key
(message.
get
().
key
());
return
maybeReturn;
}
boost::optional<ConfigPair::Key>
ProtobufTranslator::TranslateToErase
(
const
sysadminctl::Command& message)
{
boost::optional<ConfigPair::Key> maybeReturn;
if
(!message.
has_erase
())
{
return
maybeReturn;
}
maybeReturn =
ConfigPair::Key
(message.
erase
().
key
());
return
maybeReturn;
}
boost::optional<ConfigPair>
ProtobufTranslator::TranslateToSet
(
const
sysadminctl::Command& message)
{
boost::optional<ConfigPair> maybeReturn;
if
(!message.
has_set
())
{
return
maybeReturn;
}
if
(!message.
set
().
has_value
())
{
return
maybeReturn;
}
const
auto
& field =
decibel::GetOneofFieldName
(message.
set
().
value
(),
"
value
"
);
if
(!field)
{
return
maybeReturn;
}
const
auto
& value = message.
set
().
value
();
const
auto
& fieldname = field.
get
();
if
(fieldname ==
"
int32val
"
)
{
maybeReturn =
ConfigPair
(message.
set
().
key
(), ConfigType<
int32_t
>(value.
int32val
()));
}
else
if
(fieldname ==
"
strval
"
)
{
maybeReturn =
ConfigPair
(message.
set
().
key
(), ConfigType<std::string>(value.
strval
()));
}
else
if
(fieldname ==
"
boolval
"
)
{
maybeReturn =
ConfigPair
(message.
set
().
key
(), ConfigType<
bool
>(value.
boolval
()));
}
else
if
(fieldname ==
"
int32list
"
)
{
std::vector<
int32_t
> list;
PROTO_LIST_TRANSLATE
(int32, list, value);
maybeReturn =
ConfigPair
(message.
set
().
key
(), ConfigType<std::vector<
int32_t
>>(list));
}
else
if
(fieldname ==
"
boollist
"
)
{
std::vector<
bool
> list;
PROTO_LIST_TRANSLATE
(
bool
, list, value);
maybeReturn =
ConfigPair
(message.
set
().
key
(), ConfigType<std::vector<
bool
>>(list));
}
else
if
(fieldname ==
"
stringlist
"
)
{
std::vector<std::string> list;
PROTO_LIST_TRANSLATE
(string, list, value);
maybeReturn =
ConfigPair
(message.
set
().
key
(), ConfigType<std::vector<std::string>>(list));
}
else
{
LOG4CXX_ERROR
(spLogger,
"
No types for set field:
"
<< field.
get
());
}
return
maybeReturn;
}
boost::optional<ConfigPair::Key>
ProtobufTranslator::TranslateToBlame
(
const
sysadminctl::Command& message)
{
boost::optional<ConfigPair::Key> maybeReturn;
if
(!message.
has_blame
())
{
return
maybeReturn;
}
maybeReturn =
ConfigPair::Key
(message.
blame
().
key
());
return
maybeReturn;
}
sysadminctl::Response
ProtobufTranslator::TranslateFromGet
(
const
std::vector<ConfigPair>& items)
{
sysadminctl::Response output;
sysadminctl::GetResponse* getOutput = output.
mutable_get
();
if
(items.
size
() ==
0
)
{
output.
set_status
(sysadminctl::StatusCode::
KEY_NOT_FOUND
);
return
output;
}
for
(
const
auto
& item : items)
{
//
Emptied values "exist" in the database, but they never exist from the clients perspective
if
(item.
GetValue
().
Empty
())
{
continue
;
}
const
auto
& value = item.
GetValue
();
const
auto
& keystr = item.
GetKey
();
auto
mapitem = getOutput->
add_kvs
();
mapitem->
set_key
(keystr.
ToString
());
auto
msgvalue = mapitem->
mutable_value
();
CopyConfigValue
(value, msgvalue);
}
return
output;
}
sysadminctl::Response
ProtobufTranslator::TranslateFromInFlight
(
const
std::vector<ConfigPair>& items)
{
sysadminctl::Response output;
sysadminctl::InFlightResponse* subOutput = output.
mutable_inflight
();
for
(
const
auto
& item : items)
{
const
auto
& value = item.
GetValue
();
const
auto
& keystr = item.
GetKey
();
auto
mapitem = subOutput->
add_kvs
();
mapitem->
set_key
(keystr.
ToString
());
if
(!item.
GetValue
().
Empty
())
{
auto
msgvalue = mapitem->
mutable_value
();
CopyConfigValue
(value, msgvalue);
}
}
return
output;
}
boost::optional<CommitHistory::CommitId>
ProtobufTranslator::TranslateToRollback
(
const
sysadminctl::Command& message)
{
if
(!message.
has_rollback
())
{
return
boost::optional<CommitHistory::CommitId>();
}
CommitHistory::CommitId id = message.
rollback
().
id
();
return
boost::optional<CommitHistory::CommitId>(id);
}
sysadminctl::Response
ProtobufTranslator::TranslateFromBlame
(
const
ConfigPair::Key& key,
const
std::vector<BlameDelta>& deltas)
{
sysadminctl::Response output;
sysadminctl::BlameResponse* blameOutput = output.
mutable_blame
();
blameOutput->
set_key
(key.
ToString
());
for
(
auto
& delta : deltas)
{
sysadminctl::BlameEntry* entry = blameOutput->
add_entries
();
entry->
set_commit_id
(delta.
mCommitId
);
entry->
set_commit_time
(
boost::posix_time::to_simple_string
(delta.
mCommitTime
));
sysadminctl::ConfigValue* val = entry->
mutable_val
();
CopyConfigValue
(delta.
mDelta
.
second
.
GetValue
(), val);
}
return
output;
}
Back
|
FazBrowse Home
|
New Git URL