diff options
| author | Remko Tronçon <git@el-tramo.be> | 2013-09-20 20:19:47 (GMT) | 
|---|---|---|
| committer | Remko Tronçon <git@el-tramo.be> | 2013-09-20 20:33:32 (GMT) | 
| commit | 577c0c8fd30ffc0df718a8958eb7b8784ec3b0ac (patch) | |
| tree | 95a7fabe2796cc2c234f0ab6f299ecdb4889d236 | |
| parent | 4a8cf892743284265bcc8bf9c6fbc4747aa86089 (diff) | |
| download | swift-577c0c8fd30ffc0df718a8958eb7b8784ec3b0ac.zip swift-577c0c8fd30ffc0df718a8958eb7b8784ec3b0ac.tar.bz2 | |
Sluift: Add convenient way for filling in forms
Change-Id: Ia265e8659e32f9e7a221d986625c830953a93ddc
| -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})) | 
 Swift
 Swift