11// Package main implements a plugin that checks that all rpc methods set the
2- // required options (permissions, http).
2+ // required options. The list of options is configurable.
3+ // The default value is:
4+ // - "qdrant.cloud.common.v1.permissions"
5+ // - "google.api.http"
36//
47// To use this plugin:
58//
1114// - QDRANT_CLOUD_METHOD_OPTIONS
1215// plugins:
1316// - plugin: buf-plugin-method-options
17+ // # Uncomment in case you need to configure the list of method options to validate.
18+ // # options:
19+ // # required_method_options:
20+ // # - "qdrant.cloud.common.v1.permissions"
1421package main
1522
1623import (
@@ -19,16 +26,19 @@ import (
1926 "buf.build/go/bufplugin/check"
2027 "buf.build/go/bufplugin/check/checkutil"
2128 "buf.build/go/bufplugin/info"
29+ "buf.build/go/bufplugin/option"
30+ commonv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/common/v1"
2231 googleann "google.golang.org/genproto/googleapis/api/annotations"
2332 "google.golang.org/protobuf/proto"
2433 "google.golang.org/protobuf/reflect/protoreflect"
2534 protoimpl "google.golang.org/protobuf/runtime/protoimpl"
26-
27- commonv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/common/v1"
2835)
2936
3037const (
38+ // methodOptionsRuleID is the Rule ID of the methodOptions rule.
3139 methodOptionsRuleID = "QDRANT_CLOUD_METHOD_OPTIONS"
40+ // methodOptionsOptionKey is the option key to override the default list of required options.
41+ methodOptionsOptionKey = "required_method_options"
3242)
3343
3444var (
@@ -49,20 +59,37 @@ var (
4959 LicenseURL : "" ,
5060 },
5161 }
52- requiredMethodOptionExtensions = [ ]* protoimpl.ExtensionInfo {
53- commonv1 .E_Permissions ,
54- googleann .E_Http ,
62+ extensionRegistry = map [ string ]* protoimpl.ExtensionInfo {
63+ "qdrant.cloud.common.v1.permissions" : commonv1 .E_Permissions ,
64+ "google.api.http" : googleann .E_Http ,
5565 }
66+ requiredMethodOptionExtensions = []string {"qdrant.cloud.common.v1.permissions" , "google.api.http" }
5667)
5768
5869func main () {
5970 check .Main (spec )
6071}
6172
6273func checkMethodOptions (ctx context.Context , responseWriter check.ResponseWriter , request check.Request , methodDescriptor protoreflect.MethodDescriptor ) error {
74+ requiredOptions := requiredMethodOptionExtensions
75+ optionValue , err := option .GetStringSliceValue (request .Options (), methodOptionsOptionKey )
76+ if err != nil {
77+ return err
78+ }
79+ if len (optionValue ) > 0 {
80+ requiredOptions = optionValue
81+ }
82+
6383 options := methodDescriptor .Options ()
6484
65- for _ , extension := range requiredMethodOptionExtensions {
85+ for _ , extensionKey := range requiredOptions {
86+ extension , found := extensionRegistry [extensionKey ]
87+ if ! found {
88+ responseWriter .AddAnnotation (
89+ check .WithMessagef ("extension key %q does not exist" , extensionKey ),
90+ )
91+ return nil
92+ }
6693 if ! proto .HasExtension (options , extension ) {
6794 responseWriter .AddAnnotation (
6895 check .WithMessagef ("Method %q does not define the %q option" , methodDescriptor .FullName (), extension .TypeDescriptor ().FullName ()),
0 commit comments