This is an automated email from the ASF dual-hosted git repository.
silver pushed a commit to branch cpp-more-operation
in repository https://gitbox.apache.org/repos/asf/incubator-opendal.git
The following commit(s) were added to refs/heads/cpp-more-operation by this
push:
new 12260d097 update impl to optionalstring
12260d097 is described below
commit 12260d0977ce471e4b8e15e0c9a588aa810d87ed
Author: silver-ymz <[email protected]>
AuthorDate: Sat Sep 2 18:33:20 2023 +0800
update impl to optionalstring
Signed-off-by: silver-ymz <[email protected]>
---
bindings/cpp/include/opendal.hpp | 6 +-
bindings/cpp/src/lib.rs | 124 +++++++++++++++++----------------------
bindings/cpp/src/opendal.cpp | 54 ++++++++---------
3 files changed, 80 insertions(+), 104 deletions(-)
diff --git a/bindings/cpp/include/opendal.hpp b/bindings/cpp/include/opendal.hpp
index 43e227534..0a13030c5 100644
--- a/bindings/cpp/include/opendal.hpp
+++ b/bindings/cpp/include/opendal.hpp
@@ -34,9 +34,9 @@ namespace opendal {
* @brief The mode of the entry
*/
enum EntryMode {
- FILE,
- DIR,
- UNKNOWN,
+ FILE = 1,
+ DIR = 2,
+ UNKNOWN = 0,
};
/**
diff --git a/bindings/cpp/src/lib.rs b/bindings/cpp/src/lib.rs
index 143bf62b1..66498d9e7 100644
--- a/bindings/cpp/src/lib.rs
+++ b/bindings/cpp/src/lib.rs
@@ -26,21 +26,26 @@ mod ffi {
value: String,
}
+ enum EntryMode {
+ File = 1,
+ Dir = 2,
+ Unknown = 0,
+ }
+
+ struct OptionalString {
+ has_value: bool,
+ value: String,
+ }
+
struct Metadata {
- // tag layout: (8 bits flagset)
- // 0-1: mode, 2: has_cache_control, 3: has_content_disposition, 4:
has_content_md5,
- // 5: has_content_type, 6: has_etag, 7: has_last_modified
- //
- // mode enum: (2 bits)
- // 1: file, 2: dir, 0,3: unknown
- tag: u8,
+ mode: EntryMode,
content_length: u64,
- cache_control: String,
- content_disposition: String,
- content_md5: String,
- content_type: String,
- etag: String,
- last_modified: String,
+ cache_control: OptionalString,
+ content_disposition: OptionalString,
+ content_md5: OptionalString,
+ content_type: OptionalString,
+ etag: OptionalString,
+ last_modified: OptionalString,
}
struct Entry {
@@ -123,66 +128,20 @@ impl Operator {
impl From<od::Metadata> for ffi::Metadata {
fn from(meta: od::Metadata) -> Self {
- let mut tag = 0u8;
-
- match meta.mode() {
- od::EntryMode::FILE => tag |= 0b0000_0001,
- od::EntryMode::DIR => tag |= 0b0000_0010,
- _ => {}
- }
-
+ let mode = meta.mode().into();
let content_length = meta.content_length();
-
- let cache_control = match meta.cache_control() {
- Some(v) => {
- tag |= 0b0000_0100;
- v.to_owned()
- }
- None => String::new(),
- };
-
- let content_disposition = match meta.content_disposition() {
- Some(v) => {
- tag |= 0b0000_1000;
- v.to_owned()
- }
- None => String::new(),
- };
-
- let content_md5 = match meta.content_md5() {
- Some(v) => {
- tag |= 0b0001_0000;
- v.to_owned()
- }
- None => String::new(),
- };
-
- let content_type = match meta.content_type() {
- Some(v) => {
- tag |= 0b0010_0000;
- v.to_owned()
- }
- None => String::new(),
- };
-
- let etag = match meta.etag() {
- Some(v) => {
- tag |= 0b0100_0000;
- v.to_owned()
- }
- None => String::new(),
- };
-
- let last_modified = match meta.last_modified() {
- Some(v) => {
- tag |= 0b1000_0000;
- v.to_rfc3339_opts(chrono::SecondsFormat::Nanos, false)
- }
- None => String::new(),
- };
+ let cache_control = meta.cache_control().map(ToOwned::to_owned).into();
+ let content_disposition =
meta.content_disposition().map(ToOwned::to_owned).into();
+ let content_md5 = meta.content_md5().map(ToOwned::to_owned).into();
+ let content_type = meta.content_type().map(ToOwned::to_owned).into();
+ let etag = meta.etag().map(ToOwned::to_owned).into();
+ let last_modified = meta
+ .last_modified()
+ .map(|time| time.to_rfc3339_opts(chrono::SecondsFormat::Nanos,
false))
+ .into();
Self {
- tag,
+ mode,
content_length,
cache_control,
content_disposition,
@@ -200,3 +159,28 @@ impl From<od::Entry> for ffi::Entry {
Self { path }
}
}
+
+impl From<od::EntryMode> for ffi::EntryMode {
+ fn from(mode: od::EntryMode) -> Self {
+ match mode {
+ od::EntryMode::FILE => Self::File,
+ od::EntryMode::DIR => Self::Dir,
+ _ => Self::Unknown,
+ }
+ }
+}
+
+impl From<Option<String>> for ffi::OptionalString {
+ fn from(s: Option<String>) -> Self {
+ match s {
+ Some(s) => Self {
+ has_value: true,
+ value: s,
+ },
+ None => Self {
+ has_value: false,
+ value: String::default(),
+ },
+ }
+ }
+}
diff --git a/bindings/cpp/src/opendal.cpp b/bindings/cpp/src/opendal.cpp
index 7b12588b8..c0b4a3d90 100644
--- a/bindings/cpp/src/opendal.cpp
+++ b/bindings/cpp/src/opendal.cpp
@@ -85,41 +85,33 @@ std::vector<Entry> Operator::list(std::string_view path) {
return entries;
}
-Metadata::Metadata(ffi::Metadata &&other) {
- if (other.tag & 1) {
- type = EntryMode::FILE;
- } else if (other.tag & 0b10) {
- type = EntryMode::DIR;
- } else {
- type = EntryMode::UNKNOWN;
- }
+std::optional<std::string> parse_optional_string(ffi::OptionalString &&s);
+Metadata::Metadata(ffi::Metadata &&other) {
+ type = static_cast<EntryMode>(other.mode);
content_length = other.content_length;
-
- if (other.tag & 0b100) {
- cache_control = std::string(std::move(other.cache_control));
- }
-
- if (other.tag & 0b1000) {
- content_disposition = std::string(std::move(other.content_disposition));
- }
-
- if (other.tag & 0b10000) {
- content_md5 = std::string(std::move(other.content_md5));
+ cache_control = parse_optional_string(std::move(other.cache_control));
+ content_disposition =
+ parse_optional_string(std::move(other.content_disposition));
+ content_type = parse_optional_string(std::move(other.content_type));
+ content_md5 = parse_optional_string(std::move(other.content_md5));
+ etag = parse_optional_string(std::move(other.etag));
+ auto last_modified_str =
+ parse_optional_string(std::move(other.last_modified));
+ if (last_modified_str.has_value()) {
+ last_modified =
+ boost::posix_time::from_iso_string(last_modified_str.value());
}
+}
- if (other.tag & 0b100000) {
- content_type = std::string(std::move(other.content_type));
- }
+Entry::Entry(ffi::Entry &&other) : path(std::move(other.path)) {}
- if (other.tag & 0b1000000) {
- etag = std::string(std::move(other.etag));
- }
+// helper functions
- if (other.tag & 0b10000000) {
- last_modified = boost::posix_time::from_iso_string(
- std::string(std::move(other.last_modified)));
+std::optional<std::string> parse_optional_string(ffi::OptionalString &&s) {
+ if (s.has_value) {
+ return std::string(std::move(s.value));
+ } else {
+ return std::nullopt;
}
-}
-
-Entry::Entry(ffi::Entry &&other) : path(std::move(other.path)) {}
\ No newline at end of file
+}
\ No newline at end of file