forked from googlemaps/google-maps-services-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoding.go
350 lines (282 loc) · 10.5 KB
/
encoding.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2015 Google Inc. 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.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License 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.
// More information about Google Distance Matrix API is available on
// https://developers.google.com/maps/documentation/distancematrix/
package maps
import (
"encoding/json"
"net/url"
"googlemaps.github.io/maps/internal"
)
// safeLeg is a raw version of Leg that does not have custom encoding or
// decoding methods applied.
type safeLeg Leg
// encodedLeg is the actual encoded version of Leg as per the Maps APIs.
type encodedLeg struct {
safeLeg
EncDuration *internal.Duration `json:"duration"`
EncDurationInTraffic *internal.Duration `json:"duration_in_traffic"`
EncArrivalTime *internal.DateTime `json:"arrival_time"`
EncDepartureTime *internal.DateTime `json:"departure_time"`
}
// UnmarshalJSON implements json.Unmarshaler for Leg. This decodes the API
// representation into types useful for Go developers.
func (leg *Leg) UnmarshalJSON(data []byte) error {
x := encodedLeg{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*leg = Leg(x.safeLeg)
leg.Duration = x.EncDuration.Duration()
leg.DurationInTraffic = x.EncDurationInTraffic.Duration()
leg.ArrivalTime = x.EncArrivalTime.Time()
leg.DepartureTime = x.EncDepartureTime.Time()
return nil
}
// MarshalJSON implements json.Marshaler for Leg. This encodes Go types back to
// the API representation.
func (leg *Leg) MarshalJSON() ([]byte, error) {
x := encodedLeg{}
x.safeLeg = safeLeg(*leg)
x.EncDuration = internal.NewDuration(leg.Duration)
x.EncDurationInTraffic = internal.NewDuration(leg.DurationInTraffic)
x.EncArrivalTime = internal.NewDateTime(leg.ArrivalTime)
x.EncDepartureTime = internal.NewDateTime(leg.DepartureTime)
return json.Marshal(x)
}
// safeStep is a raw version of Step that does not have custom encoding or
// decoding methods applied.
type safeStep Step
// encodedStep is the actual encoded version of Step as per the Maps APIs.
type encodedStep struct {
safeStep
EncDuration *internal.Duration `json:"duration"`
}
// UnmarshalJSON implements json.Unmarshaler for Step. This decodes the API
// representation into types useful for Go developers.
func (step *Step) UnmarshalJSON(data []byte) error {
x := encodedStep{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*step = Step(x.safeStep)
step.Duration = x.EncDuration.Duration()
return nil
}
// MarshalJSON implements json.Marshaler for Step. This encodes Go types back to
// the API representation.
func (step *Step) MarshalJSON() ([]byte, error) {
x := encodedStep{}
x.safeStep = safeStep(*step)
x.EncDuration = internal.NewDuration(step.Duration)
return json.Marshal(x)
}
// safeTransitDetails is a raw version of TransitDetails that does not have
// custom encoding or decoding methods applied.
type safeTransitDetails TransitDetails
// encodedTransitDetails is the actual encoded version of TransitDetails as per
// the Maps APIs
type encodedTransitDetails struct {
safeTransitDetails
EncArrivalTime *internal.DateTime `json:"arrival_time"`
EncDepartureTime *internal.DateTime `json:"departure_time"`
}
// UnmarshalJSON implements json.Unmarshaler for TransitDetails. This decodes
// the API representation into types useful for Go developers.
func (transitDetails *TransitDetails) UnmarshalJSON(data []byte) error {
x := encodedTransitDetails{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*transitDetails = TransitDetails(x.safeTransitDetails)
transitDetails.ArrivalTime = x.EncArrivalTime.Time()
transitDetails.DepartureTime = x.EncDepartureTime.Time()
return nil
}
// MarshalJSON implements json.Marshaler for TransitDetails. This encodes Go
// types back to the API representation.
func (transitDetails *TransitDetails) MarshalJSON() ([]byte, error) {
x := encodedTransitDetails{}
x.safeTransitDetails = safeTransitDetails(*transitDetails)
x.EncArrivalTime = internal.NewDateTime(transitDetails.ArrivalTime)
x.EncDepartureTime = internal.NewDateTime(transitDetails.DepartureTime)
return json.Marshal(x)
}
// safeTransitLine is the raw version of TransitLine that does not have custom
// encoding or decoding methods applied.
type safeTransitLine TransitLine
// encodedTransitLine is the actual encoded version of TransitLine as per the
// Maps APIs
type encodedTransitLine struct {
safeTransitLine
EncURL string `json:"url"`
EncIcon string `json:"icon"`
}
// UnmarshalJSON imlpements json.Unmarshaler for TransitLine. This decodes the
// API representation into types useful for Go developers.
func (transitLine *TransitLine) UnmarshalJSON(data []byte) error {
x := encodedTransitLine{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*transitLine = TransitLine(x.safeTransitLine)
transitLine.URL, err = url.Parse(x.EncURL)
if err != nil {
return err
}
transitLine.Icon, err = url.Parse(x.EncIcon)
if err != nil {
return err
}
return nil
}
// MarshalJSON implements json.Marshaler for TransitLine. This encodes Go
// types back to the API representation.
func (transitLine *TransitLine) MarshalJSON() ([]byte, error) {
x := encodedTransitLine{}
x.safeTransitLine = safeTransitLine(*transitLine)
x.EncURL = transitLine.URL.String()
x.EncIcon = transitLine.Icon.String()
return json.Marshal(x)
}
// safeTransitAgency is the raw version of TransitAgency that does not have
// custom encoding or decoding methods applied.
type safeTransitAgency TransitAgency
// encodedTransitAgency is the actual encoded version of TransitAgency as per the
// Maps APIs
type encodedTransitAgency struct {
safeTransitAgency
EncURL string `json:"url"`
}
// UnmarshalJSON imlpements json.Unmarshaler for TransitAgency. This decodes the
// API representation into types useful for Go developers.
func (transitAgency *TransitAgency) UnmarshalJSON(data []byte) error {
x := encodedTransitAgency{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*transitAgency = TransitAgency(x.safeTransitAgency)
transitAgency.URL, err = url.Parse(x.EncURL)
if err != nil {
return err
}
return nil
}
// MarshalJSON implements json.Marshaler for TransitAgency. This encodes Go
// types back to the API representation.
func (transitAgency *TransitAgency) MarshalJSON() ([]byte, error) {
x := encodedTransitAgency{}
x.safeTransitAgency = safeTransitAgency(*transitAgency)
x.EncURL = transitAgency.URL.String()
return json.Marshal(x)
}
// safeTransitLineVehicle is the raw version of TransitLineVehicle that does not
// have custom encoding or decoding methods applied.
type safeTransitLineVehicle TransitLineVehicle
// encodedTransitLineVehicle is the actual encoded version of TransitLineVehicle
// as per the Maps APIs
type encodedTransitLineVehicle struct {
safeTransitLineVehicle
EncIcon string `json:"icon"`
}
// UnmarshalJSON imlpements json.Unmarshaler for TransitLineVehicle. This
// decodes the API representation into types useful for Go developers.
func (transitLineVehicle *TransitLineVehicle) UnmarshalJSON(data []byte) error {
x := encodedTransitLineVehicle{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*transitLineVehicle = TransitLineVehicle(x.safeTransitLineVehicle)
transitLineVehicle.Icon, err = url.Parse(x.EncIcon)
if err != nil {
return err
}
return nil
}
// MarshalJSON implements json.Marshaler for TransitLineVehicle. This encodes
// Go types back to the API representation.
func (transitLineVehicle *TransitLineVehicle) MarshalJSON() ([]byte, error) {
x := encodedTransitLineVehicle{}
x.safeTransitLineVehicle = safeTransitLineVehicle(*transitLineVehicle)
x.EncIcon = transitLineVehicle.Icon.String()
return json.Marshal(x)
}
// safeDistanceMatrixElement is a raw version of DistanceMatrixElement that
// does not have custom encoding or decoding methods applied.
type safeDistanceMatrixElement DistanceMatrixElement
// encodedDistanceMatrixElement is the actual encoded version of
// DistanceMatrixElement as per the Maps APIs.
type encodedDistanceMatrixElement struct {
safeDistanceMatrixElement
EncDuration *internal.Duration `json:"duration"`
EncDurationInTraffic *internal.Duration `json:"duration_in_traffic"`
}
// UnmarshalJSON implements json.Unmarshaler for DistanceMatrixElement. This
// decodes the API representation into types useful for Go developers.
func (dme *DistanceMatrixElement) UnmarshalJSON(data []byte) error {
x := encodedDistanceMatrixElement{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*dme = DistanceMatrixElement(x.safeDistanceMatrixElement)
dme.Duration = x.EncDuration.Duration()
dme.DurationInTraffic = x.EncDurationInTraffic.Duration()
return nil
}
// MarshalJSON implements json.Marshaler for DistanceMatrixElement. This encodes
// Go types back to the API representation.
func (dme *DistanceMatrixElement) MarshalJSON() ([]byte, error) {
x := encodedDistanceMatrixElement{}
x.safeDistanceMatrixElement = safeDistanceMatrixElement(*dme)
x.EncDuration = internal.NewDuration(dme.Duration)
return json.Marshal(x)
}
// safeSnappedPoint is a raw version of SnappedPoint that does not have custom
// encoding or decoding methods applied.
type safeSnappedPoint SnappedPoint
// encodedSnappedPoint is the actual encoded version of SnappedPoint as per the
// Roads API.
type encodedSnappedPoint struct {
safeSnappedPoint
EncLocation internal.Location `json:"location"`
}
// UnmarshalJSON implements json.Unmarshaler for SnappedPoint. This decode the
// API representation into types useful for Go developers.
func (sp *SnappedPoint) UnmarshalJSON(data []byte) error {
x := encodedSnappedPoint{}
err := json.Unmarshal(data, &x)
if err != nil {
return err
}
*sp = SnappedPoint(x.safeSnappedPoint)
sp.Location.Lat = x.EncLocation.Latitude
sp.Location.Lng = x.EncLocation.Longitude
return nil
}
// MarshalJSON implements json.Marshaler for SnappedPoint. This encodes Go
// types back to the API representation.
func (sp *SnappedPoint) MarshalJSON() ([]byte, error) {
x := encodedSnappedPoint{}
x.safeSnappedPoint = safeSnappedPoint(*sp)
x.EncLocation.Latitude = sp.Location.Lat
x.EncLocation.Longitude = sp.Location.Lng
return json.Marshal(x)
}