github.com/googleapis/api-linter@v1.65.2/locations/field_locations.go (about) 1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package locations 16 17 import ( 18 "github.com/jhump/protoreflect/desc" 19 apb "google.golang.org/genproto/googleapis/api/annotations" 20 "google.golang.org/protobuf/reflect/protoreflect" 21 dpb "google.golang.org/protobuf/types/descriptorpb" 22 ) 23 24 // FieldOption returns the precise location for the given extension defintion on 25 // the given field. This is useful for writing rules against custom extensions. 26 // 27 // Example: locations.FieldOption(field, fieldbehaviorpb.E_FieldBehavior) 28 func FieldOption(f *desc.FieldDescriptor, e protoreflect.ExtensionType) *dpb.SourceCodeInfo_Location { 29 return pathLocation(f, 8, int(e.TypeDescriptor().Number())) // FieldDescriptor.options == 8 30 } 31 32 // FieldResourceReference returns the precise location for a field's 33 // resource reference annotation. 34 func FieldResourceReference(f *desc.FieldDescriptor) *dpb.SourceCodeInfo_Location { 35 return pathLocation(f, 8, int(apb.E_ResourceReference.TypeDescriptor().Number())) // FieldDescriptor.options == 8 36 } 37 38 // FieldType returns the precise location for a field's type. 39 func FieldType(f *desc.FieldDescriptor) *dpb.SourceCodeInfo_Location { 40 if f.GetMessageType() != nil || f.GetEnumType() != nil { 41 return pathLocation(f, 6) // FieldDescriptor.type_name == 6 42 } 43 return pathLocation(f, 5) // FieldDescriptor.type == 5 44 } 45 46 // FieldLabel returns the precise location for a field's label. 47 func FieldLabel(f *desc.FieldDescriptor) *dpb.SourceCodeInfo_Location { 48 return pathLocation(f, 4) // FieldDescriptor.label == 4 49 }