Debian Codegraph @

cmd/mdatagen/internal/lint.go

raw symbols SCIP
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internal // import "go.opentelemetry.io/collector/cmd/mdatagen/internal"
5
6 import (
7 "errors"
8 "strings"
9 "unicode"
10
11 "go.opentelemetry.io/collector/cmd/mdatagen/third_party/golint"
12 )
13
14 // FormatIdentifier variable in a go-safe way
15 func FormatIdentifier(s string, exported bool) (string, error) {
16 if s == "" {
17 return "", errors.New("string cannot be empty")
18 }
19 // Convert various characters to . for strings.Title to operate on.
20 replace := strings.NewReplacer("_", ".", "-", ".", "<", ".", ">", ".", "/", ".", ":", ".")
21 str := replace.Replace(s)
22 str = strings.Title(str) //nolint:staticcheck // SA1019
23 str = strings.ReplaceAll(str, ".", "")
24
25 var word string
26 var output string
27
28 // Fixup acronyms to make lint happy.
29 for idx, r := range str {
30 if idx == 0 {
31 if exported {
32 r = unicode.ToUpper(r)
33 } else {
34 r = unicode.ToLower(r)
35 }
36 }
37
38 if unicode.IsUpper(r) || unicode.IsNumber(r) {
39 // If the current word is an acronym and it's either exported or it's not the
40 // beginning of an unexported variable then upper case it.
41 if golint.Acronyms[strings.ToUpper(word)] && (exported || output != "") {
42 output += strings.ToUpper(word)
43 word = string(r)
44 } else {
45 output += word
46 word = string(r)
47 }
48 } else {
49 word += string(r)
50 }
51 }
52
53 if golint.Acronyms[strings.ToUpper(word)] && output != "" {
54 output += strings.ToUpper(word)
55 } else {
56 output += word
57 }
58
59 // Remove white spaces
60 output = strings.Join(strings.Fields(output), "")
61
62 return output, nil
63 }