-
Notifications
You must be signed in to change notification settings - Fork 78
/
unmarshal.go
164 lines (145 loc) · 3.56 KB
/
unmarshal.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package v1
import (
"encoding/json"
"errors"
)
// UnmarshalJSON implements the json.Unmarshaller interface for ProbeHandler.
func (p *ProbeHandler) UnmarshalJSON(data []byte) error {
var probeType TypeWrapper
err := json.Unmarshal(data, &probeType)
if err != nil {
return err
}
p.Type = probeType.Type
switch p.Type {
case TypeHTTP:
handler := &HTTPGetAction{}
err = json.Unmarshal(data, handler)
p.HTTPGetAction = handler
case TypeExec:
handler := &ExecAction{}
err = json.Unmarshal(data, handler)
p.ExecAction = handler
case TypeTCP:
handler := &TCPSocketAction{}
err = json.Unmarshal(data, handler)
p.TCPSocketAction = handler
default:
return errors.New("unrecognized probe handler type")
}
return err
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for ProbeHandler.
func (p *ProbeHandler) UnmarshalYAML(unmarshal func(interface{}) error) error {
var probeType TypeWrapper
err := unmarshal(&probeType)
if err != nil {
return err
}
p.Type = probeType.Type
switch p.Type {
case TypeHTTP:
handler := &HTTPGetAction{}
err = unmarshal(handler)
p.HTTPGetAction = handler
case TypeExec:
handler := &ExecAction{}
err = unmarshal(handler)
p.ExecAction = handler
case TypeTCP:
handler := &TCPSocketAction{}
err = unmarshal(handler)
p.TCPSocketAction = handler
default:
return errors.New("unrecognized probe handler type")
}
return err
}
// UnmarshalJSON implements the json.Unmarshaller interface for LifecycleHandler.
func (l *LifecycleHandler) UnmarshalJSON(data []byte) error {
var handlerType TypeWrapper
err := json.Unmarshal(data, &handlerType)
if err != nil {
return err
}
l.Type = handlerType.Type
switch l.Type {
case TypeHTTP:
handler := &HTTPGetAction{}
err = json.Unmarshal(data, handler)
l.HTTPGetAction = handler
case TypeExec:
handler := &ExecAction{}
err = json.Unmarshal(data, handler)
l.ExecAction = handler
default:
return errors.New("unrecognized lifecycle handler type")
}
return err
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for LifecycleHandler.
func (l *LifecycleHandler) UnmarshalYAML(unmarshal func(interface{}) error) error {
var handlerType TypeWrapper
err := unmarshal(&handlerType)
if err != nil {
return err
}
l.Type = handlerType.Type
switch l.Type {
case TypeHTTP:
handler := &HTTPGetAction{}
err = unmarshal(handler)
l.HTTPGetAction = handler
case TypeExec:
handler := &ExecAction{}
err = unmarshal(handler)
l.ExecAction = handler
default:
return errors.New("unrecognized lifecycle handler type")
}
return err
}
// UnmarshalJSON implements the json.Unmarshaller interface for Workload.
func (w *Workload) UnmarshalJSON(data []byte) error {
var workloadData Header
err := json.Unmarshal(data, &workloadData)
if err != nil {
return err
}
w.Header.Type = workloadData.Type
switch w.Header.Type {
case TypeJob:
var v Job
err = json.Unmarshal(data, &v)
w.Job = &v
case TypeService:
var v Service
err = json.Unmarshal(data, &v)
w.Service = &v
default:
err = errors.New("unknown workload type unmarshall")
}
return err
}
// UnmarshalYAML implements the yaml.Unmarshaler interface for Workload.
func (w *Workload) UnmarshalYAML(unmarshal func(interface{}) error) error {
var workloadData Header
err := unmarshal(&workloadData)
if err != nil {
return err
}
w.Header.Type = workloadData.Type
switch w.Header.Type {
case TypeJob:
var v Job
err = unmarshal(&v)
w.Job = &v
case TypeService:
var v Service
err = unmarshal(&v)
w.Service = &v
default:
err = errors.New("unknown workload type")
}
return err
}