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 8c7463594c feat(bindings/cpp): generate CopyOptions from core (#9399)
8c7463594c is described below
commit 8c7463594cf14993c28f8672482083474dbdf06b
Author: Chitral Verma <[email protected]>
AuthorDate: Mon Nov 30 01:07:46 2026 +0530
feat(bindings/cpp): generate CopyOptions from core (#9399)
* feat(bindings/cpp): generate CopyOptions from core
Generate the existing C++ `CopyOptions` from core's `CopyOptions`, as
the earlier PRs for #9324 do for the other options structs. The members
keep their order, types, and default initializers. `if_none_match`,
`if_version_match`, `if_version_not_match`, `if_not_changed`,
`user_metadata`, `content_type`, and `content_language` stay
unexposed.
The docs are core's full docs. `Self::x` code spans read `x`. The
`concurrent` doc names `Operator::Copy` instead of a copier, and the
`source_content_length_hint` doc drops the returned metadata, which
C++ `Copy` does not return.
Signed-off-by: Chitral Verma <[email protected]>
* fixup! feat(bindings/cpp): generate CopyOptions from core
Drop the sentence that a service can ignore `source_content_length_hint`
when it reports the copied size. Core skips the hint only for the returned
metadata, which C++ `Copy` does not return, and a chunked S3 copy still
plans from the hint.
Fail generation when a doc names `Self::x` for a field that C++ does
not expose. Drop the `memory` case, which tests the service, not an
option.
Signed-off-by: Chitral Verma <[email protected]>
---------
Signed-off-by: Chitral Verma <[email protected]>
---
bindings/cpp/include/data_structure.hpp | 13 --
bindings/cpp/include/options.hpp | 83 ++++++++++
bindings/cpp/src/lib.rs | 52 +-----
bindings/cpp/src/operator.cpp | 35 +----
bindings/cpp/src/options.rs | 31 ++++
bindings/cpp/src/options_test.rs | 64 +++++++-
bindings/cpp/src/utils/options_converter.cpp | 11 ++
bindings/cpp/src/utils/options_converter.hpp | 1 +
bindings/cpp/tests/options_test.cpp | 127 +++++++++++++++
dev/src/generate/cpp.rs | 227 +++++++++++++++++++++++++--
10 files changed, 534 insertions(+), 110 deletions(-)
diff --git a/bindings/cpp/include/data_structure.hpp
b/bindings/cpp/include/data_structure.hpp
index 8623fc908a..5e6a89c95a 100644
--- a/bindings/cpp/include/data_structure.hpp
+++ b/bindings/cpp/include/data_structure.hpp
@@ -41,19 +41,6 @@ class Entry {
std::string path;
};
-/**
- * @struct CopyOptions
- * @brief Options for copy operations.
- */
-struct CopyOptions {
- bool if_not_exists{false};
- std::optional<std::string> if_match;
- std::optional<std::string> source_version;
- std::optional<std::uint64_t> source_content_length_hint;
- std::size_t concurrent{0};
- std::optional<std::size_t> chunk;
-};
-
/**
* @struct RenameOptions
* @brief Options for rename operations.
diff --git a/bindings/cpp/include/options.hpp b/bindings/cpp/include/options.hpp
index 896f0900c5..7fab50652f 100644
--- a/bindings/cpp/include/options.hpp
+++ b/bindings/cpp/include/options.hpp
@@ -734,4 +734,87 @@ struct WriteOptions {
std::optional<std::size_t> chunk;
};
+/**
+ * @struct CopyOptions
+ * @brief Options for copy operations.
+ */
+/// Each condition checks the destination file, never the source, and every
+/// condition must hold for the copy to proceed:
+///
+/// - A false condition fails the copy with `ConditionNotMatch`.
+/// - A condition the service does not advertise through its capability fails
+/// the copy with `Unsupported`; OpenDAL never silently drops a condition.
+/// - A missing source fails the copy with `NotFound`; when a condition error
+/// applies at the same time, which error the copy returns is unspecified.
+/// - A service-side conflict unrelated to these conditions surfaces as
+/// `Conflict`.
+///
+/// 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
+/// `ConditionNotMatch`, it throws a `std::exception` whose `what()` starts
with
+/// that kind.
+struct CopyOptions {
+ /// Copy only when no file exists at the destination path.
+ ///
+ /// The condition succeeds when the destination path is empty, so the copy
+ /// creates the destination file. An existing destination file fails the copy
+ /// with `ConditionNotMatch`.
+ ///
+ /// The operation returns `Unsupported` when the service does not advertise
+ /// `opendal::Capability::copy_with_if_not_exists`.
+ bool if_not_exists{false};
+ /// Copy only when the destination file has this exact ETag.
+ ///
+ /// The condition succeeds when the destination file exists and its ETag
+ /// equals this value. A destination file with a different ETag, or a missing
+ /// destination file, fails the copy with `ConditionNotMatch`. Only concrete
+ /// ETag values are portable; a wildcard such as `"*"` has no portable
meaning
+ /// here.
+ ///
+ /// The operation returns `Unsupported` when the service does not advertise
+ /// `opendal::Capability::copy_with_if_match`.
+ std::optional<std::string> if_match;
+ /// Copy from a specific source file version.
+ ///
+ /// This selects which stored version of the source the copy reads; it is not
+ /// a condition on the destination file. Destination behavior follows normal
+ /// copy semantics.
+ ///
+ /// Check `opendal::Capability::copy_with_source_version` before using this
+ /// option.
+ std::optional<std::string> source_version;
+ /// Asserted complete content length of the source object.
+ ///
+ /// OpenDAL may trust this value without reading source metadata. It can use
+ /// the value to plan copied ranges. An incorrect value can therefore cause
+ /// incomplete copy planning or failed requests.
+ ///
+ /// This option does not pin the source object or protect the copy from a
+ /// concurrent source change. Set it only when the caller can guarantee that
+ /// the value describes the object that the service will copy, for example by
+ /// selecting an immutable `source_version`. Otherwise omit it if the source
+ /// can change.
+ std::optional<std::uint64_t> source_content_length_hint;
+ /// Sets concurrent copy operations for `opendal::Operator::Copy`.
+ ///
+ /// This is a best-effort execution option. Services that cannot split copy
+ /// into concurrent server-side tasks can ignore it.
+ std::size_t concurrent{0};
+ /// Sets chunk size for segmented copy operations.
+ ///
+ /// ### Capability
+ ///
+ /// Check `opendal::Capability::copy_can_multi`,
+ /// `opendal::Capability::copy_multi_min_size` and
+ /// `opendal::Capability::copy_multi_max_size` before using this feature.
+ ///
+ /// This is a best-effort execution option. Services that support server-side
+ /// segmented copy can use it as the target size for each copy step. Services
+ /// that cannot split copy operations can ignore it. `0` has the same
behavior
+ /// as `std::nullopt`.
+ std::optional<std::size_t> chunk;
+};
+
} // namespace opendal
diff --git a/bindings/cpp/src/lib.rs b/bindings/cpp/src/lib.rs
index 4f21be3df5..991d79621b 100644
--- a/bindings/cpp/src/lib.rs
+++ b/bindings/cpp/src/lib.rs
@@ -57,25 +57,6 @@ mod ffi {
value: String,
}
- struct OptionalU64 {
- has_value: bool,
- value: u64,
- }
-
- struct OptionalUsize {
- has_value: bool,
- value: usize,
- }
-
- struct FfiCopyOptions {
- if_not_exists: bool,
- if_match: OptionalString,
- source_version: OptionalString,
- source_content_length_hint: OptionalU64,
- concurrent: usize,
- chunk: OptionalUsize,
- }
-
struct FfiRenameOptions {
if_not_exists: bool,
}
@@ -87,11 +68,6 @@ mod ffi {
End = 2,
}
- struct OptionalString {
- has_value: bool,
- value: String,
- }
-
struct OptionalEntry {
has_value: bool,
value: Entry,
@@ -129,6 +105,8 @@ mod ffi {
type FfiReaderOptions = crate::options::ffi::FfiReaderOptions;
#[namespace = "opendal::ffi::options"]
type FfiWriteOptions = crate::options::ffi::FfiWriteOptions;
+ #[namespace = "opendal::ffi::options"]
+ type FfiCopyOptions = crate::options::ffi::FfiCopyOptions;
}
extern "Rust" {
@@ -314,30 +292,6 @@ unsafe fn delete_lister(lister: *mut Lister) {
}
}
-fn optional_string(v: ffi::OptionalString) -> Option<String> {
- v.has_value.then_some(v.value)
-}
-
-fn optional_u64(v: ffi::OptionalU64) -> Option<u64> {
- v.has_value.then_some(v.value)
-}
-
-fn optional_usize(v: ffi::OptionalUsize) -> Option<usize> {
- v.has_value.then_some(v.value)
-}
-
-fn copy_options(opts: ffi::FfiCopyOptions) -> od::options::CopyOptions {
- od::options::CopyOptions {
- if_not_exists: opts.if_not_exists,
- if_match: optional_string(opts.if_match),
- source_version: optional_string(opts.source_version),
- source_content_length_hint:
optional_u64(opts.source_content_length_hint),
- concurrent: opts.concurrent,
- chunk: optional_usize(opts.chunk),
- ..Default::default()
- }
-}
-
fn rename_options(opts: ffi::FfiRenameOptions) -> od::options::RenameOptions {
od::options::RenameOptions {
if_not_exists: opts.if_not_exists,
@@ -378,7 +332,7 @@ impl Operator {
}
fn copy_options(&self, src: &str, dst: &str, opts: ffi::FfiCopyOptions) ->
Result<()> {
- self.0.copy_options(src, dst, copy_options(opts))?;
+ self.0.copy_options(src, dst, opts.into())?;
Ok(())
}
diff --git a/bindings/cpp/src/operator.cpp b/bindings/cpp/src/operator.cpp
index af41dd7fc7..2fb5f32657 100644
--- a/bindings/cpp/src/operator.cpp
+++ b/bindings/cpp/src/operator.cpp
@@ -32,39 +32,6 @@ namespace opendal {
namespace {
-ffi::OptionalString ToFfiOptionalString(
- const std::optional<std::string> &value) {
- if (value.has_value()) {
- return ffi::OptionalString{true, utils::rust_string(*value)};
- }
- return ffi::OptionalString{false, rust::String()};
-}
-
-ffi::OptionalU64 ToFfiOptionalU64(const std::optional<std::uint64_t> &value) {
- if (value.has_value()) {
- return ffi::OptionalU64{true, *value};
- }
- return ffi::OptionalU64{false, 0};
-}
-
-ffi::OptionalUsize ToFfiOptionalUsize(const std::optional<std::size_t> &value)
{
- if (value.has_value()) {
- return ffi::OptionalUsize{true, *value};
- }
- return ffi::OptionalUsize{false, 0};
-}
-
-ffi::FfiCopyOptions ToFfiOptions(const CopyOptions &options) {
- return ffi::FfiCopyOptions{
- options.if_not_exists,
- ToFfiOptionalString(options.if_match),
- ToFfiOptionalString(options.source_version),
- ToFfiOptionalU64(options.source_content_length_hint),
- options.concurrent,
- ToFfiOptionalUsize(options.chunk),
- };
-}
-
ffi::FfiRenameOptions ToFfiOptions(const RenameOptions &options) {
return ffi::FfiRenameOptions{options.if_not_exists};
}
@@ -171,7 +138,7 @@ void Operator::Copy(std::string_view src, std::string_view
dst) {
void Operator::Copy(std::string_view src, std::string_view dst,
const CopyOptions &options) {
CheckedOperator(operator_).copy_options(
- utils::rust_str(src), utils::rust_str(dst), ToFfiOptions(options));
+ utils::rust_str(src), utils::rust_str(dst),
utils::ToFfiOptions(options));
}
void Operator::Rename(std::string_view src, std::string_view dst) {
diff --git a/bindings/cpp/src/options.rs b/bindings/cpp/src/options.rs
index 43574108e9..8b851272ff 100644
--- a/bindings/cpp/src/options.rs
+++ b/bindings/cpp/src/options.rs
@@ -128,6 +128,15 @@ pub mod ffi {
concurrent: usize,
chunk: OptionalUsize,
}
+
+ struct FfiCopyOptions {
+ if_not_exists: bool,
+ if_match: OptionalString,
+ source_version: OptionalString,
+ source_content_length_hint: OptionalU64,
+ concurrent: usize,
+ chunk: OptionalUsize,
+ }
}
fn optional_string(v: ffi::OptionalString) -> Option<String> {
@@ -305,3 +314,25 @@ impl From<ffi::FfiWriteOptions> for
opendal::options::WriteOptions {
}
}
}
+
+impl From<ffi::FfiCopyOptions> for opendal::options::CopyOptions {
+ fn from(opts: ffi::FfiCopyOptions) -> Self {
+ Self {
+ if_not_exists: opts.if_not_exists,
+ if_match: optional_string(opts.if_match),
+ source_version: optional_string(opts.source_version),
+ source_content_length_hint:
optional_u64(opts.source_content_length_hint),
+ concurrent: opts.concurrent,
+ chunk: optional_usize(opts.chunk),
+ // Not exposed in C++ yet:
+ // - `if_none_match`
+ // - `if_version_match`
+ // - `if_version_not_match`
+ // - `if_not_changed`
+ // - `user_metadata`
+ // - `content_type`
+ // - `content_language`
+ ..Default::default()
+ }
+ }
+}
diff --git a/bindings/cpp/src/options_test.rs b/bindings/cpp/src/options_test.rs
index b8067850d5..2aef0d4d23 100644
--- a/bindings/cpp/src/options_test.rs
+++ b/bindings/cpp/src/options_test.rs
@@ -16,13 +16,14 @@
// under the License.
//! The generated `ListOptions`, `StatOptions`, `DeleteOptions`,
-//! `ReadOptions`, `ReaderOptions`, and `WriteOptions` conversions in
-//! `options.rs` carry every bridge field to the core field of the same name,
-//! and absent values stay absent.
+//! `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.
use std::collections::HashMap;
use opendal::BytesRange;
+use opendal::options::CopyOptions;
use opendal::options::DeleteOptions;
use opendal::options::ListOptions;
use opendal::options::ReadOptions;
@@ -534,3 +535,60 @@ fn test_write_options_keep_absent_values_absent() {
WriteOptions::default()
);
}
+
+fn absent_copy_options() -> ffi::FfiCopyOptions {
+ ffi::FfiCopyOptions {
+ if_not_exists: false,
+ if_match: bridge_string(None),
+ source_version: bridge_string(None),
+ source_content_length_hint: bridge_u64(None),
+ concurrent: 0,
+ chunk: bridge_usize(None),
+ }
+}
+
+#[test]
+fn test_copy_options_carry_every_field() {
+ let opts = ffi::FfiCopyOptions {
+ if_not_exists: true,
+ if_match: bridge_string(Some("if_match")),
+ source_version: bridge_string(Some("source_version")),
+ source_content_length_hint: bridge_u64(Some(1024)),
+ concurrent: 4,
+ chunk: bridge_usize(Some(16)),
+ };
+
+ // The options that C++ does not expose keep their core defaults.
+ let expected = CopyOptions {
+ if_not_exists: true,
+ if_match: Some("if_match".to_string()),
+ source_version: Some("source_version".to_string()),
+ source_content_length_hint: Some(1024),
+ concurrent: 4,
+ chunk: Some(16),
+ ..Default::default()
+ };
+ assert_eq!(CopyOptions::from(opts), expected);
+}
+
+#[test]
+fn test_copy_options_keep_explicit_zeros() {
+ let opts = ffi::FfiCopyOptions {
+ source_content_length_hint: bridge_u64(Some(0)),
+ chunk: bridge_usize(Some(0)),
+ ..absent_copy_options()
+ };
+
+ let options = CopyOptions::from(opts);
+ assert_eq!(options.source_content_length_hint, Some(0));
+ assert_eq!(options.chunk, Some(0));
+}
+
+#[test]
+fn test_copy_options_keep_absent_values_absent() {
+ // C++ sends a value behind `has_value == false`; it must not reach core.
+ assert_eq!(
+ CopyOptions::from(absent_copy_options()),
+ CopyOptions::default()
+ );
+}
diff --git a/bindings/cpp/src/utils/options_converter.cpp
b/bindings/cpp/src/utils/options_converter.cpp
index 5fb31bd7fe..3c9bbcebe2 100644
--- a/bindings/cpp/src/utils/options_converter.cpp
+++ b/bindings/cpp/src/utils/options_converter.cpp
@@ -181,4 +181,15 @@ ffi::options::FfiWriteOptions ToFfiOptions(const
WriteOptions &options) {
};
}
+ffi::options::FfiCopyOptions ToFfiOptions(const CopyOptions &options) {
+ return ffi::options::FfiCopyOptions{
+ options.if_not_exists,
+ ToFfiOptionalString(options.if_match),
+ ToFfiOptionalString(options.source_version),
+ ToFfiOptionalU64(options.source_content_length_hint),
+ options.concurrent,
+ ToFfiOptionalUsize(options.chunk),
+ };
+}
+
} // namespace opendal::utils
diff --git a/bindings/cpp/src/utils/options_converter.hpp
b/bindings/cpp/src/utils/options_converter.hpp
index 2046399a21..a701b3bd08 100644
--- a/bindings/cpp/src/utils/options_converter.hpp
+++ b/bindings/cpp/src/utils/options_converter.hpp
@@ -33,5 +33,6 @@ ffi::options::FfiDeleteOptions ToFfiOptions(const
DeleteOptions &options);
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);
} // namespace opendal::utils
diff --git a/bindings/cpp/tests/options_test.cpp
b/bindings/cpp/tests/options_test.cpp
index bbd3684877..ff44044865 100644
--- a/bindings/cpp/tests/options_test.cpp
+++ b/bindings/cpp/tests/options_test.cpp
@@ -1155,3 +1155,130 @@ TEST(OptionsTest, WriteOptionsAppendAndConditionOnFs) {
ThrowsMessage<std::exception>(StartsWith("ConditionNotMatch")));
EXPECT_EQ(op.Read("append"), "hello world");
}
+
+TEST(OptionsTest, CopyOptionsPositionalInitializerReachesBridge) {
+ // Every member in its released order.
+ const opendal::CopyOptions options{
+ /*if_not_exists=*/true,
+ /*if_match=*/"if_match",
+ /*source_version=*/"source_version",
+ /*source_content_length_hint=*/1024,
+ /*concurrent=*/4,
+ /*chunk=*/16,
+ };
+
+ const auto ffi = opendal::utils::ToFfiOptions(options);
+
+ EXPECT_TRUE(ffi.if_not_exists);
+ EXPECT_TRUE(ffi.if_match.has_value);
+ EXPECT_EQ(std::string(ffi.if_match.value), "if_match");
+ EXPECT_TRUE(ffi.source_version.has_value);
+ EXPECT_EQ(std::string(ffi.source_version.value), "source_version");
+ EXPECT_TRUE(ffi.source_content_length_hint.has_value);
+ EXPECT_EQ(ffi.source_content_length_hint.value, std::uint64_t{1024});
+ EXPECT_EQ(ffi.concurrent, std::size_t{4});
+ EXPECT_TRUE(ffi.chunk.has_value);
+ EXPECT_EQ(ffi.chunk.value, std::size_t{16});
+}
+
+TEST(OptionsTest, CopyOptionsStructuredBindingsKeepMemberOrder) {
+ opendal::CopyOptions options;
+ auto &[if_not_exists, if_match, source_version, source_content_length_hint,
+ concurrent, chunk] = options;
+
+ EXPECT_EQ(&if_not_exists, &options.if_not_exists);
+ EXPECT_EQ(&if_match, &options.if_match);
+ EXPECT_EQ(&source_version, &options.source_version);
+ EXPECT_EQ(&source_content_length_hint, &options.source_content_length_hint);
+ EXPECT_EQ(&concurrent, &options.concurrent);
+ EXPECT_EQ(&chunk, &options.chunk);
+}
+
+TEST(OptionsTest, DefaultCopyOptionsReachBridgeAbsent) {
+ const opendal::CopyOptions options;
+
+ const auto ffi = opendal::utils::ToFfiOptions(options);
+
+ EXPECT_FALSE(ffi.if_not_exists);
+ EXPECT_FALSE(ffi.if_match.has_value);
+ EXPECT_FALSE(ffi.source_version.has_value);
+ EXPECT_FALSE(ffi.source_content_length_hint.has_value);
+ EXPECT_EQ(ffi.concurrent, std::size_t{0});
+ EXPECT_FALSE(ffi.chunk.has_value);
+}
+
+TEST(OptionsTest, CopyOptionsSucceedOnFs) {
+ // `memory` cannot copy, and `fs` copies in one step, so it ignores the
+ // execution options.
+ ReaderOptionsTempDir dir;
+ opendal::Operator op("fs", {{"root", dir.path()}});
+ ASSERT_TRUE(op.Info().copy);
+ ASSERT_FALSE(op.Info().copy_can_multi);
+ op.Write("source", "0123456789");
+
+ opendal::CopyOptions hint;
+ hint.source_content_length_hint = 10;
+ opendal::CopyOptions chunked;
+ chunked.concurrent = 2;
+ chunked.chunk = 3;
+ opendal::CopyOptions no_chunk;
+ no_chunk.concurrent = 2;
+ no_chunk.chunk = 0;
+ const std::pair<std::string, opendal::CopyOptions> cases[] = {
+ {"default", opendal::CopyOptions{}},
+ {"hint", hint},
+ {"chunked", chunked},
+ {"no_chunk", no_chunk},
+ };
+ for (const auto &item : cases) {
+ op.Copy("source", "copy_" + item.first, item.second);
+ EXPECT_EQ(op.Read("copy_" + item.first), "0123456789") << item.first;
+ }
+ EXPECT_EQ(op.Read("source"), "0123456789");
+}
+
+TEST(OptionsTest, CopyOptionsErrorsStartWithErrorKind) {
+ 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");
+
+ // `fs` supports none of these, so each one fails before the copy reaches
+ // storage, even when the source is missing.
+ const auto info = op.Info();
+ ASSERT_FALSE(info.copy_with_if_not_exists);
+ ASSERT_FALSE(info.copy_with_if_match);
+ ASSERT_FALSE(info.copy_with_source_version);
+ opendal::CopyOptions if_not_exists;
+ if_not_exists.if_not_exists = true;
+ opendal::CopyOptions if_match;
+ if_match.if_match = "\"etag\"";
+ opendal::CopyOptions source_version;
+ source_version.source_version = "version";
+ const std::pair<std::string, opendal::CopyOptions> cases[] = {
+ {"if_not_exists", if_not_exists},
+ {"if_match", if_match},
+ {"source_version", source_version},
+ };
+ for (const auto &item : cases) {
+ for (const char *source : {"source", "missing"}) {
+ EXPECT_THAT([&] { op.Copy(source, "destination", item.second); },
+ ThrowsMessage<std::exception>(AllOf(
+ StartsWith("Unsupported"),
+ HasSubstr("with the arguments " + item.first + "."))))
+ << item.first << " from " << source;
+ }
+ }
+ EXPECT_EQ(op.Read("destination"), "world");
+
+ // A missing source fails the copy.
+ EXPECT_THAT(
+ [&] { op.Copy("missing", "destination", opendal::CopyOptions{}); },
+ 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 b77cdebdd8..801a110700 100644
--- a/dev/src/generate/cpp.rs
+++ b/dev/src/generate/cpp.rs
@@ -565,8 +565,8 @@ struct OptionsSpec {
cpp_rewrites: &'static [(&'static str, &'static str)],
}
-/// Options structs generated into `include/options.hpp`. `CopyOptions` and
-/// `RenameOptions` are still hand-written in `include/data_structure.hpp`.
+/// Options structs generated into `include/options.hpp`. `RenameOptions` is
+/// still hand-written in `include/data_structure.hpp`.
const OPTIONS: &[OptionsSpec] = {
use OptionKind::*;
@@ -822,6 +822,40 @@ const OPTIONS: &[OptionsSpec] = {
),
],
},
+ OptionsSpec {
+ name: "CopyOptions",
+ brief: "Options for copy operations.",
+ fields: &[
+ ("if_not_exists", Bool),
+ ("if_match", OptionString),
+ ("source_version", OptionString),
+ ("source_content_length_hint", OptionU64),
+ ("concurrent", Usize),
+ ("chunk", OptionUsize),
+ ],
+ cpp_doc: &[],
+ cpp_member_docs: &[],
+ // C++ has no copier, and `Operator::Copy` returns no metadata. A
service
+ // that reports the copied size skips the hint only for that
metadata.
+ cpp_rewrites: &[
+ (
+ "It can use the value to plan copied ranges and to
construct the returned \
+ metadata. An incorrect value can therefore cause
incomplete copy planning, \
+ failed requests, or incorrect result metadata.",
+ "It can use the value to plan copied ranges. An incorrect
value can \
+ therefore cause incomplete copy planning or failed
requests.",
+ ),
+ (
+ "A service can ignore this value when its copy operation
already reports an \
+ authoritative copied size.",
+ "",
+ ),
+ (
+ "Sets concurrent copy operations for this copier.",
+ "Sets concurrent copy operations for
`opendal::Operator::Copy`.",
+ ),
+ ],
+ },
]
};
@@ -996,8 +1030,9 @@ fn option_member(
/// and ``[`crate::layers::SimulateLayer`]`` keep only their code span, and
/// `[the specification][crate::docs::specs::checksum]` reads
/// `the specification`.
-/// - Code spans that name a core capability or error kind use their C++
-/// spelling (see `cpp_code_span`); other code spans stay as written.
+/// - Code spans that name a core capability, an error kind, or a `Self::`
+/// member use their C++ spelling (see `cpp_code_span`); other code spans
+/// stay as written.
/// - Outside code spans, `\` becomes `\\` and `@` becomes `\@`, so Doxygen
/// shows them instead of reading a command. Code spans stay verbatim.
/// - A paragraph that ends with a backslash gets an empty HTML comment,
@@ -1177,8 +1212,8 @@ fn strip_crate_reference_links(line: &str) -> String {
out
}
-/// The C++ spelling of a code span that names a core capability or error
-/// kind, or `None` to keep the code span as written.
+/// The C++ spelling of a code span that names a core capability, an error
+/// kind, or a `Self::` member, or `None` to keep the code span as written.
///
/// - A core capability such as `Capability::list_with_start_after` reads
/// `opendal::Capability::list_with_start_after`, the member of the
@@ -1188,11 +1223,15 @@ fn strip_crate_reference_links(line: &str) -> String {
/// name that begins the `what()` of the exception that C++ callers catch.
///
/// Option members keep their core names in C++, so a span such as
-/// `start_after` stays as written.
+/// `start_after` stays as written, and `Self::source_version` reads
+/// `source_version`.
fn cpp_code_span(code: &str, names: &CppNames) -> Option<String> {
if let Some(kind) = error_kind_name(code, names) {
return Some(kind.to_string());
}
+ if let Some(member) = code.strip_prefix("Self::") {
+ return Some(member.to_string());
+ }
let name = code
.strip_prefix("crate::")
.unwrap_or(code)
@@ -1248,7 +1287,8 @@ fn doxygen_text(text: &str) -> String {
/// change the public C++ member type, or if a spec's `cpp_doc` does not name
/// one of its fields, because the doc would then list stale members. It also
/// fails if a C++ member doc documents no listed field, if a `BytesRange`
-/// field has no C++ doc, or if a C++ rewrite matches no core doc text.
+/// field has no C++ doc, if a C++ rewrite matches no core doc text, or if a
+/// doc names `Self::x` for a field `x` that C++ does not expose.
fn options_models(
parsed: &[core_options::OptionsStruct],
specs: &[OptionsSpec],
@@ -1318,6 +1358,17 @@ fn options_models(
);
}
let member_docs: Vec<&[String]> =
member_docs.iter().map(Vec::as_slice).collect();
+ for line in
core_doc.iter().chain(member_docs.iter().copied().flatten()) {
+ for reference in line.split("`Self::").skip(1) {
+ let member = reference.split('`').next().unwrap_or_default();
+ if !spec.fields.iter().any(|(name, _)| *name == member) {
+ bail!(
+ "`{}` docs name `Self::{member}`, which C++ does not
expose",
+ spec.name
+ );
+ }
+ }
+ }
if !spec.cpp_doc.is_empty() {
let cpp_doc = spec.cpp_doc.join(" ");
if let Some((name, _)) = spec
@@ -1538,7 +1589,7 @@ mod tests {
}
/// `ListOptions`, `StatOptions`, `DeleteOptions`, `ReadRange`,
- /// `ReadOptions`, `ReaderOptions`, and `WriteOptions` as
+ /// `ReadOptions`, `ReaderOptions`, `WriteOptions`, and `CopyOptions` 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,
@@ -1666,6 +1717,19 @@ struct WriteOptions {
std::optional<std::size_t> chunk;
};"#;
+ const MAIN_COPY_OPTIONS: &str = r#"/**
+ * @struct CopyOptions
+ * @brief Options for copy operations.
+ */
+struct CopyOptions {
+ bool if_not_exists{false};
+ std::optional<std::string> if_match;
+ std::optional<std::string> source_version;
+ std::optional<std::uint64_t> source_content_length_hint;
+ std::size_t concurrent{0};
+ std::optional<std::size_t> chunk;
+};"#;
+
fn core_names() -> CppNames {
CppNames::parse(&workspace_dir()).expect("parse core names")
}
@@ -1709,6 +1773,7 @@ struct WriteOptions {
MAIN_READ_OPTIONS,
MAIN_READER_OPTIONS,
MAIN_WRITE_OPTIONS,
+ MAIN_COPY_OPTIONS,
] {
assert!(
code.contains(&code_lines(main)),
@@ -1718,7 +1783,7 @@ struct WriteOptions {
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(), 7);
+ assert_eq!(header.matches("\nstruct ").count(), 8);
// `ReadOptions` needs `ReadRange` declared before it.
let position = |name: &str| header.find(&format!("\nstruct {name}
{{\n")).unwrap();
assert!(position("ReadRange") < position("ReadOptions"));
@@ -1769,6 +1834,7 @@ struct WriteOptions {
.replace("`Capability::", "`opendal::Capability::")
.replace("`crate::ErrorKind::", "`")
.replace("`ErrorKind::", "`")
+ .replace("`Self::", "`")
.replace(
"`Some(0)` has the same behavior as `None`",
"`0` has the same behavior as `std::nullopt`",
@@ -1952,6 +2018,21 @@ single byte range, so `gap` has no effect.",
"[`crate::Operator::write_with`] and
[`crate::Operator::write_options`] without `chunk` pass the whole buffer to the
service writer as one write.",
"`opendal::Operator::Write` without `chunk` passes the whole
buffer to the service writer as one write.",
),
+ (
+ "CopyOptions",
+ "It can use the value to plan copied ranges and to construct the
returned metadata. An incorrect value can therefore cause incomplete copy
planning, failed requests, or incorrect result metadata.",
+ "It can use the value to plan copied ranges. An incorrect value
can therefore cause incomplete copy planning or failed requests.",
+ ),
+ (
+ "CopyOptions",
+ "A service can ignore this value when its copy operation already
reports an authoritative copied size.",
+ "",
+ ),
+ (
+ "CopyOptions",
+ "Sets concurrent copy operations for this copier.",
+ "Sets concurrent copy operations for `opendal::Operator::Copy`.",
+ ),
];
#[test]
@@ -2059,6 +2140,12 @@ single byte range, so `gap` has no effect.",
let reader = &header[header.find(" * @struct
ReaderOptions\n").unwrap()..];
let reader = &reader[..reader.find("\n};\n").unwrap()];
assert!(!reader.contains("::fetch"), "{reader}");
+ // C++ has no copier, and `Operator::Copy` returns no metadata.
+ let copy = &header[header.find(" * @struct CopyOptions\n").unwrap()..];
+ let copy = ©[..copy.find("\n};\n").unwrap()];
+ for rust in ["copier", "returned metadata", "result metadata"] {
+ assert!(!copy.contains(rust), "{copy}");
+ }
// The rules for `Some(0)` and the performance guide match exact core
// text, so a reworded one must not slip through.
for rust in [
@@ -2066,6 +2153,7 @@ single byte range, so `gap` has no effect.",
"crate::",
"ErrorKind::",
"`Capability::",
+ "Self::",
"Some(",
"`None`",
"performance guide",
@@ -2107,6 +2195,7 @@ single byte range, so `gap` has no effect.",
" Reader GetReader(std::string_view path, const ReaderOptions
&options);\n",
" 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",
] {
assert!(
operator.contains(declaration),
@@ -2163,7 +2252,7 @@ single byte range, so `gap` has no effect.",
[
"Requires `opendal::Capability::list_with_limit`.",
"",
- "Rely on `crate::layers::SimulateLayer` and `Self::recursive`.
A",
+ "Rely on `crate::layers::SimulateLayer` and `recursive`. A",
"[plain](https://opendal.apache.org) link and `code` stay.",
]
);
@@ -2471,6 +2560,19 @@ single byte range, so `gap` has no effect.",
"checksum",
]
);
+ let copy = models.iter().find(|m| m.name == "CopyOptions").unwrap();
+ assert_eq!(
+ copy.unexposed,
+ [
+ "if_none_match",
+ "if_version_match",
+ "if_version_not_match",
+ "if_not_changed",
+ "user_metadata",
+ "content_type",
+ "content_language",
+ ]
+ );
}
#[test]
@@ -2486,6 +2588,7 @@ single byte range, so `gap` has no effect.",
("ReadOptions", true),
("ReaderOptions", true),
("WriteOptions", false),
+ ("CopyOptions", false),
]
);
@@ -2511,6 +2614,9 @@ single byte range, so `gap` has no effect.",
assert!(
rust.contains("impl From<ffi::FfiWriteOptions> for
opendal::options::WriteOptions {")
);
+ assert!(
+ rust.contains("impl From<ffi::FfiCopyOptions> for
opendal::options::CopyOptions {")
+ );
}
#[test]
@@ -2662,6 +2768,42 @@ single byte range, so `gap` has no effect.",
OptionU64,
"core `WriteOptions::chunk` has kind OptionU64, but OPTIONS
lists OptionUsize",
),
+ (
+ "CopyOptions",
+ "if_not_exists",
+ OptionString,
+ "core `CopyOptions::if_not_exists` has kind OptionString, but
OPTIONS lists Bool",
+ ),
+ (
+ "CopyOptions",
+ "if_match",
+ Bool,
+ "core `CopyOptions::if_match` has kind Bool, but OPTIONS lists
OptionString",
+ ),
+ (
+ "CopyOptions",
+ "source_version",
+ Bool,
+ "core `CopyOptions::source_version` has kind Bool, but OPTIONS
lists OptionString",
+ ),
+ (
+ "CopyOptions",
+ "source_content_length_hint",
+ OptionUsize,
+ "core `CopyOptions::source_content_length_hint` has kind
OptionUsize, but OPTIONS lists OptionU64",
+ ),
+ (
+ "CopyOptions",
+ "concurrent",
+ OptionUsize,
+ "core `CopyOptions::concurrent` has kind OptionUsize, but
OPTIONS lists Usize",
+ ),
+ (
+ "CopyOptions",
+ "chunk",
+ OptionU64,
+ "core `CopyOptions::chunk` has kind OptionU64, but OPTIONS
lists OptionUsize",
+ ),
] {
let mut changed = parsed.clone();
changed
@@ -2732,6 +2874,25 @@ single byte range, so `gap` has no effect.",
.to_string(),
"OPTIONS member `WriteOptions::user_metadata` is missing from core"
);
+ let mut without_source_version = parsed.clone();
+ without_source_version
+ .iter_mut()
+ .find(|s| s.name == "CopyOptions")
+ .unwrap()
+ .fields
+ .retain(|f| f.name != "source_version");
+ assert_eq!(
+ options_models(&without_source_version, OPTIONS, &core_names())
+ .unwrap_err()
+ .to_string(),
+ "OPTIONS member `CopyOptions::source_version` is missing from core"
+ );
+ // In C++, `Self::source_version` names a member that this spec does
+ // not expose.
+ assert_eq!(
+ error("CopyOptions", &[("source_content_length_hint", OptionU64)]),
+ "`CopyOptions` docs name `Self::source_version`, which C++ does
not expose"
+ );
assert_eq!(
error("WriteOptions", &[("expires", OptionDuration)]),
"the C++ bridge cannot carry `WriteOptions::expires` of kind
OptionDuration yet"
@@ -2917,6 +3078,50 @@ single byte range, so `gap` has no effect.",
);
}
+ let copy = OPTIONS.iter().find(|s| s.name == "CopyOptions").unwrap();
+ let rewordings = [
+ (
+ "source_content_length_hint",
+ "construct the returned metadata",
+ "build the returned metadata",
+ ),
+ (
+ "source_content_length_hint",
+ "an authoritative copied size",
+ "an exact copied size",
+ ),
+ ("concurrent", "for this copier", "for the copier"),
+ ];
+ assert_eq!(rewordings.len(), copy.cpp_rewrites.len());
+ for (i, (field, core_text, reworded_text)) in
rewordings.into_iter().enumerate() {
+ let (from, _) = copy.cpp_rewrites[i];
+ let mut reworded = parsed_options();
+ let doc = &mut reworded
+ .iter_mut()
+ .find(|s| s.name == "CopyOptions")
+ .unwrap()
+ .fields
+ .iter_mut()
+ .find(|f| f.name == field)
+ .unwrap()
+ .doc;
+ assert!(
+ doc.iter().any(|line| line.contains(core_text)),
+ "{core_text}"
+ );
+ for line in doc.iter_mut() {
+ *line = line.replace(core_text, reworded_text);
+ }
+ assert_eq!(
+ options_models(&reworded, OPTIONS, &core_names())
+ .unwrap_err()
+ .to_string(),
+ format!(
+ "core `CopyOptions` docs no longer contain `{from}`;
review its C++ rewrite"
+ )
+ );
+ }
+
// A rewrite also applies to member docs, and a match there counts.
let spec = OptionsSpec {
name: "ReadOptions",