Skip to content

Commit

Permalink
feat(binding/lua): add rename and create_dir operator function (#2564)
Browse files Browse the repository at this point in the history
* feat(binding/lua): add rename and create_dir operator function

Signed-off-by: owl <[email protected]>

* feat(binding/lua): fix code

Signed-off-by: owl <[email protected]>

* feat(binding/lua): fix code

Signed-off-by: owl <[email protected]>

* feat(binding/lua): fix code

Signed-off-by: owl <[email protected]>

---------

Signed-off-by: owl <[email protected]>
  • Loading branch information
oowl authored Jun 29, 2023
1 parent 01feba4 commit 7078c2b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
27 changes: 27 additions & 0 deletions bindings/lua/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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),
Expand Down
15 changes: 15 additions & 0 deletions bindings/lua/src/operator_doc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions bindings/lua/test/opendal_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 7078c2b

Please sign in to comment.