-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetadata.cpp
184 lines (158 loc) · 6.77 KB
/
metadata.cpp
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
#include <stdexcept>
#include <cmath>
#include <cstring>
#include <unity/scopes/ActionMetadata.h>
#include <unity/scopes/SearchMetadata.h>
#include <unity/scopes/ScopeMetadata.h>
#include <unity/scopes/ScopeExceptions.h>
extern "C" {
#include "_cgo_export.h"
}
#include "helpers.h"
using namespace unity::scopes;
using namespace gounityscopes::internal;
/* SearchMetadata objects */
_SearchMetadata *new_search_metadata(int cardinality, const StrData locale, const StrData form_factor) {
return reinterpret_cast<_SearchMetadata*>(new SearchMetadata(cardinality,
from_gostring(locale),
from_gostring(form_factor)));
}
void destroy_search_metadata(_SearchMetadata *metadata) {
delete reinterpret_cast<SearchMetadata*>(metadata);
}
char *query_metadata_get_locale(_QueryMetadata *metadata) {
auto m = reinterpret_cast<QueryMetadata*>(metadata);
try {
return strdup(m->locale().c_str());
} catch (const NotFoundException &) {
return nullptr;
}
}
char *query_metadata_get_form_factor(_QueryMetadata *metadata) {
auto m = reinterpret_cast<QueryMetadata*>(metadata);
try {
return strdup(m->form_factor().c_str());
} catch (const NotFoundException &) {
return nullptr;
}
}
void query_metadata_set_internet_connectivity(_QueryMetadata *metadata, int status) {
reinterpret_cast<QueryMetadata*>(metadata)->set_internet_connectivity(static_cast<QueryMetadata::ConnectivityStatus>(status));
}
int query_metadata_get_internet_connectivity(_QueryMetadata *metadata) {
return static_cast<int>(reinterpret_cast<QueryMetadata*>(metadata)->internet_connectivity());
}
int search_metadata_get_cardinality(_SearchMetadata *metadata) {
return reinterpret_cast<SearchMetadata*>(metadata)->cardinality();
}
void *search_metadata_get_location(_SearchMetadata *metadata, int *length) {
auto m = reinterpret_cast<SearchMetadata*>(metadata);
VariantMap location;
try {
location = m->location().serialize();
} catch (const NotFoundException &) {
return nullptr;
}
// libjsoncpp generates invalid JSON for NaN or Inf values, so
// filter them out here.
for (auto &pair : location) {
if (pair.second.which() == Variant::Double) {
double value = pair.second.get_double();
if (!std::isfinite(value)) {
pair.second = Variant();
}
}
}
return as_bytes(Variant(location).serialize_json(), length);
}
void search_metadata_set_location(_SearchMetadata *metadata, char *json_data, int json_data_length, char **error) {
try {
Variant value = Variant::deserialize_json(std::string(json_data, json_data_length));
Location location(value.get_dict());
reinterpret_cast<SearchMetadata*>(metadata)->set_location(location);
} catch (const std::exception & e) {
*error = strdup(e.what());
}
}
void search_metadata_set_aggregated_keywords(_SearchMetadata *metadata, const StrData keyword_list, char **error) {
try {
std::set<std::string> keywords;
for (auto &k : split_strings(keyword_list)) {
keywords.emplace(std::move(k));
}
reinterpret_cast<SearchMetadata*>(metadata)->set_aggregated_keywords(keywords);
} catch (const std::exception & e) {
*error = strdup(e.what());
}
}
void *search_metadata_get_aggregated_keywords(_SearchMetadata *metadata, int *length) {
std::set<std::string> keywords = reinterpret_cast<SearchMetadata*>(metadata)->aggregated_keywords();
// Marshal via JSON for now. This is probably faster than calling
// C.free() on each of a list of strings.
VariantArray array(keywords.begin(), keywords.end());
return as_bytes(Variant(array).serialize_json(), length);
}
int search_metadata_is_aggregated(_SearchMetadata *metadata) {
return reinterpret_cast<SearchMetadata*>(metadata)->is_aggregated();
}
/* ActionMetadata objects */
_ActionMetadata *new_action_metadata(const StrData locale, const StrData form_factor) {
return reinterpret_cast<_ActionMetadata*>(new ActionMetadata(from_gostring(locale),
from_gostring(form_factor)));
}
void destroy_action_metadata(_ActionMetadata *metadata) {
delete reinterpret_cast<ActionMetadata*>(metadata);
}
void *action_metadata_get_scope_data(_ActionMetadata *metadata, int *data_length) {
const std::string data = reinterpret_cast<ActionMetadata*>(metadata)->scope_data().serialize_json();
return as_bytes(data, data_length);
}
void action_metadata_set_scope_data(_ActionMetadata *metadata, char *json_data, int json_data_length, char **error) {
try {
Variant value = Variant::deserialize_json(std::string(json_data, json_data_length));
reinterpret_cast<ActionMetadata*>(metadata)->set_scope_data(value);
} catch (const std::exception & e) {
*error = strdup(e.what());
}
}
void action_metadata_set_hint(_ActionMetadata *metadata, const StrData key, char *json_data, int json_data_length, char **error) {
try {
Variant value = Variant::deserialize_json(std::string(json_data, json_data_length));
reinterpret_cast<ActionMetadata*>(metadata)->set_hint(from_gostring(key), value);
} catch (const std::exception & e) {
*error = strdup(e.what());
}
}
void *action_metadata_get_hint(_ActionMetadata *metadata, const StrData key, int *data_length, char **error) {
try {
ActionMetadata const*api_metadata = reinterpret_cast<ActionMetadata const*>(metadata);
Variant value = (*api_metadata)[from_gostring(key)];
const std::string data = value.serialize_json();
return as_bytes(data, data_length);
} catch (const std::exception & e) {
*data_length = 0;
*error = strdup(e.what());
return 0;
}
}
void *action_metadata_get_hints(_ActionMetadata *metadata, int *length) {
VariantMap hints = reinterpret_cast<ActionMetadata const*>(metadata)->hints();
// libjsoncpp generates invalid JSON for NaN or Inf values, so
// filter them out here.
for (auto &pair : hints) {
if (pair.second.which() == Variant::Double) {
double value = pair.second.get_double();
if (!std::isfinite(value)) {
pair.second = Variant();
}
}
}
return as_bytes(Variant(hints).serialize_json(), length);
}
char *get_scope_metadata_serialized(_ScopeMetadata *metadata) {
ScopeMetadata const*api_metadata = reinterpret_cast<ScopeMetadata const*>(metadata);
return strdup(Variant(api_metadata->serialize()).serialize_json().c_str());
}
void destroy_scope_metadata_ptr(_ScopeMetadata *metadata) {
delete reinterpret_cast<ScopeMetadata*>(metadata);
}