Skip to content

Commit

Permalink
feat: add operator.list support for OCaml binding (#3706)
Browse files Browse the repository at this point in the history
feat: add operator.list support for OCaml binding
  • Loading branch information
Young-Flash authored Dec 6, 2023
1 parent 924f44b commit 876b185
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bindings/ocaml/lib/operator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*)

let new_operator = Opendal_core.Operator.operator
let list = Opendal_core.Operator.blocking_list
let stat = Opendal_core.Operator.blocking_stat
let is_exist = Opendal_core.Operator.blocking_is_exist
let create_dir = Opendal_core.Operator.blocking_create_dir
Expand Down Expand Up @@ -53,3 +54,9 @@ module Metadata = struct
let etag = Opendal_core.Operator.metadata_etag
let last_modified = Opendal_core.Operator.metadata_last_modified
end

module Entry = struct
let path = Opendal_core.Operator.entry_path
let name = Opendal_core.Operator.entry_name
let metadata = Opendal_core.Operator.entry_metadata
end
11 changes: 11 additions & 0 deletions bindings/ocaml/lib/operator.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ val new_operator :
@return The block operator
*)

val list :
Opendal_core.Operator.operator ->
string ->
(Opendal_core.Operator.entry array, string) result

val stat :
Opendal_core.Operator.operator ->
string ->
Expand Down Expand Up @@ -177,3 +182,9 @@ module Metadata : sig
val last_modified : Opendal_core.Operator.metadata -> int64 option
(** [last_modified metadata] Last modified of this entry.*)
end

module Entry : sig
val path : Opendal_core.Operator.entry -> string
val name : Opendal_core.Operator.entry -> string
val metadata : Opendal_core.Operator.entry -> Opendal_core.Operator.metadata
end
8 changes: 8 additions & 0 deletions bindings/ocaml/src/operator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ open! Bigarray
type operator
type reader
type metadata
type entry

(* file: entry.rs *)

external entry_path: entry -> string = "entry_path"
external entry_name: entry -> string = "entry_name"
external entry_metadata: entry -> metadata = "entry_metadata"

(* file: metadata.rs *)

Expand All @@ -22,6 +29,7 @@ external metadata_last_modified: metadata -> int64 option = "metadata_last_modi
(* file: mod.rs *)

external operator: string -> (string * string) list -> (operator, string) Result.t = "operator"
external blocking_list: operator -> string -> (entry array, string) Result.t = "blocking_list"
external blocking_stat: operator -> string -> (metadata, string) Result.t = "blocking_stat"
external blocking_is_exist: operator -> string -> (bool, string) Result.t = "blocking_is_exist"
external blocking_create_dir: operator -> string -> (bool, string) Result.t = "blocking_create_dir"
Expand Down
8 changes: 8 additions & 0 deletions bindings/ocaml/src/operator.mli
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ open! Bigarray
type operator
type reader
type metadata
type entry

(* file: entry.rs *)

external entry_path: entry -> string = "entry_path"
external entry_name: entry -> string = "entry_name"
external entry_metadata: entry -> metadata = "entry_metadata"

(* file: metadata.rs *)

Expand All @@ -22,6 +29,7 @@ external metadata_last_modified: metadata -> int64 option = "metadata_last_modi
(* file: mod.rs *)

external operator: string -> (string * string) list -> (operator, string) Result.t = "operator"
external blocking_list: operator -> string -> (entry array, string) Result.t = "blocking_list"
external blocking_stat: operator -> string -> (metadata, string) Result.t = "blocking_stat"
external blocking_is_exist: operator -> string -> (bool, string) Result.t = "blocking_is_exist"
external blocking_create_dir: operator -> string -> (bool, string) Result.t = "blocking_create_dir"
Expand Down
4 changes: 4 additions & 0 deletions bindings/ocaml/src/operator/_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ ocaml::custom!(Reader);
#[ocaml::sig]
pub struct Metadata(pub(crate) od::Metadata);
ocaml::custom!(Metadata);

#[ocaml::sig]
pub struct Entry(pub(crate) od::Entry);
ocaml::custom!(Entry);
36 changes: 36 additions & 0 deletions bindings/ocaml/src/operator/entry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

use super::*;

#[ocaml::func]
#[ocaml::sig("entry -> string ")]
pub fn entry_path(entry: &mut Entry) -> String {
entry.0.path().to_string()
}

#[ocaml::func]
#[ocaml::sig("entry -> string ")]
pub fn entry_name(entry: &mut Entry) -> String {
entry.0.name().to_string()
}

#[ocaml::func]
#[ocaml::sig("entry -> metadata ")]
pub fn entry_metadata(entry: &mut Entry) -> ocaml::Pointer<Metadata> {
Metadata(entry.0.metadata().clone()).into()
}
15 changes: 15 additions & 0 deletions bindings/ocaml/src/operator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.

mod _type;
mod entry;
mod metadata;
mod reader;

Expand All @@ -33,6 +34,20 @@ pub fn operator(
Ok(Operator(op.blocking()).into())
}

#[ocaml::func]
#[ocaml::sig("operator -> string -> (entry array, string) Result.t ")]
pub fn blocking_list(
operator: &mut Operator,
path: String,
) -> Result<Vec<ocaml::Pointer<Entry>>, String> {
map_res_error(
operator
.0
.list(path.as_str())
.map(|m| m.into_iter().map(|it| Entry(it).into()).collect()),
)
}

#[ocaml::func]
#[ocaml::sig("operator -> string -> (metadata, string) Result.t ")]
pub fn blocking_stat(
Expand Down
18 changes: 18 additions & 0 deletions bindings/ocaml/test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,23 @@ let test_operator_stat test_ctxt =
assert_equal 10L (Operator.Metadata.content_length metadata);
()

let test_list test_ctxt =
let bo = new_test_block_operator test_ctxt in
ignore (test_check_result (Operator.create_dir bo "/testdir/"));
ignore
(test_check_result
(Operator.write bo "/testdir/foo" (Bytes.of_string "bar")));
ignore
(test_check_result
(Operator.write bo "/testdir/bar" (Bytes.of_string "foo")));
let array = Operator.list bo "testdir/" |> test_check_result in
let names = Array.map Operator.Entry.name array in
let expected = [| "foo"; "bar" |] in
Array.sort compare expected;
assert_equal expected names;
assert_equal 2 (Array.length array);
()

let suite =
"suite"
>::: [
Expand All @@ -91,6 +108,7 @@ let suite =
"test_copy_and_read" >:: test_copy_and_read;
"test_operator_reader" >:: test_operator_reader;
"test_operator_stat" >:: test_operator_stat;
"test_list" >:: test_list;
]

let () = run_test_tt_main suite

0 comments on commit 876b185

Please sign in to comment.