-
Notifications
You must be signed in to change notification settings - Fork 2
/
protobuf_provider.go
109 lines (94 loc) · 3.46 KB
/
protobuf_provider.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
package provider
import (
"flag"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/rule"
sppb "github.com/stackb/scala-gazelle/build/stack/gazelle/scala/parse"
"github.com/stackb/scala-gazelle/pkg/resolver"
)
// ProvidedImports is the protoc.ImportProvider interface func.
type ProvidedImports func(lang, impLang string) map[label.Label][]string
// ProtobufProvider is a provider of symbols for the
// stackb/rules_proto gazelle extension.
type ProtobufProvider struct {
lang string
impLang string
// indexFile string
scope resolver.Scope
importProvider ProvidedImports
}
// NewProtobufProvider constructs a new provider. The lang/impLang
// arguments are used to fetch the provided imports in the given importProvider
// struct.
func NewProtobufProvider(lang, impLang string, importProvider ProvidedImports) *ProtobufProvider {
return &ProtobufProvider{
lang: lang,
impLang: impLang,
importProvider: importProvider,
}
}
// Name implements part of the resolver.SymbolProvider interface.
func (p *ProtobufProvider) Name() string {
return "protobuf"
}
// RegisterFlags implements part of the resolver.SymbolProvider interface.
func (p *ProtobufProvider) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
// fs.StringVar(&p.indexFile, "protobuf_index_file", "", "path to javaindex.pb or javaindex.json file")
}
// CheckFlags implements part of the resolver.SymbolProvider interface.
func (p *ProtobufProvider) CheckFlags(fs *flag.FlagSet, c *config.Config, scope resolver.Scope) error {
p.scope = scope
return nil
}
// OnResolve implements part of the resolver.SymbolProvider interface.
func (p *ProtobufProvider) OnResolve() error {
// messageRequires := p.resolveScalapbMessageRequires()
for from, symbols := range p.importProvider(p.lang, "package") {
for _, symbol := range symbols {
p.putSymbol(sppb.ImportType_PROTO_PACKAGE, symbol, from)
}
}
for from, symbols := range p.importProvider(p.lang, "enum") {
for _, symbol := range symbols {
p.putSymbol(sppb.ImportType_PROTO_ENUM, symbol, from)
}
}
for from, symbols := range p.importProvider(p.lang, "enumfield") {
for _, symbol := range symbols {
p.putSymbol(sppb.ImportType_PROTO_ENUM_FIELD, symbol, from)
}
}
for from, symbols := range p.importProvider(p.lang, "message") {
for _, symbol := range symbols {
p.putSymbol(sppb.ImportType_PROTO_MESSAGE, symbol, from)
// sym.Requires = messageRequires
}
}
for from, symbols := range p.importProvider(p.lang, "service") {
for _, symbol := range symbols {
p.putSymbol(sppb.ImportType_PROTO_SERVICE, symbol, from)
}
}
for from, symbols := range p.importProvider(p.lang, "class") {
for _, symbol := range symbols {
p.putSymbol(sppb.ImportType_CLASS, symbol, from)
}
}
return nil
}
// OnEnd implements part of the resolver.SymbolProvider interface.
func (p *ProtobufProvider) OnEnd() error {
return nil
}
// CanProvide implements part of the resolver.SymbolProvider interface.
func (p *ProtobufProvider) CanProvide(dep label.Label, knownRule func(from label.Label) (*rule.Rule, bool)) bool {
return strings.HasSuffix(dep.Name, "proto_scala_library") ||
strings.HasSuffix(dep.Name, "grpc_scala_library")
}
func (p *ProtobufProvider) putSymbol(impType sppb.ImportType, imp string, from label.Label) *resolver.Symbol {
sym := resolver.NewSymbol(impType, imp, p.Name(), from)
p.scope.PutSymbol(sym)
return sym
}