github.com/googleapis/api-linter@v1.65.2/lint/rule_urls.go (about)

     1  package lint
     2  
     3  import "strings"
     4  
     5  // A list of mapping functions, each of which returns the rule URL for
     6  // the given rule name, and if not found, return an empty string.
     7  //
     8  // At Google, we inject additional rule URL mappings into this list.
     9  // Example: google_rule_url_mappings.go
    10  // package lint
    11  //
    12  //	func init() {
    13  //	  ruleURLMappings = append(ruleURLMappings, internalRuleURLMapping)
    14  //	}
    15  //
    16  //	func internalRuleURLMapping(ruleName string) string {
    17  //	  ...
    18  //	}
    19  var ruleURLMappings = []func(string) string{
    20  	coreRuleURL,
    21  	clientLibrariesRuleURL,
    22  	cloudRuleURL,
    23  }
    24  
    25  func coreRuleURL(ruleName string) string {
    26  	return groupURL(ruleName, "core")
    27  }
    28  
    29  func clientLibrariesRuleURL(ruleName string) string {
    30  	return groupURL(ruleName, "client-libraries")
    31  }
    32  
    33  func cloudRuleURL(ruleName string) string {
    34  	return groupURL(ruleName, "cloud")
    35  }
    36  
    37  func groupURL(ruleName, groupName string) string {
    38  	base := "https://linter.aip.dev/"
    39  	nameParts := strings.Split(ruleName, "::") // e.g., client-libraries::0122::camel-case-uris -> ["client-libraries", "0122", "camel-case-uris"]
    40  	if len(nameParts) == 0 || nameParts[0] != groupName {
    41  		return ""
    42  	}
    43  	path := strings.TrimPrefix(strings.Join(nameParts[1:], "/"), "0")
    44  	return base + path
    45  }
    46  
    47  func getRuleURL(ruleName string, nameURLMappings []func(string) string) string {
    48  	for i := len(nameURLMappings) - 1; i >= 0; i-- {
    49  		if url := nameURLMappings[i](ruleName); url != "" {
    50  			return url
    51  		}
    52  	}
    53  	return ""
    54  }