-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_group.go
375 lines (308 loc) · 10.4 KB
/
operation_group.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package surveygo
import (
"encoding/json"
"errors"
"fmt"
"github.com/rendis/surveygo/v2/question"
"github.com/rendis/surveygo/v2/question/types"
"github.com/rendis/surveygo/v2/question/types/choice"
)
// UpdateGroupsOrder updates the groups order in the survey.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) UpdateGroupsOrder(order []string) error {
var errs []error
// check if all groups exist and are unique
groups := map[string]bool{}
for _, id := range order {
// check if group exists
if _, ok := s.Groups[id]; !ok {
errs = append(errs, fmt.Errorf("group '%s' not found", id))
continue
}
// check if group is duplicated
if _, ok := groups[id]; ok {
errs = append(errs, fmt.Errorf("group '%s' is duplicated", id))
continue
}
groups[id] = true
}
// check if there are errors
if len(errs) > 0 {
return errors.Join(errs...)
}
// update groups order
s.GroupsOrder = order
// check consistency
if err := s.checkConsistency(); err != nil {
return err
}
// update positions
s.positionUpdater()
return nil
}
// EnableGroup enables a group in the survey.
func (s *Survey) EnableGroup(groupNameId string) error {
// check if group exists
if _, ok := s.Groups[groupNameId]; !ok {
return fmt.Errorf("group '%s' not found", groupNameId)
}
// enable group
s.Groups[groupNameId].Disabled = false
// check consistency
return s.checkConsistency()
}
// DisableGroup disables a group in the survey.
func (s *Survey) DisableGroup(groupNameId string) error {
// check if group exists
if _, ok := s.Groups[groupNameId]; !ok {
return fmt.Errorf("group '%s' not found", groupNameId)
}
// disable group
s.Groups[groupNameId].Disabled = true
// check consistency
return s.checkConsistency()
}
// AddGroup adds a group to the survey.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddGroup(g *question.Group) error {
if err := s.addGroup(g); err != nil {
return err
}
// update positions
s.positionUpdater()
return nil
}
// AddGroupMap adds a group to the survey given its representation as a map[string]any
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddGroupMap(g map[string]any) error {
b, _ := json.Marshal(g)
return s.AddGroupBytes(b)
}
// AddGroupJson adds a group to the survey given its representation as a JSON string
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddGroupJson(g string) error {
return s.AddGroupBytes([]byte(g))
}
// AddGroupBytes adds a group to the survey given its representation as a byte array
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddGroupBytes(g []byte) error {
// unmarshal group
var pg *question.Group
err := json.Unmarshal(g, pg)
if err != nil {
return err
}
// add group to survey
return s.AddGroup(pg)
}
// UpdateGroup updates an existing group in the survey with the data provided.
func (s *Survey) UpdateGroup(pg *question.Group) error {
if err := s.updateGroup(pg); err != nil {
return err
}
// update positions
s.positionUpdater()
return nil
}
// UpdateGroupMap updates an existing group in the survey with the data provided as a map.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) UpdateGroupMap(ug map[string]any) error {
b, _ := json.Marshal(ug)
return s.UpdateGroupBytes(b)
}
// UpdateGroupJson updates an existing group in the survey with the data provided as a JSON string.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) UpdateGroupJson(ug string) error {
return s.UpdateGroupBytes([]byte(ug))
}
// UpdateGroupBytes updates a group in the survey given its representation as a byte array
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) UpdateGroupBytes(ug []byte) error {
// unmarshal group
var pg *question.Group
err := json.Unmarshal(ug, pg)
if err != nil {
return err
}
return s.UpdateGroup(pg)
}
// AddOrUpdateGroupMap adds or updates a group in the survey given its representation as a map.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddOrUpdateGroupMap(g map[string]any) error {
b, _ := json.Marshal(g)
return s.AddOrUpdateGroupBytes(b)
}
// AddOrUpdateGroupJson adds or updates a group in the survey given its representation as a JSON string.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddOrUpdateGroupJson(g string) error {
return s.AddOrUpdateGroupBytes([]byte(g))
}
// AddOrUpdateGroupBytes adds or updates a group in the survey given its representation as a byte array.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddOrUpdateGroupBytes(g []byte) error {
// unmarshal group
var pg = new(question.Group)
err := json.Unmarshal(g, pg)
if err != nil {
return err
}
// check if group already exists
if _, ok := s.Groups[pg.NameId]; ok {
return s.UpdateGroup(pg)
}
// add group to survey
return s.AddGroup(pg)
}
// RemoveGroup removes a group from the survey given its nameId.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) RemoveGroup(groupNameId string) error {
// check if group exists
if _, ok := s.Groups[groupNameId]; !ok {
return fmt.Errorf("group '%s' not found", groupNameId)
}
// remove group from options groups
for _, q := range s.Questions {
if types.IsChoiceType(q.QTyp) {
c, _ := choice.CastToChoice(q.Value)
if removed := c.RemoveGroupId(groupNameId); removed {
break
}
}
}
// remove group from survey groups order
for i, id := range s.GroupsOrder {
if id == groupNameId {
s.GroupsOrder = append(s.GroupsOrder[:i], s.GroupsOrder[i+1:]...)
break
}
}
// remove group from groups order in other groups
for _, g := range s.Groups {
for i, id := range g.GroupsOrder {
if id == groupNameId {
g.GroupsOrder = append(g.GroupsOrder[:i], g.GroupsOrder[i+1:]...)
break
}
}
}
// remove group from survey
delete(s.Groups, groupNameId)
// check consistency
if err := s.checkConsistency(); err != nil {
return err
}
// update positions
s.positionUpdater()
return nil
}
// AddQuestionToGroup adds a question to a group in the survey.
// Args:
// * questionNameId: the nameId of the question to add.
// * groupNameId: the nameId of the group to add the question to.
// * position: the position of the question in the group. If position is -1, the question will be added at the end of the group.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) AddQuestionToGroup(questionNameId, groupNameId string, position int) error {
// check if question exists
if _, ok := s.Questions[questionNameId]; !ok {
return fmt.Errorf("question '%s' not found", questionNameId)
}
// check if group exists
if _, ok := s.Groups[groupNameId]; !ok {
return fmt.Errorf("group '%s' not found", groupNameId)
}
// add question to group
s.Groups[groupNameId].AddQuestionId(questionNameId, position)
// check consistency
if err := s.checkConsistency(); err != nil {
return err
}
// update positions
s.positionUpdater()
return nil
}
// RemoveQuestionFromGroup removes a question from a group in the survey.
// Args:
// * questionNameId: the nameId of the question to remove.
// * groupNameId: the nameId of the group to remove the question from.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) RemoveQuestionFromGroup(questionNameId, groupNameId string) error {
// check if question exists
if _, ok := s.Questions[questionNameId]; !ok {
return fmt.Errorf("question '%s' not found", questionNameId)
}
// check if group exists
if _, ok := s.Groups[groupNameId]; !ok {
return fmt.Errorf("group '%s' not found", groupNameId)
}
// remove question from group
s.Groups[groupNameId].RemoveQuestionId(questionNameId)
// check consistency
if err := s.checkConsistency(); err != nil {
return err
}
// update positions
s.positionUpdater()
return nil
}
// UpdateGroupQuestions updates the questions of a group in the survey.
// Args:
// * groupNameId: the nameId of the group to update.
// * questionsIds: the list of questions ids to update the group with.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) UpdateGroupQuestions(groupNameId string, questionsIds []string) error {
// check if group exists
if _, ok := s.Groups[groupNameId]; !ok {
return fmt.Errorf("group '%s' not found", groupNameId)
}
// check if questions exist
for _, questionNameId := range questionsIds {
if _, ok := s.Questions[questionNameId]; !ok {
return fmt.Errorf("question '%s' not found", questionNameId)
}
}
// update group questions
s.Groups[groupNameId].QuestionsIds = questionsIds
// check consistency
if err := s.checkConsistency(); err != nil {
return err
}
// update positions
s.positionUpdater()
return nil
}
// addGroup adds a group to the survey.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) addGroup(pg *question.Group) error {
// validate group
if err := SurveyValidator.Struct(pg); err != nil {
errs := TranslateValidationErrors(err)
errs = append([]error{fmt.Errorf("error validating group")}, errs...)
return errors.Join(errs...)
}
// check if group already exists
if _, ok := s.Groups[pg.NameId]; ok {
return fmt.Errorf("group nameId '%s' already exists", pg.NameId)
}
// add group to survey
s.Groups[pg.NameId] = pg
// check consistency
return s.checkConsistency()
}
// updateGroup updates an existing group in the survey with the data provided.
// It also validates the group and checks if the group is consistent with the survey.
func (s *Survey) updateGroup(pg *question.Group) error {
group, ok := s.Groups[pg.NameId]
if !ok {
return fmt.Errorf("group '%s' not found", pg.NameId)
}
// update group
group.Title = pg.Title
group.Description = pg.Description
group.Hidden = pg.Hidden
group.Disabled = pg.Disabled
group.Metadata = pg.Metadata
group.IsExternalSurvey = pg.IsExternalSurvey
group.QuestionsIds = pg.QuestionsIds
// check consistency
return s.checkConsistency()
}