Skip to content

Commit 32d84a9

Browse files
authored
CLI: Use set.Set for Definition.supportedCommands (#9644)
This change makes use of the `set.Set` type to make the code a little more concise and readable.
1 parent 9735d1c commit 32d84a9

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

cli/parser/options/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ go_library(
1010
"//cli/parser/arguments",
1111
"//cli/parser/bazelrc",
1212
"//proto:bazel_flags_go_proto",
13+
"//server/util/lib/set",
1314
],
1415
)
1516

cli/parser/options/options.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package options
33
import (
44
"fmt"
55
"iter"
6+
"slices"
67
"strings"
78

89
"github.com/buildbuddy-io/buildbuddy/cli/log"
910
"github.com/buildbuddy-io/buildbuddy/cli/parser/arguments"
1011
"github.com/buildbuddy-io/buildbuddy/cli/parser/bazelrc"
12+
"github.com/buildbuddy-io/buildbuddy/server/util/lib/set"
1113

1214
bfpb "github.com/buildbuddy-io/buildbuddy/proto/bazel_flags"
1315
)
@@ -101,7 +103,7 @@ type Definition struct {
101103
requiresValue bool
102104

103105
// The list of commands that support this option.
104-
supportedCommands map[string]struct{}
106+
supportedCommands set.Set[string]
105107

106108
// pluginID is the ID of the bb cli plugin associated with this option
107109
// definition, if applicable (or a pseudo-plugin ID for so-called "built-in"
@@ -145,7 +147,7 @@ func (d *Definition) SupportedCommands() iter.Seq[string] {
145147

146148
func (d *Definition) Supports(command string) bool {
147149
for cmd, ok := command, true; ok; cmd, ok = bazelrc.Parent(cmd) {
148-
if _, ok := d.supportedCommands[cmd]; ok {
150+
if d.supportedCommands.Contains(cmd) {
149151
return true
150152
}
151153
}
@@ -155,11 +157,9 @@ func (d *Definition) Supports(command string) bool {
155157

156158
func (d *Definition) AddSupportedCommand(commands ...string) {
157159
if d.supportedCommands == nil {
158-
d.supportedCommands = make(map[string]struct{}, 1)
159-
}
160-
for _, command := range commands {
161-
d.supportedCommands[command] = struct{}{}
160+
d.supportedCommands = make(set.Set[string], 1)
162161
}
162+
d.supportedCommands.AddSeq(slices.Values(commands))
163163
}
164164

165165
func (d *Definition) PluginID() string {
@@ -180,15 +180,7 @@ func WithPluginID(pluginID string) DefinitionOpt {
180180

181181
func WithSupportFor(commands ...string) DefinitionOpt {
182182
return func(d *Definition) {
183-
if len(commands) == 0 {
184-
return
185-
}
186-
if d.supportedCommands == nil {
187-
d.supportedCommands = make(map[string]struct{}, len(commands))
188-
}
189-
for _, command := range commands {
190-
d.supportedCommands[command] = struct{}{}
191-
}
183+
d.AddSupportedCommand(commands...)
192184
}
193185
}
194186

@@ -263,11 +255,9 @@ func DefinitionFrom(info *bfpb.FlagInfo) *Definition {
263255
multi: info.GetAllowsMultiple(),
264256
hasNegative: info.GetHasNegativeFlag(),
265257
requiresValue: info.GetRequiresValue(),
266-
supportedCommands: make(map[string]struct{}, len(info.GetCommands())),
267-
}
268-
for _, cmd := range info.GetCommands() {
269-
d.supportedCommands[cmd] = struct{}{}
258+
supportedCommands: make(set.Set[string], len(info.GetCommands())),
270259
}
260+
d.AddSupportedCommand(info.GetCommands()...)
271261
return d
272262
}
273263

0 commit comments

Comments
 (0)