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

     1  package aip0126
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/googleapis/api-linter/lint"
     8  	"github.com/googleapis/api-linter/locations"
     9  	"github.com/jhump/protoreflect/desc"
    10  	"github.com/stoewer/go-strcase"
    11  )
    12  
    13  // All enum values must use UPPER_SNAKE_CASE.
    14  var enumValueUpperSnakeCase = &lint.EnumRule{
    15  	Name: lint.NewRuleName(126, "upper-snake-values"),
    16  	LintEnum: func(e *desc.EnumDescriptor) []lint.Problem {
    17  		var problems []lint.Problem
    18  		for _, v := range e.GetValues() {
    19  			if got, want := v.GetName(), toUpperSnakeCase(v.GetName()); got != want {
    20  				problems = append(problems, lint.Problem{
    21  					Message:    fmt.Sprintf("Enum value %q must use UPPER_SNAKE_CASE.", got),
    22  					Suggestion: want,
    23  					Descriptor: v,
    24  					Location:   locations.DescriptorName(v),
    25  				})
    26  			}
    27  		}
    28  		return problems
    29  	},
    30  }
    31  
    32  func toUpperSnakeCase(s string) string {
    33  	return strings.ToUpper(strcase.SnakeCase(s))
    34  }