/
models_serde.go
336 lines (315 loc) · 9.93 KB
/
models_serde.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//go:build go1.18
// +build go1.18
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) AutoRest Code Generator. DO NOT EDIT.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
package azmetrics
import (
"encoding/json"
"fmt"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"reflect"
)
// MarshalJSON implements the json.Marshaller interface for type LocalizableString.
func (l LocalizableString) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "localizedValue", l.LocalizedValue)
populate(objectMap, "value", l.Value)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type LocalizableString.
func (l *LocalizableString) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", l, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "localizedValue":
err = unpopulate(val, "LocalizedValue", &l.LocalizedValue)
delete(rawMsg, key)
case "value":
err = unpopulate(val, "Value", &l.Value)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", l, err)
}
}
return nil
}
// MarshalJSON implements the json.Marshaller interface for type MetadataValue.
func (m MetadataValue) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "name", m.Name)
populate(objectMap, "value", m.Value)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type MetadataValue.
func (m *MetadataValue) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "name":
err = unpopulate(val, "Name", &m.Name)
delete(rawMsg, key)
case "value":
err = unpopulate(val, "Value", &m.Value)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
}
return nil
}
// MarshalJSON implements the json.Marshaller interface for type Metric.
func (m Metric) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "displayDescription", m.DisplayDescription)
populate(objectMap, "errorCode", m.ErrorCode)
populate(objectMap, "errorMessage", m.ErrorMessage)
populate(objectMap, "id", m.ID)
populate(objectMap, "name", m.Name)
populate(objectMap, "timeseries", m.TimeSeries)
populate(objectMap, "type", m.Type)
populate(objectMap, "unit", m.Unit)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type Metric.
func (m *Metric) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "displayDescription":
err = unpopulate(val, "DisplayDescription", &m.DisplayDescription)
delete(rawMsg, key)
case "errorCode":
err = unpopulate(val, "ErrorCode", &m.ErrorCode)
delete(rawMsg, key)
case "errorMessage":
err = unpopulate(val, "ErrorMessage", &m.ErrorMessage)
delete(rawMsg, key)
case "id":
err = unpopulate(val, "ID", &m.ID)
delete(rawMsg, key)
case "name":
err = unpopulate(val, "Name", &m.Name)
delete(rawMsg, key)
case "timeseries":
err = unpopulate(val, "TimeSeries", &m.TimeSeries)
delete(rawMsg, key)
case "type":
err = unpopulate(val, "Type", &m.Type)
delete(rawMsg, key)
case "unit":
err = unpopulate(val, "Unit", &m.Unit)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
}
return nil
}
// MarshalJSON implements the json.Marshaller interface for type MetricResults.
func (m MetricResults) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "values", m.Values)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type MetricResults.
func (m *MetricResults) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "values":
err = unpopulate(val, "Values", &m.Values)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
}
return nil
}
// MarshalJSON implements the json.Marshaller interface for type MetricData.
func (m MetricData) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "endtime", m.EndTime)
populate(objectMap, "interval", m.Interval)
populate(objectMap, "namespace", m.Namespace)
populate(objectMap, "resourceid", m.ResourceID)
populate(objectMap, "resourceregion", m.ResourceRegion)
populate(objectMap, "starttime", m.StartTime)
populate(objectMap, "value", m.Values)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type MetricData.
func (m *MetricData) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "endtime":
err = unpopulate(val, "EndTime", &m.EndTime)
delete(rawMsg, key)
case "interval":
err = unpopulate(val, "Interval", &m.Interval)
delete(rawMsg, key)
case "namespace":
err = unpopulate(val, "Namespace", &m.Namespace)
delete(rawMsg, key)
case "resourceid":
err = unpopulate(val, "ResourceID", &m.ResourceID)
delete(rawMsg, key)
case "resourceregion":
err = unpopulate(val, "ResourceRegion", &m.ResourceRegion)
delete(rawMsg, key)
case "starttime":
err = unpopulate(val, "StartTime", &m.StartTime)
delete(rawMsg, key)
case "value":
err = unpopulate(val, "Values", &m.Values)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
}
return nil
}
// MarshalJSON implements the json.Marshaller interface for type MetricValue.
func (m MetricValue) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "average", m.Average)
populate(objectMap, "count", m.Count)
populate(objectMap, "maximum", m.Maximum)
populate(objectMap, "minimum", m.Minimum)
populateDateTimeRFC3339(objectMap, "timeStamp", m.TimeStamp)
populate(objectMap, "total", m.Total)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type MetricValue.
func (m *MetricValue) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "average":
err = unpopulate(val, "Average", &m.Average)
delete(rawMsg, key)
case "count":
err = unpopulate(val, "Count", &m.Count)
delete(rawMsg, key)
case "maximum":
err = unpopulate(val, "Maximum", &m.Maximum)
delete(rawMsg, key)
case "minimum":
err = unpopulate(val, "Minimum", &m.Minimum)
delete(rawMsg, key)
case "timeStamp":
err = unpopulateDateTimeRFC3339(val, "TimeStamp", &m.TimeStamp)
delete(rawMsg, key)
case "total":
err = unpopulate(val, "Total", &m.Total)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", m, err)
}
}
return nil
}
// MarshalJSON implements the json.Marshaller interface for type ResourceIDList.
func (r ResourceIDList) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "resourceids", r.ResourceIDs)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type ResourceIDList.
func (r *ResourceIDList) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", r, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "resourceids":
err = unpopulate(val, "ResourceIDs", &r.ResourceIDs)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", r, err)
}
}
return nil
}
// MarshalJSON implements the json.Marshaller interface for type TimeSeriesElement.
func (t TimeSeriesElement) MarshalJSON() ([]byte, error) {
objectMap := make(map[string]any)
populate(objectMap, "data", t.Data)
populate(objectMap, "metadatavalues", t.MetadataValues)
return json.Marshal(objectMap)
}
// UnmarshalJSON implements the json.Unmarshaller interface for type TimeSeriesElement.
func (t *TimeSeriesElement) UnmarshalJSON(data []byte) error {
var rawMsg map[string]json.RawMessage
if err := json.Unmarshal(data, &rawMsg); err != nil {
return fmt.Errorf("unmarshalling type %T: %v", t, err)
}
for key, val := range rawMsg {
var err error
switch key {
case "data":
err = unpopulate(val, "Data", &t.Data)
delete(rawMsg, key)
case "metadatavalues":
err = unpopulate(val, "MetadataValues", &t.MetadataValues)
delete(rawMsg, key)
}
if err != nil {
return fmt.Errorf("unmarshalling type %T: %v", t, err)
}
}
return nil
}
func populate(m map[string]any, k string, v any) {
if v == nil {
return
} else if azcore.IsNullValue(v) {
m[k] = nil
} else if !reflect.ValueOf(v).IsNil() {
m[k] = v
}
}
func unpopulate(data json.RawMessage, fn string, v any) error {
if data == nil {
return nil
}
if err := json.Unmarshal(data, v); err != nil {
return fmt.Errorf("struct field %s: %v", fn, err)
}
return nil
}