]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
sysrepo-lua: implemented bidirectional binding
authorVasek Sraier <git@vakabus.cz>
Wed, 11 Mar 2020 15:08:46 +0000 (16:08 +0100)
committerAleš <ales.mrazek@nic.cz>
Mon, 20 Jul 2020 14:54:40 +0000 (16:54 +0200)
modules/sysrepo-lua/model.lua

index 52b38460d50f53122aae3151a493c2c07e1f3b39..1e00d2ba5e0870092d0ea347889f6c52d4a58e56 100644 (file)
@@ -124,7 +124,9 @@ local function ContainerNode(name, container_model)
     return Node:create(name, handle_cont_read, handle_cont_write, schema_init)
 end
 
-local function StateNode(name, get_val)
+local function StateNode(name, type, bind_variable)
+    local get_val = load("return " .. bind_variable)
+
     --- Node's apply function
     local function handle_apply(self, node)
         -- do nothing, it's only a state node
@@ -138,16 +140,38 @@ local function StateNode(name, get_val)
     return Node:create(name, handle_apply, handle_serialize, nil)
 end
 
+local function ConfigFnNode(name, type, bind_variable)
+    local get_val = load("return " .. bind_variable .. "()")
+    local set_val = load("return function(data) " .. bind_variable .. "(data) end")()
+
+    --- Node's apply function
+    local function handle_apply(self, node)
+        local val = nil
+        if type == "uint32" or type == "uint64" then
+            val = tonumber(ffi.string(clib().node_get_value_str(node)))
+        else
+            assert(false, "Trying to serialize unknown type")
+        end
+        set_val(val)
+    end
+
+    --- Node's serialize function
+    local function handle_serialize(self, parent_node)
+        return clib().node_new_leaf(parent_node, self.module, self.name, tostring(get_val()))
+    end
+
+    return Node:create(name, handle_apply, handle_serialize, nil)
+end
+
 
 --- Configuration schema reprezentation
 local model = 
     ContainerNode("dns-resolver", {
         ContainerNode("cache", {
-            StateNode("current-size", function() return cache.current_size end),
+            StateNode("current-size", "uint64", "cache.current_size"),
             DummyLeafNode("max-size"),
-            DummyLeafNode("max-ttl"),
-            DummyLeafNode("min-ttl"),
-            DummyLeafNode("prefill"),
+            ConfigFnNode("max-ttl", "uint32", "cache.max_ttl"),
+            ConfigFnNode("min-ttl", "uint32", "cache.min_ttl"),
         }),
         DummyLeafNode("debugging", true),
         DummyLeafNode("dns64", true),