/
question.go
165 lines (137 loc) · 4.92 KB
/
question.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
package question
import (
"encoding/json"
"errors"
"fmt"
"github.com/rendis/surveygo/v2/question/types"
"github.com/rendis/surveygo/v2/question/types/choice"
"github.com/rendis/surveygo/v2/question/types/external"
"github.com/rendis/surveygo/v2/question/types/text"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
)
// BaseQuestion is a struct that contains common fields for all types of questions in a survey.
type BaseQuestion struct {
// NameId is the identifier of the question.
// Validations:
// - required
// - valid name id
NameId string `json:"nameId,omitempty" bson:"nameId,omitempty" validate:"required,validNameId"`
// Visible is a flag that indicates if the question is visible.
Visible bool `json:"visible,omitempty" bson:"visible,omitempty"`
// QTyp is the type of question, such as single_select, multi_select, radio, checkbox, or text_area.
// Validations:
// - required
// - must be a valid question type
QTyp types.QuestionType `json:"type,omitempty" bson:"type,omitempty" validate:"required,questionType"`
// Label is a label for the question.
// Validations:
// - required
// - min length: 1
Label string `json:"label,omitempty" bson:"label,omitempty" validate:"required,min=1"`
// Required indicates whether the question is required. Defaults to false.
Required bool `json:"required,omitempty" bson:"required,omitempty"`
}
// Question is a struct that represents a question in a survey.
type Question struct {
// BaseQuestion contains common fields for all types of questions.
BaseQuestion `bson:",inline"`
// Value is the value of the question, which can be of different types depending on the type of question.
// Validations:
// - required
Value any `json:"value,omitempty" bson:"value,omitempty" validate:"required"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (q *Question) UnmarshalJSON(b []byte) error {
var bq BaseQuestion
if err := json.Unmarshal(b, &bq); err != nil {
return err
}
var realQuestion *Question
var err error
// unmarshal the question based on its type
switch bq.QTyp {
case types.QTypeSingleSelect, types.QTypeMultipleSelect, types.QTypeRadio, types.QTypeCheckbox:
realQuestion, err = unmarshalJSONQuestionByType[choice.Choice](b)
case types.QTypeTextArea, types.QTypeInputText:
realQuestion, err = unmarshalJSONQuestionByType[text.FreeText](b)
case types.QTypeEmail:
realQuestion, err = unmarshalJSONQuestionByType[text.Email](b)
case types.QTypeTelephone:
realQuestion, err = unmarshalJSONQuestionByType[text.Telephone](b)
case types.QTypeInformation:
realQuestion, err = unmarshalJSONQuestionByType[text.InformationText](b)
case types.QTypeExternalQuestion:
realQuestion, err = unmarshalJSONQuestionByType[external.ExternalQuestion](b)
default:
return fmt.Errorf("invalid question type: %s", bq.QTyp)
}
if err != nil {
return errors.Join(fmt.Errorf("error unmarshalling question '%s'", bq.NameId), err)
}
*q = *realQuestion
return nil
}
func (q *Question) UnmarshalBSONValue(typ bsontype.Type, b []byte) error {
var bq BaseQuestion
if err := bson.Unmarshal(b, &bq); err != nil {
return fmt.Errorf("BSON unmarshal error, %s", err)
}
var value any
var err error
// unmarshal the question based on its type
switch bq.QTyp {
case types.QTypeSingleSelect, types.QTypeMultipleSelect, types.QTypeRadio, types.QTypeCheckbox:
value, err = unmarshalBSONQuestionByType[choice.Choice](b)
case types.QTypeTextArea, types.QTypeInputText:
value, err = unmarshalBSONQuestionByType[text.FreeText](b)
case types.QTypeEmail:
value, err = unmarshalBSONQuestionByType[text.Email](b)
case types.QTypeTelephone:
value, err = unmarshalBSONQuestionByType[text.Telephone](b)
case types.QTypeInformation:
value, err = unmarshalBSONQuestionByType[text.InformationText](b)
case types.QTypeExternalQuestion:
value, err = unmarshalBSONQuestionByType[external.ExternalQuestion](b)
default:
return fmt.Errorf("invalid question type: %s", bq.QTyp)
}
if err != nil {
return errors.Join(fmt.Errorf("error unmarshalling question '%s'", bq.NameId), err)
}
*q = Question{
BaseQuestion: bq,
Value: value,
}
return nil
}
// unmarshalJSONQuestionByType returns a question of a specific type.
func unmarshalJSONQuestionByType[T any](b []byte) (*Question, error) {
// build a temporary struct with the base question and the value of the specific type
var tq = struct {
BaseQuestion
Value *T `json:"value"`
}{}
if err := json.Unmarshal(b, &tq); err != nil {
return nil, err
}
if tq.Value == nil {
return nil, fmt.Errorf("value is not defined")
}
return &Question{
BaseQuestion: tq.BaseQuestion,
Value: tq.Value,
}, nil
}
func unmarshalBSONQuestionByType[T any](b []byte) (*T, error) {
var tq = struct {
Value *T `bson:"value"`
}{}
if err := bson.Unmarshal(b, &tq); err != nil {
return nil, err
}
if tq.Value == nil {
return nil, fmt.Errorf("value is not defined")
}
return tq.Value, nil
}