-
Notifications
You must be signed in to change notification settings - Fork 5
/
version.go
473 lines (421 loc) · 13.2 KB
/
version.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Package vervet supports opinionated API versioning tools.
package vervet
import (
"fmt"
"os"
"sort"
"strings"
"time"
)
var timeNow = time.Now
// Version defines an API version. API versions may be dates of the form
// "YYYY-mm-dd", or stability tags "beta", "experimental".
type Version struct {
Date time.Time
Stability Stability
}
// DateString returns the string representation of the version date in
// YYYY-mm-dd form.
func (v Version) DateString() string {
return v.Date.Format("2006-01-02")
}
// String returns the string representation of the version in
// YYYY-mm-dd~Stability form. This method will panic if the value is empty.
func (v Version) String() string {
d := v.Date.Format("2006-01-02")
if v.Stability != StabilityGA {
return d + "~" + v.Stability.String()
}
return d
}
// AddDays returns the version corresponding to adding the given number of days
// to the version date.
func (v Version) AddDays(days int) Version {
return Version{
Date: v.Date.AddDate(0, 0, days),
Stability: v.Stability,
}
}
// Stability defines the stability level of the version.
type Stability int
const (
stabilityUndefined Stability = iota
// StabilityWIP means the API is a work-in-progress and not yet ready.
StabilityWIP Stability = iota
// StabilityExperimental means the API is experimental and still subject to
// drastic change.
StabilityExperimental Stability = iota
// StabilityBeta means the API is becoming more stable, but may undergo some
// final changes before being released.
StabilityBeta Stability = iota
// StabilityGA means the API has been released and will not change.
StabilityGA Stability = iota
numStabilityLevels = iota
)
// String returns a string representation of the stability level. This method
// will panic if the value is empty.
func (s Stability) String() string {
switch s {
case StabilityWIP:
return "wip"
case StabilityExperimental:
return "experimental"
case StabilityBeta:
return "beta"
case StabilityGA:
return "ga"
}
panic(fmt.Sprintf("invalid stability (%d)", int(s)))
}
// ParseVersion parses a version string into a Version type, returning an error
// if the string is invalid.
func ParseVersion(s string) (Version, error) {
parts := strings.Split(s, "~")
if len(parts) < 1 {
return Version{}, fmt.Errorf("invalid version %q", s)
}
d, err := time.ParseInLocation("2006-01-02", parts[0], time.UTC)
if err != nil {
return Version{}, fmt.Errorf("invalid version %q", s)
}
stab := StabilityGA
if len(parts) > 1 {
stab, err = ParseStability(parts[1])
if err != nil {
return Version{}, err
}
}
return Version{Date: d.UTC(), Stability: stab}, nil
}
// MustParseVersion parses a version string into a Version type, panicking if
// the string is invalid.
func MustParseVersion(s string) Version {
v, err := ParseVersion(s)
if err != nil {
panic(err)
}
return v
}
// ParseStability parses a stability string into a Stability type, returning an
// error if the string is invalid.
func ParseStability(s string) (Stability, error) {
switch s {
case "wip":
return StabilityWIP, nil
case "experimental":
return StabilityExperimental, nil
case "beta":
return StabilityBeta, nil
case "ga":
return StabilityGA, nil
default:
return stabilityUndefined, fmt.Errorf("invalid stability %q", s)
}
}
// MustParseStability parses a stability string into a Stability type,
// panicking if the string is invalid.
func MustParseStability(s string) Stability {
stab, err := ParseStability(s)
if err != nil {
panic(err)
}
return stab
}
// Compare returns -1 if the given stability level is less than, 0 if equal to,
// and 1 if greater than the caller target stability level.
func (s Stability) Compare(sr Stability) int {
if s < sr {
return -1
} else if s > sr {
return 1
}
return 0
}
// Compare returns -1 if the given version is less than, 0 if equal to, and 1
// if greater than the caller target version.
func (v Version) Compare(vr Version) int {
dateCmp, stabilityCmp := v.compareDateStability(&vr)
if dateCmp != 0 {
return dateCmp
}
return stabilityCmp
}
// DeprecatedBy returns true if the given version deprecates the caller target
// version.
func (v Version) DeprecatedBy(vr Version) bool {
dateCmp, stabilityCmp := v.compareDateStability(&vr)
// A version is deprecated by a newer version of equal or greater stability.
return dateCmp == -1 && stabilityCmp <= 0
}
const (
// SunsetWIP is the duration past deprecation after which a work-in-progress version may be sunset.
SunsetWIP = 0
// SunsetExperimental is the duration past deprecation after which an experimental version may be sunset.
SunsetExperimental = 24 * time.Hour
// SunsetBeta is the duration past deprecation after which a beta version may be sunset.
SunsetBeta = 91 * 24 * time.Hour
// SunsetGA is the duration past deprecation after which a GA version may be sunset.
SunsetGA = 181 * 24 * time.Hour
)
// Sunset returns, given a potentially deprecating version, the eligible sunset
// date and whether the caller target version would actually be deprecated and
// sunset by the given version.
func (v Version) Sunset(vr Version) (time.Time, bool) {
if !v.DeprecatedBy(vr) {
return time.Time{}, false
}
switch v.Stability {
case StabilityWIP:
return vr.Date.Add(SunsetWIP), true
case StabilityExperimental:
return vr.Date.Add(SunsetExperimental), true
case StabilityBeta:
return vr.Date.Add(SunsetBeta), true
case StabilityGA:
return vr.Date.Add(SunsetGA), true
}
return time.Time{}, false
}
// compareDateStability returns the comparison of both the date and stability
// between two versions. Used internally where these need to be evaluated
// independently, such as when searching for the best matching version.
func (v *Version) compareDateStability(vr *Version) (int, int) {
dateCmp := 0
if v.Date.Before(vr.Date) {
dateCmp = -1
} else if v.Date.After(vr.Date) {
dateCmp = 1
}
stabilityCmp := v.Stability.Compare(vr.Stability)
return dateCmp, stabilityCmp
}
// VersionDateStrings returns a slice of distinct version date strings for a
// slice of Versions. Consecutive duplicate dates are removed.
func VersionDateStrings(vs []Version) []string {
var result []string
for i := range vs {
ds := vs[i].DateString()
if len(result) == 0 || result[len(result)-1] != ds {
result = append(result, ds)
}
}
return result
}
// VersionSlice is a sortable slice of Versions.
type VersionSlice []Version
// VersionIndex provides a search over versions, resolving which version is in
// effect for a given date and stability level.
type VersionIndex struct {
versions []effectiveVersion
}
type effectiveVersion struct {
date time.Time
stabilities [numStabilityLevels]time.Time
}
// NewVersionIndex returns a new VersionIndex of the given versions. The given
// VersionSlice will be sorted.
func NewVersionIndex(vs VersionSlice) (vi VersionIndex) {
sort.Sort(vs)
evIndex := -1
currentStabilities := [numStabilityLevels]time.Time{}
for i := range vs {
if evIndex == -1 || !vi.versions[evIndex].date.Equal(vs[i].Date) {
vi.versions = append(vi.versions, effectiveVersion{
date: vs[i].Date,
stabilities: [numStabilityLevels]time.Time{},
})
evIndex++
for stab, date := range currentStabilities {
vi.versions[evIndex].stabilities[stab] = date
}
}
vi.versions[evIndex].stabilities[vs[i].Stability] = vs[i].Date
currentStabilities[vs[i].Stability] = vs[i].Date
}
return vi
}
// resolveIndex performs a binary search on the stability versions in effect on
// the query date.
func (vi *VersionIndex) resolveIndex(query time.Time) (int, error) {
if len(vi.versions) == 0 || vi.versions[0].date.After(query) {
return -1, ErrNoMatchingVersion
}
lower, curr, upper := 0, len(vi.versions)/2, len(vi.versions)
for lower < upper-1 {
if vi.versions[curr].date.After(query) {
upper = curr
} else {
lower = curr
}
curr = lower + (upper-lower)/2
}
return lower, nil
}
// Resolve returns the released version effective on the query version date at
// the given version stability. Returns ErrNoMatchingVersion if no version matches.
//
// Resolve should be used on a collection of already "compiled" or
// "collated" API versions.
func (vi *VersionIndex) Resolve(query Version) (Version, error) {
i, err := vi.resolveIndex(query.Date)
if err != nil {
return Version{}, err
}
for stab := query.Stability; stab < numStabilityLevels; stab++ {
if stabDate := vi.versions[i].stabilities[stab]; !stabDate.IsZero() {
return Version{Date: stabDate, Stability: stab}, nil
}
}
return Version{}, ErrNoMatchingVersion
}
// resolveForBuild returns the most stable version effective on the query
// version date with respect to the given version stability. Returns
// ErrNoMatchingVersion if no version matches.
//
// Use resolveForBuild when resolving version deprecation and effective releases
// _within a single resource_ during the "compilation" or "collation" process.
func (vi *VersionIndex) resolveForBuild(query Version) (Version, error) {
i, err := vi.resolveIndex(query.Date)
if err != nil {
return Version{}, err
}
var matchDate time.Time
var matchStab Stability
for stab := query.Stability; stab < numStabilityLevels; stab++ {
if stabDate := vi.versions[i].stabilities[stab]; !stabDate.IsZero() && !stabDate.Before(matchDate) && !stabDate.After(query.Date) {
matchDate, matchStab = stabDate, stab
}
}
if matchDate.IsZero() {
return Version{}, ErrNoMatchingVersion
}
return Version{Date: matchDate, Stability: matchStab}, nil
}
// Deprecates returns the version that deprecates the given version in the
// slice.
func (vi *VersionIndex) Deprecates(q Version) (Version, bool) {
match, err := vi.resolveIndex(q.Date)
if err == ErrNoMatchingVersion {
return Version{}, false
}
if err != nil {
panic(err)
}
for i := match + 1; i < len(vi.versions); i++ {
for stab := q.Stability; stab < numStabilityLevels; stab++ {
if stabDate := vi.versions[i].stabilities[stab]; stabDate.After(q.Date) {
return Version{
Date: vi.versions[i].date,
Stability: stab,
}, true
}
}
}
return Version{}, false
}
// Len implements sort.Interface.
func (vs VersionSlice) Len() int { return len(vs) }
// Less implements sort.Interface.
func (vs VersionSlice) Less(i, j int) bool {
return vs[i].Compare(vs[j]) < 0
}
// Swap implements sort.Interface.
func (vs VersionSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
// Strings returns a slice of string versions
func (vs VersionSlice) Strings() []string {
s := make([]string, len(vs))
for i := range vs {
s[i] = vs[i].String()
}
return s
}
// Lifecycle defines the release lifecycle.
type Lifecycle int
const (
lifecycleUndefined Lifecycle = iota
// LifecycleUnreleased means the version has not been released yet.
LifecycleUnreleased Lifecycle = iota
// LifecycleReleased means the version is released.
LifecycleReleased Lifecycle = iota
// LifecycleDeprecated means the version is deprecated.
LifecycleDeprecated Lifecycle = iota
// LifecycleSunset means the version is eligible to be sunset.
LifecycleSunset Lifecycle = iota
// ExperimentalTTL is the duration after which experimental releases expire
// and should be considered sunset.
ExperimentalTTL = 90 * 24 * time.Hour
)
// ParseLifecycle parses a lifecycle string into a Lifecycle type, returning an
// error if the string is invalid.
func ParseLifecycle(s string) (Lifecycle, error) {
switch s {
case "released":
return LifecycleReleased, nil
case "deprecated":
return LifecycleDeprecated, nil
case "sunset":
return LifecycleSunset, nil
default:
return lifecycleUndefined, fmt.Errorf("invalid lifecycle %q", s)
}
}
// String returns a string representation of the lifecycle stage. This method
// will panic if the value is empty.
func (l Lifecycle) String() string {
switch l {
case LifecycleReleased:
return "released"
case LifecycleDeprecated:
return "deprecated"
case LifecycleSunset:
return "sunset"
}
panic(fmt.Sprintf("invalid lifecycle (%d)", int(l)))
}
func (l Lifecycle) Valid() bool {
switch l {
case LifecycleReleased, LifecycleDeprecated, LifecycleSunset:
return true
default:
return false
}
}
// LifecycleAt returns the Lifecycle of the version at the given time. If the
// time is the zero value (time.Time{}), then the following are used to
// determine the reference time:
//
// If VERVET_LIFECYCLE_AT is set to an ISO date string of the form YYYY-mm-dd,
// this date is used as the reference time for deprecation, at midnight UTC.
//
// Otherwise `time.Now().UTC()` is used for the reference time.
//
// The current time is always used for determining whether a version is unreleased.
func (v *Version) LifecycleAt(t time.Time) Lifecycle {
if t.IsZero() {
t = defaultLifecycleAt()
}
deprecationDelta := t.Sub(v.Date)
releaseDelta := timeNow().UTC().Sub(v.Date)
if releaseDelta < 0 {
return LifecycleUnreleased
}
if v.Stability.Compare(StabilityExperimental) <= 0 {
if v.Stability == StabilityWIP {
return LifecycleSunset
}
// experimental
if deprecationDelta > ExperimentalTTL {
return LifecycleSunset
}
return LifecycleDeprecated
}
return LifecycleReleased
}
func defaultLifecycleAt() time.Time {
if dateStr := os.Getenv("VERVET_LIFECYCLE_AT"); dateStr != "" {
if t, err := time.ParseInLocation("2006-01-02", dateStr, time.UTC); err == nil {
return t
}
}
return timeNow().UTC()
}