Skip to content

Commit b5bdf81

Browse files
committed
Add exclude_options configuration for generation
This adds a new plugin option `exclude_options` for use on generation. The excluded options are stripped from the Image creating a shallow clone of the FileDescriptors that make up the ImageFiles. Any imports that are no longer required due to options being removed are also removed. The option is applied for the plugin. Multiple filtered images that reference the original image may be created when exclude options are set.
1 parent cc52cd6 commit b5bdf81

24 files changed

+6587
-0
lines changed

private/buf/bufgen/generator.go

+14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/bufbuild/buf/private/bufpkg/bufconfig"
2727
"github.com/bufbuild/buf/private/bufpkg/bufimage"
2828
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimagemodify"
29+
"github.com/bufbuild/buf/private/bufpkg/bufimage/bufimageutil"
2930
"github.com/bufbuild/buf/private/bufpkg/bufprotoplugin"
3031
"github.com/bufbuild/buf/private/bufpkg/bufprotoplugin/bufprotopluginos"
3132
"github.com/bufbuild/buf/private/bufpkg/bufremoteplugin"
@@ -235,13 +236,15 @@ func (g *generator) execPlugins(
235236
if includeWellKnownTypesOverride != nil {
236237
includeWellKnownTypes = *includeWellKnownTypesOverride
237238
}
239+
excludeOptions := currentPluginConfig.ExcludeOptions()
238240
response, err := g.execLocalPlugin(
239241
ctx,
240242
container,
241243
imageProvider,
242244
currentPluginConfig,
243245
includeImports,
244246
includeWellKnownTypes,
247+
excludeOptions,
245248
)
246249
if err != nil {
247250
return err
@@ -309,17 +312,28 @@ func (g *generator) execLocalPlugin(
309312
pluginConfig bufconfig.GeneratePluginConfig,
310313
includeImports bool,
311314
includeWellKnownTypes bool,
315+
excludeOptions []string,
312316
) (*pluginpb.CodeGeneratorResponse, error) {
313317
pluginImages, err := imageProvider.GetImages(Strategy(pluginConfig.Strategy()))
314318
if err != nil {
315319
return nil, err
316320
}
321+
if len(excludeOptions) > 0 {
322+
for i, pluginImage := range pluginImages {
323+
pluginImage, err := bufimageutil.ExcludeOptions(pluginImage, excludeOptions)
324+
if err != nil {
325+
return nil, err
326+
}
327+
pluginImages[i] = pluginImage
328+
}
329+
}
317330
requests, err := bufimage.ImagesToCodeGeneratorRequests(
318331
pluginImages,
319332
pluginConfig.Opt(),
320333
nil,
321334
includeImports,
322335
includeWellKnownTypes,
336+
excludeOptions,
323337
)
324338
if err != nil {
325339
return nil, err

private/buf/cmd/buf/command/alpha/protoc/plugin.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func executePlugin(
5959
bufprotopluginexec.DefaultVersion,
6060
false,
6161
false,
62+
nil, // TODO
6263
)
6364
if err != nil {
6465
return nil, err

private/bufpkg/bufconfig/buf_gen_yaml_file.go

+3
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ type externalGeneratePluginConfigV2 struct {
514514
IncludeWKT bool `json:"include_wkt,omitempty" yaml:"include_wkt,omitempty"`
515515
// Strategy is only valid with ProtoBuiltin and Local.
516516
Strategy *string `json:"strategy,omitempty" yaml:"strategy,omitempty"`
517+
518+
// ExcludeOptions removes options from the image.
519+
ExcludeOptions []string `json:"exclude_options,omitempty" yaml:"exclude_options,omitempty"`
517520
}
518521

519522
// externalGenerateManagedConfigV2 represents the managed mode config in a v2 buf.gen.yaml file.

private/bufpkg/bufconfig/buf_gen_yaml_file_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ plugins:
180180
strategy: all
181181
include_imports: true
182182
include_wkt: true
183+
exclude_options:
184+
- buf.validate.oneof
183185
inputs:
184186
- git_repo: github.com/acme/weather
185187
branch: dev

private/bufpkg/bufconfig/generate_plugin_config.go

+36
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ type GeneratePluginConfig interface {
112112
//
113113
// This is not empty only when the plugin is remote.
114114
Revision() int
115+
// ExcludeOptions returns the options to exclude.
116+
ExcludeOptions() []string
115117

116118
isGeneratePluginConfig()
117119
}
@@ -123,6 +125,7 @@ func NewRemoteGeneratePluginConfig(
123125
opt []string,
124126
includeImports bool,
125127
includeWKT bool,
128+
excludeOptions []string,
126129
revision int,
127130
) (GeneratePluginConfig, error) {
128131
return newRemoteGeneratePluginConfig(
@@ -131,6 +134,7 @@ func NewRemoteGeneratePluginConfig(
131134
opt,
132135
includeImports,
133136
includeWKT,
137+
excludeOptions,
134138
revision,
135139
)
136140
}
@@ -142,6 +146,7 @@ func NewLocalOrProtocBuiltinGeneratePluginConfig(
142146
opt []string,
143147
includeImports bool,
144148
includeWKT bool,
149+
excludeOptions []string,
145150
strategy *GenerateStrategy,
146151
) (GeneratePluginConfig, error) {
147152
return newLocalOrProtocBuiltinGeneratePluginConfig(
@@ -150,6 +155,7 @@ func NewLocalOrProtocBuiltinGeneratePluginConfig(
150155
opt,
151156
includeImports,
152157
includeWKT,
158+
excludeOptions,
153159
strategy,
154160
)
155161
}
@@ -161,6 +167,7 @@ func NewLocalGeneratePluginConfig(
161167
opt []string,
162168
includeImports bool,
163169
includeWKT bool,
170+
excludeOptions []string,
164171
strategy *GenerateStrategy,
165172
path []string,
166173
) (GeneratePluginConfig, error) {
@@ -170,6 +177,7 @@ func NewLocalGeneratePluginConfig(
170177
opt,
171178
includeImports,
172179
includeWKT,
180+
excludeOptions,
173181
strategy,
174182
path,
175183
)
@@ -183,6 +191,7 @@ func NewProtocBuiltinGeneratePluginConfig(
183191
opt []string,
184192
includeImports bool,
185193
includeWKT bool,
194+
excludeOptions []string,
186195
strategy *GenerateStrategy,
187196
protocPath []string,
188197
) (GeneratePluginConfig, error) {
@@ -192,6 +201,7 @@ func NewProtocBuiltinGeneratePluginConfig(
192201
opt,
193202
includeImports,
194203
includeWKT,
204+
excludeOptions,
195205
strategy,
196206
protocPath,
197207
)
@@ -203,6 +213,7 @@ func NewGeneratePluginConfigWithIncludeImportsAndWKT(
203213
config GeneratePluginConfig,
204214
includeImports bool,
205215
includeWKT bool,
216+
excludeOptions []string,
206217
) (GeneratePluginConfig, error) {
207218
originalConfig, ok := config.(*generatePluginConfig)
208219
if !ok {
@@ -215,6 +226,9 @@ func NewGeneratePluginConfigWithIncludeImportsAndWKT(
215226
if includeWKT {
216227
generatePluginConfig.includeWKT = true
217228
}
229+
if len(excludeOptions) > 0 {
230+
generatePluginConfig.excludeOptions = excludeOptions
231+
}
218232
return &generatePluginConfig, nil
219233
}
220234

@@ -227,6 +241,7 @@ type generatePluginConfig struct {
227241
opts []string
228242
includeImports bool
229243
includeWKT bool
244+
excludeOptions []string
230245
strategy *GenerateStrategy
231246
path []string
232247
protocPath []string
@@ -258,6 +273,7 @@ func newGeneratePluginConfigFromExternalV1Beta1(
258273
opt,
259274
false,
260275
false,
276+
nil, // TODO
261277
strategy,
262278
[]string{externalConfig.Path},
263279
)
@@ -268,6 +284,7 @@ func newGeneratePluginConfigFromExternalV1Beta1(
268284
opt,
269285
false,
270286
false,
287+
nil, // TODO
271288
strategy,
272289
)
273290
}
@@ -327,6 +344,7 @@ func newGeneratePluginConfigFromExternalV1(
327344
opt,
328345
false,
329346
false,
347+
nil, // TODO
330348
externalConfig.Revision,
331349
)
332350
}
@@ -339,6 +357,7 @@ func newGeneratePluginConfigFromExternalV1(
339357
opt,
340358
false,
341359
false,
360+
nil, // TODO
342361
strategy,
343362
path,
344363
)
@@ -350,6 +369,7 @@ func newGeneratePluginConfigFromExternalV1(
350369
opt,
351370
false,
352371
false,
372+
nil, // TODO
353373
strategy,
354374
protocPath,
355375
)
@@ -362,6 +382,7 @@ func newGeneratePluginConfigFromExternalV1(
362382
opt,
363383
false,
364384
false,
385+
nil, // TODO
365386
strategy,
366387
)
367388
}
@@ -418,6 +439,7 @@ func newGeneratePluginConfigFromExternalV2(
418439
opt,
419440
externalConfig.IncludeImports,
420441
externalConfig.IncludeWKT,
442+
externalConfig.ExcludeOptions,
421443
revision,
422444
)
423445
case externalConfig.Local != nil:
@@ -438,6 +460,7 @@ func newGeneratePluginConfigFromExternalV2(
438460
opt,
439461
externalConfig.IncludeImports,
440462
externalConfig.IncludeWKT,
463+
externalConfig.ExcludeOptions,
441464
parsedStrategy,
442465
path,
443466
)
@@ -455,6 +478,7 @@ func newGeneratePluginConfigFromExternalV2(
455478
opt,
456479
externalConfig.IncludeImports,
457480
externalConfig.IncludeWKT,
481+
externalConfig.ExcludeOptions,
458482
parsedStrategy,
459483
protocPath,
460484
)
@@ -469,6 +493,7 @@ func newRemoteGeneratePluginConfig(
469493
opt []string,
470494
includeImports bool,
471495
includeWKT bool,
496+
excludeOptions []string,
472497
revision int,
473498
) (*generatePluginConfig, error) {
474499
if includeWKT && !includeImports {
@@ -490,6 +515,7 @@ func newRemoteGeneratePluginConfig(
490515
opts: opt,
491516
includeImports: includeImports,
492517
includeWKT: includeWKT,
518+
excludeOptions: excludeOptions,
493519
}, nil
494520
}
495521

@@ -499,6 +525,7 @@ func newLocalOrProtocBuiltinGeneratePluginConfig(
499525
opt []string,
500526
includeImports bool,
501527
includeWKT bool,
528+
excludeOptions []string,
502529
strategy *GenerateStrategy,
503530
) (*generatePluginConfig, error) {
504531
if includeWKT && !includeImports {
@@ -512,6 +539,7 @@ func newLocalOrProtocBuiltinGeneratePluginConfig(
512539
opts: opt,
513540
includeImports: includeImports,
514541
includeWKT: includeWKT,
542+
excludeOptions: excludeOptions,
515543
}, nil
516544
}
517545

@@ -521,6 +549,7 @@ func newLocalGeneratePluginConfig(
521549
opt []string,
522550
includeImports bool,
523551
includeWKT bool,
552+
excludeOptions []string,
524553
strategy *GenerateStrategy,
525554
path []string,
526555
) (*generatePluginConfig, error) {
@@ -539,6 +568,7 @@ func newLocalGeneratePluginConfig(
539568
opts: opt,
540569
includeImports: includeImports,
541570
includeWKT: includeWKT,
571+
excludeOptions: excludeOptions,
542572
}, nil
543573
}
544574

@@ -548,6 +578,7 @@ func newProtocBuiltinGeneratePluginConfig(
548578
opt []string,
549579
includeImports bool,
550580
includeWKT bool,
581+
excludeOptions []string,
551582
strategy *GenerateStrategy,
552583
protocPath []string,
553584
) (*generatePluginConfig, error) {
@@ -563,6 +594,7 @@ func newProtocBuiltinGeneratePluginConfig(
563594
strategy: strategy,
564595
includeImports: includeImports,
565596
includeWKT: includeWKT,
597+
excludeOptions: excludeOptions,
566598
}, nil
567599
}
568600

@@ -590,6 +622,10 @@ func (p *generatePluginConfig) IncludeWKT() bool {
590622
return p.includeWKT
591623
}
592624

625+
func (p *generatePluginConfig) ExcludeOptions() []string {
626+
return p.excludeOptions
627+
}
628+
593629
func (p *generatePluginConfig) Strategy() GenerateStrategy {
594630
if p.strategy == nil {
595631
return GenerateStrategyDirectory

private/bufpkg/bufimage/bufimage.go

+12
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ func ImageToCodeGeneratorRequest(
594594
compilerVersion *pluginpb.Version,
595595
includeImports bool,
596596
includeWellKnownTypes bool,
597+
excludeOptions []string,
597598
) (*pluginpb.CodeGeneratorRequest, error) {
598599
return imageToCodeGeneratorRequest(
599600
image,
@@ -619,6 +620,7 @@ func ImagesToCodeGeneratorRequests(
619620
compilerVersion *pluginpb.Version,
620621
includeImports bool,
621622
includeWellKnownTypes bool,
623+
excludeOptions []string,
622624
) ([]*pluginpb.CodeGeneratorRequest, error) {
623625
requests := make([]*pluginpb.CodeGeneratorRequest, len(images))
624626
// alreadyUsedPaths is a map of paths that have already been added to an image.
@@ -652,7 +654,17 @@ func ImagesToCodeGeneratorRequests(
652654
}
653655
}
654656
}
657+
655658
for i, image := range images {
659+
// Exclude options may modify the image, removing the options from the image.
660+
// TODO: this moved to bufimageutil
661+
//if len(excludeOptions) > 0 {
662+
// var err error
663+
// image, err = stripOptions(image, excludeOptions)
664+
// if image != nil {
665+
// return nil, err
666+
// }
667+
//}
656668
var err error
657669
requests[i], err = imageToCodeGeneratorRequest(
658670
image,

0 commit comments

Comments
 (0)