github.com/googleapis/api-linter@v1.65.2/rules/aip0162/aip0162.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 aip0162 contains rules defined in https://aip.dev/162.
    16  package aip0162
    17  
    18  import (
    19  	"regexp"
    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  		162,
    31  		commitHTTPBody,
    32  		commitHTTPMethod,
    33  		commitHTTPURISuffix,
    34  		commitRequestMessageName,
    35  		commitRequestNameBehavior,
    36  		commitRequestNameField,
    37  		commitRequestNameReference,
    38  		commitResponseMessageName,
    39  		deleteRevisionHTTPBody,
    40  		deleteRevisionHTTPMethod,
    41  		deleteRevisionHTTPURISuffix,
    42  		deleteRevisionRequestMessageName,
    43  		deleteRevisionRequestNameBehavior,
    44  		deleteRevisionRequestNameField,
    45  		deleteRevisionRequestNameReference,
    46  		deleteRevisionResponseMessageName,
    47  		rollbackHTTPBody,
    48  		rollbackHTTPMethod,
    49  		rollbackHTTPURISuffix,
    50  		rollbackRequestMessageName,
    51  		rollbackRequestNameBehavior,
    52  		rollbackRequestNameField,
    53  		rollbackRequestNameReference,
    54  		rollbackRequestRevisionIDBehavior,
    55  		rollbackRequestRevisionIDField,
    56  		rollbackResponseMessageName,
    57  		tagRevisionHTTPBody,
    58  		tagRevisionHTTPMethod,
    59  		tagRevisionHTTPURISuffix,
    60  		tagRevisionRequestMessageName,
    61  		tagRevisionRequestNameBehavior,
    62  		tagRevisionRequestNameField,
    63  		tagRevisionRequestNameReference,
    64  		tagRevisionRequestTagBehavior,
    65  		tagRevisionRequestTagField,
    66  		tagRevisionResponseMessageName,
    67  	)
    68  }
    69  
    70  var (
    71  	tagRevisionMethodRegexp     = regexp.MustCompile(`^Tag([A-Za-z0-9]+)Revision$`)
    72  	tagRevisionReqMessageRegexp = regexp.MustCompile(`^Tag(?:[A-Za-z0-9]+)RevisionRequest$`)
    73  	tagRevisionURINameRegexp    = regexp.MustCompile(`:tagRevision$`)
    74  )
    75  
    76  // Returns true if this is an AIP-162 Tag Revision method, false otherwise.
    77  func isTagRevisionMethod(m *desc.MethodDescriptor) bool {
    78  	return tagRevisionMethodRegexp.MatchString(m.GetName())
    79  }
    80  
    81  // Returns true if this is an AIP-162 Tag Revision request message, false otherwise.
    82  func isTagRevisionRequestMessage(m *desc.MessageDescriptor) bool {
    83  	return tagRevisionReqMessageRegexp.MatchString(m.GetName())
    84  }
    85  
    86  var (
    87  	commitMethodRegexp     = regexp.MustCompile(`^Commit([A-Za-z0-9]+)$`)
    88  	commitReqMessageRegexp = regexp.MustCompile(`^Commit(?:[A-Za-z0-9]+)Request$`)
    89  	commitURINameRegexp    = regexp.MustCompile(`:commit$`)
    90  )
    91  
    92  // Returns true if this is an AIP-162 Commit method, false otherwise.
    93  func isCommitMethod(m *desc.MethodDescriptor) bool {
    94  	return commitMethodRegexp.MatchString(m.GetName())
    95  }
    96  
    97  // Returns true if this is an AIP-162 Commit request message, false otherwise.
    98  func isCommitRequestMessage(m *desc.MessageDescriptor) bool {
    99  	return commitReqMessageRegexp.MatchString(m.GetName())
   100  }
   101  
   102  var (
   103  	rollbackMethodRegexp     = regexp.MustCompile(`^Rollback([A-Za-z0-9]+)$`)
   104  	rollbackReqMessageRegexp = regexp.MustCompile(`^Rollback(?:[A-Za-z0-9]+)Request$`)
   105  	rollbackURINameRegexp    = regexp.MustCompile(`:rollback$`)
   106  )
   107  
   108  // Returns true if this is an AIP-162 Rollback method, false otherwise.
   109  func isRollbackMethod(m *desc.MethodDescriptor) bool {
   110  	return rollbackMethodRegexp.MatchString(m.GetName())
   111  }
   112  
   113  // Returns true if this is an AIP-162 Rollback request message, false otherwise.
   114  func isRollbackRequestMessage(m *desc.MessageDescriptor) bool {
   115  	return rollbackReqMessageRegexp.MatchString(m.GetName())
   116  }
   117  
   118  var (
   119  	deleteRevisionMethodRegexp     = regexp.MustCompile(`^Delete(?:[A-Za-z0-9]+)Revision$`)
   120  	deleteRevisionReqMessageRegexp = regexp.MustCompile(`^Delete(?:[A-Za-z0-9]+)RevisionRequest$`)
   121  	deleteRevisionURINameRegexp    = regexp.MustCompile(`:deleteRevision$`)
   122  )
   123  
   124  // Returns true if this is an AIP-162 Delete Revision method, false otherwise.
   125  func isDeleteRevisionMethod(m *desc.MethodDescriptor) bool {
   126  	return deleteRevisionMethodRegexp.MatchString(m.GetName())
   127  }
   128  
   129  // Returns true if this is an AIP-162 Delete Revision request message, false otherwise.
   130  func isDeleteRevisionRequestMessage(m *desc.MessageDescriptor) bool {
   131  	return deleteRevisionReqMessageRegexp.MatchString(m.GetName())
   132  }
   133  
   134  // IsListRevisionsRequestMessage returns true if this is an AIP-162 List
   135  // Revisions request message, false otherwise.
   136  //
   137  // Deprecated: Use the same method from the utils package instead.
   138  func IsListRevisionsRequestMessage(m *desc.MessageDescriptor) bool {
   139  	return utils.IsListRevisionsRequestMessage(m)
   140  }
   141  
   142  // IsListRevisionsResponseMessage returns true if this is an AIP-162 List
   143  // Revisions response message, false otherwise.
   144  //
   145  // Deprecated: Use the same method from the utils package instead.
   146  func IsListRevisionsResponseMessage(m *desc.MessageDescriptor) bool {
   147  	return utils.IsListRevisionsResponseMessage(m)
   148  }