From 7078c2bda66e578e14648d535f7d10559bac6d10 Mon Sep 17 00:00:00 2001 From: oowl Date: Thu, 29 Jun 2023 12:58:36 +0800 Subject: [PATCH] feat(binding/lua): add rename and create_dir operator function (#2564) * feat(binding/lua): add rename and create_dir operator function Signed-off-by: owl * feat(binding/lua): fix code Signed-off-by: owl * feat(binding/lua): fix code Signed-off-by: owl * feat(binding/lua): fix code Signed-off-by: owl --------- Signed-off-by: owl --- bindings/lua/src/lib.rs | 27 +++++++++++++++++++++++++++ bindings/lua/src/operator_doc.lua | 15 +++++++++++++++ bindings/lua/test/opendal_test.lua | 18 ++++++++++++++---- 3 files changed, 56 insertions(+), 4 deletions(-) diff --git a/bindings/lua/src/lib.rs b/bindings/lua/src/lib.rs index 1a198602ba52..6a48b5dcf589 100644 --- a/bindings/lua/src/lib.rs +++ b/bindings/lua/src/lib.rs @@ -69,6 +69,8 @@ fn operator_new<'a>( operator.set("write", lua.create_function(operator_write)?)?; operator.set("delete", lua.create_function(operator_delete)?)?; operator.set("is_exist", lua.create_function(operator_is_exist)?)?; + operator.set("create_dir", lua.create_function(operator_create_dir)?)?; + operator.set("rename", lua.create_function(operator_rename)?)?; operator.set("stat", lua.create_function(operator_stat)?)?; Ok(operator) } @@ -124,6 +126,31 @@ fn operator_write<'a>( } } +fn operator_create_dir<'a>(_: &'a Lua, (operator, path): (LuaTable<'a>, String)) -> LuaResult<()> { + let op = operator.get::<_, ODOperator>("_operator")?; + let op = op.operator; + + let res = op.create_dir(path.as_str()); + match res { + Ok(_) => Ok(()), + Err(e) => Err(LuaError::external(e)), + } +} + +fn operator_rename<'a>( + _: &'a Lua, + (operator, src, dst): (LuaTable<'a>, String, String), +) -> LuaResult<()> { + let op = operator.get::<_, ODOperator>("_operator")?; + let op = op.operator; + + let res = op.rename(&src, &dst); + match res { + Ok(_) => Ok(()), + Err(e) => Err(LuaError::external(e)), + } +} + fn operator_read<'a>( lua: &'a Lua, (operator, path): (LuaTable<'a>, String), diff --git a/bindings/lua/src/operator_doc.lua b/bindings/lua/src/operator_doc.lua index f863cc4f4926..d3ee1413fec0 100644 --- a/bindings/lua/src/operator_doc.lua +++ b/bindings/lua/src/operator_doc.lua @@ -55,6 +55,21 @@ local _M = {} -- @return error error nil if success, otherwise error message -- @function delete +--- Blockingly rename the object of src to dst. +--- Rename the object in path blockingly, returns error nil +--- if success, others otherwise +-- @param string src the designated path you want to rename your source path +-- @param string dst the designated path you want to rename your destination path +-- @return error error nil if success, otherwise error message +-- @function rename + +--- Blockingly create the object in path. +--- Create directory the object in path blockingly, returns error nil +--- if success, others otherwise +-- @param string path the designated path you want to create your directory +-- @return error error nil if success, otherwise error message +-- @function create_dir + --- Check whether the path exists. -- @param string path the designated path you want to write your delete -- @return bool, error true or false depend on operator instance and path, error nil if success, otherwise error message diff --git a/bindings/lua/test/opendal_test.lua b/bindings/lua/test/opendal_test.lua index c02337ed30ae..9d7b2e5e0732 100644 --- a/bindings/lua/test/opendal_test.lua +++ b/bindings/lua/test/opendal_test.lua @@ -29,9 +29,16 @@ describe("opendal unit test", function() local res, err = op:read("test.txt") assert.is_nil(err) assert.are.equal(res, "hello world") - assert.equal(op:is_exist("test.txt"), true) - assert.is_nil(op:delete("test.txt")) + assert.is_nil(op:delete("test_dir/")) + assert.is_nil(op:delete("test_dir_1/")) + assert.is_nil(op:create_dir("test_dir/")) + assert.equal(op:is_exist("test_dir/"), true) + assert.is_nil(op:rename("test.txt", "test_1.txt")) + assert.equal(op:is_exist("test_1.txt"), true) assert.equal(op:is_exist("test.txt"), false) + assert.equal(op:is_exist("test_1.txt"), true) + assert.is_nil(op:delete("test_1.txt")) + assert.equal(op:is_exist("test_1.txt"), false) end) it("meta function in fs schema", function() local opendal = require("opendal") @@ -60,9 +67,12 @@ describe("opendal unit test", function() local res, err = op:read("test.txt") assert.is_nil(err) assert.are.equal(res, "hello world") - assert.equal(op:is_exist("test.txt"), true) - assert.is_nil(op:delete("test.txt")) + assert.is_nil(op:rename("test.txt", "test_1.txt")) + assert.equal(op:is_exist("test_1.txt"), true) assert.equal(op:is_exist("test.txt"), false) + assert.equal(op:is_exist("test_1.txt"), true) + assert.is_nil(op:delete("test_1.txt")) + assert.equal(op:is_exist("test_1.txt"), false) end) it("meta function in memory schema", function() local opendal = require("opendal")