| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"strings" |
| 9 |
"unicode" |
| 10 |
|
| 11 |
"go.opentelemetry.io/collector/cmd/mdatagen/third_party/golint" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
func FormatIdentifier(s string, exported bool) (string, error) { |
| 16 |
if s == "" { |
| 17 |
return "", errors.New("string cannot be empty") |
| 18 |
} |
| 19 |
|
| 20 |
replace := strings.NewReplacer("_", ".", "-", ".", "<", ".", ">", ".", "/", ".", ":", ".") |
| 21 |
str := replace.Replace(s) |
| 22 |
str = strings.Title(str) |
| 23 |
str = strings.ReplaceAll(str, ".", "") |
| 24 |
|
| 25 |
var word string |
| 26 |
var output string |
| 27 |
|
| 28 |
|
| 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 |
|
| 40 |
|
| 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 |
|
| 60 |
output = strings.Join(strings.Fields(output), "") |
| 61 |
|
| 62 |
return output, nil |
| 63 |
} |