]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hlua_fcn: add Patref:add()
authorAurelien DARRAGON <adarragon@haproxy.com>
Tue, 26 Nov 2024 07:38:23 +0000 (08:38 +0100)
committerAurelien DARRAGON <adarragon@haproxy.com>
Fri, 29 Nov 2024 06:23:32 +0000 (07:23 +0100)
Just like "add map" and "add acl" on the cli, the Patref:add() method can
be used to add a new entry to the pattern reference pointed to by the
Lua Patref object. The update will target the live pattern reference
version, unless Patref:prepare() is ongoing.

doc/lua-api/index.rst
src/hlua_fcn.c

index b2bda2da18016cc15746b14dea43763af44fa349..111954446d3ba4b99f5d18823ba234a750315b58 100644 (file)
@@ -3485,6 +3485,19 @@ Patref class
   Drop the pending patref version created using Patref:prepare(): get back to
   live dataset.
 
+.. js:function:: Patref.add(ref, key[, value])
+
+  Add a new key to the pattern reference, with associated value for maps.
+
+  :param string key: the string used as a key
+  :param string value: the string used as value to be associated with the key
+   (only relevant for maps)
+  :returns: true on success and nil on failure (followed by an error message).
+
+  .. Note::
+     Affects the live pattern reference version, unless :js:func:`Patref.prepare()`
+     was called and is still ongoing (waiting for commit or giveup)
+
 .. _applethttp_class:
 
 AppletHTTP class
index d44d63733755d4a03bfcdbe3eae70d853c95ac87..d111bc44e39440b5a770791e6c7b6e74e258b3b7 100644 (file)
@@ -2739,6 +2739,40 @@ int hlua_patref_purge(lua_State *L)
        return _hlua_patref_clear(L, LUA_OK, 0);
 }
 
+int hlua_patref_add(lua_State *L)
+{
+       struct hlua_patref *ref;
+       const char *key;
+       const char *value = NULL;
+       char *errmsg = NULL;
+       int ret;
+
+       ref = hlua_checkudata(L, 1, class_patref_ref);
+
+       BUG_ON(!ref);
+
+       key = luaL_checkstring(L, 2);
+       if (lua_gettop(L) == 3)
+               value = luaL_checkstring(L, 3);
+
+       HA_RWLOCK_WRLOCK(PATREF_LOCK, &ref->ptr->lock);
+       if ((ref->flags & HLUA_PATREF_FL_GEN) &&
+           pat_ref_may_commit(ref->ptr, ref->curr_gen))
+               ret = !!pat_ref_load(ref->ptr, ref->curr_gen, key, value, -1, &errmsg);
+       else
+               ret = pat_ref_add(ref->ptr, key, value, &errmsg);
+       HA_RWLOCK_WRUNLOCK(PATREF_LOCK, &ref->ptr->lock);
+
+
+       if (!ret) {
+               ret = hlua_error(L, errmsg);
+               ha_free(&errmsg);
+               return ret;
+       }
+       lua_pushboolean(L, 1);
+       return 1;
+}
+
 void hlua_fcn_new_patref(lua_State *L, struct pat_ref *ref)
 {
        struct hlua_patref *_ref;
@@ -2770,6 +2804,7 @@ void hlua_fcn_new_patref(lua_State *L, struct pat_ref *ref)
        hlua_class_function(L, "commit", hlua_patref_commit);
        hlua_class_function(L, "giveup", hlua_patref_giveup);
        hlua_class_function(L, "purge", hlua_patref_purge);
+       hlua_class_function(L, "add", hlua_patref_add);
 }
 
 int hlua_patref_gc(lua_State *L)