(v string, pattern *regexp.Regexp)
| 58 | } |
| 59 | |
| 60 | func newVersion(v string, pattern *regexp.Regexp) (*Version, error) { |
| 61 | matches := pattern.FindStringSubmatch(v) |
| 62 | if matches == nil { |
| 63 | return nil, fmt.Errorf("malformed version: %s", v) |
| 64 | } |
| 65 | segmentsStr := strings.Split(matches[1], ".") |
| 66 | segments := make([]int64, len(segmentsStr)) |
| 67 | for i, str := range segmentsStr { |
| 68 | val, err := strconv.ParseInt(str, 10, 64) |
| 69 | if err != nil { |
| 70 | return nil, fmt.Errorf( |
| 71 | "error parsing version: %s", err) |
| 72 | } |
| 73 | |
| 74 | segments[i] = val |
| 75 | } |
| 76 | |
| 77 | // Even though we could support more than three segments, if we |
| 78 | // got less than three, pad it with 0s. This is to cover the basic |
| 79 | // default usecase of semver, which is MAJOR.MINOR.PATCH at the minimum |
| 80 | for i := len(segments); i < 3; i++ { |
| 81 | segments = append(segments, 0) |
| 82 | } |
| 83 | |
| 84 | pre := matches[7] |
| 85 | if pre == "" { |
| 86 | pre = matches[4] |
| 87 | } |
| 88 | |
| 89 | return &Version{ |
| 90 | metadata: matches[10], |
| 91 | pre: pre, |
| 92 | segments: segments, |
| 93 | si: len(segmentsStr), |
| 94 | original: v, |
| 95 | }, nil |
| 96 | } |
| 97 | |
| 98 | // Must is a helper that wraps a call to a function returning (*Version, error) |
| 99 | // and panics if error is non-nil. |
no test coverage detected