-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_bag.go
69 lines (54 loc) · 1.87 KB
/
data_bag.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
package choreograph
import (
"context"
"strings"
"sync"
"github.com/pkg/errors"
)
var ErrNoDataBagInContext = errors.New("no data bag in context")
// GetDataBagFromCtx allows to retrieve data bag from provided context.
// It will return ErrNoDataBagInContext if there is no data bag in the context.
func GetDataBagFromCtx(ctx context.Context) (*DataBag, error) {
db, ok := ctx.Value(DataBagContextKey).(*DataBag)
if !ok {
return nil, ErrNoDataBagInContext
}
return db, nil
}
// DataBag is a structure which stores a data.
type DataBag struct {
bag sync.Map
}
// GetJobData allows to fetch the job data from data bag using step name.
// If ok is false, it means that no data was stored.
func (d *DataBag) GetJobData(name string) (interface{}, bool) {
return d.getData(name + jobDataPostfix)
}
// GetPreCheckData allows to fetch the pre-check data from data bag using step name.
// If ok is false, it means that no data was stored.
func (d *DataBag) GetPreCheckData(name string) (interface{}, bool) {
return d.getData(name + preCheckDataPostfix)
}
// getData allows to fetch the data from bag.
// If ok is false, it means that no data with specified key was stored.
func (d *DataBag) getData(name string) (interface{}, bool) {
val, ok := d.bag.Load(d.getKeyName(name))
return val, ok
}
func (d *DataBag) setData(name string, data interface{}) {
d.bag.Store(d.getKeyName(name), data)
}
// SetJobData allows to set the job data from data bag using step name.
func (d *DataBag) SetJobData(name string, data interface{}) {
d.setData(name+jobDataPostfix, data)
}
// SetPreCheckData allows to set the pre-check data to data bag using step name.
func (d *DataBag) SetPreCheckData(name string, data interface{}) {
d.setData(name+preCheckDataPostfix, data)
}
func (*DataBag) getKeyName(name string) string {
return strings.ToLower(name)
}
func (d *DataBag) clear() {
d.bag = sync.Map{}
}