-
Notifications
You must be signed in to change notification settings - Fork 151
/
question.go
203 lines (180 loc) · 6.21 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
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
package participation
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/iotaledger/hive.go/serializer"
)
const (
QuestionTextMaxLength = 255
QuestionAdditionalInfoMaxLength = 500
QuestionMinAnswersCount = 2
QuestionMaxAnswersCount = 10
)
var (
answersArrayRules = &serializer.ArrayRules{
Min: QuestionMinAnswersCount,
Max: QuestionMaxAnswersCount,
ValidationMode: serializer.ArrayValidationModeNone,
}
ErrDuplicateAnswerValue = errors.New("duplicate answer value found")
)
// Question defines a single question inside a Ballot that can have multiple Answers.
type Question struct {
// Text is the text of the question.
Text string
// Answers are the possible answers to the question.
Answers serializer.Serializables
// AdditionalInfo is an additional description text about the question.
AdditionalInfo string
}
func (q *Question) Deserialize(data []byte, deSeriMode serializer.DeSerializationMode) (int, error) {
return serializer.NewDeserializer(data).
ReadString(&q.Text, serializer.SeriLengthPrefixTypeAsByte, func(err error) error {
return fmt.Errorf("unable to deserialize participation question text: %w", err)
}, QuestionTextMaxLength).
ReadSliceOfObjects(func(seri serializer.Serializables) { q.Answers = seri }, deSeriMode, serializer.SeriLengthPrefixTypeAsByte, serializer.TypeDenotationNone, func(_ uint32) (serializer.Serializable, error) {
// there is no real selector, so we always return a fresh Answer
return &Answer{}, nil
}, answersArrayRules, func(err error) error {
return fmt.Errorf("unable to deserialize participation question answers: %w", err)
}).
ReadString(&q.AdditionalInfo, serializer.SeriLengthPrefixTypeAsUint16, func(err error) error {
return fmt.Errorf("unable to deserialize participation question additional info: %w", err)
}, QuestionAdditionalInfoMaxLength).
AbortIf(func(err error) error {
if deSeriMode.HasMode(serializer.DeSeriModePerformValidation) {
seenValues := make(map[uint8]struct{})
for _, s := range q.Answers {
switch a := s.(type) {
case *Answer:
if _, found := seenValues[a.Value]; found {
return ErrDuplicateAnswerValue
}
seenValues[a.Value] = struct{}{}
default:
return errors.New("invalid answer type")
}
}
}
return nil
}).
Done()
}
func (q *Question) Serialize(deSeriMode serializer.DeSerializationMode) ([]byte, error) {
return serializer.NewSerializer().
AbortIf(func(err error) error {
if deSeriMode.HasMode(serializer.DeSeriModePerformValidation) {
if len(q.Text) > QuestionTextMaxLength {
return fmt.Errorf("%w: text too long. Max allowed %d", ErrSerializationStringLengthInvalid, QuestionTextMaxLength)
}
if len(q.AdditionalInfo) > QuestionAdditionalInfoMaxLength {
return fmt.Errorf("%w: additional info too long. Max allowed %d", ErrSerializationStringLengthInvalid, QuestionAdditionalInfoMaxLength)
}
if err := answersArrayRules.CheckBounds(uint(len(q.Answers))); err != nil {
return fmt.Errorf("unable to serialize question answers: %w", err)
}
seenValues := make(map[uint8]struct{})
for _, s := range q.Answers {
switch a := s.(type) {
case *Answer:
if _, found := seenValues[a.Value]; found {
return ErrDuplicateAnswerValue
}
seenValues[a.Value] = struct{}{}
default:
return errors.New("invalid answer type")
}
}
}
return nil
}).
WriteString(q.Text, serializer.SeriLengthPrefixTypeAsByte, func(err error) error {
return fmt.Errorf("unable to serialize participation question text: %w", err)
}).
WriteSliceOfObjects(q.Answers, deSeriMode, serializer.SeriLengthPrefixTypeAsByte, nil, func(err error) error {
return fmt.Errorf("unable to serialize participation question answers: %w", err)
}).
WriteString(q.AdditionalInfo, serializer.SeriLengthPrefixTypeAsUint16, func(err error) error {
return fmt.Errorf("unable to serialize participation question additional info: %w", err)
}).
Serialize()
}
func (q *Question) MarshalJSON() ([]byte, error) {
jQuestion := &jsonQuestion{
Text: q.Text,
AdditionalInfo: q.AdditionalInfo,
}
jQuestion.Answers = make([]*json.RawMessage, len(q.Answers))
for i, answer := range q.Answers {
jsonAnswer, err := answer.MarshalJSON()
if err != nil {
return nil, err
}
rawJSONAnswer := json.RawMessage(jsonAnswer)
jQuestion.Answers[i] = &rawJSONAnswer
}
return json.Marshal(jQuestion)
}
func (q *Question) UnmarshalJSON(bytes []byte) error {
jQuestion := &jsonQuestion{}
if err := json.Unmarshal(bytes, jQuestion); err != nil {
return err
}
seri, err := jQuestion.ToSerializable()
if err != nil {
return err
}
*q = *seri.(*Question)
return nil
}
// jsonQuestion defines the json representation of a Question.
type jsonQuestion struct {
// Text is the text of the question.
Text string `json:"text"`
// Answers are the possible answers to the question.
Answers []*json.RawMessage `json:"answers"`
// AdditionalInfo is an additional description text about the question.
AdditionalInfo string `json:"additionalInfo"`
}
func (j *jsonQuestion) ToSerializable() (serializer.Serializable, error) {
payload := &Question{
Text: j.Text,
AdditionalInfo: j.AdditionalInfo,
}
answers := make(serializer.Serializables, len(j.Answers))
for i, ele := range j.Answers {
answer := &Answer{}
rawJSON, err := ele.MarshalJSON()
if err != nil {
return nil, fmt.Errorf("pos %d: %w", i, err)
}
if err := json.Unmarshal(rawJSON, answer); err != nil {
return nil, fmt.Errorf("pos %d: %w", i, err)
}
answers[i] = answer
}
payload.Answers = answers
return payload, nil
}
// QuestionAnswers returns the possible answers for a Question
func (q *Question) QuestionAnswers() []*Answer {
answers := make([]*Answer, len(q.Answers))
for i := range q.Answers {
answers[i] = q.Answers[i].(*Answer)
}
return answers
}
// answerValueForByte checks if the given value is a valid answer and maps any other values to AnswerValueInvalid
func (q *Question) answerValueForByte(byteValue byte) uint8 {
if byteValue == 0 {
return 0
}
for i := range q.Answers {
a := q.Answers[i].(*Answer)
if a.Value == byteValue {
return a.Value
}
}
return AnswerValueInvalid
}