-
Notifications
You must be signed in to change notification settings - Fork 8
/
params.go
268 lines (223 loc) · 5.63 KB
/
params.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package jsonapi
import (
"sort"
"strings"
)
// NewParams creates and returns a Params object built from a SimpleURL and a
// given resource type. A schema is used for validation.
//
// If validation is not expected, it is recommended to simply build a SimpleURL
// object with NewSimpleURL.
func NewParams(schema *Schema, su SimpleURL, resType string) (*Params, error) {
params := &Params{
Fields: map[string][]string{},
Attrs: map[string][]Attr{},
Rels: map[string][]Rel{},
RelData: map[string][]string{},
Filter: nil,
SortingRules: []string{},
Include: [][]Rel{},
}
// Include
incs := make([]string, len(su.Include))
copy(incs, su.Include)
sort.Strings(incs)
// Remove duplicates and uncessary includes
for i := len(incs) - 1; i >= 0; i-- {
if i > 0 {
if strings.HasPrefix(incs[i], incs[i-1]) {
incs = append(incs[:i-1], incs[i:]...)
}
}
}
// Check inclusions
for i := 0; i < len(incs); i++ {
words := strings.Split(incs[i], ".")
incRel := Rel{ToType: resType}
for _, word := range words {
if typ := schema.GetType(incRel.ToType); typ.Name != "" {
var ok bool
if incRel, ok = typ.Rels[word]; ok {
params.Fields[incRel.ToType] = []string{}
} else {
incs = append(incs[:i], incs[i+1:]...)
break
}
}
}
}
// Build params.Include
params.Include = make([][]Rel, len(incs))
for i := range incs {
words := strings.Split(incs[i], ".")
params.Include[i] = make([]Rel, len(words))
var incRel Rel
for w := range words {
if w == 0 {
typ := schema.GetType(resType)
incRel = typ.Rels[words[0]]
}
params.Include[i][w] = incRel
if w < len(words)-1 {
typ := schema.GetType(incRel.ToType)
incRel = typ.Rels[words[w+1]]
}
}
}
if resType != "" {
params.Fields[resType] = []string{}
}
// Fields
for t, fields := range su.Fields {
if t != resType {
if typ := schema.GetType(t); typ.Name == "" {
return nil, NewErrUnknownTypeInURL(t)
}
}
if typ := schema.GetType(t); typ.Name != "" {
params.Fields[t] = []string{}
for _, f := range fields {
if f == "id" {
params.Fields[t] = append(params.Fields[t], "id")
} else {
for _, ff := range typ.Fields() {
if f == ff {
params.Fields[t] = append(params.Fields[t], f)
}
}
}
}
// Check for duplicates
for i := range params.Fields[t] {
for j := i + 1; j < len(params.Fields[t]); j++ {
if params.Fields[t][i] == params.Fields[t][j] {
return nil, NewErrDuplicateFieldInFieldsParameter(
typ.Name,
params.Fields[t][i],
)
}
}
}
}
}
for t := range params.Fields {
if len(params.Fields[t]) == 0 {
typ := schema.GetType(t)
params.Fields[t] = make([]string, len(typ.Fields()))
copy(params.Fields[t], typ.Fields())
}
}
// Attrs and Rels
for typeName, fields := range params.Fields {
// This should always return a type since
// it is checked earlier.
typ := schema.GetType(typeName)
params.Attrs[typeName] = make([]Attr, 0, len(typ.Attrs))
params.Rels[typeName] = make([]Rel, 0, len(typ.Attrs))
for _, field := range typ.Fields() {
for _, field2 := range fields {
if field == field2 {
if typ = schema.GetType(typeName); typ.Name != "" {
if attr, ok := typ.Attrs[field]; ok {
// Append to list of attributes
params.Attrs[typeName] = append(
params.Attrs[typeName],
attr,
)
} else if rel, ok := typ.Rels[field]; ok {
// Append to list of relationships
params.Rels[typeName] = append(
params.Rels[typeName],
rel,
)
}
}
}
}
}
}
// Filter
params.FilterLabel = su.FilterLabel
params.Filter = su.Filter
// TODO Check whether the filter is valid
// Sorting
// TODO All of the following is just to figure out
// if the URL represents a single resource or a
// collection. It should be done in a better way.
isCol := false
if len(su.Fragments) == 1 {
isCol = true
} else if len(su.Fragments) >= 3 {
relName := su.Fragments[len(su.Fragments)-1]
typ := schema.GetType(su.Fragments[0])
// Checked earlier, assuming should be safe
rel := typ.Rels[relName]
isCol = !rel.ToOne
}
if isCol {
typ := schema.GetType(resType)
sortingRules := make([]string, 0, len(typ.Attrs))
idFound := false
for _, rule := range su.SortingRules {
urule := rule
if urule[0] == '-' {
urule = urule[1:]
}
if urule == "id" {
idFound = true
sortingRules = append(sortingRules, rule)
break
}
for _, attr := range typ.Attrs {
if urule == attr.Name {
sortingRules = append(sortingRules, rule)
break
}
}
}
// Add 1 because of id
restOfRules := make([]string, 0, len(typ.Attrs)+1-len(sortingRules))
for _, attr := range typ.Attrs {
found := false
for _, rule := range sortingRules {
urule := rule
if urule[0] == '-' {
urule = urule[1:]
}
if urule == attr.Name {
found = true
break
}
}
if !found {
restOfRules = append(restOfRules, attr.Name)
}
}
sort.Strings(restOfRules)
sortingRules = append(sortingRules, restOfRules...)
if !idFound {
sortingRules = append(sortingRules, "id")
}
params.SortingRules = sortingRules
}
// Pagination
params.Page = su.Page
return params, nil
}
// A Params object represents all the query parameters from the URL.
type Params struct {
// Fields
Fields map[string][]string
Attrs map[string][]Attr
Rels map[string][]Rel
RelData map[string][]string
// Filter
FilterLabel string
Filter *Filter
// Sorting
SortingRules []string
// Pagination
Page map[string]any
// Include
Include [][]Rel
}