...

Source file src/go.einride.tech/aip/reflect/aipreflect/grammaticalname.go

Documentation: go.einride.tech/aip/reflect/aipreflect

     1  package aipreflect
     2  
     3  import (
     4  	"fmt"
     5  	"unicode"
     6  	"unicode/utf8"
     7  )
     8  
     9  // GrammaticalName is the grammatical name for the singular or plural form of resource type.
    10  // Grammatical names must be URL-safe and use lowerCamelCase.
    11  type GrammaticalName string // e.g. "userEvents"
    12  
    13  // Validate checks that the grammatical name is non-empty, URL-safe, and uses lowerCamelCase.
    14  func (g GrammaticalName) Validate() error {
    15  	if len(g) == 0 {
    16  		return fmt.Errorf("validate grammatical name: must be non-empty")
    17  	}
    18  	for _, r := range g {
    19  		if !unicode.In(r, unicode.Letter, unicode.Digit) {
    20  			return fmt.Errorf("validate grammatical name '%s': contains forbidden character '%s'", g, string(r))
    21  		}
    22  	}
    23  	if r, _ := utf8.DecodeRuneInString(string(g)); !unicode.IsLower(r) {
    24  		return fmt.Errorf("validate grammatical name '%s': must be lowerCamelCase", g)
    25  	}
    26  	return nil
    27  }
    28  
    29  // UpperCamelCase returns the UpperCamelCase version of the grammatical name, for use in e.g. method names.
    30  func (g GrammaticalName) UpperCamelCase() string {
    31  	return initialUpperCase(string(g))
    32  }
    33  

View as plain text