-
Notifications
You must be signed in to change notification settings - Fork 4
/
orderby_attribute.go
169 lines (150 loc) · 4.81 KB
/
orderby_attribute.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package validation
import (
"fmt"
"github.com/samber/lo"
"github.com/teamkeel/keel/schema/parser"
"github.com/teamkeel/keel/schema/query"
"github.com/teamkeel/keel/schema/validation/errorhandling"
)
func OrderByAttributeRule(asts []*parser.AST, errs *errorhandling.ValidationErrors) Visitor {
var currentModel *parser.ModelNode
var currentOperation *parser.ActionNode
var currentAttribute *parser.AttributeNode
var orderByAttributeDefined bool
var argumentLabels []string
return Visitor{
EnterModel: func(model *parser.ModelNode) {
currentModel = model
},
LeaveModel: func(_ *parser.ModelNode) {
currentModel = nil
},
EnterAction: func(action *parser.ActionNode) {
currentOperation = action
orderByAttributeDefined = false
},
LeaveAction: func(_ *parser.ActionNode) {
currentOperation = nil
orderByAttributeDefined = false
},
EnterAttribute: func(attribute *parser.AttributeNode) {
currentAttribute = attribute
argumentLabels = []string{}
if attribute.Name.Value != parser.AttributeOrderBy {
return
}
if currentOperation == nil {
return
}
if currentOperation.Type.Value != parser.ActionTypeList {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeNotAllowedError,
errorhandling.ErrorDetails{
Message: "@orderBy can only be used on list actions",
},
attribute.Name,
))
}
if orderByAttributeDefined {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeNotAllowedError,
errorhandling.ErrorDetails{
Message: "@orderBy can only be defined once per action",
},
attribute.Name,
))
}
orderByAttributeDefined = true
if len(attribute.Arguments) == 0 {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: "@orderBy requires at least once argument",
},
attribute,
))
}
},
EnterAttributeArgument: func(arg *parser.AttributeArgumentNode) {
if currentAttribute.Name.Value != parser.AttributeOrderBy {
return
}
if arg.Label == nil {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: "@orderBy arguments must be specified with a label corresponding with a field on this model",
Hint: "For example, @orderBy(surname: asc, firstName: asc)",
},
arg,
))
return
}
modelField := query.ModelField(currentModel, arg.Label.Value)
if modelField == nil {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: fmt.Sprintf("@orderBy argument label '%s' must correspond to a field on this model", arg.Label.Value),
},
arg.Label,
))
return
}
if query.IsHasOneModelField(asts, modelField) || query.IsHasManyModelField(asts, modelField) {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: "@orderBy does not support ordering of relationships fields",
},
arg.Label,
))
return
}
if modelField.Repeated {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: "@orderBy does not support ordering of array fields",
},
arg.Label,
))
return
}
if lo.SomeBy(argumentLabels, func(a string) bool { return a == arg.Label.Value }) {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: fmt.Sprintf("@orderBy argument name '%s' already defined", arg.Label.Value),
},
arg.Label,
))
return
}
argumentLabels = append(argumentLabels, arg.Label.Value)
operand, err := arg.Expression.ToValue()
if err != nil {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: "@orderBy argument is not correctly formatted",
Hint: "For example, @orderBy(surname: asc, firstName: asc)",
},
arg,
))
return
}
if operand.Ident == nil || (operand.Ident.Fragments[0].Fragment != parser.OrderByAscending && operand.Ident.Fragments[0].Fragment != parser.OrderByDescending) {
errs.AppendError(errorhandling.NewValidationErrorWithDetails(
errorhandling.AttributeArgumentError,
errorhandling.ErrorDetails{
Message: "@orderBy argument value must either be asc or desc",
Hint: "For example, @orderBy(surname: asc, firstName: asc)",
},
arg.Expression,
))
return
}
},
}
}