-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_serde.go
69 lines (55 loc) · 1.58 KB
/
operation_serde.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
package surveygo
import (
"encoding/json"
"errors"
"fmt"
)
// ------------ Deserializers ------------- //
// ParseFromJsonStr converts the given json string into a Survey instance.
func ParseFromJsonStr(jsonSurvey string) (*Survey, error) {
return ParseFromBytes([]byte(jsonSurvey))
}
// ParseFromBytes converts the given json byte slice into a Survey instance.
func ParseFromBytes(b []byte) (*Survey, error) {
var survey = &Survey{}
// unmarshal the json survey into the survey struct
if err := json.Unmarshal(b, survey); err != nil {
return nil, errors.Join(fmt.Errorf("error unmarshalling json survey"), err)
}
// validate the survey struct
if err := SurveyValidator.Struct(survey); err != nil {
errs := TranslateValidationErrors(err)
errs = append([]error{fmt.Errorf("error validating survey")}, errs...)
return nil, errors.Join(errs...)
}
// check survey consistency
if err := survey.checkConsistency(); err != nil {
return nil, err
}
// run position updater
survey.positionUpdater()
return survey, nil
}
// -------------- Serializers -------------- //
// ToMap returns a map representation of the survey.
func (s *Survey) ToMap() (map[string]any, error) {
r := make(map[string]any)
// s to map
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
// unmarshal to map[string]any
if err = json.Unmarshal(b, &r); err != nil {
return nil, err
}
return r, nil
}
// ToJson returns a JSON string representation of the survey.
func (s *Survey) ToJson() (string, error) {
b, err := json.Marshal(s)
if err != nil {
return "", err
}
return string(b), nil
}