forked from vmware-archive/atc
/
plan_factory.go
57 lines (49 loc) · 953 Bytes
/
plan_factory.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
package atc
import (
"encoding/json"
"fmt"
"sync/atomic"
)
type PlanFactory struct {
currentNum *int64
}
func NewPlanFactory(startingNum int64) PlanFactory {
return PlanFactory{
currentNum: &startingNum,
}
}
type Step interface {
Public() *json.RawMessage
}
func (factory PlanFactory) NewPlan(step Step) Plan {
num := atomic.AddInt64(factory.currentNum, 1)
var plan Plan
switch t := step.(type) {
case AggregatePlan:
plan.Aggregate = &t
case DoPlan:
plan.Do = &t
case GetPlan:
plan.Get = &t
case PutPlan:
plan.Put = &t
case TaskPlan:
plan.Task = &t
case EnsurePlan:
plan.Ensure = &t
case OnSuccessPlan:
plan.OnSuccess = &t
case OnFailurePlan:
plan.OnFailure = &t
case TryPlan:
plan.Try = &t
case TimeoutPlan:
plan.Timeout = &t
case RetryPlan:
plan.Retry = &t
default:
panic(fmt.Sprintf("don't know how to construct plan from %T", step))
}
plan.ID = PlanID(fmt.Sprintf("%x", num))
return plan
}