2021-06-30 15:06:19 +00:00
|
|
|
package version
|
|
|
|
|
2023-04-13 17:19:06 +00:00
|
|
|
import "github.com/pkg/errors"
|
|
|
|
|
2021-06-30 15:06:19 +00:00
|
|
|
const (
|
|
|
|
Phase0 = iota
|
|
|
|
Altair
|
2022-01-10 16:47:30 +00:00
|
|
|
Bellatrix
|
2022-10-12 16:39:19 +00:00
|
|
|
Capella
|
2023-08-23 20:07:25 +00:00
|
|
|
Deneb
|
2021-06-30 15:06:19 +00:00
|
|
|
)
|
2021-10-09 13:27:34 +00:00
|
|
|
|
2023-04-13 17:19:06 +00:00
|
|
|
var versionToString = map[int]string{
|
|
|
|
Phase0: "phase0",
|
|
|
|
Altair: "altair",
|
|
|
|
Bellatrix: "bellatrix",
|
|
|
|
Capella: "capella",
|
2023-08-23 20:07:25 +00:00
|
|
|
Deneb: "deneb",
|
2023-04-13 17:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// stringToVersion and allVersions are populated in init()
|
|
|
|
var stringToVersion = map[string]int{}
|
2023-10-23 16:36:11 +00:00
|
|
|
var allVersions []int
|
2023-04-13 17:19:06 +00:00
|
|
|
|
|
|
|
// ErrUnrecognizedVersionName means a string does not match the list of canonical version names.
|
|
|
|
var ErrUnrecognizedVersionName = errors.New("version name doesn't map to a known value in the enum")
|
|
|
|
|
|
|
|
// FromString translates a canonical version name to the version number.
|
|
|
|
func FromString(name string) (int, error) {
|
|
|
|
v, ok := stringToVersion[name]
|
|
|
|
if !ok {
|
|
|
|
return 0, errors.Wrap(ErrUnrecognizedVersionName, name)
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the canonical string form of a version.
|
|
|
|
// Unrecognized versions won't generate an error and are represented by the string "unknown version".
|
2021-10-09 13:27:34 +00:00
|
|
|
func String(version int) string {
|
2023-04-13 17:19:06 +00:00
|
|
|
name, ok := versionToString[version]
|
|
|
|
if !ok {
|
2021-10-09 13:27:34 +00:00
|
|
|
return "unknown version"
|
|
|
|
}
|
2023-04-13 17:19:06 +00:00
|
|
|
return name
|
2021-10-09 13:27:34 +00:00
|
|
|
}
|
2022-11-23 14:22:24 +00:00
|
|
|
|
|
|
|
// All returns a list of all known fork versions.
|
|
|
|
func All() []int {
|
2023-04-13 17:19:06 +00:00
|
|
|
return allVersions
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
allVersions = make([]int, len(versionToString))
|
|
|
|
i := 0
|
|
|
|
for v, s := range versionToString {
|
|
|
|
allVersions[i] = v
|
|
|
|
stringToVersion[s] = v
|
|
|
|
i++
|
|
|
|
}
|
2022-11-23 14:22:24 +00:00
|
|
|
}
|