/
version.go
91 lines (76 loc) · 2.23 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
// Copyright 2016 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package internal
import (
"encoding/xml"
"errors"
"fmt"
"strconv"
"strings"
)
// Common XMPP versions.
var (
DefaultVersion = Version{1, 0} // The default version to send.
EmptyVersion = Version{0, 9} // The value of a missing version attribute.
)
// Version is a version of XMPP.
type Version struct {
Major uint8
Minor uint8
}
// MustParseVersion parses a version string and panics if an error is returned.
func MustParseVersion(s string) Version {
v, err := ParseVersion(s)
if err != nil {
panic(err)
}
return v
}
// ParseVersion parses a string of the form "Major.Minor" into a Version struct
// or returns an error.
func ParseVersion(s string) (Version, error) {
v := Version{}
versions := strings.Split(s, ".")
if len(versions) != 2 {
return v, errors.New("XMPP version must have a single separator")
}
// Parse major version number
major, err := strconv.ParseUint(versions[0], 10, 8)
if err != nil {
return v, err
}
v.Major = uint8(major)
// Parse minor version number
minor, err := strconv.ParseUint(versions[1], 10, 8)
if err != nil {
return v, err
}
v.Minor = uint8(minor)
return v, nil
}
// Less compares the major and minor version numbers, returning true if a is
// less than b.
func (v Version) Less(b Version) bool {
return v.Major < b.Major || (v.Major == b.Major && v.Minor < b.Minor)
}
// Prints a string representation of the XMPP version in the form "Major.Minor".
func (v Version) String() string {
return fmt.Sprintf("%d.%d", v.Major, v.Minor)
}
// MarshalXMLAttr satisfies the MarshalerAttr interface and marshals the version
// as an XML attribute using its string representation.
func (v Version) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
return xml.Attr{Name: name, Value: v.String()}, nil
}
// UnmarshalXMLAttr satisfies the UnmarshalerAttr interface and unmarshals an
// XML attribute into a valid XMPP version (or returns an error).
func (v *Version) UnmarshalXMLAttr(attr xml.Attr) error {
newVersion, err := ParseVersion(attr.Value)
if err != nil {
return err
}
v.Major = newVersion.Major
v.Minor = newVersion.Minor
return nil
}