1package atc
2
3import "encoding/json"
4
5func (plan Plan) Public() *json.RawMessage {
6	var public struct {
7		ID PlanID `json:"id"`
8
9		Aggregate      *json.RawMessage `json:"aggregate,omitempty"`
10		InParallel     *json.RawMessage `json:"in_parallel,omitempty"`
11		Do             *json.RawMessage `json:"do,omitempty"`
12		Get            *json.RawMessage `json:"get,omitempty"`
13		Put            *json.RawMessage `json:"put,omitempty"`
14		Task           *json.RawMessage `json:"task,omitempty"`
15		OnAbort        *json.RawMessage `json:"on_abort,omitempty"`
16		OnError        *json.RawMessage `json:"on_error,omitempty"`
17		Ensure         *json.RawMessage `json:"ensure,omitempty"`
18		OnSuccess      *json.RawMessage `json:"on_success,omitempty"`
19		OnFailure      *json.RawMessage `json:"on_failure,omitempty"`
20		Try            *json.RawMessage `json:"try,omitempty"`
21		DependentGet   *json.RawMessage `json:"dependent_get,omitempty"`
22		Timeout        *json.RawMessage `json:"timeout,omitempty"`
23		Retry          *json.RawMessage `json:"retry,omitempty"`
24		ArtifactInput  *json.RawMessage `json:"artifact_input,omitempty"`
25		ArtifactOutput *json.RawMessage `json:"artifact_output,omitempty"`
26	}
27
28	public.ID = plan.ID
29
30	if plan.Aggregate != nil {
31		public.Aggregate = plan.Aggregate.Public()
32	}
33
34	if plan.InParallel != nil {
35		public.InParallel = plan.InParallel.Public()
36	}
37
38	if plan.Do != nil {
39		public.Do = plan.Do.Public()
40	}
41
42	if plan.Get != nil {
43		public.Get = plan.Get.Public()
44	}
45
46	if plan.Put != nil {
47		public.Put = plan.Put.Public()
48	}
49
50	if plan.Task != nil {
51		public.Task = plan.Task.Public()
52	}
53
54	if plan.OnAbort != nil {
55		public.OnAbort = plan.OnAbort.Public()
56	}
57
58	if plan.OnError != nil {
59		public.OnError = plan.OnError.Public()
60	}
61
62	if plan.Ensure != nil {
63		public.Ensure = plan.Ensure.Public()
64	}
65
66	if plan.OnSuccess != nil {
67		public.OnSuccess = plan.OnSuccess.Public()
68	}
69
70	if plan.OnFailure != nil {
71		public.OnFailure = plan.OnFailure.Public()
72	}
73
74	if plan.Try != nil {
75		public.Try = plan.Try.Public()
76	}
77
78	if plan.Timeout != nil {
79		public.Timeout = plan.Timeout.Public()
80	}
81
82	if plan.Retry != nil {
83		public.Retry = plan.Retry.Public()
84	}
85
86	if plan.ArtifactInput != nil {
87		public.ArtifactInput = plan.ArtifactInput.Public()
88	}
89
90	if plan.ArtifactOutput != nil {
91		public.ArtifactOutput = plan.ArtifactOutput.Public()
92	}
93
94	if plan.DependentGet != nil {
95		public.DependentGet = plan.DependentGet.Public()
96	}
97
98	return enc(public)
99}
100
101func (plan AggregatePlan) Public() *json.RawMessage {
102	public := make([]*json.RawMessage, len(plan))
103
104	for i := 0; i < len(plan); i++ {
105		public[i] = plan[i].Public()
106	}
107
108	return enc(public)
109}
110
111func (plan InParallelPlan) Public() *json.RawMessage {
112	steps := make([]*json.RawMessage, len(plan.Steps))
113
114	for i := 0; i < len(plan.Steps); i++ {
115		steps[i] = plan.Steps[i].Public()
116	}
117
118	return enc(struct {
119		Steps    []*json.RawMessage `json:"steps"`
120		Limit    int                `json:"limit,omitempty"`
121		FailFast bool               `json:"fail_fast,omitempty"`
122	}{
123		Steps:    steps,
124		Limit:    plan.Limit,
125		FailFast: plan.FailFast,
126	})
127}
128
129func (plan DoPlan) Public() *json.RawMessage {
130	public := make([]*json.RawMessage, len(plan))
131
132	for i := 0; i < len(plan); i++ {
133		public[i] = plan[i].Public()
134	}
135
136	return enc(public)
137}
138
139func (plan EnsurePlan) Public() *json.RawMessage {
140	return enc(struct {
141		Step *json.RawMessage `json:"step"`
142		Next *json.RawMessage `json:"ensure"`
143	}{
144		Step: plan.Step.Public(),
145		Next: plan.Next.Public(),
146	})
147}
148
149func (plan GetPlan) Public() *json.RawMessage {
150	return enc(struct {
151		Type     string   `json:"type"`
152		Name     string   `json:"name,omitempty"`
153		Resource string   `json:"resource"`
154		Version  *Version `json:"version,omitempty"`
155	}{
156		Type:     plan.Type,
157		Name:     plan.Name,
158		Resource: plan.Resource,
159		Version:  plan.Version,
160	})
161}
162
163func (plan DependentGetPlan) Public() *json.RawMessage {
164	return enc(struct {
165		Type     string `json:"type"`
166		Name     string `json:"name,omitempty"`
167		Resource string `json:"resource"`
168	}{
169		Type:     plan.Type,
170		Name:     plan.Name,
171		Resource: plan.Resource,
172	})
173}
174
175func (plan OnAbortPlan) Public() *json.RawMessage {
176	return enc(struct {
177		Step *json.RawMessage `json:"step"`
178		Next *json.RawMessage `json:"on_abort"`
179	}{
180		Step: plan.Step.Public(),
181		Next: plan.Next.Public(),
182	})
183}
184
185func (plan OnErrorPlan) Public() *json.RawMessage {
186	return enc(struct {
187		Step *json.RawMessage `json:"step"`
188		Next *json.RawMessage `json:"on_error"`
189	}{
190		Step: plan.Step.Public(),
191		Next: plan.Next.Public(),
192	})
193}
194
195func (plan OnFailurePlan) Public() *json.RawMessage {
196	return enc(struct {
197		Step *json.RawMessage `json:"step"`
198		Next *json.RawMessage `json:"on_failure"`
199	}{
200		Step: plan.Step.Public(),
201		Next: plan.Next.Public(),
202	})
203}
204
205func (plan OnSuccessPlan) Public() *json.RawMessage {
206	return enc(struct {
207		Step *json.RawMessage `json:"step"`
208		Next *json.RawMessage `json:"on_success"`
209	}{
210		Step: plan.Step.Public(),
211		Next: plan.Next.Public(),
212	})
213}
214
215func (plan PutPlan) Public() *json.RawMessage {
216	return enc(struct {
217		Type     string `json:"type"`
218		Name     string `json:"name,omitempty"`
219		Resource string `json:"resource"`
220	}{
221		Type:     plan.Type,
222		Name:     plan.Name,
223		Resource: plan.Resource,
224	})
225}
226
227func (plan TaskPlan) Public() *json.RawMessage {
228	return enc(struct {
229		Name       string `json:"name"`
230		Privileged bool   `json:"privileged"`
231	}{
232		Name:       plan.Name,
233		Privileged: plan.Privileged,
234	})
235}
236
237func (plan TimeoutPlan) Public() *json.RawMessage {
238	return enc(struct {
239		Step     *json.RawMessage `json:"step"`
240		Duration string           `json:"duration"`
241	}{
242		Step:     plan.Step.Public(),
243		Duration: plan.Duration,
244	})
245}
246
247func (plan TryPlan) Public() *json.RawMessage {
248	return enc(struct {
249		Step *json.RawMessage `json:"step"`
250	}{
251		Step: plan.Step.Public(),
252	})
253}
254
255func (plan RetryPlan) Public() *json.RawMessage {
256	public := make([]*json.RawMessage, len(plan))
257
258	for i := 0; i < len(plan); i++ {
259		public[i] = plan[i].Public()
260	}
261
262	return enc(public)
263}
264
265func (plan ArtifactInputPlan) Public() *json.RawMessage {
266	return enc(plan)
267}
268
269func (plan ArtifactOutputPlan) Public() *json.RawMessage {
270	return enc(plan)
271}
272
273func enc(public interface{}) *json.RawMessage {
274	enc, _ := json.Marshal(public)
275	return (*json.RawMessage)(&enc)
276}
277