summaryrefslogtreecommitdiffstats
blob: 4ebd6a33384015f9da1285c3df3bc7dfaaefdf83 (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
/*
 * Copyright (c) 2013-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Sluift/ElementConvertors/DiscoInfoConvertor.h>

#include <boost/numeric/conversion/cast.hpp>
#include <boost/smart_ptr/make_shared.hpp>

#include <lua.hpp>

#include <Sluift/Lua/LuaUtils.h>

using namespace Swift;

DiscoInfoConvertor::DiscoInfoConvertor() : GenericLuaElementConvertor<DiscoInfo>("disco_info") {
}

DiscoInfoConvertor::~DiscoInfoConvertor() {
}

boost::shared_ptr<DiscoInfo> DiscoInfoConvertor::doConvertFromLua(lua_State* L) {
	boost::shared_ptr<DiscoInfo> result = boost::make_shared<DiscoInfo>();
	if (boost::optional<std::string> value = Lua::getStringField(L, -1, "node")) {
		result->setNode(*value);
	}

	lua_getfield(L, -1, "identities");
	if (lua_istable(L, -1)) {
		for (lua_pushnil(L); lua_next(L, -2); ) {
			result->addIdentity(DiscoInfo::Identity(
					Lua::getStringField(L, -1, "name").get_value_or(""),
					Lua::getStringField(L, -1, "category").get_value_or("client"),
					Lua::getStringField(L, -1, "type").get_value_or("pc"),
					Lua::getStringField(L, -1, "language").get_value_or("")));
			lua_pop(L, 1);
		}
	}
	lua_pop(L, 1);

	lua_getfield(L, -1, "features");
	if (lua_istable(L, -1)) {
		for (lua_pushnil(L); lua_next(L, -2); ) {
			if (lua_isstring(L, -1)) {
				result->addFeature(lua_tostring(L, -1));
			}
			lua_pop(L, 1);
		}
	}
	lua_pop(L, 1);

	// TODO: Extension

	return result;
}

void DiscoInfoConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<DiscoInfo> payload) {
	lua_newtable(L);
	if (!payload->getNode().empty()) {
		lua_pushstring(L, payload->getNode().c_str());
		lua_setfield(L, -2, "node");
	}

	const std::vector<DiscoInfo::Identity>& identities = payload->getIdentities();
	if (!identities.empty()) {
		lua_createtable(L, boost::numeric_cast<int>(identities.size()), 0);
		for (size_t i = 0; i < identities.size(); ++i) {
			lua_createtable(L, 0, 0);
			if (!identities[i].getName().empty()) {
				lua_pushstring(L, identities[i].getName().c_str());
				lua_setfield(L, -2, "name");
			}
			if (!identities[i].getCategory().empty()) {
				lua_pushstring(L, identities[i].getCategory().c_str());
				lua_setfield(L, -2, "category");
			}
			if (!identities[i].getType().empty()) {
				lua_pushstring(L, identities[i].getType().c_str());
				lua_setfield(L, -2, "type");
			}
			if (!identities[i].getLanguage().empty()) {
				lua_pushstring(L, identities[i].getLanguage().c_str());
				lua_setfield(L, -2, "language");
			}
			lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
		}
		lua_setfield(L, -2, "identities");
	}

	const std::vector<std::string>& features = payload->getFeatures();
	if (!features.empty()) {
		lua_createtable(L, boost::numeric_cast<int>(features.size()), 0);
		for (size_t i = 0; i < features.size(); ++i) {
			lua_pushstring(L, features[i].c_str());
			lua_rawseti(L, -2, boost::numeric_cast<int>(i+1));
		}
		lua_setfield(L, -2, "features");
	}
	
	// TODO: Extension
}

boost::optional<LuaElementConvertor::Documentation> DiscoInfoConvertor::getDocumentation() const {
	return Documentation(
			"DiscoInfo", 
			"Represents `disco#info` service discovery data.\n\n"
			"This table has the following structure:\n\n"
			"- `node`: string\n"
			"- `identities`: array(table)\n"
			"  - `name`: string\n"
			"  - `category`: string\n"
			"  - `type`: string\n"
			"  - `language`: string\n"
			"- `features`: array(string)\n"
	);
}