-
Notifications
You must be signed in to change notification settings - Fork 276
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
Separate matchers as a bucket filter #3229
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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,108 @@ | ||
// Copyright 2020-2024 Buf Technologies, Inc. | ||
// | ||
// Licensed 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. | ||
|
||
package storage | ||
|
||
import ( | ||
"context" | ||
"io/fs" | ||
|
||
"github.com/bufbuild/buf/private/pkg/normalpath" | ||
) | ||
|
||
// FilterReadBucket filters the ReadBucket. | ||
// | ||
// If the Matchers are empty, the original ReadBucket is returned. | ||
// If there is more than one Matcher, the Matchers are anded together. | ||
func FilterReadBucket(readBucket ReadBucket, matchers ...Matcher) ReadBucket { | ||
if len(matchers) == 0 { | ||
return readBucket | ||
} | ||
return newFilterReadBucketCloser(readBucket, nil, MatchAnd(matchers...)) | ||
} | ||
|
||
// FilterReadBucketCloser filters the ReadBucketCloser. | ||
// | ||
// If the Matchers are empty, the original ReadBucketCloser is returned. | ||
// If there is more than one Matcher, the Matchers are anded together. | ||
func FilterReadBucketCloser(readBucketCloser ReadBucketCloser, matchers ...Matcher) ReadBucket { | ||
if len(matchers) == 0 { | ||
return readBucketCloser | ||
} | ||
return newFilterReadBucketCloser(readBucketCloser, readBucketCloser.Close, MatchAnd(matchers...)) | ||
} | ||
|
||
type filterReadBucketCloser struct { | ||
delegate ReadBucket | ||
closeFunc func() error | ||
matcher Matcher | ||
} | ||
|
||
func newFilterReadBucketCloser( | ||
delegate ReadBucket, | ||
closeFunc func() error, | ||
matcher Matcher, | ||
) *filterReadBucketCloser { | ||
return &filterReadBucketCloser{ | ||
delegate: delegate, | ||
closeFunc: closeFunc, | ||
matcher: matcher, | ||
} | ||
} | ||
|
||
func (r *filterReadBucketCloser) Get(ctx context.Context, path string) (ReadObjectCloser, error) { | ||
path, err := normalpath.NormalizeAndValidate(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !r.matcher.MatchPath(path) { | ||
return nil, &fs.PathError{Op: "read", Path: path, Err: fs.ErrNotExist} | ||
} | ||
return r.delegate.Get(ctx, path) | ||
} | ||
|
||
func (r *filterReadBucketCloser) Stat(ctx context.Context, path string) (ObjectInfo, error) { | ||
path, err := normalpath.NormalizeAndValidate(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !r.matcher.MatchPath(path) { | ||
return nil, &fs.PathError{Op: "read", Path: path, Err: fs.ErrNotExist} | ||
} | ||
return r.delegate.Stat(ctx, path) | ||
} | ||
|
||
func (r *filterReadBucketCloser) Walk(ctx context.Context, prefix string, f func(ObjectInfo) error) error { | ||
prefix, err := normalpath.NormalizeAndValidate(prefix) | ||
if err != nil { | ||
return err | ||
} | ||
return r.delegate.Walk( | ||
ctx, | ||
prefix, | ||
func(objectInfo ObjectInfo) error { | ||
if !r.matcher.MatchPath(objectInfo.Path()) { | ||
return nil | ||
} | ||
return f(objectInfo) | ||
}, | ||
) | ||
} | ||
|
||
func (r *filterReadBucketCloser) Close() error { | ||
if r.closeFunc != nil { | ||
return r.closeFunc() | ||
} | ||
return nil | ||
} |
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 |
---|---|---|
|
@@ -28,12 +28,6 @@ type Mapper interface { | |
// The returned path is expected to be normalized and validated. | ||
// If the path cannot be mapped, this returns false. | ||
MapPath(path string) (string, bool) | ||
// Map maps the prefix to the full prefix. | ||
// | ||
// The path is expected to be normalized and validated. | ||
// The returned path is expected to be normalized and validated. | ||
// If the path cannot be mapped, this returns false. | ||
MapPrefix(prefix string) (string, bool) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// UnmapFullPath maps the full path to the path. | ||
// | ||
// Returns false if the full path does not apply. | ||
|
@@ -58,7 +52,7 @@ func MapOnPrefix(prefix string) Mapper { | |
// | ||
// If the Mappers are empty, a no-op Mapper is returned. | ||
// If there is more than one Mapper, the Mappers are called in order | ||
// for UnmapFullPath, with the order reversed for MapPath and MapPrefix. | ||
// for UnmapFullPath, with the order reversed for MapPath. | ||
// | ||
// That is, order these assuming you are starting with a full path and | ||
// working to a path. | ||
|
@@ -83,10 +77,6 @@ func (p prefixMapper) MapPath(path string) (string, bool) { | |
return normalpath.Join(p.prefix, path), true | ||
} | ||
|
||
func (p prefixMapper) MapPrefix(prefix string) (string, bool) { | ||
return normalpath.Join(p.prefix, prefix), true | ||
} | ||
|
||
func (p prefixMapper) UnmapFullPath(fullPath string) (string, bool, error) { | ||
if !normalpath.EqualsOrContainsPath(p.prefix, fullPath, normalpath.Relative) { | ||
return "", false, nil | ||
|
@@ -108,10 +98,6 @@ func (c chainMapper) MapPath(path string) (string, bool) { | |
return c.mapFunc(path, Mapper.MapPath) | ||
} | ||
|
||
func (c chainMapper) MapPrefix(prefix string) (string, bool) { | ||
return c.mapFunc(prefix, Mapper.MapPrefix) | ||
} | ||
|
||
func (c chainMapper) UnmapFullPath(fullPath string) (string, bool, error) { | ||
path := fullPath | ||
var matches bool | ||
|
@@ -152,10 +138,6 @@ func (n nopMapper) MapPath(path string) (string, bool) { | |
return path, true | ||
} | ||
|
||
func (n nopMapper) MapPrefix(prefix string) (string, bool) { | ||
return prefix, true | ||
} | ||
|
||
func (nopMapper) UnmapFullPath(fullPath string) (string, bool, error) { | ||
return fullPath, true, nil | ||
} | ||
|
Oops, something went wrong.
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.
Chose
FilterReadBucket
overMatchReadBucket
, the naming of filter with the filter funcs being called matchers I think is still clear. We could also rename Matchers to FIlters?