-
Notifications
You must be signed in to change notification settings - Fork 533
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat(bindings/ruby): add lister #5600
Merged
+256
−0
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# 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. | ||
|
||
# frozen_string_literal: true | ||
|
||
module OpenDAL | ||
class Entry | ||
def to_h | ||
{ | ||
path: path, | ||
metadata: metadata | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// 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::cell::RefCell; | ||
|
||
use magnus::class; | ||
use magnus::method; | ||
use magnus::prelude::*; | ||
use magnus::Error; | ||
use magnus::RModule; | ||
use magnus::Ruby; | ||
use magnus::Value; | ||
|
||
use crate::metadata::Metadata; | ||
use crate::*; | ||
|
||
/// Represents the result when list a directory | ||
#[magnus::wrap(class = "OpenDAL::Lister", free_immediately, size)] | ||
pub struct Lister(RefCell<ocore::BlockingLister>); | ||
|
||
/// Entry returned by Lister to represent a path and it's relative metadata. | ||
#[magnus::wrap(class = "OpenDAL::Entry", free_immediately, size)] | ||
pub struct Entry(ocore::Entry); | ||
|
||
impl Entry { | ||
/// Gets the path of entry. Path is relative to operator's root. | ||
/// | ||
/// Only valid in current operator. | ||
/// | ||
/// If this entry is a dir, `path` MUST end with `/` | ||
/// Otherwise, `path` MUST NOT end with `/`. | ||
fn path(&self) -> Result<&str, Error> { | ||
Ok(self.0.path()) | ||
} | ||
|
||
/// Gets the name of entry. Name is the last segment of path. | ||
/// | ||
/// If this entry is a dir, `name` MUST end with `/` | ||
/// Otherwise, `name` MUST NOT end with `/`. | ||
fn name(&self) -> Result<&str, Error> { | ||
Ok(self.0.name()) | ||
} | ||
|
||
/// Fetches the metadata of this entry. | ||
fn metadata(&self) -> Result<Metadata, Error> { | ||
Ok(Metadata::new(self.0.metadata().clone())) | ||
} | ||
} | ||
|
||
impl Lister { | ||
/// Creates a new blocking Lister. | ||
pub fn new(inner: ocore::BlockingLister) -> Self { | ||
Self(RefCell::new(inner)) | ||
} | ||
|
||
/// Returns the next element. | ||
fn each(ruby: &Ruby, rb_self: &Self) -> Result<(), Error> { | ||
while let Some(Ok(entry)) = rb_self.0.borrow_mut().next() { | ||
// we don't need the return value of the yield block | ||
let _ = ruby.yield_value::<lister::Entry, Value>(Entry(entry)); | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn include(ruby: &Ruby, gem_module: &RModule) -> Result<(), Error> { | ||
let entry_class = gem_module.define_class("Entry", class::object())?; | ||
entry_class.define_method("path", method!(Entry::path, 0))?; | ||
entry_class.define_method("name", method!(Entry::name, 0))?; | ||
entry_class.define_method("metadata", method!(Entry::metadata, 0))?; | ||
|
||
let lister_class = gem_module.define_class("Lister", class::object())?; | ||
let _ = lister_class | ||
.include_module(ruby.module_enumerable()) | ||
.map_err(|err| Error::new(ruby.exception_runtime_error(), err.to_string()))?; | ||
lister_class.define_method("each", method!(Lister::each, 0))?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 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. | ||
|
||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
require "tmpdir" | ||
|
||
class ListerTest < ActiveSupport::TestCase | ||
setup do | ||
@root = Dir.mktmpdir | ||
File.write("#{@root}/sample", "Sample data for testing") | ||
Dir.mkdir("#{@root}/sub") | ||
File.write("#{@root}/sub/sample", "Sample data for testing") | ||
@op = OpenDAL::Operator.new("fs", {"root" => @root}) | ||
end | ||
|
||
test "lists the directory" do | ||
lister = @op.list("") | ||
|
||
lists = lister.map(&:to_h).map { |e| e[:path] }.sort | ||
|
||
assert_equal ["/", "sample", "sub/"], lists | ||
end | ||
|
||
test "list returns the entry" do | ||
entry = @op.list("/").first | ||
|
||
assert entry.is_a?(OpenDAL::Entry) | ||
assert_equal "sample", entry.name | ||
end | ||
|
||
test "entry has the metadata" do | ||
metadata = @op.list("sample").first.metadata | ||
|
||
assert metadata.file? | ||
assert !metadata.dir? | ||
end | ||
|
||
test "scans the directory" do | ||
lister = @op.scan("") | ||
|
||
lists = lister.map(&:to_h).map { |e| e[:path] }.sort | ||
|
||
assert_equal ["/", "sample", "sub/", "sub/sample"], lists | ||
end | ||
|
||
test "scan returns the entry" do | ||
entry = @op.scan("sample").first | ||
|
||
assert entry.is_a?(OpenDAL::Entry) | ||
assert_equal "sample", entry.name | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi,
scan
has been deprecated, and bindings should not expose such an API. Instead, thelist
function should accept an argument that allows settingrecursive: true
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, fixed.