-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
95 lines (81 loc) · 2.12 KB
/
parser.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
package surveydefinition
import (
"log/slog"
studyTypes "github.com/case-framework/case-backend/pkg/study/types"
"github.com/case-framework/case-backend/pkg/utils"
)
func SurveyDefToVersionPreview(
original *studyTypes.Survey,
options *ExtractOptions,
) SurveyVersionPreview {
sp := SurveyVersionPreview{
VersionID: original.VersionID,
Published: original.Published,
Unpublished: original.Unpublished,
Questions: []SurveyQuestion{},
}
sp.Questions = extractQuestions(&original.SurveyDefinition, options)
return sp
}
func extractQuestions(
root *studyTypes.SurveyItem,
options *ExtractOptions,
) []SurveyQuestion {
questions := []SurveyQuestion{}
if root == nil {
return questions
}
var includeItemNames []string
var excludeItemNames []string
useLabels := false
var prefLang string
if options != nil {
includeItemNames = options.IncludeItems
excludeItemNames = options.ExcludeItems
useLabels = options.UseLabelLang != ""
prefLang = options.UseLabelLang
}
for _, item := range root.Items {
if item.Type == studyTypes.SURVEY_ITEM_TYPE_PAGE_BREAK || item.Type == studyTypes.SURVEY_ITEM_TYPE_END {
continue
}
if item.ConfidentialMode != "" {
continue
}
if isItemGroup(&item) {
questions = append(questions, extractQuestions(&item, options)...)
continue
}
if len(includeItemNames) > 0 {
if !utils.ContainsString(includeItemNames, item.Key) {
continue
}
} else if utils.ContainsString(excludeItemNames, item.Key) {
continue
}
rg := getResponseGroupComponent(&item)
if rg == nil {
continue
}
responses, qType := extractResponses(rg, prefLang)
title := ""
if useLabels {
titleComp := getTitleComponent(&item)
if titleComp != nil {
var err error
title, err = getPreviewText(titleComp, prefLang)
if err != nil {
slog.Debug("could not find question title", slog.String("item key", item.Key), slog.String("error", err.Error()))
}
}
}
question := SurveyQuestion{
ID: item.Key,
Title: title,
QuestionType: qType,
Responses: responses,
}
questions = append(questions, question)
}
return questions
}