github.com/googleapis/api-linter@v1.65.2/rules/aip0152/request_resource_suffix.go (about)

     1  // Copyright 2021 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 aip0152
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/googleapis/api-linter/lint"
    22  	"github.com/googleapis/api-linter/locations"
    23  	"github.com/googleapis/api-linter/rules/internal/utils"
    24  	"github.com/jhump/protoreflect/desc"
    25  )
    26  
    27  // The name of the resource must end with the word "Job".
    28  var requestResourceSuffix = &lint.FieldRule{
    29  	Name: lint.NewRuleName(152, "request-resource-suffix"),
    30  	OnlyIf: func(f *desc.FieldDescriptor) bool {
    31  		return isRunRequestMessage(f.GetOwner()) && f.GetName() == "name"
    32  	},
    33  	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
    34  		// Rule check: Establish that the `resource_reference` annotation's
    35  		// type ends in "Job".
    36  		ref := utils.GetResourceReference(f)
    37  		if ref != nil && !strings.HasSuffix(ref.GetType(), "Job") {
    38  			suggestion := strings.TrimPrefix(f.GetOwner().GetName(), "Run")
    39  			suggestion = strings.TrimSuffix(suggestion, "Request")
    40  
    41  			return []lint.Problem{{
    42  				Message:    fmt.Sprintf("The `type` of the `google.api.resource_reference` annotation should end in %q.", "Job"),
    43  				Descriptor: f,
    44  				Location:   locations.FieldResourceReference(f),
    45  				Suggestion: suggestion,
    46  			}}
    47  		}
    48  		return nil
    49  	},
    50  }