github.com/googleapis/api-linter@v1.65.2/locations/file_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 dpb "google.golang.org/protobuf/types/descriptorpb" 21 ) 22 23 // FileSyntax returns the location of the syntax definition in a file descriptor. 24 // 25 // If the location can not be found (for example, because there is no syntax 26 // statement), it returns nil. 27 func FileSyntax(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location { 28 return pathLocation(f, 12) // FileDescriptor.syntax == 12 29 } 30 31 // FilePackage returns the location of the package definition in a file descriptor. 32 // 33 // If the location can not be found (for example, because there is no package 34 // statement), it returns nil. 35 func FilePackage(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location { 36 return pathLocation(f, 2) // FileDescriptor.package == 2 37 } 38 39 // FileCsharpNamespace returns the location of the csharp_namespace file option 40 // in a file descriptor. 41 // 42 // If the location can not be found (for example, because there is no 43 // csharp_namespace option), it returns nil. 44 func FileCsharpNamespace(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location { 45 return pathLocation(f, 8, 37) // 8 == options, 37 == csharp_namespace 46 } 47 48 // FileJavaPackage returns the location of the java_package file option 49 // in a file descriptor. 50 // 51 // If the location can not be found (for example, because there is no 52 // java_package option), it returns nil. 53 func FileJavaPackage(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location { 54 return pathLocation(f, 8, 1) // 8 == options, 1 == java_package 55 } 56 57 // FilePhpNamespace returns the location of the php_namespace file option 58 // in a file descriptor. 59 // 60 // If the location can not be found (for example, because there is no 61 // php_namespace option), it returns nil. 62 func FilePhpNamespace(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location { 63 return pathLocation(f, 8, 41) // 8 == options, 41 == php_namespace 64 } 65 66 // FileRubyPackage returns the location of the ruby_package file option 67 // in a file descriptor. 68 // 69 // If the location can not be found (for example, because there is no 70 // ruby_package option), it returns nil. 71 func FileRubyPackage(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location { 72 return pathLocation(f, 8, 45) // 8 == options, 45 == ruby_package 73 } 74 75 // FileResourceDefinition returns the precise location of the `google.api.resource_definition` 76 // annotation. 77 func FileResourceDefinition(f *desc.FileDescriptor, index int) *dpb.SourceCodeInfo_Location { 78 // 8 == options 79 return pathLocation(f, 8, int(apb.E_ResourceDefinition.TypeDescriptor().Number()), index) 80 } 81 82 // FileImport returns the location of the import on the given `index`, or `nil` 83 // if no import with such `index` is found. 84 func FileImport(f *desc.FileDescriptor, index int) *dpb.SourceCodeInfo_Location { 85 return pathLocation(f, 3, index) // 3 == dependency 86 } 87 88 // FileCCEnableArenas returns the location of the `cc_enable_arenas` option. 89 func FileCCEnableArenas(f *desc.FileDescriptor) *dpb.SourceCodeInfo_Location { 90 return pathLocation(f, 8, 31) // 8 == (file) options, 31 == cc_enable_arenas 91 }