-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
task.lookup.go
48 lines (38 loc) · 1.01 KB
/
task.lookup.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
package pipeline
import (
"context"
"github.com/pkg/errors"
"github.com/smartcontractkit/chainlink/core/logger"
)
// Look up a field on a map
//
// Return types:
//
// interface{}
type LookupTask struct {
BaseTask `mapstructure:",squash"`
Key string `json:"key"`
}
var _ Task = (*LookupTask)(nil)
func (t *LookupTask) Type() TaskType {
return TaskTypeLookup
}
func (t *LookupTask) Run(ctx context.Context, lggr logger.Logger, vars Vars, inputs []Result) (result Result, runInfo RunInfo) {
_, err := CheckInputs(inputs, 1, 1, 0)
if err != nil {
return Result{Error: errors.Wrap(err, "task inputs")}, runInfo
}
var key StringParam
err = errors.Wrap(ResolveParam(&key, From(t.Key)), "key")
if err != nil {
return Result{Error: err}, runInfo
}
var val interface{}
switch m := inputs[0].Value.(type) {
case map[string]interface{}:
val = m[(string)(key)]
default:
return Result{Error: errors.Errorf("unexpected input type: %T", inputs[0].Value)}, runInfo
}
return Result{Value: val}, runInfo
}