generated from GalvinGao/gofiber-template
/
functions.go
139 lines (123 loc) · 3.45 KB
/
functions.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
package exprutils
import (
"errors"
)
type ExprFunction struct{}
// Public methods here will be available in the expression environment. Return type must be any and error.
func (e ExprFunction) FlattenDropTickets(dropRecruitTickets any) (any, error) {
if dropRecruitTickets == nil {
return nil, nil
}
ticketsArray, err := convertToSliceOfSliceString(dropRecruitTickets)
if err != nil {
return nil, err
}
elements := make([]any, 0)
for _, el := range ticketsArray {
for _, s := range el {
elements = append(elements, s)
}
}
return elements, nil
}
func (e ExprFunction) MapTotemArrayToValues(totemArray any) (any, error) {
if totemArray == nil {
return nil, nil
}
results := make([]any, 0)
for _, totemInterface := range totemArray.([]any) {
totem := totemInterface.(string)
lastChar := totem[len(totem)-1:]
var value int
if lastChar == "1" || lastChar == "2" || lastChar == "3" {
value = 8
} else if lastChar == "4" || lastChar == "5" {
value = 12
} else if lastChar == "6" {
value = 16
}
results = append(results, value)
}
return results, nil
}
func (e ExprFunction) MapTotemArrayToColors(totemArray any) (any, error) {
if totemArray == nil {
return nil, nil
}
results := make([]any, 0)
for _, totemInterface := range totemArray.([]any) {
totem := totemInterface.(string)
results = append(results, totem[len(totem)-4:len(totem)-3])
}
return results, nil
}
func (e ExprFunction) MapRecruitTicketsToOperatorClasses(dropRecruitTickets any) (any, error) {
if dropRecruitTickets == nil {
return nil, nil
}
mapping := GetExprCommonData().GetRecruitTicketOperatorClassMap()
classes := make([]any, 0)
slice, _ := dropRecruitTickets.([]any)
for _, ticketsForOneDropBox := range slice {
classSet := make(map[string]any)
innerSlice, _ := ticketsForOneDropBox.([]any)
for _, ticket := range innerSlice {
classNames := mapping[ticket.(string)]
for _, className := range classNames {
classSet[className] = nil
}
}
for class := range classSet {
classes = append(classes, class)
}
}
return classes, nil
}
func (e ExprFunction) MapIncidentTypeToName(incidentType any) (any, error) {
if incidentType == nil {
return nil, nil
}
mapping := GetExprCommonData().GetIncidentTypeNameMap()
return mapping[incidentType.(string)], nil
}
func (e ExprFunction) MapRestChoicesToNames(restChoices any) (any, error) {
if restChoices == nil {
return nil, nil
}
mapping := GetExprCommonData().GetRestChoicesNameMap()
results := make([]any, 0)
for _, choice := range restChoices.([]any) {
results = append(results, mapping[choice.(string)])
}
return results, nil
}
func (e ExprFunction) MapVariationToName(variation any) (any, error) {
if variation == nil {
return nil, nil
}
mapping := GetExprCommonData().GetVariationNameMap()
return mapping[variation.(string)], nil
}
func convertToSliceOfSliceString(input any) ([][]string, error) {
result := [][]string{}
if slice, ok := input.([]any); ok {
result = make([][]string, len(slice))
for i, v := range slice {
if innerSlice, ok := v.([]any); ok {
result[i] = make([]string, len(innerSlice))
for j, s := range innerSlice {
if str, ok := s.(string); ok {
result[i][j] = str
} else {
return nil, errors.New("element " + s.(string) + " is not a string")
}
}
} else {
return nil, errors.New("element " + v.(string) + " is not a slice")
}
}
} else {
return nil, errors.New("input is not a slice")
}
return result, nil
}