github.com/googleapis/api-linter@v1.65.2/rules/aip0191/file_layout.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 aip0191
    16  
    17  import (
    18  	"github.com/googleapis/api-linter/lint"
    19  	"github.com/jhump/protoreflect/desc"
    20  )
    21  
    22  var fileLayout = &lint.FileRule{
    23  	Name: lint.NewRuleName(191, "file-layout"),
    24  	LintFile: func(f *desc.FileDescriptor) (problems []lint.Problem) {
    25  		// Verify that services precede messages.
    26  		if len(f.GetMessageTypes()) > 0 {
    27  			firstMessage := f.GetMessageTypes()[0]
    28  			for _, service := range f.GetServices() {
    29  				if isAfter(firstMessage, service) {
    30  					problems = append(problems, lint.Problem{
    31  						Message:    "Services should precede all messages.",
    32  						Descriptor: service,
    33  					})
    34  				}
    35  			}
    36  		}
    37  
    38  		// Verify that messages precede top-level enums.
    39  		if len(f.GetEnumTypes()) > 0 {
    40  			firstEnum := f.GetEnumTypes()[0]
    41  			for _, message := range f.GetMessageTypes() {
    42  				if isBefore(message, firstEnum) {
    43  					problems = append(problems, lint.Problem{
    44  						Message:    "Messages should precede all top-level enums.",
    45  						Descriptor: firstEnum,
    46  					})
    47  					break // Sending this over and over would be obnoxious.
    48  				}
    49  			}
    50  		}
    51  
    52  		return
    53  	},
    54  }
    55  
    56  // isBefore returns true if `d` is known to precede `anchor` in the file.
    57  //
    58  // NOTE: A false value here may indicate that there is no source info at all;
    59  //
    60  //	use `isAfter` if the goal is to know that `d` comes after `anchor`.
    61  func isBefore(anchor desc.Descriptor, d desc.Descriptor) bool {
    62  	return d.GetSourceInfo().GetSpan()[0] < anchor.GetSourceInfo().GetSpan()[0]
    63  }
    64  
    65  // isBefore returns true if `d` is known to follow `anchor` in the file.
    66  //
    67  // NOTE: A false value here may indicate that there is no source info at all;
    68  //
    69  //	use `isBefore` if the goal is to know that `d` comes before `anchor`.
    70  func isAfter(anchor desc.Descriptor, d desc.Descriptor) bool {
    71  	return d.GetSourceInfo().GetSpan()[0] > anchor.GetSourceInfo().GetSpan()[0]
    72  }