/
time.vdl.go
356 lines (326 loc) · 9.87 KB
/
time.vdl.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
351
352
353
354
355
356
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file was auto-generated by the vanadium vdl tool.
// Package: time
// Package time defines standard representations of absolute and relative times.
//
// The representations described below are required to provide wire
// compatibility between different programming environments. Generated code for
// different environments typically provide automatic conversions into native
// representations, for simpler idiomatic usage.
//nolint:golint
package time
import (
"time"
"v.io/v23/vdl"
)
var _ = initializeVDL() // Must be first; see initializeVDL comments for details.
// Type definitions
// ================
// Duration represents the elapsed duration between two points in time, with
// up to nanosecond precision.
type Duration struct {
// Seconds represents the seconds in the duration. The range is roughly
// +/-290 billion years, larger than the estimated age of the universe.
Seconds int64
// Nanos represents the fractions of a second at nanosecond resolution. Must
// be in the inclusive range between +/-999,999,999.
//
// In normalized form, durations less than one second are represented with 0
// Seconds and +/-Nanos. For durations one second or more, the sign of Nanos
// must match Seconds, or be 0.
Nanos int32
}
func (Duration) VDLReflect(struct {
Name string `vdl:"time.Duration"`
}) {
}
func (x Duration) VDLIsZero() bool { //nolint:gocyclo
return x == Duration{}
}
func (x Duration) VDLWrite(enc vdl.Encoder) error { //nolint:gocyclo
if err := enc.StartValue(vdlTypeStruct1); err != nil {
return err
}
if x.Seconds != 0 {
if err := enc.NextFieldValueInt(0, vdl.Int64Type, x.Seconds); err != nil {
return err
}
}
if x.Nanos != 0 {
if err := enc.NextFieldValueInt(1, vdl.Int32Type, int64(x.Nanos)); err != nil {
return err
}
}
if err := enc.NextField(-1); err != nil {
return err
}
return enc.FinishValue()
}
func (x *Duration) VDLRead(dec vdl.Decoder) error { //nolint:gocyclo
*x = Duration{}
if err := dec.StartValue(vdlTypeStruct1); err != nil {
return err
}
decType := dec.Type()
for {
index, err := dec.NextField()
switch {
case err != nil:
return err
case index == -1:
return dec.FinishValue()
}
if decType != vdlTypeStruct1 {
index = vdlTypeStruct1.FieldIndexByName(decType.Field(index).Name)
if index == -1 {
if err := dec.SkipValue(); err != nil {
return err
}
continue
}
}
switch index {
case 0:
switch value, err := dec.ReadValueInt(64); {
case err != nil:
return err
default:
x.Seconds = value
}
case 1:
switch value, err := dec.ReadValueInt(32); {
case err != nil:
return err
default:
x.Nanos = int32(value)
}
}
}
}
// Time represents an absolute point in time with up to nanosecond precision.
//
// Time is represented as the duration before or after a fixed epoch. The zero
// Time represents the epoch 0001-01-01T00:00:00.000000000Z. This uses the
// proleptic Gregorian calendar; the calendar runs on an exact 400 year cycle.
// Leap seconds are "smeared", ensuring that no leap second table is necessary
// for interpretation.
//
// This is similar to Go time.Time, but always in the UTC location.
// http://golang.org/pkg/time/#Time
//
// This is similar to conventional "unix time", but with the epoch defined at
// year 1 rather than year 1970. This allows the zero Time to be used as a
// natural sentry, since it isn't a valid time for many practical applications.
// http://en.wikipedia.org/wiki/Unix_time
type Time struct {
Seconds int64
Nanos int32
}
func (Time) VDLReflect(struct {
Name string `vdl:"time.Time"`
}) {
}
func (x Time) VDLIsZero() bool { //nolint:gocyclo
return x == Time{}
}
func (x Time) VDLWrite(enc vdl.Encoder) error { //nolint:gocyclo
if err := enc.StartValue(vdlTypeStruct2); err != nil {
return err
}
if x.Seconds != 0 {
if err := enc.NextFieldValueInt(0, vdl.Int64Type, x.Seconds); err != nil {
return err
}
}
if x.Nanos != 0 {
if err := enc.NextFieldValueInt(1, vdl.Int32Type, int64(x.Nanos)); err != nil {
return err
}
}
if err := enc.NextField(-1); err != nil {
return err
}
return enc.FinishValue()
}
func (x *Time) VDLRead(dec vdl.Decoder) error { //nolint:gocyclo
*x = Time{}
if err := dec.StartValue(vdlTypeStruct2); err != nil {
return err
}
decType := dec.Type()
for {
index, err := dec.NextField()
switch {
case err != nil:
return err
case index == -1:
return dec.FinishValue()
}
if decType != vdlTypeStruct2 {
index = vdlTypeStruct2.FieldIndexByName(decType.Field(index).Name)
if index == -1 {
if err := dec.SkipValue(); err != nil {
return err
}
continue
}
}
switch index {
case 0:
switch value, err := dec.ReadValueInt(64); {
case err != nil:
return err
default:
x.Seconds = value
}
case 1:
switch value, err := dec.ReadValueInt(32); {
case err != nil:
return err
default:
x.Nanos = int32(value)
}
}
}
}
// WireDeadline represents the deadline for an operation, where the operation is
// expected to finish before the deadline. The intended usage is for a client
// to set a deadline on an operation, say one minute from "now", and send the
// deadline to a server. The server is expected to finish the operation before
// the deadline.
//
// On a single device, it is simplest to represent a deadline as an absolute
// time; when the time now reaches the deadline, the deadline has expired.
// However when sending a deadline between devices with potential clock skew, it
// is often more robust to represent the deadline as a duration from "now". The
// sender computes the duration from its notion of "now", while the receiver
// computes the absolute deadline from its own notion of "now".
//
// This representation doesn't account for propagation delay, but does ensure
// that the deadline used by the receiver is no earlier than the deadline
// intended by the client. In many common scenarios the propagation delay is
// small compared to the potential clock skew, making this a simple but
// effective approach.
//
// WireDeadline typically has a native representation called Deadline that is an
// absolute Time, which automatically performs the sender and receiver
// conversions from "now".
type WireDeadline struct {
// FromNow represents the deadline as a duration from "now". As a
// special-case, the 0 duration indicates that there is no deadline; i.e. the
// deadline is "infinite".
FromNow time.Duration
}
func (WireDeadline) VDLReflect(struct {
Name string `vdl:"time.WireDeadline"`
}) {
}
func (x WireDeadline) VDLIsZero() bool { //nolint:gocyclo
return x == WireDeadline{}
}
func (x WireDeadline) VDLWrite(enc vdl.Encoder) error { //nolint:gocyclo
if err := enc.StartValue(vdlTypeStruct3); err != nil {
return err
}
if x.FromNow != 0 {
if err := enc.NextField(0); err != nil {
return err
}
var wire Duration
if err := DurationFromNative(&wire, x.FromNow); err != nil {
return err
}
if err := wire.VDLWrite(enc); err != nil {
return err
}
}
if err := enc.NextField(-1); err != nil {
return err
}
return enc.FinishValue()
}
func (x *WireDeadline) VDLRead(dec vdl.Decoder) error { //nolint:gocyclo
*x = WireDeadline{}
if err := dec.StartValue(vdlTypeStruct3); err != nil {
return err
}
decType := dec.Type()
for {
index, err := dec.NextField()
switch {
case err != nil:
return err
case index == -1:
return dec.FinishValue()
}
if decType != vdlTypeStruct3 {
index = vdlTypeStruct3.FieldIndexByName(decType.Field(index).Name)
if index == -1 {
if err := dec.SkipValue(); err != nil {
return err
}
continue
}
}
if index == 0 {
var wire Duration
if err := wire.VDLRead(dec); err != nil {
return err
}
if err := DurationToNative(wire, &x.FromNow); err != nil {
return err
}
}
}
}
// Type-check native conversion functions.
var (
_ func(Duration, *time.Duration) error = DurationToNative
_ func(*Duration, time.Duration) error = DurationFromNative
_ func(Time, *time.Time) error = TimeToNative
_ func(*Time, time.Time) error = TimeFromNative
_ func(WireDeadline, *Deadline) error = WireDeadlineToNative
_ func(*WireDeadline, Deadline) error = WireDeadlineFromNative
)
// Hold type definitions in package-level variables, for better performance.
//nolint:unused
var (
vdlTypeStruct1 *vdl.Type
vdlTypeStruct2 *vdl.Type
vdlTypeStruct3 *vdl.Type
)
var initializeVDLCalled bool
// initializeVDL performs vdl initialization. It is safe to call multiple times.
// If you have an init ordering issue, just insert the following line verbatim
// into your source files in this package, right after the "package foo" clause:
//
// var _ = initializeVDL()
//
// The purpose of this function is to ensure that vdl initialization occurs in
// the right order, and very early in the init sequence. In particular, vdl
// registration and package variable initialization needs to occur before
// functions like vdl.TypeOf will work properly.
//
// This function returns a dummy value, so that it can be used to initialize the
// first var in the file, to take advantage of Go's defined init order.
func initializeVDL() struct{} {
if initializeVDLCalled {
return struct{}{}
}
initializeVDLCalled = true
// Register native type conversions first, so that vdl.TypeOf works.
vdl.RegisterNative(DurationToNative, DurationFromNative)
vdl.RegisterNative(TimeToNative, TimeFromNative)
vdl.RegisterNative(WireDeadlineToNative, WireDeadlineFromNative)
// Register types.
vdl.Register((*Duration)(nil))
vdl.Register((*Time)(nil))
vdl.Register((*WireDeadline)(nil))
// Initialize type definitions.
vdlTypeStruct1 = vdl.TypeOf((*Duration)(nil)).Elem()
vdlTypeStruct2 = vdl.TypeOf((*Time)(nil)).Elem()
vdlTypeStruct3 = vdl.TypeOf((*WireDeadline)(nil)).Elem()
return struct{}{}
}