@@ -105,6 +105,10 @@ type BufYAMLFile interface {
105
105
//
106
106
// For v1 buf.yaml files, this will always return nil.
107
107
PluginConfigs () []PluginConfig
108
+ // PolicyConfigs returns the PolicyConfigs for the File.
109
+ //
110
+ // For v1 buf.yaml files, this will always return nil.
111
+ PolicyConfigs () []PolicyConfig
108
112
// ConfiguredDepModuleRefs returns the configured dependencies of the Workspace as ModuleRefs.
109
113
//
110
114
// These come from buf.yaml files.
@@ -126,6 +130,7 @@ func NewBufYAMLFile(
126
130
fileVersion FileVersion ,
127
131
moduleConfigs []ModuleConfig ,
128
132
pluginConfigs []PluginConfig ,
133
+ policyConfigs []PolicyConfig ,
129
134
configuredDepModuleRefs []bufparse.Ref ,
130
135
options ... BufYAMLFileOption ,
131
136
) (BufYAMLFile , error ) {
@@ -140,6 +145,7 @@ func NewBufYAMLFile(
140
145
nil , // Do not set top-level lint config, use only module configs
141
146
nil , // Do not set top-level breaking config, use only module configs
142
147
pluginConfigs ,
148
+ policyConfigs ,
143
149
configuredDepModuleRefs ,
144
150
bufYAMLFileOptions .includeDocsLink ,
145
151
)
@@ -254,6 +260,7 @@ type bufYAMLFile struct {
254
260
topLevelLintConfig LintConfig
255
261
topLevelBreakingConfig BreakingConfig
256
262
pluginConfigs []PluginConfig
263
+ policyConfigs []PolicyConfig
257
264
configuredDepModuleRefs []bufparse.Ref
258
265
includeDocsLink bool
259
266
}
@@ -265,6 +272,7 @@ func newBufYAMLFile(
265
272
topLevelLintConfig LintConfig ,
266
273
topLevelBreakingConfig BreakingConfig ,
267
274
pluginConfigs []PluginConfig ,
275
+ policyConfigs []PolicyConfig ,
268
276
configuredDepModuleRefs []bufparse.Ref ,
269
277
includeDocsLink bool ,
270
278
) (* bufYAMLFile , error ) {
@@ -320,6 +328,7 @@ func newBufYAMLFile(
320
328
topLevelLintConfig : topLevelLintConfig ,
321
329
topLevelBreakingConfig : topLevelBreakingConfig ,
322
330
pluginConfigs : pluginConfigs ,
331
+ policyConfigs : policyConfigs ,
323
332
configuredDepModuleRefs : configuredDepModuleRefs ,
324
333
includeDocsLink : includeDocsLink ,
325
334
}, nil
@@ -353,6 +362,10 @@ func (c *bufYAMLFile) PluginConfigs() []PluginConfig {
353
362
return c .pluginConfigs
354
363
}
355
364
365
+ func (c * bufYAMLFile ) PolicyConfigs () []PolicyConfig {
366
+ return c .policyConfigs
367
+ }
368
+
356
369
func (c * bufYAMLFile ) ConfiguredDepModuleRefs () []bufparse.Ref {
357
370
return slices .Clone (c .configuredDepModuleRefs )
358
371
}
@@ -455,6 +468,7 @@ func readBufYAMLFile(
455
468
lintConfig ,
456
469
breakingConfig ,
457
470
nil ,
471
+ nil ,
458
472
configuredDepModuleRefs ,
459
473
includeDocsLink ,
460
474
)
@@ -651,6 +665,14 @@ func readBufYAMLFile(
651
665
}
652
666
pluginConfigs = append (pluginConfigs , pluginConfig )
653
667
}
668
+ var policyConfigs []PolicyConfig
669
+ for _ , externalPolicyConfig := range externalBufYAMLFile .Policies {
670
+ policyConfig , err := newPolicyConfigForExternalV2 (externalPolicyConfig )
671
+ if err != nil {
672
+ return nil , err
673
+ }
674
+ policyConfigs = append (policyConfigs , policyConfig )
675
+ }
654
676
configuredDepModuleRefs , err := getConfiguredDepModuleRefsForExternalDeps (externalBufYAMLFile .Deps )
655
677
if err != nil {
656
678
return nil , err
@@ -662,6 +684,7 @@ func readBufYAMLFile(
662
684
topLevelLintConfig ,
663
685
topLevelBreakingConfig ,
664
686
pluginConfigs ,
687
+ policyConfigs ,
665
688
configuredDepModuleRefs ,
666
689
includeDocsLink ,
667
690
)
@@ -861,6 +884,16 @@ func writeBufYAMLFile(writer io.Writer, bufYAMLFile BufYAMLFile) error {
861
884
}
862
885
externalBufYAMLFile .Plugins = externalPlugins
863
886
887
+ var externalPolicies []externalBufYAMLFilePolicyV2
888
+ for _ , policyConfig := range bufYAMLFile .PolicyConfigs () {
889
+ externalPolicy , err := newExternalV2ForPolicyConfig (policyConfig )
890
+ if err != nil {
891
+ return syserror .Wrap (err )
892
+ }
893
+ externalPolicies = append (externalPolicies , externalPolicy )
894
+ }
895
+ externalBufYAMLFile .Policies = externalPolicies
896
+
864
897
data , err := encoding .MarshalYAML (& externalBufYAMLFile )
865
898
if err != nil {
866
899
return err
@@ -1272,6 +1305,7 @@ type externalBufYAMLFileV2 struct {
1272
1305
Lint externalBufYAMLFileLintV2 `json:"lint,omitempty" yaml:"lint,omitempty"`
1273
1306
Breaking externalBufYAMLFileBreakingV1Beta1V1V2 `json:"breaking,omitempty" yaml:"breaking,omitempty"`
1274
1307
Plugins []externalBufYAMLFilePluginV2 `json:"plugins,omitempty" yaml:"plugins,omitempty"`
1308
+ Policies []externalBufYAMLFilePolicyV2 `json:"policies,omitempty" yaml:"policies,omitempty"`
1275
1309
}
1276
1310
1277
1311
// externalBufYAMLFileModuleV2 represents a single module configuration within a v2 buf.yaml file.
@@ -1302,7 +1336,7 @@ type externalBufYAMLFileLintV1Beta1V1 struct {
1302
1336
Except []string `json:"except,omitempty" yaml:"except,omitempty"`
1303
1337
// Ignore are the paths to ignore.
1304
1338
Ignore []string `json:"ignore,omitempty" yaml:"ignore,omitempty"`
1305
- /// IgnoreOnly are the ID/category to paths to ignore.
1339
+ // IgnoreOnly are the ID/category to paths to ignore.
1306
1340
IgnoreOnly map [string ][]string `json:"ignore_only,omitempty" yaml:"ignore_only,omitempty"`
1307
1341
EnumZeroValueSuffix string `json:"enum_zero_value_suffix,omitempty" yaml:"enum_zero_value_suffix,omitempty"`
1308
1342
RPCAllowSameRequestResponse bool `json:"rpc_allow_same_request_response,omitempty" yaml:"rpc_allow_same_request_response,omitempty"`
@@ -1389,12 +1423,21 @@ func (eb externalBufYAMLFileBreakingV1Beta1V1V2) isEmpty() bool {
1389
1423
! eb .DisableBuiltin
1390
1424
}
1391
1425
1392
- // externalBufYAMLFilePluginV2 represents a single plugin config in a v2 buf.gyaml file.
1426
+ // externalBufYAMLFilePluginV2 represents a single plugin config in a v2 buf.yaml file.
1393
1427
type externalBufYAMLFilePluginV2 struct {
1394
1428
Plugin any `json:"plugin,omitempty" yaml:"plugin,omitempty"`
1395
1429
Options map [string ]any `json:"options,omitempty" yaml:"options,omitempty"`
1396
1430
}
1397
1431
1432
+ // externalBufYAMLFilePolicyV2 represents a single policy config in a v2 buf.yaml file.
1433
+ type externalBufYAMLFilePolicyV2 struct {
1434
+ Policy string `json:"policy,omitempty" yaml:"policy,omitempty"`
1435
+ // Ignore are the paths to ignore.
1436
+ Ignore []string `json:"ignore,omitempty" yaml:"ignore,omitempty"`
1437
+ // IgnoreOnly are the ID/category to paths to ignore.
1438
+ IgnoreOnly map [string ][]string `json:"ignore_only,omitempty" yaml:"ignore_only,omitempty"`
1439
+ }
1440
+
1398
1441
func getZeroOrSingleValueForMap [K comparable , V any ](m map [K ]V ) (V , error ) {
1399
1442
var zero V
1400
1443
if len (m ) > 1 {
0 commit comments