diff options
-rw-r--r-- | Sluift/ElementConvertors/FormConvertor.cpp | 36 | ||||
-rw-r--r-- | Sluift/Tests/FormTest.lua | 12 |
2 files changed, 42 insertions, 6 deletions
diff --git a/Sluift/ElementConvertors/FormConvertor.cpp b/Sluift/ElementConvertors/FormConvertor.cpp index 1720037..e44ca3e 100644 --- a/Sluift/ElementConvertors/FormConvertor.cpp +++ b/Sluift/ElementConvertors/FormConvertor.cpp @@ -18,7 +18,6 @@ using namespace Swift; namespace { - // TODO: add __newindex to set a field value int formIndex(lua_State* L) { lua_getfield(L, 1, "fields"); if (lua_type(L, -1) != LUA_TTABLE) { @@ -37,6 +36,32 @@ namespace { return 0; } + int formNewIndex(lua_State* L) { + lua_getfield(L, 1, "fields"); + bool foundField = false; + if (lua_type(L, -1) == LUA_TTABLE) { + for (lua_pushnil(L); lua_next(L, -2) != 0; ) { + lua_getfield(L, -1, "name"); + if (lua_equal(L, -1, 2)) { + lua_pushvalue(L, 3); + lua_setfield(L, -3, "value"); + foundField = true; + lua_pop(L, 3); + break; + } + lua_pop(L, 2); + } + } + lua_pop(L, 1); + + if (!foundField) { + lua_pushvalue(L, 2); + lua_pushvalue(L, 3); + lua_rawset(L, 1); + } + return 0; + } + Lua::Table convertFieldToLua(boost::shared_ptr<FormField> field) { Lua::Table luaField = boost::assign::map_list_of("name", Lua::valueRef(field->getName())); std::string type; @@ -278,9 +303,12 @@ namespace { } Lua::pushValue(L, result); + lua_newtable(L); lua_pushcfunction(L, formIndex); lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, formNewIndex); + lua_setfield(L, -2, "__newindex"); lua_setmetatable(L, -2); } @@ -304,10 +332,12 @@ namespace { field->setDescription(""); form->addField(field); } + form->setType(Form::SubmitType); // Convert back convertFormToLua(L, form); Lua::registerTableToString(L, -1); + return 1; } } @@ -325,7 +355,7 @@ boost::shared_ptr<Form> FormConvertor::doConvertFromLua(lua_State* L) { void FormConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<Form> payload) { convertFormToLua(L, payload); + lua_pushstring(L, "create_submission"); lua_pushcfunction(L, createSubmission); - lua_setfield(L, -2, "create_submission"); - + lua_rawset(L, -3); } diff --git a/Sluift/Tests/FormTest.lua b/Sluift/Tests/FormTest.lua index 813e05c..7b6b4af 100644 --- a/Sluift/Tests/FormTest.lua +++ b/Sluift/Tests/FormTest.lua @@ -44,8 +44,8 @@ example_form = [[ </field> </x>]] -form = sluift.from_xml(example_form)['data'] -print(form) +form = sluift.from_xml(example_form) +--print(form) -- Test form properties assert(form['title'] == 'Bot Configuration') @@ -69,5 +69,11 @@ assert(form['features']['name'] == 'features') assert(form['FORM_TYPE']['value'] == 'jabber:bot') -- Test response form -print(form:create_submission()) +submission = form:create_submission() +assert(#(submission.fields) == 8) +submission['description'] = 'my description' +assert(submission['description']['value'] == 'my description') +submission['type'] = 'cancel' +assert(#(submission.fields) == 8) + --print(sluift.to_xml({type = 'form', data = form})) |