summaryrefslogtreecommitdiffstats
blob: 253ca3665a3df8a1f03c3dafda2c05d5a941d387 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright (c) 2013-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/ElementConvertors/UserLocationConvertor.h>

#include <memory>

#include <lua.hpp>

#include <Swiften/Base/DateTime.h>

using namespace Swift;

UserLocationConvertor::UserLocationConvertor() :
        GenericLuaElementConvertor<UserLocation>("user_location") {
}

UserLocationConvertor::~UserLocationConvertor() {
}

std::shared_ptr<UserLocation> UserLocationConvertor::doConvertFromLua(lua_State* L) {
    std::shared_ptr<UserLocation> result = std::make_shared<UserLocation>();
    lua_getfield(L, -1, "area");
    if (lua_isstring(L, -1)) {
        result->setArea(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "altitude");
    if (lua_isnumber(L, -1)) {
        result->setAltitude(lua_tonumber(L, -1));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "locality");
    if (lua_isstring(L, -1)) {
        result->setLocality(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "latitude");
    if (lua_isnumber(L, -1)) {
        result->setLatitude(lua_tonumber(L, -1));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "accuracy");
    if (lua_isnumber(L, -1)) {
        result->setAccuracy(lua_tonumber(L, -1));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "description");
    if (lua_isstring(L, -1)) {
        result->setDescription(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "country_code");
    if (lua_isstring(L, -1)) {
        result->setCountryCode(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "timestamp");
    if (lua_isstring(L, -1)) {
        result->setTimestamp(stringToDateTime(std::string(lua_tostring(L, -1))));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "floor");
    if (lua_isstring(L, -1)) {
        result->setFloor(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "building");
    if (lua_isstring(L, -1)) {
        result->setBuilding(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "room");
    if (lua_isstring(L, -1)) {
        result->setRoom(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "country");
    if (lua_isstring(L, -1)) {
        result->setCountry(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "region");
    if (lua_isstring(L, -1)) {
        result->setRegion(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "uri");
    if (lua_isstring(L, -1)) {
        result->setURI(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "longitude");
    if (lua_isnumber(L, -1)) {
        result->setLongitude(lua_tonumber(L, -1));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "error");
    if (lua_isnumber(L, -1)) {
        result->setError(lua_tonumber(L, -1));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "postal_code");
    if (lua_isstring(L, -1)) {
        result->setPostalCode(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "bearing");
    if (lua_isnumber(L, -1)) {
        result->setBearing(lua_tonumber(L, -1));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "text");
    if (lua_isstring(L, -1)) {
        result->setText(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "datum");
    if (lua_isstring(L, -1)) {
        result->setDatum(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "street");
    if (lua_isstring(L, -1)) {
        result->setStreet(std::string(lua_tostring(L, -1)));
    }
    lua_pop(L, 1);
    lua_getfield(L, -1, "speed");
    if (lua_isnumber(L, -1)) {
        result->setSpeed(lua_tonumber(L, -1));
    }
    lua_pop(L, 1);
    return result;
}

void UserLocationConvertor::doConvertToLua(lua_State* L, std::shared_ptr<UserLocation> payload) {
    lua_createtable(L, 0, 0);
    if (payload->getArea()) {
        lua_pushstring(L, (*payload->getArea()).c_str());
        lua_setfield(L, -2, "area");
    }
    if (payload->getAltitude()) {
        lua_pushnumber(L, (*payload->getAltitude()));
        lua_setfield(L, -2, "altitude");
    }
    if (payload->getLocality()) {
        lua_pushstring(L, (*payload->getLocality()).c_str());
        lua_setfield(L, -2, "locality");
    }
    if (payload->getLatitude()) {
        lua_pushnumber(L, (*payload->getLatitude()));
        lua_setfield(L, -2, "latitude");
    }
    if (payload->getAccuracy()) {
        lua_pushnumber(L, (*payload->getAccuracy()));
        lua_setfield(L, -2, "accuracy");
    }
    if (payload->getDescription()) {
        lua_pushstring(L, (*payload->getDescription()).c_str());
        lua_setfield(L, -2, "description");
    }
    if (payload->getCountryCode()) {
        lua_pushstring(L, (*payload->getCountryCode()).c_str());
        lua_setfield(L, -2, "country_code");
    }
    if (payload->getTimestamp()) {
    lua_pushstring(L, dateTimeToString((*payload->getTimestamp())).c_str());
        lua_setfield(L, -2, "timestamp");
    }
    if (payload->getFloor()) {
        lua_pushstring(L, (*payload->getFloor()).c_str());
        lua_setfield(L, -2, "floor");
    }
    if (payload->getBuilding()) {
        lua_pushstring(L, (*payload->getBuilding()).c_str());
        lua_setfield(L, -2, "building");
    }
    if (payload->getRoom()) {
        lua_pushstring(L, (*payload->getRoom()).c_str());
        lua_setfield(L, -2, "room");
    }
    if (payload->getCountry()) {
        lua_pushstring(L, (*payload->getCountry()).c_str());
        lua_setfield(L, -2, "country");
    }
    if (payload->getRegion()) {
        lua_pushstring(L, (*payload->getRegion()).c_str());
        lua_setfield(L, -2, "region");
    }
    if (payload->getURI()) {
        lua_pushstring(L, (*payload->getURI()).c_str());
        lua_setfield(L, -2, "uri");
    }
    if (payload->getLongitude()) {
        lua_pushnumber(L, (*payload->getLongitude()));
        lua_setfield(L, -2, "longitude");
    }
    if (payload->getError()) {
        lua_pushnumber(L, (*payload->getError()));
        lua_setfield(L, -2, "error");
    }
    if (payload->getPostalCode()) {
        lua_pushstring(L, (*payload->getPostalCode()).c_str());
        lua_setfield(L, -2, "postal_code");
    }
    if (payload->getBearing()) {
        lua_pushnumber(L, (*payload->getBearing()));
        lua_setfield(L, -2, "bearing");
    }
    if (payload->getText()) {
        lua_pushstring(L, (*payload->getText()).c_str());
        lua_setfield(L, -2, "text");
    }
    if (payload->getDatum()) {
        lua_pushstring(L, (*payload->getDatum()).c_str());
        lua_setfield(L, -2, "datum");
    }
    if (payload->getStreet()) {
        lua_pushstring(L, (*payload->getStreet()).c_str());
        lua_setfield(L, -2, "street");
    }
    if (payload->getSpeed()) {
        lua_pushnumber(L, (*payload->getSpeed()));
        lua_setfield(L, -2, "speed");
    }
}

boost::optional<LuaElementConvertor::Documentation> UserLocationConvertor::getDocumentation() const {
    return Documentation(
        "UserLocation",
        "This table has the following fields:\n\n"
        "- `area`: string (Optional)\n"
        "- `altitude`: @{float} (Optional)\n"
        "- `locality`: string (Optional)\n"
        "- `latitude`: @{float} (Optional)\n"
        "- `accuracy`: @{float} (Optional)\n"
        "- `description`: string (Optional)\n"
        "- `country_code`: string (Optional)\n"
        "- `timestamp`: datetime (string) (Optional)\n"
        "- `floor`: string (Optional)\n"
        "- `building`: string (Optional)\n"
        "- `room`: string (Optional)\n"
        "- `country`: string (Optional)\n"
        "- `region`: string (Optional)\n"
        "- `uri`: string (Optional)\n"
        "- `longitude`: @{float} (Optional)\n"
        "- `error`: @{float} (Optional)\n"
        "- `postal_code`: string (Optional)\n"
        "- `bearing`: @{float} (Optional)\n"
        "- `text`: string (Optional)\n"
        "- `datum`: string (Optional)\n"
        "- `street`: string (Optional)\n"
        "- `speed`: @{float} (Optional)\n"
    );
}