Skip to content

Commit 6833a10

Browse files
authored
feat(bindings/ruby): add operator info (#5584)
1 parent 0c44e07 commit 6833a10

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

bindings/ruby/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mod io;
2929
mod lister;
3030
mod metadata;
3131
mod operator;
32+
mod operator_info;
3233

3334
pub fn format_magnus_error(err: ocore::Error) -> Error {
3435
Error::new(exception::runtime_error(), err.to_string())
@@ -43,6 +44,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
4344
let _ = capability::include(&gem_module);
4445
let _ = io::include(&gem_module);
4546
let _ = lister::include(&ruby, &gem_module);
47+
let _ = operator_info::include(&gem_module);
4648

4749
Ok(())
4850
}

bindings/ruby/src/operator.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use crate::capability::Capability;
4040
use crate::io::Io;
4141
use crate::lister::Lister;
4242
use crate::metadata::Metadata;
43+
use crate::operator_info::OperatorInfo;
4344
use crate::*;
4445

4546
/// @yard
@@ -241,6 +242,11 @@ impl Operator {
241242

242243
Ok(Lister::new(lister))
243244
}
245+
246+
/// Gets meta information of the underlying accessor.
247+
fn info(&self) -> Result<OperatorInfo, Error> {
248+
Ok(OperatorInfo(self.0.info()))
249+
}
244250
}
245251

246252
pub fn include(gem_module: &RModule) -> Result<(), Error> {
@@ -258,6 +264,7 @@ pub fn include(gem_module: &RModule) -> Result<(), Error> {
258264
class.define_method("copy", method!(Operator::copy, 2))?;
259265
class.define_method("open", method!(Operator::open, 2))?;
260266
class.define_method("list", method!(Operator::list, -1))?;
267+
class.define_method("info", method!(Operator::info, 0))?;
261268

262269
Ok(())
263270
}

bindings/ruby/src/operator_info.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
#![allow(
19+
rustdoc::broken_intra_doc_links,
20+
reason = "YARD's syntax for documentation"
21+
)]
22+
23+
use magnus::class;
24+
use magnus::method;
25+
use magnus::prelude::*;
26+
use magnus::Error;
27+
use magnus::RModule;
28+
29+
use crate::capability::Capability;
30+
use crate::*;
31+
32+
/// Metadata for operator, users can use this metadata to get information of operator.
33+
#[magnus::wrap(class = "OpenDAL::OperatorInfo", free_immediately, size)]
34+
pub struct OperatorInfo(pub ocore::OperatorInfo);
35+
36+
impl OperatorInfo {
37+
/// @yard
38+
/// @def scheme
39+
/// Returns the scheme string of the operator.
40+
/// @return [String]
41+
pub fn scheme(&self) -> &str {
42+
self.0.scheme().into()
43+
}
44+
45+
/// @yard
46+
/// @def root
47+
/// Returns the root path of the operator, will be in format like `/path/to/dir/`
48+
/// @return [String]
49+
pub fn root(&self) -> String {
50+
self.0.root()
51+
}
52+
53+
/// @yard
54+
/// @def name
55+
/// Returns the name of backend, could be empty if underlying backend doesn't have namespace concept.
56+
///
57+
/// For example:
58+
///
59+
/// - name for `s3` => bucket name
60+
/// - name for `azblob` => container name
61+
///
62+
/// @return [String]
63+
pub fn name(&self) -> String {
64+
self.0.name()
65+
}
66+
67+
/// @yard
68+
/// @def capability
69+
/// Returns the [`Full Capability`] of the operator.
70+
/// @return [Capability]
71+
pub fn capability(&self) -> Capability {
72+
Capability::new(self.0.full_capability())
73+
}
74+
}
75+
76+
pub fn include(gem_module: &RModule) -> Result<(), Error> {
77+
let class = gem_module.define_class("OperatorInfo", class::object())?;
78+
class.define_method("scheme", method!(OperatorInfo::scheme, 0))?;
79+
class.define_method("root", method!(OperatorInfo::root, 0))?;
80+
class.define_method("name", method!(OperatorInfo::name, 0))?;
81+
class.define_method("capability", method!(OperatorInfo::capability, 0))?;
82+
83+
Ok(())
84+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# frozen_string_literal: true
19+
20+
require "test_helper"
21+
22+
class OperatorInfoTest < ActiveSupport::TestCase
23+
setup do
24+
@op = OpenDAL::Operator.new("memory", {})
25+
end
26+
27+
test "returns meta information" do
28+
info = @op.info
29+
30+
assert_equal "memory", info.scheme
31+
assert_equal "/", info.root
32+
assert info.name.length > 0
33+
assert info.capability.stat
34+
end
35+
end

0 commit comments

Comments
 (0)