|
| 1 | +// Package main implements a plugin that checks that: |
| 2 | +// - entity-related messages (e.g: Cluster) define a known set of common fields |
| 3 | +// for the Qdrant Cloud API. |
| 4 | +// - Request messages (e.g: ListClusters) define a known set of common fields |
| 5 | +// for the Qdrant Cloud API. |
| 6 | +// |
| 7 | +// To use this plugin: |
| 8 | +// |
| 9 | +// # buf.yaml |
| 10 | +// version: v2 |
| 11 | +// lint: |
| 12 | +// use: |
| 13 | +// - STANDARD # omit if you do not want to use the rules builtin to buf |
| 14 | +// - QDRANT_CLOUD_REQUIRED_ENTITY_FIELDS |
| 15 | +// - QDRANT_CLOUD_REQUIRED_REQUEST_FIELDS |
| 16 | +// plugins: |
| 17 | +// - plugin: buf-plugin-required-fields |
| 18 | +package main |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "buf.build/go/bufplugin/check" |
| 25 | + "buf.build/go/bufplugin/check/checkutil" |
| 26 | + "buf.build/go/bufplugin/descriptor" |
| 27 | + "buf.build/go/bufplugin/info" |
| 28 | + "buf.build/go/bufplugin/option" |
| 29 | + pluralize "github.com/gertd/go-pluralize" |
| 30 | + "google.golang.org/protobuf/reflect/protoreflect" |
| 31 | +) |
| 32 | + |
| 33 | +const ( |
| 34 | + requiredEntityFieldsRuleID = "QDRANT_CLOUD_REQUIRED_ENTITY_FIELDS" |
| 35 | + requiredEntityFieldsOptionKey = "required_entity_fields" |
| 36 | + requiredRequestFieldsRuleID = "QDRANT_CLOUD_REQUIRED_REQUEST_FIELDS" |
| 37 | + requiredRequestFieldsOptionKey = "required_request_fields" |
| 38 | +) |
| 39 | + |
| 40 | +var ( |
| 41 | + requiredEntityFieldsRuleSpec = &check.RuleSpec{ |
| 42 | + ID: requiredEntityFieldsRuleID, |
| 43 | + Default: true, |
| 44 | + Purpose: `Checks that all entity-related messages (e.g: Cluster) define a known set of fields for the Qdrant Cloud API.`, |
| 45 | + Type: check.RuleTypeLint, |
| 46 | + Handler: checkutil.NewFileRuleHandler(checkEntityFields, checkutil.WithoutImports()), |
| 47 | + } |
| 48 | + requiredRequestFieldsRuleSpec = &check.RuleSpec{ |
| 49 | + ID: requiredRequestFieldsRuleID, |
| 50 | + Default: true, |
| 51 | + Purpose: `Checks that all request methods (e.g: ListClutersRequest) define a known set of fields for the Qdrant Cloud API.`, |
| 52 | + Type: check.RuleTypeLint, |
| 53 | + Handler: checkutil.NewMessageRuleHandler(checkRequestFields, checkutil.WithoutImports()), |
| 54 | + } |
| 55 | + spec = &check.Spec{ |
| 56 | + Rules: []*check.RuleSpec{ |
| 57 | + requiredEntityFieldsRuleSpec, |
| 58 | + requiredRequestFieldsRuleSpec, |
| 59 | + }, |
| 60 | + Info: &info.Spec{ |
| 61 | + Documentation: `A plugin that checks that entity-related messages define a known set of fields for the Qdrant Cloud API.`, |
| 62 | + SPDXLicenseID: "", |
| 63 | + LicenseURL: "", |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + crudMethodPrefixes = []string{"List", "Get", "Delete", "Update", "Create"} |
| 68 | + defaultRequiredFields = []string{"id", "name", "account_id", "created_at"} |
| 69 | + defaultRequiredRequestFields = []string{"account_id"} |
| 70 | +) |
| 71 | + |
| 72 | +func main() { |
| 73 | + check.Main(spec) |
| 74 | +} |
| 75 | + |
| 76 | +func checkEntityFields(ctx context.Context, responseWriter check.ResponseWriter, request check.Request, fileDescriptor descriptor.FileDescriptor) error { |
| 77 | + requiredFields, err := getRequiredEntityFields(request) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + for entityName := range extractEntityNames(fileDescriptor) { |
| 83 | + msg := fileDescriptor.ProtoreflectFileDescriptor().Messages().ByName(protoreflect.Name(entityName)) |
| 84 | + if msg == nil { |
| 85 | + continue |
| 86 | + } |
| 87 | + missingFields := findMissingFields(msg, requiredFields) |
| 88 | + if len(missingFields) > 0 { |
| 89 | + responseWriter.AddAnnotation( |
| 90 | + check.WithMessagef("%q is missing required fields: %v", entityName, missingFields), |
| 91 | + check.WithDescriptor(msg), |
| 92 | + ) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +func checkRequestFields(ctx context.Context, responseWriter check.ResponseWriter, request check.Request, messageDescriptor protoreflect.MessageDescriptor) error { |
| 100 | + msgName := string(messageDescriptor.Name()) |
| 101 | + if !strings.HasSuffix(msgName, "Request") { |
| 102 | + return nil |
| 103 | + } |
| 104 | + var requiredFields []string |
| 105 | + if strings.HasPrefix(msgName, "List") || strings.HasPrefix(msgName, "Get") || strings.HasPrefix(msgName, "Delete") { |
| 106 | + requiredFields = defaultRequiredRequestFields |
| 107 | + } else { |
| 108 | + return nil |
| 109 | + } |
| 110 | + missingFields := findMissingFields(messageDescriptor, requiredFields) |
| 111 | + if len(missingFields) > 0 { |
| 112 | + responseWriter.AddAnnotation( |
| 113 | + check.WithMessagef("%q is missing required fields: %v", msgName, missingFields), |
| 114 | + check.WithDescriptor(messageDescriptor), |
| 115 | + ) |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// getRequiredEntityFields returns a list of required fields for a entity |
| 122 | +// message. It gets the values either from a plugin option or from the default |
| 123 | +// values. |
| 124 | +func getRequiredEntityFields(request check.Request) ([]string, error) { |
| 125 | + requiredFieldsOptionValue, err := option.GetStringSliceValue(request.Options(), requiredEntityFieldsOptionKey) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + if len(requiredFieldsOptionValue) > 0 { |
| 130 | + return requiredFieldsOptionValue, nil |
| 131 | + } |
| 132 | + return defaultRequiredFields, nil |
| 133 | +} |
| 134 | + |
| 135 | +// extractEntityNames returns a set of entity names inferred from the name of |
| 136 | +// the service methods. |
| 137 | +// e.g: [ListBooks, GetBook] -> {Book} |
| 138 | +func extractEntityNames(fileDescriptor descriptor.FileDescriptor) map[string]struct{} { |
| 139 | + entityNames := make(map[string]struct{}) |
| 140 | + services := fileDescriptor.FileDescriptorProto().GetService() |
| 141 | + for _, svc := range services { |
| 142 | + for _, method := range svc.Method { |
| 143 | + entityName := inferEntityFromMethodName(method.GetName()) |
| 144 | + if entityName != "" { |
| 145 | + entityNames[entityName] = struct{}{} |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + return entityNames |
| 150 | +} |
| 151 | + |
| 152 | +// inferEntityFromMethodName extracts the entity name by stripping CRUD prefixes |
| 153 | +func inferEntityFromMethodName(methodName string) string { |
| 154 | + p := pluralize.NewClient() |
| 155 | + for _, prefix := range crudMethodPrefixes { |
| 156 | + if strings.HasPrefix(methodName, prefix) { |
| 157 | + return p.Singular(strings.TrimPrefix(methodName, prefix)) |
| 158 | + } |
| 159 | + } |
| 160 | + return "" |
| 161 | +} |
| 162 | + |
| 163 | +// findMissingFields checks if a message contains all required fields. |
| 164 | +func findMissingFields(msg protoreflect.MessageDescriptor, requiredFields []string) []string { |
| 165 | + missingFields := []string{} |
| 166 | + fieldMap := make(map[string]bool) |
| 167 | + fields := msg.Fields() |
| 168 | + |
| 169 | + for i := 0; i < fields.Len(); i++ { |
| 170 | + field := fields.Get(i) |
| 171 | + fieldMap[string(field.Name())] = true |
| 172 | + } |
| 173 | + |
| 174 | + for _, requiredField := range requiredFields { |
| 175 | + if !fieldMap[requiredField] { |
| 176 | + missingFields = append(missingFields, requiredField) |
| 177 | + } |
| 178 | + } |
| 179 | + return missingFields |
| 180 | +} |
0 commit comments