-
Notifications
You must be signed in to change notification settings - Fork 19
/
results.go
154 lines (135 loc) · 5.02 KB
/
results.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
package flows
import (
"encoding/json"
"fmt"
"sort"
"strings"
"time"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/utils"
)
// Result describes a value captured during a run's execution. It might have been implicitly created by a router, or explicitly
// created by a [set_run_result](#action:set_run_result) action.It renders as its value in a template, and has the following
// properties which can be accessed:
//
// * `value` the value of the result
// * `category` the category of the result
// * `category_localized` the localized category of the result
// * `input` the input associated with the result
// * `node_uuid` the UUID of the node where the result was created
// * `created_on` the time when the result was created
//
// Examples:
//
// @results -> 2Factor: 34634624463525\nFavorite Color: red\nPhone Number: +12344563452\nwebhook: 200
// @results.favorite_color -> red
// @results.favorite_color.value -> red
// @results.favorite_color.category -> Red
//
// @context result
type Result struct {
Name string `json:"name"`
Value string `json:"value"`
Category string `json:"category,omitempty"`
CategoryLocalized string `json:"category_localized,omitempty"`
NodeUUID NodeUUID `json:"node_uuid"`
Input string `json:"input,omitempty"`
Extra json.RawMessage `json:"extra,omitempty"`
CreatedOn time.Time `json:"created_on"`
}
// NewResult creates a new result
func NewResult(name string, value string, category string, categoryLocalized string, nodeUUID NodeUUID, input string, extra json.RawMessage, createdOn time.Time) *Result {
return &Result{
Name: name,
Value: value,
Category: category,
CategoryLocalized: categoryLocalized,
NodeUUID: nodeUUID,
Input: input,
Extra: extra,
CreatedOn: createdOn,
}
}
// Context returns the properties available in expressions
func (r *Result) Context(env utils.Environment) map[string]types.XValue {
categoryLocalized := r.CategoryLocalized
if categoryLocalized == "" {
categoryLocalized = r.Category
}
return map[string]types.XValue{
"__default__": types.NewXArray(types.NewXText(r.Value)),
"name": types.NewXText(r.Name),
"values": types.NewXArray(types.NewXText(r.Value)),
"categories": types.NewXArray(types.NewXText(r.Category)),
"categories_localized": types.NewXArray(types.NewXText(categoryLocalized)),
"input": types.NewXText(r.Input),
"extra": types.JSONToXValue(r.Extra),
"node_uuid": types.NewXText(string(r.NodeUUID)),
"created_on": types.NewXDateTime(r.CreatedOn),
}
}
// SimpleContext returns a simpler representation of this result exposed at @results.x
func (r *Result) SimpleContext(env utils.Environment) map[string]types.XValue {
categoryLocalized := r.CategoryLocalized
if categoryLocalized == "" {
categoryLocalized = r.Category
}
return map[string]types.XValue{
"__default__": types.NewXText(r.Value),
"name": types.NewXText(r.Name),
"value": types.NewXText(r.Value),
"category": types.NewXText(r.Category),
"category_localized": types.NewXText(categoryLocalized),
"input": types.NewXText(r.Input),
"node_uuid": types.NewXText(string(r.NodeUUID)),
"created_on": types.NewXDateTime(r.CreatedOn),
}
}
// Results is our wrapper around a map of snakified result names to result objects
type Results map[string]*Result
// NewResults creates a new empty set of results
func NewResults() Results {
return make(Results, 0)
}
// Clone returns a clone of this results set
func (r Results) Clone() Results {
clone := make(Results, len(r))
for k, v := range r {
clone[k] = v
}
return clone
}
// Save saves a new result in our map. The key is saved in a snakified format
func (r Results) Save(result *Result) {
r[utils.Snakify(result.Name)] = result
}
// Get returns the result with the given key
func (r Results) Get(key string) *Result {
return r[key]
}
// Context returns the properties available in expressions
func (r Results) Context(env utils.Environment) map[string]types.XValue {
entries := make(map[string]types.XValue, len(r)+1)
entries["__default__"] = types.NewXText(r.format())
for k, v := range r {
entries[k] = Context(env, v)
}
return entries
}
// SimpleContext returns a simpler representation of these results exposed at @results
func (r Results) SimpleContext(env utils.Environment) map[string]types.XValue {
entries := make(map[string]types.XValue, len(r)+1)
entries["__default__"] = types.NewXText(r.format())
for k, v := range r {
entries[k] = ContextFunc(env, v.SimpleContext)
}
return entries
}
func (r Results) format() string {
lines := make([]string, 0, len(r))
for _, v := range r {
lines = append(lines, fmt.Sprintf("%s: %s", v.Name, v.Value))
}
sort.Strings(lines)
return strings.Join(lines, "\n")
}