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

     1  // Copyright 2020 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 aip0165 contains rules defined in https://aip.dev/165.
    16  package aip0165
    17  
    18  import (
    19  	"regexp"
    20  
    21  	"github.com/googleapis/api-linter/lint"
    22  	"github.com/jhump/protoreflect/desc"
    23  )
    24  
    25  // AddRules adds all of the AIP-165 rules to the provided registry.
    26  func AddRules(r lint.RuleRegistry) error {
    27  	return r.Register(
    28  		165,
    29  		httpBody,
    30  		httpMethod,
    31  		httpParentVariable,
    32  		httpURISuffix,
    33  		requestFilterBehavior,
    34  		requestFilterField,
    35  		requestForceField,
    36  		requestMessageName,
    37  		requestParentBehavior,
    38  		requestParentField,
    39  		requestParentReference,
    40  		responseMessageName,
    41  		responsePurgeCountField,
    42  		responsePurgeSampleField,
    43  		responsePurgeSampleReference,
    44  	)
    45  }
    46  
    47  var (
    48  	purgeMethodRegexp      = regexp.MustCompile("^Purge(?:[A-Z]|$)")
    49  	purgeReqMessageRegexp  = regexp.MustCompile("^Purge[A-Za-z0-9]*Request$")
    50  	purgeRespMessageRegexp = regexp.MustCompile("^Purge[A-Za-z0-9]*Response$")
    51  	purgeURINameRegexp     = regexp.MustCompile(`:purge$`)
    52  )
    53  
    54  // Returns true if this is a AIP-165 Purge method, false otherwise.
    55  func isPurgeMethod(m *desc.MethodDescriptor) bool {
    56  	return purgeMethodRegexp.MatchString(m.GetName())
    57  }
    58  
    59  // Returns true if this is an AIP-165 Purge request message, false otherwise.
    60  func isPurgeRequestMessage(m *desc.MessageDescriptor) bool {
    61  	return purgeReqMessageRegexp.MatchString(m.GetName())
    62  }
    63  
    64  // Returns true if this is an AIP-165 Purge response message, false otherwise.
    65  func isPurgeResponseMessage(m *desc.MessageDescriptor) bool {
    66  	return purgeRespMessageRegexp.MatchString(m.GetName())
    67  }