Skip to content

Commit bc5ed90

Browse files
authored
Separate matchers as a bucket filter (#3229)
Adds a new read bucket Filter that works exclusively on Matchers. Buckets that use Matchers as Mappers have been split as a Mapper and Filter bucket. The method MapPrefix on Mappers has been removed. Matchers no longer implement Mappers.
1 parent 5b37673 commit bc5ed90

File tree

14 files changed

+188
-141
lines changed

14 files changed

+188
-141
lines changed

private/buf/bufformat/bufformat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func FormatModuleSet(ctx context.Context, moduleSet bufmodule.ModuleSet) (_ stor
4343
// FormatBucket formats the .proto files in the bucket and returns a new bucket with the formatted files.
4444
func FormatBucket(ctx context.Context, bucket storage.ReadBucket) (_ storage.ReadBucket, retErr error) {
4545
readWriteBucket := storagemem.NewReadWriteBucket()
46-
paths, err := storage.AllPaths(ctx, storage.MapReadBucket(bucket, storage.MatchPathExt(".proto")), "")
46+
paths, err := storage.AllPaths(ctx, storage.FilterReadBucket(bucket, storage.MatchPathExt(".proto")), "")
4747
if err != nil {
4848
return nil, err
4949
}

private/buf/bufprotoc/bufprotoc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewModuleSetForProtoc(
6262
}
6363
// need to do match extension here
6464
// https://github.com/bufbuild/buf/issues/113
65-
rootBuckets = append(rootBuckets, storage.MapReadBucket(rootBucket, storage.MatchPathExt(".proto")))
65+
rootBuckets = append(rootBuckets, storage.FilterReadBucket(rootBucket, storage.MatchPathExt(".proto")))
6666
}
6767
targetPaths, err := slicesext.MapError(
6868
absFilePaths,

private/buf/bufworkspace/workspace_targeting.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,12 @@ func getMappedModuleBucketAndModuleTargeting(
527527
for root, excludes := range rootToExcludes {
528528
// Roots only applies to .proto files.
529529
mappers := []storage.Mapper{
530+
storage.MapOnPrefix(root),
531+
}
532+
matchers := []storage.Matcher{
530533
// need to do match extension here
531534
// https://github.com/bufbuild/buf/issues/113
532535
storage.MatchPathExt(".proto"),
533-
storage.MapOnPrefix(root),
534536
}
535537
if len(excludes) != 0 {
536538
var notOrMatchers []storage.Matcher
@@ -540,8 +542,8 @@ func getMappedModuleBucketAndModuleTargeting(
540542
storage.MatchPathContained(exclude),
541543
)
542544
}
543-
mappers = append(
544-
mappers,
545+
matchers = append(
546+
matchers,
545547
storage.MatchNot(
546548
storage.MatchOr(
547549
notOrMatchers...,
@@ -551,9 +553,12 @@ func getMappedModuleBucketAndModuleTargeting(
551553
}
552554
rootBuckets = append(
553555
rootBuckets,
554-
storage.MapReadBucket(
555-
moduleBucket,
556-
mappers...,
556+
storage.FilterReadBucket(
557+
storage.MapReadBucket(
558+
moduleBucket,
559+
mappers...,
560+
),
561+
matchers...,
557562
),
558563
)
559564
}

private/bufpkg/bufmodule/digest.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func getB4Digest(
233233
ctx,
234234
// This is extreme defensive programming. We've gone out of our way to make sure
235235
// that the bucket is already filtered, but it's just too important to mess up here.
236-
storage.MapReadBucket(bucketWithStorageMatcherApplied, getStorageMatcher(ctx, bucketWithStorageMatcherApplied)),
236+
storage.FilterReadBucket(bucketWithStorageMatcherApplied, getStorageMatcher(ctx, bucketWithStorageMatcherApplied)),
237237
"",
238238
func(readObject storage.ReadObject) error {
239239
digest, err := bufcas.NewDigestForContent(readObject)
@@ -373,7 +373,7 @@ func getFilesDigestForB5Digest(
373373
ctx,
374374
// This is extreme defensive programming. We've gone out of our way to make sure
375375
// that the bucket is already filtered, but it's just too important to mess up here.
376-
storage.MapReadBucket(bucketWithStorageMatcherApplied, getStorageMatcher(ctx, bucketWithStorageMatcherApplied)),
376+
storage.FilterReadBucket(bucketWithStorageMatcherApplied, getStorageMatcher(ctx, bucketWithStorageMatcherApplied)),
377377
"",
378378
func(readObject storage.ReadObject) error {
379379
digest, err := bufcas.NewDigestForContent(readObject)

private/bufpkg/bufmodule/paths.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func getSyncOnceValuesGetBucketWithStorageMatcherApplied(
9090
if err != nil {
9191
return nil, err
9292
}
93-
return storage.MapReadBucket(bucket, getStorageMatcher(ctx, bucket)), nil
93+
return storage.FilterReadBucket(bucket, getStorageMatcher(ctx, bucket)), nil
9494
},
9595
)
9696
}

private/pkg/git/cloner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ func (c *cloner) CloneToBucket(
240240
return err
241241
}
242242
var readBucket storage.ReadBucket = tmpReadWriteBucket
243-
if options.Mapper != nil {
244-
readBucket = storage.MapReadBucket(readBucket, options.Mapper)
243+
if options.Matcher != nil {
244+
readBucket = storage.FilterReadBucket(readBucket, options.Matcher)
245245
}
246246
_, err = storage.Copy(ctx, readBucket, writeBucket)
247247
return err

private/pkg/git/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type Cloner interface {
9595

9696
// CloneToBucketOptions are options for Clone.
9797
type CloneToBucketOptions struct {
98-
Mapper storage.Mapper
98+
Matcher storage.Matcher
9999
Name Name
100100
RecurseSubmodules bool
101101
}

private/pkg/git/git_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func readBucketForName(ctx context.Context, t *testing.T, runner command.Runner,
242242
depth,
243243
readWriteBucket,
244244
CloneToBucketOptions{
245-
Mapper: storage.MatchPathExt(".proto"),
245+
Matcher: storage.MatchPathExt(".proto"),
246246
Name: name,
247247
RecurseSubmodules: recurseSubmodules,
248248
},

private/pkg/protostat/protostatstorage/file_walker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type fileWalker struct {
2828

2929
func newFileWalker(readBucket storage.ReadBucket) *fileWalker {
3030
return &fileWalker{
31-
readBucket: storage.MapReadBucket(
31+
readBucket: storage.FilterReadBucket(
3232
readBucket,
3333
storage.MatchPathExt(".proto"),
3434
),

private/pkg/storage/filter.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright 2020-2024 Buf Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package storage
16+
17+
import (
18+
"context"
19+
"io/fs"
20+
21+
"github.com/bufbuild/buf/private/pkg/normalpath"
22+
)
23+
24+
// FilterReadBucket filters the ReadBucket.
25+
//
26+
// If the Matchers are empty, the original ReadBucket is returned.
27+
// If there is more than one Matcher, the Matchers are anded together.
28+
func FilterReadBucket(readBucket ReadBucket, matchers ...Matcher) ReadBucket {
29+
if len(matchers) == 0 {
30+
return readBucket
31+
}
32+
return newFilterReadBucketCloser(readBucket, nil, MatchAnd(matchers...))
33+
}
34+
35+
// FilterReadBucketCloser filters the ReadBucketCloser.
36+
//
37+
// If the Matchers are empty, the original ReadBucketCloser is returned.
38+
// If there is more than one Matcher, the Matchers are anded together.
39+
func FilterReadBucketCloser(readBucketCloser ReadBucketCloser, matchers ...Matcher) ReadBucketCloser {
40+
if len(matchers) == 0 {
41+
return readBucketCloser
42+
}
43+
return newFilterReadBucketCloser(readBucketCloser, readBucketCloser.Close, MatchAnd(matchers...))
44+
}
45+
46+
type filterReadBucketCloser struct {
47+
delegate ReadBucket
48+
closeFunc func() error
49+
matcher Matcher
50+
}
51+
52+
func newFilterReadBucketCloser(
53+
delegate ReadBucket,
54+
closeFunc func() error,
55+
matcher Matcher,
56+
) *filterReadBucketCloser {
57+
return &filterReadBucketCloser{
58+
delegate: delegate,
59+
closeFunc: closeFunc,
60+
matcher: matcher,
61+
}
62+
}
63+
64+
func (r *filterReadBucketCloser) Get(ctx context.Context, path string) (ReadObjectCloser, error) {
65+
path, err := normalpath.NormalizeAndValidate(path)
66+
if err != nil {
67+
return nil, err
68+
}
69+
if !r.matcher.MatchPath(path) {
70+
return nil, &fs.PathError{Op: "read", Path: path, Err: fs.ErrNotExist}
71+
}
72+
return r.delegate.Get(ctx, path)
73+
}
74+
75+
func (r *filterReadBucketCloser) Stat(ctx context.Context, path string) (ObjectInfo, error) {
76+
path, err := normalpath.NormalizeAndValidate(path)
77+
if err != nil {
78+
return nil, err
79+
}
80+
if !r.matcher.MatchPath(path) {
81+
return nil, &fs.PathError{Op: "read", Path: path, Err: fs.ErrNotExist}
82+
}
83+
return r.delegate.Stat(ctx, path)
84+
}
85+
86+
func (r *filterReadBucketCloser) Walk(ctx context.Context, prefix string, f func(ObjectInfo) error) error {
87+
prefix, err := normalpath.NormalizeAndValidate(prefix)
88+
if err != nil {
89+
return err
90+
}
91+
return r.delegate.Walk(
92+
ctx,
93+
prefix,
94+
func(objectInfo ObjectInfo) error {
95+
if !r.matcher.MatchPath(objectInfo.Path()) {
96+
return nil
97+
}
98+
return f(objectInfo)
99+
},
100+
)
101+
}
102+
103+
func (r *filterReadBucketCloser) Close() error {
104+
if r.closeFunc != nil {
105+
return r.closeFunc()
106+
}
107+
return nil
108+
}

0 commit comments

Comments
 (0)