lua: properly handle corner cases in changes
[project/uci.git] / lua / uci.c
index aaa5828..0667fc2 100644 (file)
--- a/lua/uci.c
+++ b/lua/uci.c
@@ -691,6 +691,7 @@ uci_lua_add_change(lua_State *L, struct uci_element *e)
 {
        struct uci_delta *h;
        const char *name;
+       const char *value;
 
        h = uci_to_delta(e);
        if (!h->section)
@@ -704,12 +705,52 @@ uci_lua_add_change(lua_State *L, struct uci_element *e)
                lua_setfield(L, -3, h->section);
        }
 
-       name = (h->e.name ? h->e.name : ".type");
-       if (h->value)
-               lua_pushstring(L, h->value);
-       else
-               lua_pushstring(L, "");
-       lua_setfield(L, -2, name);
+       name = h->e.name;
+       value = h->value ? h->value : "";
+
+       if (name) {
+               lua_getfield(L, -1, name);
+
+               /* this delta is a list add operation */
+               if (h->cmd == UCI_CMD_LIST_ADD) {
+                       /* there seems to be no table yet */
+                       if (!lua_istable(L, -1)) {
+                               lua_newtable(L);
+
+                               /* if there is a value on the stack already, add */
+                               if (!lua_isnil(L, -2)) {
+                                       lua_pushvalue(L, -2);
+                                       lua_rawseti(L, -2, 1);
+                                       lua_pushstring(L, value);
+                                       lua_rawseti(L, -2, 2);
+
+                               /* this is the first table item */
+                               } else {
+                                       lua_pushstring(L, value);
+                                       lua_rawseti(L, -2, 1);
+                               }
+
+                               lua_setfield(L, -3, name);
+
+                       /* a table is on the top of the stack and this is a subsequent,
+                        * list_add, append this value to table */
+                       } else {
+                               lua_pushstring(L, value);
+                               lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
+                       }
+
+               /* non-list change, simply set/replace field */
+               } else {
+                       lua_pushstring(L, value);
+                       lua_setfield(L, -3, name);
+               }
+
+               lua_pop(L, 1);
+       } else {
+               lua_pushstring(L, value);
+               lua_setfield(L, -2, ".type");
+       }
+
        lua_pop(L, 1);
 }