-
Notifications
You must be signed in to change notification settings - Fork 671
/
application.go
93 lines (81 loc) · 1.6 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
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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version
import (
"errors"
"fmt"
)
const (
defaultAppSeparator = "/"
)
var (
errDifferentApps = errors.New("different applications")
_ Application = &application{}
)
// Application defines what is needed to describe a versioned
// Application.
type Application interface {
Version
App() string
Compatible(Application) error
Before(Application) bool
}
type application struct {
Version
app string
str string
}
// NewDefaultApplication returns a new version with default separators
func NewDefaultApplication(
app string,
major int,
minor int,
patch int,
) Application {
return NewApplication(
app,
defaultAppSeparator,
defaultVersionSeparator,
major,
minor,
patch,
)
}
// NewApplication returns a new version
func NewApplication(
app string,
appSeparator string,
versionSeparator string,
major int,
minor int,
patch int,
) Application {
v := NewVersion(major, minor, patch, "", versionSeparator)
return &application{
Version: v,
app: app,
str: fmt.Sprintf("%s%s%s",
app,
appSeparator,
v,
),
}
}
func (v *application) App() string { return v.app }
func (v *application) String() string { return v.str }
func (v *application) Compatible(o Application) error {
switch {
case v.App() != o.App():
return errDifferentApps
case v.Major() > o.Major():
return errDifferentMajor
default:
return nil
}
}
func (v *application) Before(o Application) bool {
if v.App() != o.App() {
return false
}
return v.Compare(o) < 0
}