-
Notifications
You must be signed in to change notification settings - Fork 4
/
duplicate_message_names.go
87 lines (76 loc) · 2.14 KB
/
duplicate_message_names.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
package validation
import (
"fmt"
"github.com/teamkeel/keel/schema/parser"
"github.com/teamkeel/keel/schema/query"
"github.com/teamkeel/keel/schema/validation/errorhandling"
)
func DuplicateMessageNames(asts []*parser.AST, errs *errorhandling.ValidationErrors) Visitor {
return Visitor{
EnterMessage: func(n *parser.MessageNode) {
if n.BuiltIn {
return
}
for _, model := range query.Models(asts) {
if model.Name.Value == n.Name.Value {
var message string
if model.BuiltIn {
message = fmt.Sprintf("There already exists a reserved model with the name '%s'", n.Name.Value)
} else {
message = fmt.Sprintf("There already exists a model with the name '%s'", n.Name.Value)
}
errs.AppendError(
errorhandling.NewValidationErrorWithDetails(
errorhandling.NamingError,
errorhandling.ErrorDetails{
Message: message,
Hint: "Use unique names between models, enums and messages",
},
n.Name,
),
)
break
}
}
for _, enum := range query.Enums(asts) {
if enum.Name.Value == n.Name.Value {
errs.AppendError(
errorhandling.NewValidationErrorWithDetails(
errorhandling.NamingError,
errorhandling.ErrorDetails{
Message: fmt.Sprintf("There already exists an enum with the name '%s'", n.Name.Value),
Hint: "Use unique names between models, enums and messages",
},
n.Name,
),
)
break
}
}
for _, message := range query.Messages(asts) {
if n == message {
continue
}
if message.Name.Value == n.Name.Value {
var m string
if message.BuiltIn {
m = fmt.Sprintf("There already exists a reserved message with the name '%s'", n.Name.Value)
} else {
m = fmt.Sprintf("There already exists a message with the name '%s'", n.Name.Value)
}
errs.AppendError(
errorhandling.NewValidationErrorWithDetails(
errorhandling.NamingError,
errorhandling.ErrorDetails{
Message: m,
Hint: "Use unique names between models, enums and messages",
},
n.Name,
),
)
break
}
}
},
}
}