php-parser/internal/version/version.go

62 lines
889 B
Go
Raw Normal View History

2019-12-26 15:57:56 +00:00
package version
import (
"errors"
"strconv"
"strings"
)
type version struct {
major int
minor int
}
func Compare(a string, b string) (int, error) {
first, err := parse(a)
if err != nil {
return 0, err
}
second, err := parse(b)
if err != nil {
return 0, err
}
if first.major < second.major {
return -1, nil
}
if first.major > second.major {
return 1, nil
}
if first.minor < second.minor {
return -1, nil
}
if first.minor > second.minor {
return 1, nil
}
return 0, nil
}
func parse(v string) (version, error) {
parts := strings.Split(v, ".")
if len(parts) != 2 {
return version{}, errors.New("version must contain major and minor parts")
}
major, err := strconv.Atoi(parts[0])
if err != nil {
return version{}, err
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return version{}, err
}
return version{major, minor}, nil
}