-
Notifications
You must be signed in to change notification settings - Fork 0
/
serializer.cc
78 lines (60 loc) · 1.89 KB
/
serializer.cc
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
#include <string>
#include "atl/time.h"
#include "atl/string.h"
#include "worklog.h"
#include "serializer.h"
namespace worklog {
std::string HumanSerializer::Serialize(const Log& log) {
std::ostringstream text;
text << "date=" << atl::FormatTime(log.created_at) << "\n";
text << "tags=" << atl::Join(log.tags, ", ") << "\n\n";
text << atl::TrimSpace(log.subject) << "\n\n";
text << atl::TrimSpace(log.description) << "\n\n";
return text.str();
}
Log HumanSerializer::Unserialize(const std::string& text) {
Log log;
log.created_at = 0;
log.id = 0;
bool ignore_empty = false;
auto res = atl::Split(text, "\n", ignore_empty);
for (const auto& bit : res) {
if (IsTag(bit)) {
std::vector<std::string> tag = atl::Split(bit, "=");
if (tag.size() != 2) {
continue;
}
// The tag's can come along like 'tag= xxx', ' tag = xxx'
// thats why we TrimSpace it here:
const std::string& tag_name = atl::TrimSpace(tag[0]);
const std::string& tag_value = atl::TrimSpace(tag[1]);
if (tag_name == "" || tag_value == "") {
continue;
}
if (tag_name == "tags") {
// the tag is of type 'tags' which will contain multiple
// comma separated values:
auto tags = atl::Split(tag_value, ",");
for (auto v : tags) {
log.tags.insert(atl::TrimSpace(v));
}
} else if (tag_name == "date") {
// the tag is of type 'date' which will contain a date
// in the format: 2017-12-30
auto time = atl::ParseTime(tag_value);
if (!time.second) {
continue;
}
log.created_at = atl::UnixTimestamp(time.first);
} else {
// TODO(an): Unknkown tag - handle here
}
} else if (log.subject == "") {
log.subject = bit;
} else {
log.description += bit + "\n";
}
}
return log;
}
} // namespace worklog