Skip to content

Commit 022933b

Browse files
committed
making plan.Run side-effect free
1 parent f288752 commit 022933b

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

plan.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ import (
1010

1111
type plan struct {
1212
order []*builder
13-
initData sets.String // the initial data required for this plan
14-
dataMap map[string]interface{} // map of all data
13+
initData sets.String // the initial data required for this plan
1514
}
1615

1716
func (p *plan) Run(ctx context.Context, initData ...interface{}) (Result, error) {
18-
if p.dataMap == nil {
19-
p.dataMap = make(map[string]interface{})
20-
}
17+
dataMap := make(map[string]interface{})
2118
initialData := sets.NewString()
2219
for _, inter := range initData {
2320
if inter == nil {
@@ -32,23 +29,23 @@ func (p *plan) Run(ctx context.Context, initData ...interface{}) (Result, error)
3229
return nil, ErrMultipleInitialData
3330
}
3431
initialData.Insert(name)
35-
p.dataMap[name] = inter
32+
dataMap[name] = inter
3633
}
3734
if p.initData.Difference(initialData).Len() > 0 {
3835
return nil, ErrInitialDataMissing
3936
}
40-
return p.dataMap, p.run(ctx)
37+
return dataMap, p.run(ctx, dataMap)
4138
}
4239

43-
func (p *plan) run(ctx context.Context) error {
40+
func (p *plan) run(ctx context.Context, dataMap map[string]interface{}) error {
4441
for i := range p.order {
4542
b := p.order[i]
4643
v := reflect.ValueOf(b.Builder)
4744
input := make([]reflect.Value, 1)
48-
ctx = AddResultToCtx(ctx, p.dataMap) // allow builders to access already built data
45+
ctx = AddResultToCtx(ctx, dataMap) // allow builders to access already built data
4946
input[0] = reflect.ValueOf(ctx)
5047
for _, in := range b.In {
51-
data, ok := p.dataMap[in]
48+
data, ok := dataMap[in]
5249
if !ok {
5350
return errors.New("TODO: CRITICAL")
5451
}
@@ -62,7 +59,7 @@ func (p *plan) run(ctx context.Context) error {
6259
return outputs[1].Interface().(error)
6360
}
6461
name := getStructName(outputs[0].Type())
65-
p.dataMap[name] = outputs[0].Interface()
62+
dataMap[name] = outputs[0].Interface()
6663
}
6764
return nil
6865
}

0 commit comments

Comments
 (0)