File: json.go

package info (click to toggle)
golang-gopkg-goose.v1 0.0~git20170406.3228e4f-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,116 kB
  • sloc: python: 160; makefile: 3
file content (304 lines) | stat: -rw-r--r-- 7,947 bytes parent folder | download | duplicates (2)
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
// JSON marshaling and unmarshaling support for Openstack compute data structures.
// This package deals with the difference between the API and on-the-wire data types.
// Differences include encoding entity IDs as string vs int, depending on the Openstack
// variant used.
//
// The marshaling support is included primarily for use by the test doubles. It needs to be
// included here since Go requires methods to implemented in the same package as their receiver.

package nova

import (
	"encoding/json"
	"fmt"
	"strconv"
)

const (
	idTag            = "id"
	instanceIdTag    = "instance_id"
	groupIdTag       = "group_id"
	parentGroupIdTag = "parent_group_id"
)

var useNumericIds bool = false

// convertId returns the id as either a string or an int depending on what
// implementation of Openstack we are emulating.
func convertId(id string) interface{} {
	if !useNumericIds {
		return id
	}
	result, err := strconv.Atoi(id)
	if err != nil {
		panic(err)
	}
	return result
}

// getIdAsStringPtr extracts the field with the specified tag from the json data
// and returns it converted to a string pointer.
func getIdAsStringPtr(b []byte, tag string) (*string, error) {
	var out map[string]interface{}
	if err := json.Unmarshal(b, &out); err != nil {
		return nil, err
	}
	val, ok := out[tag]
	if !ok || val == nil {
		return nil, nil
	}
	floatVal, ok := val.(float64)
	var strVal string
	if ok {
		strVal = fmt.Sprint(int(floatVal))
	} else {
		strVal = fmt.Sprint(val)
	}
	return &strVal, nil
}

// getIdAsString extracts the field with the specified tag from the json data
// and returns it converted to a string.
func getIdAsString(b []byte, tag string) (string, error) {
	strPtr, err := getIdAsStringPtr(b, tag)
	if err != nil {
		return "", err
	}
	if strPtr == nil {
		return "", nil
	}
	return *strPtr, nil
}

// appendJSON marshals the given attribute value and appends it as an encoded value to the given json data.
// The newly encode (attr, value) is inserted just before the closing "}" in the json data.
func appendJSON(data []byte, attr string, value interface{}) ([]byte, error) {
	newData, err := json.Marshal(&value)
	if err != nil {
		return nil, err
	}
	strData := string(data)
	result := fmt.Sprintf(`%s, "%s":%s}`, strData[:len(strData)-1], attr, string(newData))
	return []byte(result), nil
}

type jsonEntity Entity

func (entity *Entity) UnmarshalJSON(b []byte) error {
	var je jsonEntity = jsonEntity(*entity)
	var err error
	if err = json.Unmarshal(b, &je); err != nil {
		// Related Bug lp:1683495, within the openstack compute api
		// response to List Servers Detailed, the image object might
		// be an empty string when you boot the server from a volume.
		// Before failing here, let's check to see if that's the case.
		if string(b) == "\"\"" {
			*entity = Entity(je)
			return nil
		} else {
			return err
		}
	}
	if je.Id, err = getIdAsString(b, idTag); err != nil {
		return err
	}
	*entity = Entity(je)
	return nil
}

func (entity Entity) MarshalJSON() ([]byte, error) {
	var je jsonEntity = jsonEntity(entity)
	data, err := json.Marshal(&je)
	if err != nil {
		return nil, err
	}
	id := convertId(entity.Id)
	return appendJSON(data, idTag, id)
}

type jsonFlavorDetail FlavorDetail

func (flavorDetail *FlavorDetail) UnmarshalJSON(b []byte) error {
	var jfd jsonFlavorDetail = jsonFlavorDetail(*flavorDetail)
	var err error
	if err = json.Unmarshal(b, &jfd); err != nil {
		return err
	}
	if jfd.Id, err = getIdAsString(b, idTag); err != nil {
		return err
	}
	*flavorDetail = FlavorDetail(jfd)
	return nil
}

func (flavorDetail FlavorDetail) MarshalJSON() ([]byte, error) {
	var jfd jsonFlavorDetail = jsonFlavorDetail(flavorDetail)
	data, err := json.Marshal(&jfd)
	if err != nil {
		return nil, err
	}
	id := convertId(flavorDetail.Id)
	return appendJSON(data, idTag, id)
}

type jsonServerDetail ServerDetail

func (serverDetail *ServerDetail) UnmarshalJSON(b []byte) error {
	var jsd jsonServerDetail = jsonServerDetail(*serverDetail)
	var err error
	if err = json.Unmarshal(b, &jsd); err != nil {
		return err
	}
	if jsd.Id, err = getIdAsString(b, idTag); err != nil {
		return err
	}
	*serverDetail = ServerDetail(jsd)
	return nil
}

