-
Notifications
You must be signed in to change notification settings - Fork 658
/
parser.go
110 lines (91 loc) · 2.54 KB
/
parser.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
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package version
import (
"fmt"
"strconv"
"strings"
)
// Parser defines the interface of a Version parser
type Parser interface {
Parse(string) (Version, error)
}
type parser struct {
prefix string
separator string
}
func NewDefaultParser() Parser { return NewParser(defaultVersionPrefix, defaultVersionSeparator) }
func NewParser(prefix, separator string) Parser {
return &parser{
prefix: prefix,
separator: separator,
}
}
func (p *parser) Parse(s string) (Version, error) {
if !strings.HasPrefix(s, p.prefix) {
return nil, fmt.Errorf("version string: %s missing required prefix: %s", s, p.prefix)
}
splitVersion := strings.SplitN(strings.TrimPrefix(s, p.prefix), p.separator, 3)
if len(splitVersion) != 3 {
return nil, fmt.Errorf("failed to parse %s as a version", s)
}
major, err := strconv.Atoi(splitVersion[0])
if err != nil {
return nil, fmt.Errorf("failed to parse %s as a version due to %w", s, err)
}
minor, err := strconv.Atoi(splitVersion[1])
if err != nil {
return nil, fmt.Errorf("failed to parse %s as a version due to %w", s, err)
}
patch, err := strconv.Atoi(splitVersion[2])
if err != nil {
return nil, fmt.Errorf("failed to parse %s as a version due to %w", s, err)
}
return NewVersion(
major,
minor,
patch,
p.prefix,
p.separator,
), nil
}
// ApplicationParser defines the interface of an ApplicationVersion parser
type ApplicationParser interface {
Parse(string) (Application, error)
}
type applicationParser struct {
appSeparator string
versionParser *parser
}
// NewDefaultApplicationParser returns a new parser with the default separators
func NewDefaultApplicationParser() ApplicationParser {
return NewApplicationParser(defaultAppSeparator, defaultVersionSeparator)
}
// NewApplicationParser returns a new parser
func NewApplicationParser(appSeparator string, versionSeparator string) ApplicationParser {
return &applicationParser{
appSeparator: appSeparator,
versionParser: &parser{
prefix: "",
separator: versionSeparator,
},
}
}
func (p *applicationParser) Parse(s string) (Application, error) {
splitApp := strings.SplitN(s, p.appSeparator, 2)
if len(splitApp) != 2 {
return nil, fmt.Errorf("failed to parse %s as a version", s)
}
version, err := p.versionParser.Parse(splitApp[1])
if err != nil {
return nil, err
}
return NewApplication(
splitApp[0],
p.appSeparator,
p.versionParser.separator,
version.Major(),
version.Minor(),
version.Patch(),
), nil
}