This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new 92d6dc4f7e feat(bindings/ocaml): generate Metadata from core (#9132)
92d6dc4f7e is described below
commit 92d6dc4f7e61ede1f5b2ee92efa8f6aba5d68c5a
Author: Chitral Verma <[email protected]>
AuthorDate: Fri Nov 27 17:59:28 2026 +0530
feat(bindings/ocaml): generate Metadata from core (#9132)
* feat(bindings/ocaml): generate Metadata from core
Part of #9129.
Signed-off-by: Chitral Verma <[email protected]>
* test(bindings/ocaml): freeze the released Metadata surface
Add a compile-time check that Operator.Metadata still provides the
accessors the hand-written module exposed, with their exact types, and
document in upgrade.md the consumer patterns that the new accessors
break.
Signed-off-by: Chitral Verma <[email protected]>
---------
Signed-off-by: Chitral Verma <[email protected]>
Co-authored-by: Chitral Verma <[email protected]>
---
.github/workflows/ci_bindings_ocaml.yml | 1 +
bindings/ocaml/.ocamlformat-ignore | 2 +
bindings/ocaml/lib/metadata.ml | 40 +++++++++
bindings/ocaml/lib/metadata.mli | 80 +++++++++++++++++
bindings/ocaml/lib/operator.ml | 11 +--
bindings/ocaml/lib/operator.mli | 44 +---------
bindings/ocaml/src/operator.ml | 13 ++-
bindings/ocaml/src/operator.mli | 13 ++-
bindings/ocaml/src/operator/metadata.rs | 111 ++++++++++++++++++++---
bindings/ocaml/test/test.ml | 39 ++++++++-
bindings/ocaml/upgrade.md | 45 ++++++++++
dev/src/generate/ocaml.rs | 151 ++++++++++++++++++++++++++++++--
dev/src/generate/ocaml/metadata_ml.j2 | 26 ++++++
dev/src/generate/ocaml/metadata_mli.j2 | 35 ++++++++
dev/src/generate/ocaml/metadata_rs.j2 | 47 ++++++++++
15 files changed, 583 insertions(+), 75 deletions(-)
diff --git a/.github/workflows/ci_bindings_ocaml.yml
b/.github/workflows/ci_bindings_ocaml.yml
index 5b978a52ec..b266ed371b 100644
--- a/.github/workflows/ci_bindings_ocaml.yml
+++ b/.github/workflows/ci_bindings_ocaml.yml
@@ -26,6 +26,7 @@ on:
- main
paths:
- "core/core/src/types/capability.rs"
+ - "core/core/src/types/metadata.rs"
- "dev/src/generate/**"
- "bindings/ocaml/**"
- ".github/actions/setup-ocaml/**"
diff --git a/bindings/ocaml/.ocamlformat-ignore
b/bindings/ocaml/.ocamlformat-ignore
index 1f5099ece8..6895638f70 100644
--- a/bindings/ocaml/.ocamlformat-ignore
+++ b/bindings/ocaml/.ocamlformat-ignore
@@ -1,3 +1,5 @@
src/**
lib/capability.ml
lib/capability.mli
+lib/metadata.ml
+lib/metadata.mli
diff --git a/bindings/ocaml/lib/metadata.ml b/bindings/ocaml/lib/metadata.ml
new file mode 100644
index 0000000000..fe5ddc57c0
--- /dev/null
+++ b/bindings/ocaml/lib/metadata.ml
@@ -0,0 +1,40 @@
+(*
+ 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.
+*)
+
+(* DO NOT EDIT IT MANUALLY. This file is generated by
+ opendal/dev/src/generate/ocaml.rs. *)
+
+type entry_mode = Opendal_core.Operator.entry_mode = File | Dir | Unknown
+
+let mode = Opendal_core.Operator.metadata_mode
+let is_file = Opendal_core.Operator.metadata_is_file
+let is_dir = Opendal_core.Operator.metadata_is_dir
+let is_current = Opendal_core.Operator.metadata_is_current
+let is_deleted = Opendal_core.Operator.metadata_is_deleted
+let cache_control = Opendal_core.Operator.metadata_cache_control
+let content_length = Opendal_core.Operator.metadata_content_length
+let content_md5 = Opendal_core.Operator.metadata_content_md5
+let content_type = Opendal_core.Operator.metadata_content_type
+let content_encoding = Opendal_core.Operator.metadata_content_encoding
+let content_language = Opendal_core.Operator.metadata_content_language
+let last_modified = Opendal_core.Operator.metadata_last_modified
+let etag = Opendal_core.Operator.metadata_etag
+let content_disposition = Opendal_core.Operator.metadata_content_disposition
+let version = Opendal_core.Operator.metadata_version
+let user_metadata = Opendal_core.Operator.metadata_user_metadata
diff --git a/bindings/ocaml/lib/metadata.mli b/bindings/ocaml/lib/metadata.mli
new file mode 100644
index 0000000000..6abd7d49f4
--- /dev/null
+++ b/bindings/ocaml/lib/metadata.mli
@@ -0,0 +1,80 @@
+(*
+ 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.
+*)
+
+(* DO NOT EDIT IT MANUALLY. This file is generated by
+ opendal/dev/src/generate/ocaml.rs. *)
+
+(** Accessors for the metadata of a file or directory.
+
+ Optional values are [None] when the service did not report them. *)
+
+(** The mode of an entry. *)
+type entry_mode = Opendal_core.Operator.entry_mode =
+ | File (** The path has data to read. *)
+ | Dir (** The path can be listed. *)
+ | Unknown (** OpenDAL does not know what can be done on the path. *)
+
+val mode : Opendal_core.Operator.metadata -> entry_mode
+(** Return this entry's mode. *)
+
+val is_file : Opendal_core.Operator.metadata -> bool
+(** Return true if this metadata is for a file. *)
+
+val is_dir : Opendal_core.Operator.metadata -> bool
+(** Return true if this metadata is for a directory. *)
+
+val is_current : Opendal_core.Operator.metadata -> bool option
+(** Return whether this metadata describes the current object version. *)
+
+val is_deleted : Opendal_core.Operator.metadata -> bool
+(** Return whether this metadata describes a deleted object or delete marker.
*)
+
+val cache_control : Opendal_core.Operator.metadata -> string option
+(** Return this entry's Cache-Control value. *)
+
+val content_length : Opendal_core.Operator.metadata -> int64
+(** Return the full content length of this entry. *)
+
+val content_md5 : Opendal_core.Operator.metadata -> string option
+(** Return this entry's Content-MD5 value. *)
+
+val content_type : Opendal_core.Operator.metadata -> string option
+(** Return this entry's Content-Type value. *)
+
+val content_encoding : Opendal_core.Operator.metadata -> string option
+(** Return this entry's Content-Encoding value. *)
+
+val content_language : Opendal_core.Operator.metadata -> string option
+(** Return this entry's Content-Language value, if available. *)
+
+val last_modified : Opendal_core.Operator.metadata -> int64 option
+(** Return this entry's Last-Modified timestamp. The value is in seconds since
the Unix epoch. *)
+
+val etag : Opendal_core.Operator.metadata -> string option
+(** Return this entry's service-provided ETag as-is. *)
+
+val content_disposition : Opendal_core.Operator.metadata -> string option
+(** Return this entry's Content-Disposition value. *)
+
+val version : Opendal_core.Operator.metadata -> string option
+(** Return this entry's service-specific version identifier. *)
+
+val user_metadata : Opendal_core.Operator.metadata -> (string * string) list
option
+(** Return a borrowed view of this entry's user metadata. The pairs are copied
into an association list. *)
+
diff --git a/bindings/ocaml/lib/operator.ml b/bindings/ocaml/lib/operator.ml
index d522cde980..a1bea3cfce 100644
--- a/bindings/ocaml/lib/operator.ml
+++ b/bindings/ocaml/lib/operator.ml
@@ -53,17 +53,8 @@ module Lister = struct
let next = Opendal_core.Operator.lister_next
end
+module Metadata = Metadata
(** Metadata module for file/directory information *)
-module Metadata = struct
- let is_file = Opendal_core.Operator.metadata_is_file
- let is_dir = Opendal_core.Operator.metadata_is_dir
- let content_length = Opendal_core.Operator.metadata_content_length
- let content_md5 = Opendal_core.Operator.metadata_content_md5
- let content_type = Opendal_core.Operator.metadata_content_type
- let content_disposition = Opendal_core.Operator.metadata_content_disposition
- let etag = Opendal_core.Operator.metadata_etag
- let last_modified = Opendal_core.Operator.metadata_last_modified
-end
(** Entry module for directory entry information *)
module Entry = struct
diff --git a/bindings/ocaml/lib/operator.mli b/bindings/ocaml/lib/operator.mli
index 1b75816a8d..e40b3677ef 100644
--- a/bindings/ocaml/lib/operator.mli
+++ b/bindings/ocaml/lib/operator.mli
@@ -480,52 +480,10 @@ module Lister : sig
]} *)
end
+module Metadata : module type of Metadata
(** {2 Metadata Operations}
Module for accessing file and directory metadata. *)
-module Metadata : sig
- val is_file : Opendal_core.Operator.metadata -> bool
- (** [is_file metadata] checks if the metadata represents a file.
-
- Example:
- {[
- if Metadata.is_file meta then print_endline "It's a file"
- ]} *)
-
- val is_dir : Opendal_core.Operator.metadata -> bool
- (** [is_dir metadata] checks if the metadata represents a directory.
-
- Example:
- {[
- if Metadata.is_dir meta then print_endline "It's a directory"
- ]} *)
-
- val content_length : Opendal_core.Operator.metadata -> int64
- (** [content_length metadata] gets the size of the content in bytes.
-
- Example:
- {[
- printf "File size: %Ld bytes\n" (Metadata.content_length meta)
- ]} *)
-
- val content_md5 : Opendal_core.Operator.metadata -> string option
- (** [content_md5 metadata] gets the MD5 hash of the content, if available. *)
-
- val content_type : Opendal_core.Operator.metadata -> string option
- (** [content_type metadata] gets the MIME type of the content, if available.
- *)
-
- val content_disposition : Opendal_core.Operator.metadata -> string option
- (** [content_disposition metadata] gets the Content-Disposition header, if
- available. *)
-
- val etag : Opendal_core.Operator.metadata -> string option
- (** [etag metadata] gets the ETag of the content, if available. *)
-
- val last_modified : Opendal_core.Operator.metadata -> int64 option
- (** [last_modified metadata] gets the last modification time as Unix
- timestamp, if available. *)
-end
(** {2 Entry Operations}
diff --git a/bindings/ocaml/src/operator.ml b/bindings/ocaml/src/operator.ml
index e18729a17e..eca0ce2214 100644
--- a/bindings/ocaml/src/operator.ml
+++ b/bindings/ocaml/src/operator.ml
@@ -127,14 +127,23 @@ external lister_next: lister -> (entry option, string)
Result.t = "lister_next"
(* file: metadata.rs *)
+type entry_mode = File | Dir | Unknown
+external metadata_mode: metadata -> entry_mode = "metadata_mode"
external metadata_is_file: metadata -> bool = "metadata_is_file"
external metadata_is_dir: metadata -> bool = "metadata_is_dir"
+external metadata_is_current: metadata -> bool option = "metadata_is_current"
+external metadata_is_deleted: metadata -> bool = "metadata_is_deleted"
+external metadata_cache_control: metadata -> string option =
"metadata_cache_control"
external metadata_content_length: metadata -> int64 =
"metadata_content_length"
external metadata_content_md5: metadata -> string option =
"metadata_content_md5"
external metadata_content_type: metadata -> string option =
"metadata_content_type"
-external metadata_content_disposition: metadata -> string option =
"metadata_content_disposition"
-external metadata_etag: metadata -> string option = "metadata_etag"
+external metadata_content_encoding: metadata -> string option =
"metadata_content_encoding"
+external metadata_content_language: metadata -> string option =
"metadata_content_language"
external metadata_last_modified: metadata -> int64 option =
"metadata_last_modified"
+external metadata_etag: metadata -> string option = "metadata_etag"
+external metadata_content_disposition: metadata -> string option =
"metadata_content_disposition"
+external metadata_version: metadata -> string option = "metadata_version"
+external metadata_user_metadata: metadata -> (string * string) list option =
"metadata_user_metadata"
(* file: mod.rs *)
diff --git a/bindings/ocaml/src/operator.mli b/bindings/ocaml/src/operator.mli
index e18729a17e..eca0ce2214 100644
--- a/bindings/ocaml/src/operator.mli
+++ b/bindings/ocaml/src/operator.mli
@@ -127,14 +127,23 @@ external lister_next: lister -> (entry option, string)
Result.t = "lister_next"
(* file: metadata.rs *)
+type entry_mode = File | Dir | Unknown
+external metadata_mode: metadata -> entry_mode = "metadata_mode"
external metadata_is_file: metadata -> bool = "metadata_is_file"
external metadata_is_dir: metadata -> bool = "metadata_is_dir"
+external metadata_is_current: metadata -> bool option = "metadata_is_current"
+external metadata_is_deleted: metadata -> bool = "metadata_is_deleted"
+external metadata_cache_control: metadata -> string option =
"metadata_cache_control"
external metadata_content_length: metadata -> int64 =
"metadata_content_length"
external metadata_content_md5: metadata -> string option =
"metadata_content_md5"
external metadata_content_type: metadata -> string option =
"metadata_content_type"
-external metadata_content_disposition: metadata -> string option =
"metadata_content_disposition"
-external metadata_etag: metadata -> string option = "metadata_etag"
+external metadata_content_encoding: metadata -> string option =
"metadata_content_encoding"
+external metadata_content_language: metadata -> string option =
"metadata_content_language"
external metadata_last_modified: metadata -> int64 option =
"metadata_last_modified"
+external metadata_etag: metadata -> string option = "metadata_etag"
+external metadata_content_disposition: metadata -> string option =
"metadata_content_disposition"
+external metadata_version: metadata -> string option = "metadata_version"
+external metadata_user_metadata: metadata -> (string * string) list option =
"metadata_user_metadata"
(* file: mod.rs *)
diff --git a/bindings/ocaml/src/operator/metadata.rs
b/bindings/ocaml/src/operator/metadata.rs
index c7cd3d3c7f..b706b96375 100644
--- a/bindings/ocaml/src/operator/metadata.rs
+++ b/bindings/ocaml/src/operator/metadata.rs
@@ -15,55 +15,146 @@
// specific language governing permissions and limitations
// under the License.
+// DO NOT EDIT IT MANUALLY. This file is generated by
opendal/dev/src/generate/ocaml.rs.
+
use super::*;
+/// The mode of an entry, mirroring core `EntryMode`.
+#[derive(ocaml::ToValue, Clone, Copy)]
+#[ocaml::sig("File | Dir | Unknown")]
+pub enum EntryMode {
+ File,
+ Dir,
+ Unknown,
+}
+
+impl From<od::EntryMode> for EntryMode {
+ fn from(mode: od::EntryMode) -> Self {
+ match mode {
+ od::EntryMode::FILE => EntryMode::File,
+ od::EntryMode::DIR => EntryMode::Dir,
+ od::EntryMode::Unknown => EntryMode::Unknown,
+ }
+ }
+}
+
+/// Return this entry's mode.
+#[ocaml::func]
+#[ocaml::sig("metadata -> entry_mode ")]
+pub fn metadata_mode(metadata: &Metadata) -> EntryMode {
+ metadata.0.mode().into()
+}
+
+/// Return true if this metadata is for a file.
#[ocaml::func]
#[ocaml::sig("metadata -> bool ")]
-pub fn metadata_is_file(metadata: &mut Metadata) -> bool {
+pub fn metadata_is_file(metadata: &Metadata) -> bool {
metadata.0.is_file()
}
+/// Return true if this metadata is for a directory.
#[ocaml::func]
#[ocaml::sig("metadata -> bool ")]
-pub fn metadata_is_dir(metadata: &mut Metadata) -> bool {
+pub fn metadata_is_dir(metadata: &Metadata) -> bool {
metadata.0.is_dir()
}
+/// Return whether this metadata describes the current object version.
+#[ocaml::func]
+#[ocaml::sig("metadata -> bool option ")]
+pub fn metadata_is_current(metadata: &Metadata) -> Option<bool> {
+ metadata.0.is_current()
+}
+
+/// Return whether this metadata describes a deleted object or delete marker.
+#[ocaml::func]
+#[ocaml::sig("metadata -> bool ")]
+pub fn metadata_is_deleted(metadata: &Metadata) -> bool {
+ metadata.0.is_deleted()
+}
+
+/// Return this entry's Cache-Control value.
+#[ocaml::func]
+#[ocaml::sig("metadata -> string option ")]
+pub fn metadata_cache_control(metadata: &Metadata) -> Option<String> {
+ metadata.0.cache_control().map(String::from)
+}
+
+/// Return the full content length of this entry.
#[ocaml::func]
#[ocaml::sig("metadata -> int64 ")]
-pub fn metadata_content_length(metadata: &mut Metadata) -> u64 {
+pub fn metadata_content_length(metadata: &Metadata) -> u64 {
metadata.0.content_length()
}
+/// Return this entry's Content-MD5 value.
#[ocaml::func]
#[ocaml::sig("metadata -> string option ")]
-pub fn metadata_content_md5(metadata: &mut Metadata) -> Option<String> {
+pub fn metadata_content_md5(metadata: &Metadata) -> Option<String> {
metadata.0.content_md5().map(String::from)
}
+/// Return this entry's Content-Type value.
#[ocaml::func]
#[ocaml::sig("metadata -> string option ")]
-pub fn metadata_content_type(metadata: &mut Metadata) -> Option<String> {
+pub fn metadata_content_type(metadata: &Metadata) -> Option<String> {
metadata.0.content_type().map(String::from)
}
+/// Return this entry's Content-Encoding value.
#[ocaml::func]
#[ocaml::sig("metadata -> string option ")]
-pub fn metadata_content_disposition(metadata: &mut Metadata) -> Option<String>
{
- metadata.0.content_disposition().map(String::from)
+pub fn metadata_content_encoding(metadata: &Metadata) -> Option<String> {
+ metadata.0.content_encoding().map(String::from)
}
+/// Return this entry's Content-Language value, if available.
#[ocaml::func]
#[ocaml::sig("metadata -> string option ")]
-pub fn metadata_etag(metadata: &mut Metadata) -> Option<String> {
- metadata.0.etag().map(String::from)
+pub fn metadata_content_language(metadata: &Metadata) -> Option<String> {
+ metadata.0.content_language().map(String::from)
}
+/// Return this entry's Last-Modified timestamp.
#[ocaml::func]
#[ocaml::sig("metadata -> int64 option ")]
-pub fn metadata_last_modified(metadata: &mut Metadata) -> Option<i64> {
+pub fn metadata_last_modified(metadata: &Metadata) -> Option<i64> {
metadata
.0
.last_modified()
.map(|t| t.into_inner().as_second())
}
+
+/// Return this entry's service-provided ETag as-is.
+#[ocaml::func]
+#[ocaml::sig("metadata -> string option ")]
+pub fn metadata_etag(metadata: &Metadata) -> Option<String> {
+ metadata.0.etag().map(String::from)
+}
+
+/// Return this entry's Content-Disposition value.
+#[ocaml::func]
+#[ocaml::sig("metadata -> string option ")]
+pub fn metadata_content_disposition(metadata: &Metadata) -> Option<String> {
+ metadata.0.content_disposition().map(String::from)
+}
+
+/// Return this entry's service-specific version identifier.
+#[ocaml::func]
+#[ocaml::sig("metadata -> string option ")]
+pub fn metadata_version(metadata: &Metadata) -> Option<String> {
+ metadata.0.version().map(String::from)
+}
+
+/// Return a borrowed view of this entry's user metadata.
+#[ocaml::func]
+#[ocaml::sig("metadata -> (string * string) list option ")]
+pub fn metadata_user_metadata(
+ metadata: &Metadata,
+) -> Option<std::collections::LinkedList<(String, String)>> {
+ metadata.0.user_metadata().map(|m| {
+ m.into_iter()
+ .map(|(k, v)| (k.to_string(), v.to_string()))
+ .collect()
+ })
+}
diff --git a/bindings/ocaml/test/test.ml b/bindings/ocaml/test/test.ml
index 5b4e485703..96fe4f4dd7 100644
--- a/bindings/ocaml/test/test.ml
+++ b/bindings/ocaml/test/test.ml
@@ -178,6 +178,11 @@ let test_operator_stat test_ctxt =
assert_equal false (Operator.Metadata.is_dir metadata);
assert_equal true (Operator.Metadata.is_file metadata);
assert_equal 10L (Operator.Metadata.content_length metadata);
+ assert_equal Operator.Metadata.File (Operator.Metadata.mode metadata);
+ assert_equal false (Operator.Metadata.is_deleted metadata);
+ ignore (test_check_result (Operator.create_dir bo "tempdir/"));
+ let dir_metadata = Operator.stat bo "tempdir/" |> test_check_result in
+ assert_equal Operator.Metadata.Dir (Operator.Metadata.mode dir_metadata);
()
(** Test list functionality *)
@@ -480,15 +485,35 @@ let test_config_duplicate_key_last_wins _ =
let info = Operator.info bo in
assert_equal "/second/" (Operator.OperatorInfo.root info)
-(* Compile-time check: wrappers that re-include [Capability] must still match
- the signature of [Opendal.Operator]. This fails to type-check if
- [operator.mli] exposes [Capability] as a module alias. *)
+(* Compile-time check: the [Metadata] accessors that the hand-written module
+ exposed before it was generated from core. The signature is a literal copy
+ from the previous [operator.mli]; do not derive it from the generator.
+ Dropping or retyping one of these accessors fails to type-check. *)
+module Released_metadata : sig
+ val is_file : Opendal_core.Operator.metadata -> bool
+ val is_dir : Opendal_core.Operator.metadata -> bool
+ val content_length : Opendal_core.Operator.metadata -> int64
+ val content_md5 : Opendal_core.Operator.metadata -> string option
+ val content_type : Opendal_core.Operator.metadata -> string option
+ val content_disposition : Opendal_core.Operator.metadata -> string option
+ val etag : Opendal_core.Operator.metadata -> string option
+ val last_modified : Opendal_core.Operator.metadata -> int64 option
+end =
+ Opendal.Operator.Metadata
+
+(* Compile-time check: wrappers that re-include [Capability] or [Metadata]
+ must still match the signature of [Opendal.Operator]. This fails to
+ type-check if [operator.mli] exposes either module as a module alias. *)
module Wrapped_operator : module type of Opendal.Operator = struct
include Opendal.Operator
module Capability = struct
include Opendal.Operator.Capability
end
+
+ module Metadata = struct
+ include Opendal.Operator.Metadata
+ end
end
(** Test that a structurally re-exported Capability module still works *)
@@ -499,6 +524,13 @@ let test_capability_wrapper _ =
(Operator.Capability.read cap)
(Wrapped_operator.Capability.read cap)
+(** Test that a structurally re-exported Metadata module still works *)
+let test_metadata_wrapper _ =
+ let bo = new_test_memory_operator () in
+ ignore (Operator.write bo "wrapped" (Bytes.of_string "x") |>
test_check_result);
+ let metadata = Operator.stat bo "wrapped" |> test_check_result in
+ assert_equal Operator.Metadata.File (Wrapped_operator.Metadata.mode metadata)
+
let suite =
"OCaml OpenDAL Test Suite"
>::: [
@@ -528,6 +560,7 @@ let suite =
"test_config_duplicate_key_last_wins"
>:: test_config_duplicate_key_last_wins;
"test_capability_wrapper" >:: test_capability_wrapper;
+ "test_metadata_wrapper" >:: test_metadata_wrapper;
]
let () = run_test_tt_main suite
diff --git a/bindings/ocaml/upgrade.md b/bindings/ocaml/upgrade.md
new file mode 100644
index 0000000000..0425a51ce2
--- /dev/null
+++ b/bindings/ocaml/upgrade.md
@@ -0,0 +1,45 @@
+# Upgrade Guide
+
+## Unreleased
+
+### Breaking Changes
+
+#### `Operator.Metadata` gains accessors and the `entry_mode` type
+
+`Operator.Metadata` is now generated from core. It keeps every existing
+accessor with the same name and type, and adds:
+
+- `type entry_mode = File | Dir | Unknown` and `mode : metadata -> entry_mode`.
+- `is_current : metadata -> bool option` and `is_deleted : metadata -> bool`.
+- `cache_control`, `content_encoding`, `content_language`, and `version`, all
+ `metadata -> string option`.
+- `user_metadata : metadata -> (string * string) list option`.
+
+Code that only calls `Operator.Metadata.*` keeps compiling. The new items break
+code that must provide the whole signature or that opens the module:
+
+- A module ascribed to `module type of Opendal.Operator.Metadata`, such as a
+ mock or a functor argument, no longer matches, because it lacks the new
+ accessors and `entry_mode`.
+- A wrapper ascribed to `module type of Opendal.Operator` that defines its own
+ `Metadata` module from scratch no longer matches, for the same reason.
+- After `open Opendal.Operator.Metadata`, the new names (`mode`, `version`,
+ `is_current`, `is_deleted`, `cache_control`, `content_encoding`,
+ `content_language`, `user_metadata`) and constructors (`File`, `Dir`,
+ `Unknown`) shadow earlier definitions with the same names.
+
+**Migration:**
+
+Include the generated module and override only the accessors you replace:
+
+```ocaml
+module Mock_metadata : module type of Opendal.Operator.Metadata = struct
+ include Opendal.Operator.Metadata
+
+ let is_file _ = true
+end
+```
+
+Instead of opening `Opendal.Operator.Metadata`, qualify the accessors, for
+example `Opendal.Operator.Metadata.is_file meta`, or rename your own
+definitions that clash with the new names.
diff --git a/dev/src/generate/ocaml.rs b/dev/src/generate/ocaml.rs
index 17d4854700..0da1f006bb 100644
--- a/dev/src/generate/ocaml.rs
+++ b/dev/src/generate/ocaml.rs
@@ -17,19 +17,23 @@
//! Generator for the OCaml binding.
//!
-//! Renders the Rust FFI accessors (`src/operator/capability.rs`) and the
-//! public `Capability` module (`lib/capability.ml{,i}`). The `external`
-//! declarations in `src/operator.mli` are derived from the Rust file by
+//! Renders the Rust FFI accessors (`src/operator/capability.rs` and
+//! `src/operator/metadata.rs`) and the public `Capability` and `Metadata`
+//! modules (`lib/capability.ml{,i}` and `lib/metadata.ml{,i}`). The `external`
+//! declarations in `src/operator.mli` are derived from the Rust files by
//! `ocaml-build` during `dune build`.
use super::capability;
-use anyhow::Result;
+use super::metadata::{self, MetadataKind};
+use anyhow::{Result, bail};
use minijinja::{Environment, context};
+use serde::Serialize;
use std::fs;
use std::path::Path;
pub fn generate(workspace_dir: &Path) -> Result<()> {
- generate_capability(workspace_dir)
+ generate_capability(workspace_dir)?;
+ generate_metadata(workspace_dir)
}
fn generate_capability(workspace_dir: &Path) -> Result<()> {
@@ -56,6 +60,128 @@ fn generate_capability(workspace_dir: &Path) -> Result<()> {
Ok(())
}
+fn generate_metadata(workspace_dir: &Path) -> Result<()> {
+ let source = workspace_dir.join("core/core/src/types/metadata.rs");
+ let accessors = metadata_accessors(metadata::parse(&source)?)?;
+
+ let mut env = Environment::new();
+ env.add_filter("ocaml_doc", ocaml_doc);
+ env.add_template("metadata_rs", include_str!("ocaml/metadata_rs.j2"))?;
+ env.add_template("metadata_ml", include_str!("ocaml/metadata_ml.j2"))?;
+ env.add_template("metadata_mli", include_str!("ocaml/metadata_mli.j2"))?;
+
+ let outputs = [
+ ("metadata_rs", "bindings/ocaml/src/operator/metadata.rs"),
+ ("metadata_ml", "bindings/ocaml/lib/metadata.ml"),
+ ("metadata_mli", "bindings/ocaml/lib/metadata.mli"),
+ ];
+ for (template, relative) in outputs {
+ let rendered = env
+ .get_template(template)?
+ .render(context! { accessors => accessors })?;
+ fs::write(workspace_dir.join(relative), format!("{rendered}\n"))?;
+ }
+ Ok(())
+}
+
+/// One `Metadata` accessor as rendered for OCaml.
+#[derive(Debug, Serialize)]
+struct MetadataAccessor {
+ /// Accessor name in snake_case, shared by core, the FFI function and
OCaml.
+ name: String,
+ /// OCaml type of the result.
+ ocaml_type: &'static str,
+ /// Rust return type of the FFI function.
+ rs_type: &'static str,
+ /// Rust expression that converts the core value into `rs_type`.
+ rs_expr: String,
+ /// First paragraph of the core doc, unescaped.
+ doc: String,
+ /// Extra odoc sentence describing the OCaml representation, if any.
+ ocaml_note: &'static str,
+}
+
+/// Accessors that core derives from `mode`.
+///
+/// The shared parser skips them because they carry no data of their own, but
+/// the OCaml binding has always exposed them, so they are rendered right after
+/// `mode`.
+const DERIVED_FROM_MODE: [(&str, &str); 2] = [
+ ("is_file", "Return true if this metadata is for a file."),
+ ("is_dir", "Return true if this metadata is for a directory."),
+];
+
+fn metadata_accessors(fields: Vec<metadata::MetadataField>) ->
Result<Vec<MetadataAccessor>> {
+ if !fields
+ .iter()
+ .any(|f| f.name == "mode" && f.kind == MetadataKind::Mode)
+ {
+ bail!(
+ "metadata accessor `mode` is gone; \
+ update DERIVED_FROM_MODE in dev/src/generate/ocaml.rs"
+ );
+ }
+
+ let mut accessors = Vec::with_capacity(fields.len() +
DERIVED_FROM_MODE.len());
+ for f in fields {
+ let is_mode = f.name == "mode";
+ accessors.push(metadata_accessor(f.name, f.kind, f.doc));
+ if is_mode {
+ for (name, doc) in DERIVED_FROM_MODE {
+ accessors.push(metadata_accessor(
+ name.to_string(),
+ MetadataKind::Bool,
+ doc.to_string(),
+ ));
+ }
+ }
+ }
+ Ok(accessors)
+}
+
+/// How each core return type crosses into OCaml.
+///
+/// `mode` becomes the `entry_mode` variant declared in `metadata_rs.j2`,
+/// `u64` and timestamps become `int64`, and user metadata becomes an
+/// association list.
+fn metadata_accessor(name: String, kind: MetadataKind, doc: String) ->
MetadataAccessor {
+ let call = format!("metadata.0.{name}()");
+ let (ocaml_type, rs_type, rs_expr, ocaml_note) = match kind {
+ MetadataKind::Mode => ("entry_mode", "EntryMode",
format!("{call}.into()"), ""),
+ MetadataKind::Bool => ("bool", "bool", call, ""),
+ MetadataKind::U64 => ("int64", "u64", call, ""),
+ MetadataKind::OptionBool => ("bool option", "Option<bool>", call, ""),
+ MetadataKind::OptionStr => (
+ "string option",
+ "Option<String>",
+ format!("{call}.map(String::from)"),
+ "",
+ ),
+ MetadataKind::OptionTimestamp => (
+ "int64 option",
+ "Option<i64>",
+ format!("{call}.map(|t| t.into_inner().as_second())"),
+ "The value is in seconds since the Unix epoch.",
+ ),
+ MetadataKind::OptionUserMetadata => (
+ "(string * string) list option",
+ "Option<std::collections::LinkedList<(String, String)>>",
+ format!(
+ "{call}.map(|m| m.into_iter().map(|(k, v)| (k.to_string(),
v.to_string())).collect())"
+ ),
+ "The pairs are copied into an association list.",
+ ),
+ };
+ MetadataAccessor {
+ name,
+ ocaml_type,
+ rs_type,
+ rs_expr,
+ doc,
+ ocaml_note,
+ }
+}
+
/// Escape text for an odoc comment: `{`, `}`, `[`, `]` and `@` are markup.
fn ocaml_doc(doc: &str) -> String {
let mut out = String::with_capacity(doc.len());
@@ -76,4 +202,19 @@ mod tests {
fn test_ocaml_doc_escapes_markup() {
assert_eq!(ocaml_doc("see [x] @a {b}"), "see \\[x\\] \\@a \\{b\\}");
}
+
+ #[test]
+ fn test_metadata_accessors_keep_derived_after_mode() {
+ let path =
crate::workspace_dir().join("core/core/src/types/metadata.rs");
+ let accessors =
metadata_accessors(metadata::parse(&path).unwrap()).unwrap();
+
+ let names: Vec<&str> = accessors.iter().map(|a|
a.name.as_str()).collect();
+ assert_eq!(names[..3], ["mode", "is_file", "is_dir"]);
+
+ let last_modified = accessors
+ .iter()
+ .find(|a| a.name == "last_modified")
+ .unwrap();
+ assert_eq!(last_modified.ocaml_type, "int64 option");
+ }
}
diff --git a/dev/src/generate/ocaml/metadata_ml.j2
b/dev/src/generate/ocaml/metadata_ml.j2
new file mode 100644
index 0000000000..c5fa133fc0
--- /dev/null
+++ b/dev/src/generate/ocaml/metadata_ml.j2
@@ -0,0 +1,26 @@
+(*
+ 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.
+*)
+
+(* DO NOT EDIT IT MANUALLY. This file is generated by
+ opendal/dev/src/generate/ocaml.rs. *)
+
+type entry_mode = Opendal_core.Operator.entry_mode = File | Dir | Unknown
+{% for a in accessors %}
+let {{ a.name }} = Opendal_core.Operator.metadata_{{ a.name }}
+{%- endfor %}
diff --git a/dev/src/generate/ocaml/metadata_mli.j2
b/dev/src/generate/ocaml/metadata_mli.j2
new file mode 100644
index 0000000000..cb40767494
--- /dev/null
+++ b/dev/src/generate/ocaml/metadata_mli.j2
@@ -0,0 +1,35 @@
+(*
+ 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.
+*)
+
+(* DO NOT EDIT IT MANUALLY. This file is generated by
+ opendal/dev/src/generate/ocaml.rs. *)
+
+(** Accessors for the metadata of a file or directory.
+
+ Optional values are [None] when the service did not report them. *)
+
+(** The mode of an entry. *)
+type entry_mode = Opendal_core.Operator.entry_mode =
+ | File (** The path has data to read. *)
+ | Dir (** The path can be listed. *)
+ | Unknown (** OpenDAL does not know what can be done on the path. *)
+{% for a in accessors %}
+val {{ a.name }} : Opendal_core.Operator.metadata -> {{ a.ocaml_type }}
+(** {{ a.doc | ocaml_doc }}{% if a.ocaml_note %} {{ a.ocaml_note }}{% endif %}
*)
+{% endfor -%}
diff --git a/dev/src/generate/ocaml/metadata_rs.j2
b/dev/src/generate/ocaml/metadata_rs.j2
new file mode 100644
index 0000000000..e7243d38b9
--- /dev/null
+++ b/dev/src/generate/ocaml/metadata_rs.j2
@@ -0,0 +1,47 @@
+// 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.
+
+// DO NOT EDIT IT MANUALLY. This file is generated by
opendal/dev/src/generate/ocaml.rs.
+
+use super::*;
+
+/// The mode of an entry, mirroring core `EntryMode`.
+#[derive(ocaml::ToValue, Clone, Copy)]
+#[ocaml::sig("File | Dir | Unknown")]
+pub enum EntryMode {
+ File,
+ Dir,
+ Unknown,
+}
+
+impl From<od::EntryMode> for EntryMode {
+ fn from(mode: od::EntryMode) -> Self {
+ match mode {
+ od::EntryMode::FILE => EntryMode::File,
+ od::EntryMode::DIR => EntryMode::Dir,
+ od::EntryMode::Unknown => EntryMode::Unknown,
+ }
+ }
+}
+{% for a in accessors %}
+/// {{ a.doc }}
+#[ocaml::func]
+#[ocaml::sig("metadata -> {{ a.ocaml_type }} ")]
+pub fn metadata_{{ a.name }}(metadata: &Metadata) -> {{ a.rs_type }} {
+ {{ a.rs_expr }}
+}
+{% endfor -%}