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 caca645971 feat(bindings/cpp): generate RenameOptions from core (#9401)
caca645971 is described below
commit caca645971a148f01d3f49475e2748c4a8e795e9
Author: Chitral Verma <[email protected]>
AuthorDate: Mon Nov 30 02:03:24 2026 +0530
feat(bindings/cpp): generate RenameOptions from core (#9401)
* feat(bindings/cpp): generate RenameOptions from core
Generate the existing C++ `RenameOptions` from core's `RenameOptions`,
as the earlier PRs for #9324 do for the other options structs. The
`if_not_exists` member keeps its type and default initializer. Core has
no other field, so C++ now exposes every core rename option.
The docs are core's full docs.
Signed-off-by: Chitral Verma <[email protected]>
* fixup! feat(bindings/cpp): generate RenameOptions from core
Skip the `RENAME_NOREPLACE` case on Android. Android defines `__linux__`,
but `fs` advertises `rename_with_if_not_exists` only for
`target_os = "linux"`.
Signed-off-by: Chitral Verma <[email protected]>
---------
Signed-off-by: Chitral Verma <[email protected]>
---
bindings/cpp/include/data_structure.hpp | 8 ---
bindings/cpp/include/options.hpp | 25 ++++++++
bindings/cpp/src/lib.rs | 14 +----
bindings/cpp/src/operator.cpp | 6 +-
bindings/cpp/src/options.rs | 12 ++++
bindings/cpp/src/options_test.rs | 20 +++++-
bindings/cpp/src/utils/options_converter.cpp | 6 ++
bindings/cpp/src/utils/options_converter.hpp | 1 +
bindings/cpp/tests/options_test.cpp | 91 ++++++++++++++++++++++++++++
dev/src/generate/cpp.rs | 52 ++++++++++++++--
10 files changed, 202 insertions(+), 33 deletions(-)
diff --git a/bindings/cpp/include/data_structure.hpp
b/bindings/cpp/include/data_structure.hpp
index 5e6a89c95a..348ae211b5 100644
--- a/bindings/cpp/include/data_structure.hpp
+++ b/bindings/cpp/include/data_structure.hpp
@@ -41,12 +41,4 @@ class Entry {
std::string path;
};
-/**
- * @struct RenameOptions
- * @brief Options for rename operations.
- */
-struct RenameOptions {
- bool if_not_exists{false};
-};
-
} // namespace opendal
diff --git a/bindings/cpp/include/options.hpp b/bindings/cpp/include/options.hpp
index 7fab50652f..5c33344c5f 100644
--- a/bindings/cpp/include/options.hpp
+++ b/bindings/cpp/include/options.hpp
@@ -817,4 +817,29 @@ struct CopyOptions {
std::optional<std::size_t> chunk;
};
+/**
+ * @struct RenameOptions
+ * @brief Options for rename operations.
+ */
+/// The condition checks the destination file, never the source. A missing
+/// source fails the rename with `NotFound`; when a condition error applies at
+/// the same time, which error the rename returns is unspecified.
+///
+/// See the conditional operation specification for the complete
cross-operation
+/// contract.
+///
+/// When the operation fails with an error kind that these docs name, such as
+/// `NotFound`, it throws a `std::exception` whose `what()` starts with that
+/// kind.
+struct RenameOptions {
+ /// Rename only when no file exists at the destination path.
+ ///
+ /// The condition succeeds when the destination path is empty. An existing
+ /// destination file fails the rename with `ConditionNotMatch`.
+ ///
+ /// The operation returns `Unsupported` when the service does not advertise
+ /// `opendal::Capability::rename_with_if_not_exists`.
+ bool if_not_exists{false};
+};
+
} // namespace opendal
diff --git a/bindings/cpp/src/lib.rs b/bindings/cpp/src/lib.rs
index 991d79621b..ab1db87aca 100644
--- a/bindings/cpp/src/lib.rs
+++ b/bindings/cpp/src/lib.rs
@@ -57,10 +57,6 @@ mod ffi {
value: String,
}
- struct FfiRenameOptions {
- if_not_exists: bool,
- }
-
#[cxx_name = "SeekDir"]
enum SeekFrom {
Start = 0,
@@ -107,6 +103,8 @@ mod ffi {
type FfiWriteOptions = crate::options::ffi::FfiWriteOptions;
#[namespace = "opendal::ffi::options"]
type FfiCopyOptions = crate::options::ffi::FfiCopyOptions;
+ #[namespace = "opendal::ffi::options"]
+ type FfiRenameOptions = crate::options::ffi::FfiRenameOptions;
}
extern "Rust" {
@@ -292,12 +290,6 @@ unsafe fn delete_lister(lister: *mut Lister) {
}
}
-fn rename_options(opts: ffi::FfiRenameOptions) -> od::options::RenameOptions {
- od::options::RenameOptions {
- if_not_exists: opts.if_not_exists,
- }
-}
-
impl Operator {
fn read(&self, path: &str) -> Result<Vec<u8>> {
Ok(self.0.read(path)?.to_vec())
@@ -341,7 +333,7 @@ impl Operator {
}
fn rename_options(&self, src: &str, dst: &str, opts:
ffi::FfiRenameOptions) -> Result<()> {
- Ok(self.0.rename_options(src, dst, rename_options(opts))?)
+ Ok(self.0.rename_options(src, dst, opts.into())?)
}
// We can't name it to delete because it's a keyword in C++
diff --git a/bindings/cpp/src/operator.cpp b/bindings/cpp/src/operator.cpp
index 2fb5f32657..cea75ee8aa 100644
--- a/bindings/cpp/src/operator.cpp
+++ b/bindings/cpp/src/operator.cpp
@@ -32,10 +32,6 @@ namespace opendal {
namespace {
-ffi::FfiRenameOptions ToFfiOptions(const RenameOptions &options) {
- return ffi::FfiRenameOptions{options.if_not_exists};
-}
-
const ffi::Operator &CheckedOperator(const ffi::Operator *op) {
if (op == nullptr) {
throw std::logic_error(
@@ -148,7 +144,7 @@ void Operator::Rename(std::string_view src,
std::string_view dst) {
void Operator::Rename(std::string_view src, std::string_view dst,
const RenameOptions &options) {
CheckedOperator(operator_).rename_options(
- utils::rust_str(src), utils::rust_str(dst), ToFfiOptions(options));
+ utils::rust_str(src), utils::rust_str(dst),
utils::ToFfiOptions(options));
}
void Operator::Remove(std::string_view path) {
diff --git a/bindings/cpp/src/options.rs b/bindings/cpp/src/options.rs
index 8b851272ff..267ca639d1 100644
--- a/bindings/cpp/src/options.rs
+++ b/bindings/cpp/src/options.rs
@@ -137,6 +137,10 @@ pub mod ffi {
concurrent: usize,
chunk: OptionalUsize,
}
+
+ struct FfiRenameOptions {
+ if_not_exists: bool,
+ }
}
fn optional_string(v: ffi::OptionalString) -> Option<String> {
@@ -336,3 +340,11 @@ impl From<ffi::FfiCopyOptions> for
opendal::options::CopyOptions {
}
}
}
+
+impl From<ffi::FfiRenameOptions> for opendal::options::RenameOptions {
+ fn from(opts: ffi::FfiRenameOptions) -> Self {
+ Self {
+ if_not_exists: opts.if_not_exists,
+ }
+ }
+}
diff --git a/bindings/cpp/src/options_test.rs b/bindings/cpp/src/options_test.rs
index 2aef0d4d23..a0c60624cc 100644
--- a/bindings/cpp/src/options_test.rs
+++ b/bindings/cpp/src/options_test.rs
@@ -16,9 +16,9 @@
// under the License.
//! The generated `ListOptions`, `StatOptions`, `DeleteOptions`,
-//! `ReadOptions`, `ReaderOptions`, `WriteOptions`, and `CopyOptions`
-//! conversions in `options.rs` carry every bridge field to the core field of
-//! the same name, and absent values stay absent.
+//! `ReadOptions`, `ReaderOptions`, `WriteOptions`, `CopyOptions`, and
+//! `RenameOptions` conversions in `options.rs` carry every bridge field to the
+//! core field of the same name, and absent values stay absent.
use std::collections::HashMap;
@@ -28,6 +28,7 @@ use opendal::options::DeleteOptions;
use opendal::options::ListOptions;
use opendal::options::ReadOptions;
use opendal::options::ReaderOptions;
+use opendal::options::RenameOptions;
use opendal::options::StatOptions;
use opendal::options::WriteOptions;
use opendal::raw::Timestamp;
@@ -592,3 +593,16 @@ fn test_copy_options_keep_absent_values_absent() {
CopyOptions::default()
);
}
+
+#[test]
+fn test_rename_options_carry_every_field() {
+ for if_not_exists in [false, true] {
+ let opts = ffi::FfiRenameOptions { if_not_exists };
+
+ assert_eq!(
+ RenameOptions::from(opts),
+ RenameOptions { if_not_exists },
+ "if_not_exists={if_not_exists}"
+ );
+ }
+}
diff --git a/bindings/cpp/src/utils/options_converter.cpp
b/bindings/cpp/src/utils/options_converter.cpp
index 3c9bbcebe2..f32ab4f364 100644
--- a/bindings/cpp/src/utils/options_converter.cpp
+++ b/bindings/cpp/src/utils/options_converter.cpp
@@ -192,4 +192,10 @@ ffi::options::FfiCopyOptions ToFfiOptions(const
CopyOptions &options) {
};
}
+ffi::options::FfiRenameOptions ToFfiOptions(const RenameOptions &options) {
+ return ffi::options::FfiRenameOptions{
+ options.if_not_exists,
+ };
+}
+
} // namespace opendal::utils
diff --git a/bindings/cpp/src/utils/options_converter.hpp
b/bindings/cpp/src/utils/options_converter.hpp
index a701b3bd08..e822778bc1 100644
--- a/bindings/cpp/src/utils/options_converter.hpp
+++ b/bindings/cpp/src/utils/options_converter.hpp
@@ -34,5 +34,6 @@ ffi::options::FfiReadOptions ToFfiOptions(const ReadOptions
&options);
ffi::options::FfiReaderOptions ToFfiOptions(const ReaderOptions &options);
ffi::options::FfiWriteOptions ToFfiOptions(const WriteOptions &options);
ffi::options::FfiCopyOptions ToFfiOptions(const CopyOptions &options);
+ffi::options::FfiRenameOptions ToFfiOptions(const RenameOptions &options);
} // namespace opendal::utils
diff --git a/bindings/cpp/tests/options_test.cpp
b/bindings/cpp/tests/options_test.cpp
index ff44044865..692a0e9433 100644
--- a/bindings/cpp/tests/options_test.cpp
+++ b/bindings/cpp/tests/options_test.cpp
@@ -1282,3 +1282,94 @@ TEST(OptionsTest, CopyOptionsErrorsStartWithErrorKind) {
ThrowsMessage<std::exception>(StartsWith("NotFound")));
EXPECT_EQ(op.Read("destination"), "world");
}
+
+TEST(OptionsTest, RenameOptionsPositionalInitializerReachesBridge) {
+ // Every member in its released order.
+ const opendal::RenameOptions options{/*if_not_exists=*/true};
+
+ const auto ffi = opendal::utils::ToFfiOptions(options);
+
+ EXPECT_TRUE(ffi.if_not_exists);
+}
+
+TEST(OptionsTest, RenameOptionsStructuredBindingsKeepMemberOrder) {
+ opendal::RenameOptions options;
+ auto &[if_not_exists] = options;
+
+ EXPECT_EQ(&if_not_exists, &options.if_not_exists);
+}
+
+TEST(OptionsTest, DefaultRenameOptionsReachBridgeAbsent) {
+ const opendal::RenameOptions options;
+
+ const auto ffi = opendal::utils::ToFfiOptions(options);
+
+ EXPECT_FALSE(ffi.if_not_exists);
+}
+
+TEST(OptionsTest, RenameOptionsSucceedOnFs) {
+ // `memory` cannot rename.
+ ReaderOptionsTempDir dir;
+ opendal::Operator op("fs", {{"root", dir.path()}});
+ ASSERT_TRUE(op.Info().rename);
+ op.Write("source", "hello");
+
+ op.Rename("source", "destination", opendal::RenameOptions{});
+
+ EXPECT_EQ(op.Read("destination"), "hello");
+ EXPECT_FALSE(op.Exists("source"));
+}
+
+TEST(OptionsTest, RenameOptionsIfNotExistsOnFs) {
+ using ::testing::AllOf;
+ using ::testing::HasSubstr;
+ using ::testing::StartsWith;
+ using ::testing::ThrowsMessage;
+
+ ReaderOptionsTempDir dir;
+ opendal::Operator op("fs", {{"root", dir.path()}});
+ op.Write("source", "hello");
+ op.Write("destination", "world");
+ opendal::RenameOptions if_not_exists;
+ if_not_exists.if_not_exists = true;
+
+#if defined(__linux__) && !defined(__ANDROID__)
+ // `fs` renames with `RENAME_NOREPLACE` on Linux.
+ ASSERT_TRUE(op.Info().rename_with_if_not_exists);
+ EXPECT_THAT([&] { op.Rename("source", "destination", if_not_exists); },
+ ThrowsMessage<std::exception>(StartsWith("ConditionNotMatch")));
+ EXPECT_EQ(op.Read("source"), "hello");
+ EXPECT_EQ(op.Read("destination"), "world");
+
+ op.Rename("source", "empty", if_not_exists);
+ EXPECT_EQ(op.Read("empty"), "hello");
+ EXPECT_FALSE(op.Exists("source"));
+#else
+ // Elsewhere `fs` does not support `if_not_exists`, so it fails before the
+ // rename reaches storage, even when the source is missing.
+ ASSERT_FALSE(op.Info().rename_with_if_not_exists);
+ for (const char *source : {"source", "missing"}) {
+ EXPECT_THAT([&] { op.Rename(source, "empty", if_not_exists); },
+ ThrowsMessage<std::exception>(
+ AllOf(StartsWith("Unsupported"),
+ HasSubstr("with the arguments if_not_exists."))))
+ << source;
+ }
+ EXPECT_EQ(op.Read("source"), "hello");
+ EXPECT_FALSE(op.Exists("empty"));
+#endif
+}
+
+TEST(OptionsTest, RenameOptionsMissingSourceFailsWithNotFound) {
+ using ::testing::StartsWith;
+ using ::testing::ThrowsMessage;
+
+ ReaderOptionsTempDir dir;
+ opendal::Operator op("fs", {{"root", dir.path()}});
+ op.Write("destination", "world");
+
+ EXPECT_THAT(
+ [&] { op.Rename("missing", "destination", opendal::RenameOptions{}); },
+ ThrowsMessage<std::exception>(StartsWith("NotFound")));
+ EXPECT_EQ(op.Read("destination"), "world");
+}
diff --git a/dev/src/generate/cpp.rs b/dev/src/generate/cpp.rs
index 801a110700..877536dcdc 100644
--- a/dev/src/generate/cpp.rs
+++ b/dev/src/generate/cpp.rs
@@ -565,8 +565,7 @@ struct OptionsSpec {
cpp_rewrites: &'static [(&'static str, &'static str)],
}
-/// Options structs generated into `include/options.hpp`. `RenameOptions` is
-/// still hand-written in `include/data_structure.hpp`.
+/// Options structs generated into `include/options.hpp`.
const OPTIONS: &[OptionsSpec] = {
use OptionKind::*;
@@ -856,6 +855,14 @@ const OPTIONS: &[OptionsSpec] = {
),
],
},
+ OptionsSpec {
+ name: "RenameOptions",
+ brief: "Options for rename operations.",
+ fields: &[("if_not_exists", Bool)],
+ cpp_doc: &[],
+ cpp_member_docs: &[],
+ cpp_rewrites: &[],
+ },
]
};
@@ -1589,9 +1596,9 @@ mod tests {
}
/// `ListOptions`, `StatOptions`, `DeleteOptions`, `ReadRange`,
- /// `ReadOptions`, `ReaderOptions`, `WriteOptions`, and `CopyOptions` as
- /// `bindings/cpp/include/data_structure.hpp` declared them on `main`
- /// before they were generated, copied verbatim.
+ /// `ReadOptions`, `ReaderOptions`, `WriteOptions`, `CopyOptions`, and
+ /// `RenameOptions` as `bindings/cpp/include/data_structure.hpp` declared
+ /// them on `main` before they were generated, copied verbatim.
/// Callers depend on each member's name, type, default member initializer,
/// and position. The generated header adds doc comments, which these texts
/// omit.
@@ -1730,6 +1737,14 @@ struct CopyOptions {
std::optional<std::size_t> chunk;
};"#;
+ const MAIN_RENAME_OPTIONS: &str = r#"/**
+ * @struct RenameOptions
+ * @brief Options for rename operations.
+ */
+struct RenameOptions {
+ bool if_not_exists{false};
+};"#;
+
fn core_names() -> CppNames {
CppNames::parse(&workspace_dir()).expect("parse core names")
}
@@ -1774,6 +1789,7 @@ struct CopyOptions {
MAIN_READER_OPTIONS,
MAIN_WRITE_OPTIONS,
MAIN_COPY_OPTIONS,
+ MAIN_RENAME_OPTIONS,
] {
assert!(
code.contains(&code_lines(main)),
@@ -1783,7 +1799,7 @@ struct CopyOptions {
let head: String = main.lines().take(3).map(|l|
format!("{l}\n")).collect();
assert!(header.contains(&head), "header lacks:\n{head}");
}
- assert_eq!(header.matches("\nstruct ").count(), 8);
+ assert_eq!(header.matches("\nstruct ").count(), 9);
// `ReadOptions` needs `ReadRange` declared before it.
let position = |name: &str| header.find(&format!("\nstruct {name}
{{\n")).unwrap();
assert!(position("ReadRange") < position("ReadOptions"));
@@ -2196,6 +2212,7 @@ single byte range, so `gap` has no effect.",
" void Write(std::string_view path, std::string_view data,\n
const WriteOptions &options);\n",
" Writer GetWriter(std::string_view path, const WriteOptions
&options);\n",
" void Copy(std::string_view src, std::string_view dst,\n
const CopyOptions &options);\n",
+ " void Rename(std::string_view src, std::string_view dst,\n
const RenameOptions &options);\n",
] {
assert!(
operator.contains(declaration),
@@ -2589,6 +2606,7 @@ single byte range, so `gap` has no effect.",
("ReaderOptions", true),
("WriteOptions", false),
("CopyOptions", false),
+ ("RenameOptions", false),
]
);
@@ -2617,6 +2635,9 @@ single byte range, so `gap` has no effect.",
assert!(
rust.contains("impl From<ffi::FfiCopyOptions> for
opendal::options::CopyOptions {")
);
+ assert!(
+ rust.contains("impl From<ffi::FfiRenameOptions> for
opendal::options::RenameOptions {")
+ );
}
#[test]
@@ -2804,6 +2825,12 @@ single byte range, so `gap` has no effect.",
OptionU64,
"core `CopyOptions::chunk` has kind OptionU64, but OPTIONS
lists OptionUsize",
),
+ (
+ "RenameOptions",
+ "if_not_exists",
+ OptionString,
+ "core `RenameOptions::if_not_exists` has kind OptionString,
but OPTIONS lists Bool",
+ ),
] {
let mut changed = parsed.clone();
changed
@@ -2887,6 +2914,19 @@ single byte range, so `gap` has no effect.",
.to_string(),
"OPTIONS member `CopyOptions::source_version` is missing from core"
);
+ let mut without_if_not_exists = parsed.clone();
+ without_if_not_exists
+ .iter_mut()
+ .find(|s| s.name == "RenameOptions")
+ .unwrap()
+ .fields
+ .retain(|f| f.name != "if_not_exists");
+ assert_eq!(
+ options_models(&without_if_not_exists, OPTIONS, &core_names())
+ .unwrap_err()
+ .to_string(),
+ "OPTIONS member `RenameOptions::if_not_exists` is missing from
core"
+ );
// In C++, `Self::source_version` names a member that this spec does
// not expose.
assert_eq!(