Xuanwo commented on code in PR #2461:
URL: 
https://github.com/apache/incubator-opendal/pull/2461#discussion_r1228949996


##########
core/src/layers/capability_check.rs:
##########
@@ -0,0 +1,201 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::fmt::Debug;
+use std::fmt::Formatter;
+
+use async_trait::async_trait;
+
+use crate::raw::*;
+use crate::types::Error;
+use crate::types::ErrorKind;
+use crate::types::Result;
+use crate::Capability;
+
+trait CheckCapability {

Review Comment:
   We don't need a new trait for this.



##########
core/src/layers/capability_check.rs:
##########
@@ -0,0 +1,201 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::fmt::Debug;
+use std::fmt::Formatter;
+
+use async_trait::async_trait;
+
+use crate::raw::*;
+use crate::types::Error;
+use crate::types::ErrorKind;
+use crate::types::Result;
+use crate::Capability;
+
+trait CheckCapability {
+    // TODO: Should we return the unsupported option in the result?
+    fn check_capability(&self, cap: &Capability) -> bool;
+}
+
+macro_rules! impl_check_fn_item {
+    ($self:ident, $cap:ident @ $opt_ident:ident => $cap_ident:ident, 
$($rest:tt)*) => {
+        impl_check_fn_item!($self, $cap @ $opt_ident => $cap_ident);
+        impl_check_fn_item!($self, $cap @ $($rest)*);
+    };
+    ($self:ident, $cap:ident @ $opt_ident:ident => $cap_ident:ident) => {
+        let has_opt = $self.$opt_ident().is_some();
+        if has_opt && !$cap.$cap_ident {
+            return false;
+        }
+    };
+    ($self:ident, $cap:ident @) => {};
+}
+
+macro_rules! impl_check_fn {
+    ($op_type:ty { $($body:tt)* }) => {
+        impl CheckCapability for $op_type {
+            fn check_capability(&self, cap: &Capability) -> bool {
+                impl_check_fn_item!(self, cap @ $($body)*);
+                true
+            }
+        }
+    };
+}
+
+impl_check_fn!(OpRead {

Review Comment:
   I prefer to expand by hand instead of macro which is easy to maintain.



##########
core/src/types/operator/builder.rs:
##########
@@ -355,6 +355,28 @@ impl<A: Accessor> OperatorBuilder<A> {
         }
     }
 
+    /// Enable the strict mode for the built Operator.
+    ///
+    /// In strict mode, Operator will return error when the given
+    /// operation arguments contain options that are unsupported by
+    /// the service. By default (without calling this method), all
+    /// unsupported options are ignored silently.
+    ///
+    /// # Notes
+    ///
+    /// `OperatorBuilder::with_strict()` uses static dispatch which
+    /// is zero cost. `Operator::with_strict()` uses dynamic dispatch
+    /// which has a bit runtime overhead with an extra vtable lookup
+    /// and unable to inline.
+    ///
+    /// It's always recommended to use `OperatorBuilder::with_strict()`.
+    #[must_use]
+    pub fn with_strict(

Review Comment:
   Check operation is mandatory, while checking args for the operation is 
optional. We can implement operation check first.



##########
core/src/layers/capability_check.rs:
##########
@@ -0,0 +1,201 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::fmt::Debug;
+use std::fmt::Formatter;
+
+use async_trait::async_trait;
+
+use crate::raw::*;
+use crate::types::Error;
+use crate::types::ErrorKind;
+use crate::types::Result;
+use crate::Capability;
+
+trait CheckCapability {
+    // TODO: Should we return the unsupported option in the result?
+    fn check_capability(&self, cap: &Capability) -> bool;
+}
+
+macro_rules! impl_check_fn_item {
+    ($self:ident, $cap:ident @ $opt_ident:ident => $cap_ident:ident, 
$($rest:tt)*) => {
+        impl_check_fn_item!($self, $cap @ $opt_ident => $cap_ident);
+        impl_check_fn_item!($self, $cap @ $($rest)*);
+    };
+    ($self:ident, $cap:ident @ $opt_ident:ident => $cap_ident:ident) => {
+        let has_opt = $self.$opt_ident().is_some();
+        if has_opt && !$cap.$cap_ident {
+            return false;
+        }
+    };
+    ($self:ident, $cap:ident @) => {};
+}
+
+macro_rules! impl_check_fn {
+    ($op_type:ty { $($body:tt)* }) => {
+        impl CheckCapability for $op_type {
+            fn check_capability(&self, cap: &Capability) -> bool {
+                impl_check_fn_item!(self, cap @ $($body)*);
+                true
+            }
+        }
+    };
+}
+
+impl_check_fn!(OpRead {
+    if_match => read_with_if_match,
+    if_none_match => read_with_if_none_match,
+    override_cache_control => read_with_override_cache_control,
+    override_content_disposition => read_with_override_content_disposition,
+});
+
+macro_rules! check_capability {
+    ($msg:literal, $self:ident, $args:ident) => {
+        let meta = $self.metadata();
+        if !$args.check_capability(&meta.capability()) {
+            return Err(Error::new(
+                ErrorKind::UnsupportedOption,
+                "args contain unsupported options",
+            )
+            .with_operation($msg));
+        }
+    };
+}
+
+/// Check whether the given operation arguments are supported by the
+/// underlying services.
+///
+/// # Notes
+///
+/// CapabilityCheckLayer is not a public accessible layer that can be
+/// used by external users. OpenDAL will apply it automatically when
+/// the operator is in strict mode.
+pub struct CapabilityCheckLayer;

Review Comment:
   We can do this in `CompleleteLayer` instead of adding a new layer.



##########
core/src/types/error.rs:
##########
@@ -53,6 +53,9 @@ pub enum ErrorKind {
     Unexpected,
     /// Underlying service doesn't support this operation.
     Unsupported,
+    /// The given argument contains some options that are not supported by the
+    /// underlying service.
+    UnsupportedOption,

Review Comment:
   We don't want to add a new error code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to