Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree predicates? #30

Open
MichaelHatherly opened this issue Aug 27, 2020 · 3 comments
Open

Tree predicates? #30

MichaelHatherly opened this issue Aug 27, 2020 · 3 comments

Comments

@MichaelHatherly
Copy link

(I might have completely overlooked it if it's already implemented?)

I'd like to check whether a given path exists in a FileTree, such as

t = FileTree(".")
"my/file.ext" in t

Does anything like this currently exist? Or is there an idiomatic alternative that's reasonably efficient? There's the getindex methods that take globs or regex, but I'm just needing a Bool result given a path.

@shashi
Copy link
Owner

shashi commented Aug 29, 2020

That's would be a really nice API addition! I agree it's super clunky to do that right now.

@shashi
Copy link
Owner

shashi commented Aug 29, 2020

an easy way to do this would be:

!isempty(t[glob"my/file.ext"])

@MichaelHatherly
Copy link
Author

With a glob it still throws an error if _glob_filter returns nothing:

function Base.getindex(t::FileTree, g::GlobMatch)
sub = _glob_filter(t, name(t), g.pattern...)
if sub === nothing
error("$g did not match in tree")
end
sub
end

Should that method be returning an empty tree instead?

A couple of one-liner in methods could be added to bypass that nothing check I guess?

Base.in(g::GlobMatch, t::FileTree) = _glob_filter(t, name(t), g.pattern...) !== nothing
Base.in(p::PathLike, t::FileTree) = GlobMatch(string(p)) in t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants