Skip to content

Commit

Permalink
Public set methods for data bag
Browse files Browse the repository at this point in the history
It's very hard or almost impossible to create some tests if we can't
define some data in the data bag, so I'm making it public.
  • Loading branch information
marekfilip committed Jan 19, 2024
1 parent 4869c63 commit 8e6052f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
10 changes: 6 additions & 4 deletions data_bag.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ type DataBag struct {
bag sync.Map
}

// GetJobData allows to fetch the job data from bag using step name.
// 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 job data from bag using step name.
// 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)
Expand All @@ -50,11 +50,13 @@ func (d *DataBag) setData(name string, data interface{}) {
d.bag.Store(d.getKeyName(name), data)
}

func (d *DataBag) setJobData(name string, data interface{}) {
// 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)
}

func (d *DataBag) setPreCheckData(name string, data interface{}) {
// 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)
}

Expand Down
4 changes: 2 additions & 2 deletions data_bag_inner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestDataBag_GetJobData(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
d := new(DataBag)

d.setJobData(tt.args.keyName, tt.args.storeVal)
d.SetJobData(tt.args.keyName, tt.args.storeVal)

got, exists := d.GetJobData(tt.getKeyName)

Expand All @@ -52,7 +52,7 @@ func TestDataBag_GetPreCheckData(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
d := new(DataBag)

d.setPreCheckData(tt.args.keyName, tt.args.storeVal)
d.SetPreCheckData(tt.args.keyName, tt.args.storeVal)

got, exists := d.GetPreCheckData(tt.getKeyName)

Expand Down
4 changes: 2 additions & 2 deletions worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (w *worker) executeStep(ctx context.Context, stepIdx int, input interface{}

if output, ok := isReturnOutput(preCheckResp); ok {
if db, ok := ctx.Value(DataBagContextKey).(*DataBag); ok {
db.setPreCheckData(w.steps.StepName(stepIdx), output)
db.SetPreCheckData(w.steps.StepName(stepIdx), output)
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ func (w *worker) executeStep(ctx context.Context, stepIdx int, input interface{}

if output, ok := isReturnOutput(jobResp); ok {
if db, ok := ctx.Value(DataBagContextKey).(*DataBag); ok {
db.setJobData(w.steps.StepName(stepIdx), output)
db.SetJobData(w.steps.StepName(stepIdx), output)
}
}

Expand Down

0 comments on commit 8e6052f

Please sign in to comment.