-
Notifications
You must be signed in to change notification settings - Fork 4
/
casing.go
117 lines (104 loc) · 2.97 KB
/
casing.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package validation
import (
"fmt"
"regexp"
"strings"
"github.com/teamkeel/keel/casing"
"github.com/teamkeel/keel/schema/parser"
"github.com/teamkeel/keel/schema/validation/errorhandling"
)
// Casing checks that entities in the schema conform to our casing
// conventions.
//
// Models, enums, enum values, roles, and API's must written in UpperCamelCase.
// Fields, actions, and inputs must be written in lowerCamelCase.
func CasingRule(_ []*parser.AST, errs *errorhandling.ValidationErrors) Visitor {
return Visitor{
EnterModel: func(n *parser.ModelNode) {
errs.AppendError(checkCasing(n.Name, "model"))
},
EnterField: func(n *parser.FieldNode) {
errs.AppendError(checkCasing(n.Name, "field"))
},
EnterAction: func(n *parser.ActionNode) {
errs.AppendError(checkCasing(n.Name, "action"))
},
EnterActionInput: func(n *parser.ActionInputNode) {
if n.Label == nil {
return
}
errs.AppendError(checkCasing(*n.Label, "input"))
},
EnterEnum: func(n *parser.EnumNode) {
errs.AppendError(checkCasing(n.Name, "enum"))
for _, v := range n.Values {
errs.AppendError(checkCasing(v.Name, "enum value"))
}
},
EnterMessage: func(n *parser.MessageNode) {
errs.AppendError(checkCasing(n.Name, "message"))
},
EnterRole: func(n *parser.RoleNode) {
errs.AppendError(checkCasing(n.Name, "role"))
},
EnterAPI: func(n *parser.APINode) {
errs.AppendError(checkCasing(n.Name, "api"))
},
EnterJob: func(n *parser.JobNode) {
errs.AppendError(checkCasing(n.Name, "job"))
},
EnterJobInput: func(n *parser.JobInputNode) {
errs.AppendError(checkCasing(n.Name, "job input"))
},
}
}
func checkCasing(node parser.NameNode, entity string) *errorhandling.ValidationError {
expected := toCamelCase(node.Value)
casing := "UpperCamelCase"
switch entity {
case "field", "action", "input", "job input":
expected = toLowerCamelCase(node.Value)
casing = "lowerCamelCase"
}
if entity == "enum value" {
entity = "enum values"
} else {
entity = fmt.Sprintf("%s names", entity)
}
if node.Value == expected {
return nil
}
return errorhandling.NewValidationErrorWithDetails(
errorhandling.NamingError,
errorhandling.ErrorDetails{
Message: fmt.Sprintf("%s must use %s", entity, casing),
Hint: fmt.Sprintf("e.g. %s", expected),
},
node,
)
}
var (
allCapsRe = regexp.MustCompile("^[A-Z_]+$")
)
func toCamelCase(s string) string {
// Special cases:
// - For "FOOBAR" we want "Foobar"
// - For "FOO_BAR" we want "FooBar"
// To get there we have to first lower case the string so
// casing.ToCamel does the right thing
if allCapsRe.MatchString(s) {
s = strings.ToLower(s)
}
return casing.ToCamel(s)
}
func toLowerCamelCase(s string) string {
// Special cases:
// - For "FOOBAR" we want "foobar"
// - For "FOO_BAR" we want "fooBar"
// To get there we have to first lower case the string so
// casing.ToLowerCamel does the right thing
if allCapsRe.MatchString(s) {
s = strings.ToLower(s)
}
return casing.ToLowerCamel(s)
}