forked from RichardKnop/machinery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
40 lines (34 loc) · 986 Bytes
/
result.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
package tasks
import (
"fmt"
"reflect"
"strings"
)
// TaskResult represents an actual return value of a processed task
type TaskResult struct {
Type string `bson:"type"`
Value interface{} `bson:"value"`
}
// ReflectTaskResults ...
func ReflectTaskResults(taskResults []*TaskResult) ([]reflect.Value, error) {
resultValues := make([]reflect.Value, len(taskResults))
for i, taskResult := range taskResults {
resultValue, err := ReflectValue(taskResult.Type, taskResult.Value)
if err != nil {
return nil, err
}
resultValues[i] = resultValue
}
return resultValues, nil
}
// HumanReadableResults ...
func HumanReadableResults(results []reflect.Value) string {
if len(results) == 1 {
return fmt.Sprintf("%v", results[0].Interface())
}
readableResults := make([]string, len(results))
for i := 0; i < len(results); i++ {
readableResults[i] = fmt.Sprintf("%v", results[i].Interface())
}
return fmt.Sprintf("[%s]", strings.Join(readableResults, ", "))
}