github.com/googleapis/api-linter@v1.65.2/rules/aip0135/method_signature.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 aip0135
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"bitbucket.org/creachadair/stringset"
    22  	"github.com/googleapis/api-linter/lint"
    23  	"github.com/googleapis/api-linter/locations"
    24  	"github.com/googleapis/api-linter/rules/internal/utils"
    25  	"github.com/jhump/protoreflect/desc"
    26  )
    27  
    28  var methodSignature = &lint.MethodRule{
    29  	Name:   lint.NewRuleName(135, "method-signature"),
    30  	OnlyIf: utils.IsDeleteMethod,
    31  	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
    32  		signatures := utils.GetMethodSignatures(m)
    33  		in := m.GetInputType()
    34  
    35  		fields := []string{"name"}
    36  		if etag := in.FindFieldByName("etag"); etag != nil {
    37  			fields = append(fields, etag.GetName())
    38  		}
    39  		if force := in.FindFieldByName("force"); force != nil {
    40  			fields = append(fields, force.GetName())
    41  		}
    42  		want := strings.Join(fields, ",")
    43  
    44  		// Check if the signature is missing.
    45  		if len(signatures) == 0 {
    46  			return []lint.Problem{{
    47  				Message: fmt.Sprintf(
    48  					"Delete methods should include `(google.api.method_signature) = %q`",
    49  					want,
    50  				),
    51  				Descriptor: m,
    52  			}}
    53  		}
    54  
    55  		// Check if the signature contains a disallowed field or doesn't contain
    56  		// "name".
    57  		first := signatures[0]
    58  		fieldSet := stringset.New(fields...)
    59  		if !fieldSet.Contains(first...) || !stringset.New(first...).Contains("name") {
    60  			return []lint.Problem{{
    61  				Message:    fmt.Sprintf("The method signature for Delete methods should be %q.", want),
    62  				Suggestion: fmt.Sprintf("option (google.api.method_signature) = %q;", want),
    63  				Descriptor: m,
    64  				Location:   locations.MethodSignature(m, 0),
    65  			}}
    66  		}
    67  
    68  		return nil
    69  	},
    70  }