-
Notifications
You must be signed in to change notification settings - Fork 2
/
score_token.go
52 lines (46 loc) · 1.38 KB
/
score_token.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
package payloads
import (
"crypto/rsa"
"errors"
"strconv"
)
// ScoreToken represents data inside a score token
type ScoreToken struct {
// Nullable fields are of pointer types
Date string `json:"date" validate:"dmy-date"` // dd-mm-yyyy
UserID string `json:"idUser"`
ItemID *string `json:"idItem,omitempty"`
LocalItemID string `json:"idItemLocal"`
AttemptID string `json:"idAttempt"`
ItemURL string `json:"itemUrl"`
Score string `json:"score"`
UserAnswerID string `json:"idUserAnswer"`
Answer *string `json:"sAnswer"`
Converted ScoreTokenConverted
PublicKey *rsa.PublicKey
PrivateKey *rsa.PrivateKey
}
// ScoreTokenConverted contains converted field values of ScoreToken payload
type ScoreTokenConverted struct {
UserID int64
UserAnswerID int64
Score float64
}
// Bind validates a score token and converts some needed field values (called by ParseMap)
func (tt *ScoreToken) Bind() error {
var err error
tt.Converted.UserID, err = strconv.ParseInt(tt.UserID, 10, 64)
if err != nil {
return errors.New("wrong idUser")
}
tt.Converted.UserAnswerID, err = strconv.ParseInt(tt.UserAnswerID, 10, 64)
if err != nil {
return errors.New("wrong idUserAnswer")
}
tt.Converted.Score, err = strconv.ParseFloat(tt.Score, 64)
if err != nil {
return errors.New("wrong score")
}
return nil
}
var _ Binder = (*HintToken)(nil)