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


##########
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:
   > If all op types will have the same check function, I think we should use a 
trait to abstract it.
   
   No, I don't think so. We only need to add a trait if we want to work with a 
trait without knowing its actual type. However, all of our logic here always 
receives an `OpXxx` struct, so we can call its functions directly.
   
   In OpenDAL, we prefer simplicity.



-- 
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