-
Notifications
You must be signed in to change notification settings - Fork 672
/
application.go
67 lines (56 loc) · 1.27 KB
/
application.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version
import (
"errors"
"fmt"
"sync/atomic"
)
var (
errDifferentMajor = errors.New("different major version")
_ fmt.Stringer = (*Semantic)(nil)
)
type Application struct {
Major int `json:"major" yaml:"major"`
Minor int `json:"minor" yaml:"minor"`
Patch int `json:"patch" yaml:"patch"`
str atomic.Value
}
// The only difference here between Application and Semantic is that Application
// prepends "avalanche/" rather than "v".
func (a *Application) String() string {
strIntf := a.str.Load()
if strIntf != nil {
return strIntf.(string)
}
str := fmt.Sprintf(
"avalanche/%d.%d.%d",
a.Major,
a.Minor,
a.Patch,
)
a.str.Store(str)
return str
}
func (a *Application) Compatible(o *Application) error {
switch {
case a.Major > o.Major:
return errDifferentMajor
default:
return nil
}
}
func (a *Application) Before(o *Application) bool {
return a.Compare(o) < 0
}
// Compare returns a positive number if s > o, 0 if s == o, or a negative number
// if s < o.
func (a *Application) Compare(o *Application) int {
if a.Major != o.Major {
return a.Major - o.Major
}
if a.Minor != o.Minor {
return a.Minor - o.Minor
}
return a.Patch - o.Patch
}