-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
run_input.go
87 lines (73 loc) · 1.95 KB
/
run_input.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
package models
import (
"fmt"
uuid "github.com/satori/go.uuid"
"github.com/tidwall/gjson"
)
// RunInput represents the input for performing a Task
type RunInput struct {
jobRun JobRun
taskRunID uuid.UUID
data JSON
status RunStatus
}
// NewRunInput creates a new RunInput with arbitrary data
func NewRunInput(jobRun JobRun, taskRunID uuid.UUID, data JSON, status RunStatus) *RunInput {
return &RunInput{
jobRun: jobRun,
taskRunID: taskRunID,
data: data,
status: status,
}
}
// NewRunInputWithResult creates a new RunInput with a value in the "result" field
func NewRunInputWithResult(jobRun JobRun, taskRunID uuid.UUID, value interface{}, status RunStatus) *RunInput {
data, err := JSON{}.Add(ResultKey, value)
if err != nil {
panic(fmt.Sprintf("invariant violated, add should not fail on empty JSON %v", err))
}
return &RunInput{
jobRun: jobRun,
taskRunID: taskRunID,
data: data,
status: status,
}
}
func (ri RunInput) ResultCollection() gjson.Result {
return ri.data.Get(ResultCollectionKey)
}
// Result returns the result as a gjson object
func (ri RunInput) Result() gjson.Result {
return ri.data.Get(ResultKey)
}
// ResultString returns the string result of the Data JSON field.
func (ri RunInput) ResultString() (string, error) {
val := ri.Result()
if val.Type != gjson.String {
return "", fmt.Errorf("non string result")
}
return val.String(), nil
}
// Status returns the RunInput's status
func (ri RunInput) Status() RunStatus {
return ri.status
}
// Data returns the RunInput's data
func (ri RunInput) Data() JSON {
return ri.data
}
// JobRunID returns this RunInput's JobRunID
func (ri RunInput) JobRunID() uuid.UUID {
return ri.jobRun.ID
}
func (ri RunInput) JobRun() JobRun {
return ri.jobRun
}
// TaskRunID returns this RunInput's TaskRunID
func (ri RunInput) TaskRunID() uuid.UUID {
return ri.taskRunID
}
func (ri RunInput) CloneWithData(data JSON) RunInput {
ri.data = data
return ri
}