forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
224 lines (201 loc) · 6.31 KB
/
json.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package api
import (
"encoding/json"
"errors"
"strings"
"github.com/aws/amazon-ecs-agent/agent/utils"
)
func (ts *TaskStatus) UnmarshalJSON(b []byte) error {
if strings.ToLower(string(b)) == "null" {
*ts = TaskStatusNone
return nil
}
if b[0] != '"' || b[len(b)-1] != '"' {
*ts = TaskStatusNone
return errors.New("TaskStatus must be a string or null")
}
strStatus := string(b[1 : len(b)-1])
// 'UNKNOWN' and 'DEAD' for Compatibility with v1.0.0 state files
if strStatus == "UNKNOWN" {
*ts = TaskStatusNone
return nil
}
if strStatus == "DEAD" {
*ts = TaskStopped
return nil
}
stat, ok := taskStatusMap[strStatus]
if !ok {
*ts = TaskStatusNone
return errors.New("Unrecognized TaskStatus")
}
*ts = stat
return nil
}
func (ts *TaskStatus) MarshalJSON() ([]byte, error) {
if ts == nil {
return nil, nil
}
return []byte(`"` + ts.String() + `"`), nil
}
func (cs *ContainerStatus) UnmarshalJSON(b []byte) error {
if strings.ToLower(string(b)) == "null" {
*cs = ContainerStatusNone
return nil
}
if b[0] != '"' || b[len(b)-1] != '"' {
*cs = ContainerStatusNone
return errors.New("ContainerStatus must be a string or null; Got " + string(b))
}
strStatus := string(b[1 : len(b)-1])
// 'UNKNOWN' and 'DEAD' for Compatibility with v1.0.0 state files
if strStatus == "UNKNOWN" {
*cs = ContainerStatusNone
return nil
}
if strStatus == "DEAD" {
*cs = ContainerStopped
return nil
}
stat, ok := containerStatusMap[strStatus]
if !ok {
*cs = ContainerStatusNone
return errors.New("Unrecognized ContainerStatus")
}
*cs = stat
return nil
}
func (cs *ContainerStatus) MarshalJSON() ([]byte, error) {
if cs == nil {
return nil, nil
}
return []byte(`"` + cs.String() + `"`), nil
}
// A type alias that doesn't have a custom unmarshaller so we can unmarshal into
// something without recursing
type ContainerOverridesCopy ContainerOverrides
// This custom unmarshaller is needed because the json sent to us as a string
// rather than a fully typed object. We support both formats in the hopes that
// one day everything will be fully typed
// Note: the `json:",string"` tag DOES NOT apply here; it DOES NOT work with
// struct types, only ints/floats/etc. We're basically doing that though
// We also intentionally fail if there are any keys we were unable to unmarshal
// into our struct
func (overrides *ContainerOverrides) UnmarshalJSON(b []byte) error {
regular := ContainerOverridesCopy{}
// Try to do it the strongly typed way first
err := json.Unmarshal(b, ®ular)
if err == nil {
err = utils.CompleteJsonUnmarshal(b, regular)
if err == nil {
*overrides = ContainerOverrides(regular)
return nil
}
err = utils.NewMultiError(errors.New("Error unmarshalling ContainerOverrides"), err)
}
// Now the stringly typed way
var str string
err2 := json.Unmarshal(b, &str)
if err2 != nil {
return utils.NewMultiError(errors.New("Could not unmarshal ContainerOverrides into either an object or string respectively"), err, err2)
}
// We have a string, let's try to unmarshal that into a typed object
err3 := json.Unmarshal([]byte(str), ®ular)
if err3 == nil {
err3 = utils.CompleteJsonUnmarshal([]byte(str), regular)
if err3 == nil {
*overrides = ContainerOverrides(regular)
return nil
} else {
err3 = utils.NewMultiError(errors.New("Error unmarshalling ContainerOverrides"), err3)
}
}
return utils.NewMultiError(errors.New("Could not unmarshal ContainerOverrides in any supported way"), err, err2, err3)
}
// UnmarshalJSON for TaskVolume determines the name and volume type, and
// unmarshals it into the appropriate HostVolume fulfilling interfaces
func (tv *TaskVolume) UnmarshalJSON(b []byte) error {
// Format: {name: volumeName, host: emptyVolumeOrHostVolume}
intermediate := make(map[string]json.RawMessage)
if err := json.Unmarshal(b, &intermediate); err != nil {
return err
}
name, ok := intermediate["name"]
if !ok {
return errors.New("invalid Volume; must include a name")
}
if err := json.Unmarshal(name, &tv.Name); err != nil {
return err
}
if rawhostdata, ok := intermediate["host"]; ok {
// Default to trying to unmarshal it as a FSHostVolume
var hostvolume FSHostVolume
err := json.Unmarshal(rawhostdata, &hostvolume)
if err != nil {
return err
}
if hostvolume.FSSourcePath == "" {
// If the FSSourcePath is empty, that must mean it was not an
// FSHostVolume (empty path is invalid for that type). The only other
// type is an empty volume, so unmarshal it as such.
emptyVolume := &EmptyHostVolume{}
json.Unmarshal(rawhostdata, emptyVolume)
tv.Volume = emptyVolume
} else {
tv.Volume = &hostvolume
}
return nil
}
return errors.New("unrecognized volume type; try updating me")
}
func (tv *TaskVolume) MarshalJSON() ([]byte, error) {
result := make(map[string]interface{})
result["name"] = tv.Name
switch v := tv.Volume.(type) {
case *FSHostVolume:
result["host"] = v
case *EmptyHostVolume:
result["host"] = v
default:
log.Crit("Unknown task volume type in marshal")
}
return json.Marshal(result)
}
// UnmarshalJSON for TransportProtocol determines whether to use TCP or UDP,
// setting TCP as the zero-value but treating other unrecognized values as
// errors
func (tp *TransportProtocol) UnmarshalJSON(b []byte) error {
if strings.ToLower(string(b)) == "null" {
*tp = TransportProtocolTCP
log.Warn("Unmarshalled nil TransportProtocol as TCP")
return nil
}
switch string(b) {
case `"tcp"`:
*tp = TransportProtocolTCP
case `"udp"`:
*tp = TransportProtocolUDP
default:
*tp = TransportProtocolTCP
return errors.New("TransportProtocol must be \"tcp\" or \"udp\"; Got " + string(b))
}
return nil
}
func (tp *TransportProtocol) MarshalJSON() ([]byte, error) {
if tp == nil {
return []byte("null"), nil
}
return []byte(`"` + tp.String() + `"`), nil
}