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

     1  // Copyright 2023 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 utils
    16  
    17  import (
    18  	"regexp"
    19  
    20  	"github.com/jhump/protoreflect/desc"
    21  	"github.com/stoewer/go-strcase"
    22  )
    23  
    24  var (
    25  	getReqMessageRegexp            = regexp.MustCompile("^Get[A-Za-z0-9]*Request$")
    26  	listReqMessageRegexp           = regexp.MustCompile("^List[A-Za-z0-9]*Request$")
    27  	listRespMessageRegexp          = regexp.MustCompile("^List([A-Za-z0-9]*)Response$")
    28  	listRevisionsReqMessageRegexp  = regexp.MustCompile(`^List(?:[A-Za-z0-9]+)RevisionsRequest$`)
    29  	listRevisionsRespMessageRegexp = regexp.MustCompile(`^List(?:[A-Za-z0-9]+)RevisionsResponse$`)
    30  	createReqMessageRegexp         = regexp.MustCompile("^Create[A-Za-z0-9]*Request$")
    31  	updateReqMessageRegexp         = regexp.MustCompile("^Update[A-Za-z0-9]*Request$")
    32  	deleteReqMessageRegexp         = regexp.MustCompile("^Delete[A-Za-z0-9]*Request$")
    33  )
    34  
    35  // Returns true if this is an AIP-131 Get request message, false otherwise.
    36  func IsGetRequestMessage(m *desc.MessageDescriptor) bool {
    37  	return getReqMessageRegexp.MatchString(m.GetName())
    38  }
    39  
    40  // Return true if this is an AIP-132 List request message, false otherwise.
    41  func IsListRequestMessage(m *desc.MessageDescriptor) bool {
    42  	return listReqMessageRegexp.MatchString(m.GetName()) && !IsListRevisionsRequestMessage(m)
    43  }
    44  
    45  // Return true if this is an AIP-132 List response message, false otherwise.
    46  func IsListResponseMessage(m *desc.MessageDescriptor) bool {
    47  	return listRespMessageRegexp.MatchString(m.GetName()) && !IsListRevisionsResponseMessage(m)
    48  }
    49  
    50  // Returns the name of the resource type from the response message name based on
    51  // Standard List response message naming convention. If the message is not a
    52  // Standard List response message, empty string is returned.
    53  func ListResponseResourceName(m *desc.MessageDescriptor) string {
    54  	if !IsListResponseMessage(m) {
    55  		return ""
    56  	}
    57  
    58  	return strcase.SnakeCase(listRespMessageRegexp.FindStringSubmatch(m.GetName())[1])
    59  }
    60  
    61  // IsListRevisionsRequestMessage returns true if this is an AIP-162 List
    62  // Revisions request message, false otherwise.
    63  func IsListRevisionsRequestMessage(m *desc.MessageDescriptor) bool {
    64  	return listRevisionsReqMessageRegexp.MatchString(m.GetName())
    65  }
    66  
    67  // IsListRevisionsResponseMessage returns true if this is an AIP-162 List
    68  // Revisions response message, false otherwise.
    69  func IsListRevisionsResponseMessage(m *desc.MessageDescriptor) bool {
    70  	return listRevisionsRespMessageRegexp.MatchString(m.GetName())
    71  }
    72  
    73  // Returns true if this is an AIP-133 Get request message, false otherwise.
    74  func IsCreateRequestMessage(m *desc.MessageDescriptor) bool {
    75  	return createReqMessageRegexp.MatchString(m.GetName())
    76  }
    77  
    78  // Returns true if this is an AIP-134 Update request message, false otherwise.
    79  func IsUpdateRequestMessage(m *desc.MessageDescriptor) bool {
    80  	return updateReqMessageRegexp.MatchString(m.GetName())
    81  }
    82  
    83  // Returns true if this is an AIP-135 Delete request message, false otherwise.
    84  func IsDeleteRequestMessage(m *desc.MessageDescriptor) bool {
    85  	return deleteReqMessageRegexp.MatchString(m.GetName())
    86  }