func (serverDetail ServerDetail) MarshalJSON() ([]byte, error) {
	var jsd jsonServerDetail = jsonServerDetail(serverDetail)
	data, err := json.Marshal(&jsd)
	if err != nil {
		return nil, err
	}
	id := convertId(serverDetail.Id)
	return appendJSON(data, idTag, id)
}

type jsonFloatingIP FloatingIP

func (floatingIP *FloatingIP) UnmarshalJSON(b []byte) error {
	var jfip jsonFloatingIP = jsonFloatingIP(*floatingIP)
	var err error
	if err = json.Unmarshal(b, &jfip); err != nil {
		return err
	}
	if instIdPtr, err := getIdAsStringPtr(b, instanceIdTag); err != nil {
		return err
	} else {
		jfip.InstanceId = instIdPtr
	}
	if jfip.Id, err = getIdAsString(b, idTag); err != nil {
		return err
	}
	*floatingIP = FloatingIP(jfip)
	return nil
}

func (floatingIP FloatingIP) MarshalJSON() ([]byte, error) {
	var jfip jsonFloatingIP = jsonFloatingIP(floatingIP)
	data, err := json.Marshal(&jfip)
	if err != nil {
		return nil, err
	}
	id := convertId(floatingIP.Id)
	data, err = appendJSON(data, idTag, id)
	if err != nil {
		return nil, err
	}
	if floatingIP.InstanceId == nil {
		return data, nil
	}
	instId := convertId(*floatingIP.InstanceId)
	return appendJSON(data, instanceIdTag, instId)
}

type jsonSecurityGroup SecurityGroup

func (securityGroup *SecurityGroup) UnmarshalJSON(b []byte) error {
	var jsg jsonSecurityGroup = jsonSecurityGroup(*securityGroup)
	var err error
	if err = json.Unmarshal(b, &jsg); err != nil {
		return err
	}
	if jsg.Id, err = getIdAsString(b, idTag); err != nil {
		return err
	}
	*securityGroup = SecurityGroup(jsg)
	return nil
}

func (securityGroup SecurityGroup) MarshalJSON() ([]byte, error) {
	var jsg jsonSecurityGroup = jsonSecurityGroup(securityGroup)
	data, err := json.Marshal(&jsg)
	if err != nil {
		return nil, err
	}
	id := convertId(securityGroup.Id)
	return appendJSON(data, idTag, id)
}

type jsonSecurityGroupRule SecurityGroupRule

func (securityGroupRule *SecurityGroupRule) UnmarshalJSON(b []byte) error {
	var jsgr jsonSecurityGroupRule = jsonSecurityGroupRule(*securityGroupRule)
	var err error
	if err = json.Unmarshal(b, &jsgr); err != nil {
		return err
	}
	if jsgr.Id, err = getIdAsString(b, idTag); err != nil {
		return err
	}
	if jsgr.ParentGroupId, err = getIdAsString(b, parentGroupIdTag); err != nil {
		return err
	}
	*securityGroupRule = SecurityGroupRule(jsgr)
	return nil
}

func (securityGroupRule SecurityGroupRule) MarshalJSON() ([]byte, error) {
	var jsgr jsonSecurityGroupRule = jsonSecurityGroupRule(securityGroupRule)
	data, err := json.Marshal(&jsgr)
	if err != nil {
		return nil, err
	}
	id := convertId(securityGroupRule.Id)
	data, err = appendJSON(data, idTag, id)
	if err != nil {
		return nil, err
	}
	if securityGroupRule.ParentGroupId == "" {
		return data, nil
	}
	id = convertId(securityGroupRule.ParentGroupId)
	return appendJSON(data, parentGroupIdTag, id)
}

type jsonRuleInfo RuleInfo

func (ruleInfo *RuleInfo) UnmarshalJSON(b []byte) error {
	var jri jsonRuleInfo = jsonRuleInfo(*ruleInfo)
	var err error
	if err = json.Unmarshal(b, &jri); err != nil {
		return err
	}
	if jri.ParentGroupId, err = getIdAsString(b, parentGroupIdTag); err != nil {
		return err
	}
	if groupIdPtr, err := getIdAsStringPtr(b, groupIdTag); err != nil {
		return err
	} else {
		jri.GroupId = groupIdPtr
	}
	*ruleInfo = RuleInfo(jri)
	return nil
}

func (ruleInfo RuleInfo) MarshalJSON() ([]byte, error) {
	var jri jsonRuleInfo = jsonRuleInfo(ruleInfo)
	data, err := json.Marshal(&jri)
	if err != nil {
		return nil, err
	}
	id := convertId(ruleInfo.ParentGroupId)
	data, err = appendJSON(data, parentGroupIdTag, id)
	if err != nil {
		return nil, err
	}
	if ruleInfo.GroupId == nil {
		return data, nil
	}
	id = convertId(*ruleInfo.GroupId)
	return appendJSON(data, groupIdTag, id)
}