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
Add new filtering fs: FilePredicateFs #309
base: master
Are you sure you want to change the base?
Conversation
A filtered view on predicates which takes file path as an argument, any file will be treated as non-existing when the predicate returns false.
This one is pretty nice as it may filter according to any kind of path related rule, not just a regular expression. You removed a Imo it shouldn't handle directories in any special way. I could think of a use case where I want my Fs to be only able to interact within specific directories somewhat like BasePathFs but with multiple directories e.g. provided in a regular expression or in a list. |
This is not related with this request directly but I think HttpFs is not filtering fs.
Good point. Consider the situation where we
(yes, this will be minor) If we apply the function with both files and dirs, Instead of it, as you describe, it will be clear to combine |
In case that you provide the underlying ^ Might as well rename |
@jxsl13 thanks for reviewing. |
Sorry for being very late to the game, this looks very useful. I have read the discussion above, but I suspect that we're missing out on some potential when we apply this to files (and not dirs) only. If I walk a directory I certainly want to be able to skip a dir before reading it. How about making the predicate: func(isDir bool, path string) bool |
@bep thank you. |
predicatefs.go
Outdated
return nil, err | ||
} | ||
for _, i := range pfi { | ||
if i.IsDir() || f.pred(filepath.Join(f.f.Name(), i.Name())) { |
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.
@satotake I'm not sure what you mean by "skip directories"; to me it looks like every directory is included.
I would have found it much more useful if the above was replaced with:
if f.pred(i.IsDir(), filepath.Join(f.f.Name(), i.Name())) {
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.
One might as well just pass the file info object instead of a bool which can then be manually checked whether the path is a directory or a file or a symlink, etc.
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.
That would break if you want to use this with fs.ReadDirFile
(see my recent commit to the main branch).
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.
@bep I got your point.
isDir
will be nice to ignore some dirs without scanning their children, for instance.
Fixed.
While golang's built-in
regexp
package has the advantage, constant time guarantees,it (RE2) has some limitations to express complex rules compared to other languages (PCRE2).
(For example, it does not have backtracking.)
Here, I would like to introduce a new filtering backend,
PredicateFs
, to make afero's filtering more flexible and composable.PredicateFs
is a filtered view on predicates which takes file path as an argument.Any file will be treated as non-existing when the predicate returns false.
Unlike
RegexpFs
, the fs targets not file name but file path.Like,
RegexpFs
, files will not be created when the predicate returns false and directories are always not filtered.