-
Notifications
You must be signed in to change notification settings - Fork 7
/
nameFormatter.go
67 lines (52 loc) · 1.49 KB
/
nameFormatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package dingo
import (
"bytes"
"errors"
"unicode"
)
var chars = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var digits = []byte("0123456789")
// FormatDefName is the function used to turn the definition name
// into something that can be used in the generated container.
func FormatDefName(name string) string {
formatted := bytes.NewBuffer(nil)
start := true
for _, c := range name {
if !bytes.ContainsRune(chars, c) {
start = true
continue
}
if !start {
formatted.WriteRune(c)
continue
}
formatted.WriteRune(unicode.ToUpper(c))
start = false
}
return formatted.String()
}
// DefNameIsAllowed returns an error if the definition name is not allowed.
func DefNameIsAllowed(name string) error {
names := []string{"", "C", "ErrorCallback", "Container", "NewContainer"}
formatted := FormatDefName(name)
for _, n := range names {
if n == formatted {
return errors.New("DefName '" + name + "' is not allowed (reserved key word)")
}
}
if bytes.ContainsRune(digits, rune(formatted[0])) {
return errors.New("DefName '" + name + "' is not allowed (first char is a digit)")
}
return nil
}
// FormatPkgName formats a package name by keeping only the letters.
func FormatPkgName(name string) string {
formatted := bytes.NewBuffer(nil)
for _, c := range []byte(name) {
if bytes.Contains(letters, []byte{c}) {
formatted.WriteByte(c)
}
}
return formatted.String()
}