-
Notifications
You must be signed in to change notification settings - Fork 0
/
protobuf_xml_adapter.h
66 lines (58 loc) · 2 KB
/
protobuf_xml_adapter.h
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
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <google/protobuf/message.h>
#include <google/protobuf/descriptor.h>
#include <libxml2/libxml/tree.h>
using namespace std;
using namespace google::protobuf;
inline void
ReadFieldValue(Message *message, const FieldDescriptor *field, xmlNode *node);
// public
inline void
ProtoReadXml(Message *message, xmlNode *node) {
for (int index = 0; index < message->GetDescriptor()->field_count(); ++index) {
const FieldDescriptor *field = message->GetDescriptor()->field(index);
for (xmlNode *childNode = node->children; childNode != 0; childNode = childNode->next) {
if (childNode->type == XML_ELEMENT_NODE) {
string name(reinterpret_cast<const char *>(childNode->name));
if (field->name() == name)
ReadFieldValue(message, field, childNode);
}
}
}
}
// private
inline void
ReadFieldValue(Message *message, const FieldDescriptor *field, xmlNode *node) {
string value;
for (xmlNode *childNode = node->children; childNode != 0; childNode = childNode->next) {
if (childNode->content)
value += reinterpret_cast<char *>(childNode->content);
}
switch (field->cpp_type()) {
case FieldDescriptor::CPPTYPE_ENUM:
{
for (int index = 0; index < field->enum_type()->value_count(); ++index) {
if (field->enum_type()->value(index)->name() == value)
field->is_repeated() ?
message->GetReflection()->AddEnum(message, field, field->enum_type()->value(index)) :
message->GetReflection()->SetEnum(message, field, field->enum_type()->value(index));
}
break;
}
case FieldDescriptor::CPPTYPE_STRING:
field->is_repeated() ?
message->GetReflection()->AddString(message, field, value) :
message->GetReflection()->SetString(message, field, value);
break;
case FieldDescriptor::CPPTYPE_MESSAGE:
field->is_repeated() ?
ProtoReadXml(message->GetReflection()->AddMessage(message, field, 0), node) :
ProtoReadXml(message->GetReflection()->MutableMessage(message, field, 0), node);
break;
default:
break;
}
}