|
| 1 | +// Package main implements a plugin that checks that all rpc methods set the |
| 2 | +// required options (permissions, http). |
| 3 | +// |
| 4 | +// To use this plugin: |
| 5 | +// |
| 6 | +// # buf.yaml |
| 7 | +// version: v2 |
| 8 | +// lint: |
| 9 | +// use: |
| 10 | +// - STANDARD # omit if you do not want to use the rules builtin to buf |
| 11 | +// - QDRANT_CLOUD_METHOD_OPTIONS |
| 12 | +// plugins: |
| 13 | +// - plugin: buf-plugin-method-options |
| 14 | +package main |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + "buf.build/go/bufplugin/check" |
| 20 | + "buf.build/go/bufplugin/check/checkutil" |
| 21 | + "buf.build/go/bufplugin/info" |
| 22 | + googleann "google.golang.org/genproto/googleapis/api/annotations" |
| 23 | + "google.golang.org/protobuf/proto" |
| 24 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 25 | + protoimpl "google.golang.org/protobuf/runtime/protoimpl" |
| 26 | + |
| 27 | + commonv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/common/v1" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + methodOptionsRuleID = "QDRANT_CLOUD_METHOD_OPTIONS" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + methodOptionsRuleSpec = &check.RuleSpec{ |
| 36 | + ID: methodOptionsRuleID, |
| 37 | + Default: true, |
| 38 | + Purpose: `Checks that all rpc methods define a set of required options.`, |
| 39 | + Type: check.RuleTypeLint, |
| 40 | + Handler: checkutil.NewMethodRuleHandler(checkMethodOptions, checkutil.WithoutImports()), |
| 41 | + } |
| 42 | + spec = &check.Spec{ |
| 43 | + Rules: []*check.RuleSpec{ |
| 44 | + methodOptionsRuleSpec, |
| 45 | + }, |
| 46 | + Info: &info.Spec{ |
| 47 | + Documentation: `A plugin that checks that all rpc methods define a set of required options.`, |
| 48 | + SPDXLicenseID: "", |
| 49 | + LicenseURL: "", |
| 50 | + }, |
| 51 | + } |
| 52 | + requiredMethodOptionExtensions = []*protoimpl.ExtensionInfo{ |
| 53 | + commonv1.E_Permissions, |
| 54 | + googleann.E_Http, |
| 55 | + } |
| 56 | +) |
| 57 | + |
| 58 | +func main() { |
| 59 | + check.Main(spec) |
| 60 | +} |
| 61 | + |
| 62 | +func checkMethodOptions(ctx context.Context, responseWriter check.ResponseWriter, request check.Request, methodDescriptor protoreflect.MethodDescriptor) error { |
| 63 | + options := methodDescriptor.Options() |
| 64 | + |
| 65 | + for _, extension := range requiredMethodOptionExtensions { |
| 66 | + if !proto.HasExtension(options, extension) { |
| 67 | + responseWriter.AddAnnotation( |
| 68 | + check.WithMessagef("Method %q does not define the %q option", methodDescriptor.FullName(), extension.TypeDescriptor().FullName()), |
| 69 | + check.WithDescriptor(methodDescriptor), |
| 70 | + ) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
0 commit comments