-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
167 lines (153 loc) · 3.95 KB
/
config.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package scalarule
import (
"fmt"
"sort"
"strconv"
"strings"
"github.com/bazelbuild/bazel-gazelle/config"
"github.com/stackb/scala-gazelle/pkg/collections"
)
// Config carries metadata about a rule and its dependencies.
type Config struct {
// Rel is the relative directory of this rule config
Rel string
// Config is the parent gazelle Config
Config *config.Config
// Deps is a mapping from label to +/- intent.
Deps map[string]bool
// Attr is a mapping from string to intent map.
Attrs map[string]map[string]bool
// Options is a generic key -> value string mapping. Various rule
// implementations are free to document/interpret options in an
// implementation-dependenct manner.
Options map[string]bool
// Enabled is a flag that marks language generation as enabled or not
Enabled bool
// Implementation is the registry identifier for the provider
Implementation string
// Provider is the actual implementation
Provider Provider
// Name is the name of the Rule config
Name string
}
// NewConfig returns a pointer to a new Config config with the
// 'Enabled' bit set to true.
func NewConfig(config *config.Config, name string) *Config {
return &Config{
Config: config,
Name: name,
Enabled: true,
Attrs: make(map[string]map[string]bool),
Deps: make(map[string]bool),
Options: make(map[string]bool),
}
}
// GetDeps returns the sorted list of dependencies
func (c *Config) GetDeps() []string {
deps := make([]string, 0)
for dep, want := range c.Deps {
if !want {
continue
}
deps = append(deps, dep)
}
sort.Strings(deps)
return deps
}
// GetOptions returns the rule options.
func (c *Config) GetOptions() []string {
opts := make([]string, 0)
for opt, want := range c.Options {
if !want {
continue
}
opts = append(opts, opt)
}
sort.Strings(opts)
return opts
}
// GetAttr returns the positive-intent attr values under the given key.
func (c *Config) GetAttr(name string) []string {
vals := make([]string, 0)
for val, want := range c.Attrs[name] {
if !want {
continue
}
vals = append(vals, val)
}
sort.Strings(vals)
return vals
}
// Clone copies this config to a new one
func (c *Config) Clone() *Config {
clone := NewConfig(c.Config, c.Name)
clone.Enabled = c.Enabled
clone.Implementation = c.Implementation
for name, vals := range c.Attrs {
clone.Attrs[name] = make(map[string]bool)
for k, v := range vals {
clone.Attrs[name][k] = v
}
}
for k, v := range c.Deps {
clone.Deps[k] = v
}
for k, v := range c.Options {
clone.Options[k] = v
}
return clone
}
// ParseDirective parses the directive string or returns error.
func (c *Config) ParseDirective(d, param, value string) error {
intent := collections.ParseIntent(param)
switch intent.Value {
case "dep", "deps":
if intent.Want {
c.Deps[value] = true
} else {
delete(c.Deps, value)
}
case "option":
if intent.Want {
c.Options[value] = true
} else {
delete(c.Options, value)
}
case "attr":
kv := strings.Fields(value)
if len(kv) == 0 {
return fmt.Errorf("malformed attr (missing attr name and value) %q: expected form is 'gazelle:proto_rule {RULE_NAME} attr {ATTR_NAME} [+/-]{VALUE}'", value)
}
key := collections.ParseIntent(kv[0])
if len(kv) == 1 {
if intent.Want {
return fmt.Errorf("malformed attr %q (missing named attr value): expected form is 'gazelle:proto_rule {RULE_NAME} attr {ATTR_NAME} [+/-]{VALUE}'", value)
} else {
delete(c.Attrs, key.Value)
return nil
}
}
val := strings.Join(kv[1:], " ")
if intent.Want {
values, ok := c.Attrs[key.Value]
if !ok {
values = make(map[string]bool)
c.Attrs[key.Value] = values
}
values[val] = key.Want
} else {
delete(c.Attrs, key.Value)
}
case "implementation":
c.Implementation = value
case "enabled":
enabled, err := strconv.ParseBool(value)
if err != nil {
return fmt.Errorf("enabled %s: %w", value, err)
}
c.Enabled = enabled
default:
return fmt.Errorf("unknown parameter %q", intent.Value)
}
return nil
}