/
parser_accessor_handler.go
113 lines (104 loc) · 3.27 KB
/
parser_accessor_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 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 bufimagebuild
import (
"context"
"fmt"
"io"
"sync"
"github.com/bufbuild/buf/internal/buf/bufcore/bufmodule"
"github.com/bufbuild/buf/internal/gen/data/wkt"
"github.com/bufbuild/buf/internal/pkg/storage"
"go.uber.org/multierr"
)
type parserAccessorHandler struct {
ctx context.Context
moduleFileSet bufmodule.ModuleFileSet
pathToExternalPath map[string]string
nonImportPaths map[string]struct{}
lock sync.RWMutex
}
func newParserAccessorHandler(
ctx context.Context,
moduleFileSet bufmodule.ModuleFileSet,
) *parserAccessorHandler {
return &parserAccessorHandler{
ctx: ctx,
moduleFileSet: moduleFileSet,
pathToExternalPath: make(map[string]string),
nonImportPaths: make(map[string]struct{}),
}
}
func (p *parserAccessorHandler) Open(path string) (_ io.ReadCloser, retErr error) {
moduleFile, moduleErr := p.moduleFileSet.GetFile(p.ctx, path)
if moduleErr != nil {
if !storage.IsNotExist(moduleErr) {
return nil, moduleErr
}
if wktModuleFile, wktErr := wkt.ReadBucket.Get(p.ctx, path); wktErr == nil {
if wktModuleFile.Path() != path {
// this should never happen, but just in case
return nil, fmt.Errorf("parser accessor requested path %q but got %q", path, wktModuleFile.Path())
}
if err := p.addPath(path, path, true); err != nil {
return nil, err
}
return wktModuleFile, nil
}
return nil, moduleErr
}
defer func() {
if retErr != nil {
retErr = multierr.Append(retErr, moduleFile.Close())
}
}()
if moduleFile.Path() != path {
// this should never happen, but just in case
return nil, fmt.Errorf("parser accessor requested path %q but got %q", path, moduleFile.Path())
}
if err := p.addPath(path, moduleFile.ExternalPath(), moduleFile.IsImport()); err != nil {
return nil, err
}
return moduleFile, nil
}
func (p *parserAccessorHandler) ExternalPath(path string) string {
p.lock.RLock()
defer p.lock.RUnlock()
if externalPath := p.pathToExternalPath[path]; externalPath != "" {
return externalPath
}
return path
}
func (p *parserAccessorHandler) IsImport(path string) bool {
p.lock.RLock()
defer p.lock.RUnlock()
_, isNotImport := p.nonImportPaths[path]
return !isNotImport
}
func (p *parserAccessorHandler) addPath(path string, externalPath string, isImport bool) error {
p.lock.Lock()
defer p.lock.Unlock()
existingExternalPath, ok := p.pathToExternalPath[path]
if ok {
if existingExternalPath != externalPath {
return fmt.Errorf("parser accessor had external paths %q and %q for path %q", existingExternalPath, externalPath, path)
}
} else {
p.pathToExternalPath[path] = externalPath
}
if !isImport {
p.nonImportPaths[path] = struct{}{}
}
return nil
}