github.com/googleapis/api-linter@v1.65.2/rules/aip0136/aip0136.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 aip0136 contains rules defined in https://aip.dev/136.
    16  package aip0136
    17  
    18  import (
    19  	"strings"
    20  
    21  	"github.com/googleapis/api-linter/lint"
    22  	"github.com/googleapis/api-linter/rules/internal/utils"
    23  	"github.com/jhump/protoreflect/desc"
    24  )
    25  
    26  // AddRules accepts a register function and registers each of
    27  // this AIP's rules to it.
    28  func AddRules(r lint.RuleRegistry) error {
    29  	return r.Register(
    30  		136,
    31  		httpBody,
    32  		httpMethod,
    33  		noPrepositions,
    34  		standardMethodsOnly,
    35  		uriSuffix,
    36  		verbNoun,
    37  		// These rules are disabled as they have no matching AIP guidance.
    38  		// See https://github.com/aip-dev/google.aip.dev/issues/955 for details.
    39  		// httpNameVariable,
    40  		// httpParentVariable,
    41  	)
    42  }
    43  
    44  func isCustomMethod(m *desc.MethodDescriptor) bool {
    45  	// Anything with a `:` in the method URI is automatically a custom
    46  	// method, regardless of the RPC name.
    47  	for _, httpRule := range utils.GetHTTPRules(m) {
    48  		if strings.Contains(httpRule.GetPlainURI(), ":") {
    49  			return true
    50  		}
    51  	}
    52  
    53  	// Methods with no `:` in the URI are standard methods if they begin with
    54  	// one of the standard method names.
    55  	for _, prefix := range []string{"Get", "List", "Create", "Update", "Delete", "Replace"} {
    56  		if strings.HasPrefix(m.GetName(), prefix) {
    57  			return false
    58  		}
    59  	}
    60  	return true
    61  }